ProjectCacheCleaner { public void deleteProjectDirsIfNecessary(final long newProjectSizeInBytes) { final long cachePartitionSize = this.projectCacheDir.getTotalSpace(); final long availablePartitionSize = this.projectCacheDir.getUsableSpace(); final long start = System.currentTimeMillis(); loadAllProjects(); log.info("Loading {} project dirs metadata completed in {} msecs", cachedProjects.size(), System.currentTimeMillis() - start); final long currentCacheSize = getProjectDirsTotalSizeInBytes(); final long projectCacheDirCapacity = currentCacheSize + availablePartitionSize; boolean throttleAfterDeletion = false; final long highWatermark = (long) (projectCacheDirCapacity * this.percentageOfDisk); final long throttleWatermark = (long) (projectCacheDirCapacity * this.throttlePercentage); long projectedCacheSize = currentCacheSize + newProjectSizeInBytes; log.info("Partition = {} MB, Total Capacity = {} MB, Cache Size = {} MB, Projected Size = {} MB", bytesToMB(cachePartitionSize), bytesToMB(projectCacheDirCapacity), bytesToMB(currentCacheSize), bytesToMB(projectedCacheSize)); log.info("High Watermark = {} MB, Throttle Watermark = {} MB", bytesToMB(highWatermark), bytesToMB(throttleWatermark)); if (projectedCacheSize >= throttleWatermark) { throttleAfterDeletion = true; } if (projectedCacheSize >= highWatermark) { log.info("Projected cache size exceeds High Watermark. LRU Eviction will kick in"); deleteLeastRecentlyUsedProjects(projectedCacheSize - highWatermark); } if (throttleAfterDeletion) { log.info("Throttle Watermark was hit. Blocking till LRU eviction is complete."); finishPendingCleanup(); } } ProjectCacheCleaner(final File projectCacheDir, final double percentageOfDisk);  ProjectCacheCleaner(final File projectCacheDir, final double percentageOfDisk,
      final double throttlePercentage); void deleteProjectDirsIfNecessary(final long newProjectSizeInBytes); String queryState(); void shutdown(); static final String STATE_AVAILABLE; static final String STATE_CLEANING; }
@Test public void testNotDeleting() { final ProjectCacheCleaner cleaner = new ProjectCacheCleaner(this.cacheDir, 0.7); cleaner.deleteProjectDirsIfNecessary(1); assertThat(this.cacheDir.list()).hasSize(3); }
@Test public void testThrottleCondition() { final ProjectCacheCleaner cleaner = new ProjectCacheCleaner(this.cacheDir, 0.65, 0.7); cleaner.deleteProjectDirsIfNecessary(3000000); assertThat(this.cacheDir.list()).hasSize(1); assertThat(this.cacheDir.list()).contains("3.1"); }