Release cached searcher

This commit is contained in:
Andrea Cavalli 2021-09-24 02:44:12 +02:00
parent 20c19f662b
commit 23fa46c775
3 changed files with 36 additions and 12 deletions

View File

@ -140,18 +140,18 @@ public class CachedIndexSearcherManager implements IndexSearcherManager {
return Mono.fromCallable(() -> {
activeSearchers.register();
IndexSearcher indexSearcher;
boolean decRef;
if (snapshot == null) {
indexSearcher = searcherManager.acquire();
decRef = true;
} else {
indexSearcher = snapshotsManager.resolveSnapshot(snapshot).getIndexSearcher();
decRef = false;
}
indexSearcher.setSimilarity(similarity);
assert indexSearcher.getIndexReader().getRefCount() > 0;
return indexSearcher;
return new LLIndexSearcher(indexSearcher, decRef, this::dropCachedIndexSearcher).send();
})
// todo: re-enable caching if needed
//.cacheInvalidateWhen(tuple -> onInvalidateCache)
.map(indexSearcher -> new LLIndexSearcher(indexSearcher, this::dropCachedIndexSearcher).send())
.takeUntilOther(onClose)
.doOnDiscard(Send.class, Send::close);
});

View File

@ -2,23 +2,24 @@ package it.cavallium.dbengine.database.disk;
import io.net5.buffer.api.Drop;
import io.net5.buffer.api.Owned;
import io.net5.buffer.api.internal.ResourceSupport;
import it.cavallium.dbengine.database.LiveResourceSupport;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.SearcherManager;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LLIndexSearcher extends LiveResourceSupport<LLIndexSearcher, LLIndexSearcher> {
private IndexSearcher indexSearcher;
private static final Logger logger = LoggerFactory.getLogger(LLIndexSearcher.class);
public LLIndexSearcher(IndexSearcher indexSearcher, Drop<LLIndexSearcher> drop) {
super(drop);
private IndexSearcher indexSearcher;
private final boolean decRef;
public LLIndexSearcher(IndexSearcher indexSearcher, boolean decRef, Drop<LLIndexSearcher> drop) {
super(new CloseOnDrop(drop));
this.indexSearcher = indexSearcher;
this.decRef = decRef;
}
public IndexReader getIndexReader() {
@ -43,10 +44,33 @@ public class LLIndexSearcher extends LiveResourceSupport<LLIndexSearcher, LLInde
@Override
protected Owned<LLIndexSearcher> prepareSend() {
var indexSearcher = this.indexSearcher;
return drop -> new LLIndexSearcher(indexSearcher, drop);
return drop -> new LLIndexSearcher(indexSearcher, decRef, drop);
}
protected void makeInaccessible() {
this.indexSearcher = null;
}
private static class CloseOnDrop implements Drop<LLIndexSearcher> {
private final Drop<LLIndexSearcher> delegate;
public CloseOnDrop(Drop<LLIndexSearcher> drop) {
if (drop instanceof CloseOnDrop closeOnDrop) {
this.delegate = closeOnDrop.delegate;
} else {
this.delegate = drop;
}
}
@Override
public void drop(LLIndexSearcher obj) {
try {
obj.indexSearcher.getIndexReader().decRef();
} catch (IOException ex) {
logger.error("Failed to drop IndexReader", ex);
}
delegate.drop(obj);
}
}
}

View File

@ -43,7 +43,7 @@ public class SimpleUnsortedUnscoredLuceneMultiSearcher implements LuceneMultiSea
indexSearchers -> Flux
.fromIterable(indexSearchers.shards())
.flatMap(searcher -> {
var llSearcher = Mono.fromCallable(() -> new LLIndexSearcher(searcher, d -> {}).send());
var llSearcher = Mono.fromCallable(() -> new LLIndexSearcher(searcher, false, d -> {}).send());
return localSearcher.collect(llSearcher, localQueryParams, keyFieldName, transformer);
})
.collectList()