Skip to content
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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

dtfiedler
Copy link
Collaborator

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.

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.
Copy link

codecov bot commented Oct 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.78%. Comparing base (ebde843) to head (0b094db).

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.
📢 Have feedback on the report? Share it here.

So we can discern logs from multiple workers.
Comment on lines +227 to +254
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;

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,
});

Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants