diff --git a/oak-segment-remote/src/main/java/org/apache/jackrabbit/oak/segment/remote/persistentcache/PersistentDiskCache.java b/oak-segment-remote/src/main/java/org/apache/jackrabbit/oak/segment/remote/persistentcache/PersistentDiskCache.java index 731884339a..f90c56ac89 100644 --- a/oak-segment-remote/src/main/java/org/apache/jackrabbit/oak/segment/remote/persistentcache/PersistentDiskCache.java +++ b/oak-segment-remote/src/main/java/org/apache/jackrabbit/oak/segment/remote/persistentcache/PersistentDiskCache.java @@ -63,15 +63,10 @@ public class PersistentDiskCache extends AbstractPersistentCache { final AtomicLong evictionCount = new AtomicLong(); - private static final Comparator sortedByAccessTime = (path1, path2) -> { - try { - FileTime lastAccessFile1 = Files.readAttributes(path1, BasicFileAttributes.class).lastAccessTime(); - FileTime lastAccessFile2 = Files.readAttributes(path2, BasicFileAttributes.class).lastAccessTime(); + private static final Comparator sortedByAccessTime = (segmentCacheEntry1, segmentCacheEntry2) -> { + FileTime lastAccessFile1 = segmentCacheEntry1.getLastAccessTime(); + FileTime lastAccessFile2 = segmentCacheEntry2.getLastAccessTime(); return lastAccessFile1.compareTo(lastAccessFile2); - } catch (IOException e) { - logger.error("A problem occurred while cleaning up the cache: ", e); - } - return 0; }; public PersistentDiskCache(File directory, int cacheMaxSizeMB, IOMonitor diskCacheIOMonitor) { @@ -193,16 +188,27 @@ public class PersistentDiskCache extends AbstractPersistentCache { private void cleanUpInternal() { if (isCacheFull()) { try { - Stream segmentsPaths = Files.walk(directory.toPath()) + Stream segmentCacheEntryStream = Files.walk(directory.toPath()) + .map(path -> { + try { + return new SegmentCacheEntry(path, Files.readAttributes(path, BasicFileAttributes.class).lastAccessTime()); + } catch (IOException e) { + logger.error("Error while getting the last access time for {}", path.toFile().getName()); + } + return new SegmentCacheEntry(null, FileTime.fromMillis(Long.MAX_VALUE)); + }) .sorted(sortedByAccessTime) - .filter(filePath -> !filePath.toFile().isDirectory()); + .filter(segmentCacheEntry -> !segmentCacheEntry.getPath().toFile().isDirectory()); - StreamConsumer.forEach(segmentsPaths, (path, breaker) -> { + StreamConsumer.forEach(segmentCacheEntryStream, (segmentCacheEntry, breaker) -> { if (cacheSize.get() > maxCacheSizeBytes * 0.66) { - cacheSize.addAndGet(-path.toFile().length()); - path.toFile().delete(); + if (segmentCacheEntry.getPath() != null) { // it can be null if error has occurred while processing the stream + File segment = segmentCacheEntry.getPath().toFile(); + cacheSize.addAndGet(-segment.length()); + segment.delete(); evictionCount.incrementAndGet(); + } } else { breaker.stop(); } @@ -213,6 +219,23 @@ public class PersistentDiskCache extends AbstractPersistentCache { } } + private class SegmentCacheEntry { + private Path path; + private FileTime lastAccessTime; + + public SegmentCacheEntry(Path path, FileTime lastAccessTime) { + this.path = path; + this.lastAccessTime = lastAccessTime; + } + + public Path getPath() { + return path; + } + + public FileTime getLastAccessTime() { + return lastAccessTime; + } + } static class StreamConsumer { public static class Breaker {