Skip to content

Commit ac6050f

Browse files
committed
feedback
1 parent 9e9ba7c commit ac6050f

File tree

1 file changed

+31
-49
lines changed
  • x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache

1 file changed

+31
-49
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/PersistentCache.java

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import java.util.HashMap;
6161
import java.util.List;
6262
import java.util.Map;
63-
import java.util.Objects;
6463
import java.util.SortedSet;
6564
import java.util.TreeSet;
6665
import java.util.concurrent.atomic.AtomicBoolean;
@@ -148,14 +147,36 @@ void loadCacheFiles(CacheService cacheService) {
148147

149148
if (Files.isDirectory(shardCachePath)) {
150149
logger.trace("found snapshot cache dir at [{}], loading cache files from disk and index", shardCachePath);
151-
Files.walkFileTree(shardCachePath, new CacheFileVisitor(cacheService, writer, documents));
150+
Files.walkFileTree(shardCachePath, new SimpleFileVisitor<>() {
151+
@Override
152+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
153+
try {
154+
final String id = buildId(file);
155+
final Document cacheDocument = documents.get(id);
156+
if (cacheDocument != null) {
157+
logger.trace("indexing cache file with id [{}] in persistent cache index", id);
158+
writer.updateCacheFile(id, cacheDocument);
159+
160+
final CacheKey cacheKey = buildCacheKey(cacheDocument);
161+
final long fileLength = getFileLength(cacheDocument);
162+
final SortedSet<Tuple<Long, Long>> ranges = buildCacheFileRanges(cacheDocument);
163+
164+
logger.trace("adding cache file with [id={}, cache key={}, ranges={}]", id, cacheKey, ranges);
165+
cacheService.put(cacheKey, fileLength, file.getParent(), id, ranges);
166+
} else {
167+
logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file);
168+
Files.delete(file);
169+
}
170+
} catch (Exception e) {
171+
throw ExceptionsHelper.convertToRuntime(e);
172+
}
173+
return FileVisitResult.CONTINUE;
174+
}
175+
});
152176
}
153177
}
154178
}
155179
}
156-
for (CacheIndexWriter writer : writers) {
157-
writer.prepareCommit();
158-
}
159180
for (CacheIndexWriter writer : writers) {
160181
writer.commit();
161182
}
@@ -228,8 +249,11 @@ public long getNumDocs() {
228249
@Override
229250
public void close() throws IOException {
230251
if (closed.compareAndSet(false, true)) {
231-
IOUtils.close(writers);
232-
documents.clear();
252+
try {
253+
IOUtils.close(writers);
254+
} finally {
255+
documents.clear();
256+
}
233257
}
234258
}
235259

@@ -446,48 +470,6 @@ public String toString() {
446470
}
447471
}
448472

449-
/**
450-
* {@link CacheFileVisitor} is used to visit cache files on disk and find information about them using the Lucene documents loaded
451-
* at startup from the persistent cache index. If there are no corresponding document for a cache file, the cache file is deleted
452-
* from disk. If a corresponding document is found, the cache file is added to the current persistent cache index and inserted in
453-
* the searchable snapshots cache.
454-
*/
455-
private static class CacheFileVisitor extends SimpleFileVisitor<Path> {
456-
457-
private final CacheService cacheService;
458-
private final Map<String, Document> documents;
459-
private final CacheIndexWriter writer;
460-
461-
private CacheFileVisitor(CacheService cacheService, CacheIndexWriter writer, Map<String, Document> documents) {
462-
this.cacheService = Objects.requireNonNull(cacheService);
463-
this.documents = Objects.requireNonNull(documents);
464-
this.writer = Objects.requireNonNull(writer);
465-
}
466-
467-
@Override
468-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
469-
try {
470-
final String id = buildId(file);
471-
final Document cacheDocument = documents.get(id);
472-
if (cacheDocument != null) {
473-
logger.trace("indexing cache file with id [{}] in persistent cache index", id);
474-
writer.updateCacheFile(id, cacheDocument);
475-
476-
final CacheKey cacheKey = buildCacheKey(cacheDocument);
477-
logger.trace("adding cache file with [id={}, cache key={}]", id, cacheKey);
478-
final long fileLength = getFileLength(cacheDocument);
479-
cacheService.put(cacheKey, fileLength, file.getParent(), id, buildCacheFileRanges(cacheDocument));
480-
} else {
481-
logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file);
482-
Files.delete(file);
483-
}
484-
} catch (Exception e) {
485-
throw ExceptionsHelper.convertToRuntime(e);
486-
}
487-
return FileVisitResult.CONTINUE;
488-
}
489-
}
490-
491473
private static final String CACHE_ID_FIELD = "cache_id";
492474
private static final String CACHE_PATH_FIELD = "cache_path";
493475
private static final String CACHE_RANGES_FIELD = "cache_ranges";

0 commit comments

Comments
 (0)