Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx committed Dec 14, 2020
1 parent 9e9ba7c commit ac6050f
Showing 1 changed file with 31 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -148,14 +147,36 @@ void loadCacheFiles(CacheService cacheService) {

if (Files.isDirectory(shardCachePath)) {
logger.trace("found snapshot cache dir at [{}], loading cache files from disk and index", shardCachePath);
Files.walkFileTree(shardCachePath, new CacheFileVisitor(cacheService, writer, documents));
Files.walkFileTree(shardCachePath, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
final String id = buildId(file);
final Document cacheDocument = documents.get(id);
if (cacheDocument != null) {
logger.trace("indexing cache file with id [{}] in persistent cache index", id);
writer.updateCacheFile(id, cacheDocument);

final CacheKey cacheKey = buildCacheKey(cacheDocument);
final long fileLength = getFileLength(cacheDocument);
final SortedSet<Tuple<Long, Long>> ranges = buildCacheFileRanges(cacheDocument);

logger.trace("adding cache file with [id={}, cache key={}, ranges={}]", id, cacheKey, ranges);
cacheService.put(cacheKey, fileLength, file.getParent(), id, ranges);
} else {
logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file);
Files.delete(file);
}
} catch (Exception e) {
throw ExceptionsHelper.convertToRuntime(e);
}
return FileVisitResult.CONTINUE;
}
});
}
}
}
}
for (CacheIndexWriter writer : writers) {
writer.prepareCommit();
}
for (CacheIndexWriter writer : writers) {
writer.commit();
}
Expand Down Expand Up @@ -228,8 +249,11 @@ public long getNumDocs() {
@Override
public void close() throws IOException {
if (closed.compareAndSet(false, true)) {
IOUtils.close(writers);
documents.clear();
try {
IOUtils.close(writers);
} finally {
documents.clear();
}
}
}

Expand Down Expand Up @@ -446,48 +470,6 @@ public String toString() {
}
}

/**
* {@link CacheFileVisitor} is used to visit cache files on disk and find information about them using the Lucene documents loaded
* at startup from the persistent cache index. If there are no corresponding document for a cache file, the cache file is deleted
* from disk. If a corresponding document is found, the cache file is added to the current persistent cache index and inserted in
* the searchable snapshots cache.
*/
private static class CacheFileVisitor extends SimpleFileVisitor<Path> {

private final CacheService cacheService;
private final Map<String, Document> documents;
private final CacheIndexWriter writer;

private CacheFileVisitor(CacheService cacheService, CacheIndexWriter writer, Map<String, Document> documents) {
this.cacheService = Objects.requireNonNull(cacheService);
this.documents = Objects.requireNonNull(documents);
this.writer = Objects.requireNonNull(writer);
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
final String id = buildId(file);
final Document cacheDocument = documents.get(id);
if (cacheDocument != null) {
logger.trace("indexing cache file with id [{}] in persistent cache index", id);
writer.updateCacheFile(id, cacheDocument);

final CacheKey cacheKey = buildCacheKey(cacheDocument);
logger.trace("adding cache file with [id={}, cache key={}]", id, cacheKey);
final long fileLength = getFileLength(cacheDocument);
cacheService.put(cacheKey, fileLength, file.getParent(), id, buildCacheFileRanges(cacheDocument));
} else {
logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file);
Files.delete(file);
}
} catch (Exception e) {
throw ExceptionsHelper.convertToRuntime(e);
}
return FileVisitResult.CONTINUE;
}
}

private static final String CACHE_ID_FIELD = "cache_id";
private static final String CACHE_PATH_FIELD = "cache_path";
private static final String CACHE_RANGES_FIELD = "cache_ranges";
Expand Down

0 comments on commit ac6050f

Please sign in to comment.