-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fs): introduce chunk fs cache cleanup worker #226
base: develop
Are you sure you want to change the base?
Conversation
Chunks can quickly fill up a disk, even if contiguous data and header filesystem caches are cleaned up. This PR introduces a chunk filesystem cleanup worker that is disabled by default and responsible for deleting old(er) chunks.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #226 +/- ##
===========================================
+ Coverage 68.74% 68.78% +0.03%
===========================================
Files 32 32
Lines 7983 7992 +9
Branches 434 434
===========================================
+ Hits 5488 5497 +9
Misses 2494 2494
Partials 1 1 ☔ View full report in Codecov by Sentry. |
So we can discern logs from multiple workers.
export const chunkDataFsCacheCleanupWorker = | ||
config.ENABLE_FS_CHUNK_CACHE_CLEANUP && | ||
!isNaN(config.CHUNK_DATA_CACHE_CLEANUP_THRESHOLD) | ||
? new FsCleanupWorker({ | ||
log, | ||
basePath: 'data/chunks', | ||
shouldDelete: async (path) => { | ||
try { | ||
const stats = await fs.promises.stat(path); | ||
const mostRecentTime = | ||
stats.atime > stats.mtime ? stats.atime : stats.mtime; | ||
|
||
const currentTimestamp = Date.now(); | ||
|
||
const thresholdDate = new Date( | ||
currentTimestamp - | ||
config.CHUNK_DATA_CACHE_CLEANUP_THRESHOLD * 1000, | ||
); | ||
|
||
return mostRecentTime <= thresholdDate; | ||
} catch (err) { | ||
log.error(`Error getting file stats for ${path}`, err); | ||
return false; | ||
} | ||
}, | ||
}) | ||
: undefined; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block is almost identical to the contiguousDataFsCacheCleanupWorker
.
Consider abstracting the shared logic into a reusable function, for example:
function createFsCacheCleanupWorker({ basePath, thresholdInSeconds, isEnabled = true }) {
if (!isEnabled || isNaN(thresholdInSeconds)) {
return undefined;
}
return new FsCleanupWorker({
log,
basePath,
shouldDelete: async (path) => {
try {
const stats = await fs.promises.stat(path);
const mostRecentTime =
stats.atime > stats.mtime ? stats.atime : stats.mtime;
const currentTimestamp = Date.now();
const thresholdDate = new Date(
currentTimestamp - thresholdInSeconds * 1000,
);
return mostRecentTime <= thresholdDate;
} catch (err) {
log.error(`Error getting file stats for ${path}`, err);
return false;
}
},
});
}
export const contiguousDataFsCacheCleanupWorker = createFsCacheCleanupWorker({
basePath: 'data/contiguous',
thresholdInSeconds: contiguousDataCacheCleanupThresholdInSeconds,
});
export const chunkDataFsCacheCleanupWorker = createFsCacheCleanupWorker({
basePath: 'data/chunks',
thresholdInSeconds: config.CHUNK_DATA_CACHE_CLEANUP_THRESHOLD,
isEnabled: config.ENABLE_FS_CHUNK_CACHE_CLEANUP,
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed it is - good rec! will pull in
Chunks can quickly fill up a disk, even if contiguous data and header filesystem caches are cleaned up. This PR introduces a chunk filesystem cleanup worker that is disabled by default and responsible for deleting old(er) chunks.