2021-09-06 15:06:51 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-12-16 16:14:44 +01:00
|
|
|
import static it.cavallium.dbengine.client.UninterruptibleScheduler.uninterruptibleScheduler;
|
|
|
|
|
2021-09-06 15:06:51 +02:00
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
2021-11-08 12:06:32 +01:00
|
|
|
import io.net5.buffer.api.Resource;
|
2021-09-18 18:34:21 +02:00
|
|
|
import io.net5.buffer.api.Send;
|
2021-09-06 15:06:51 +02:00
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.time.Duration;
|
2021-09-25 13:06:24 +02:00
|
|
|
import java.util.concurrent.Executor;
|
2021-11-09 02:14:21 +01:00
|
|
|
import java.util.concurrent.Executors;
|
2021-09-25 13:06:24 +02:00
|
|
|
import java.util.concurrent.ForkJoinPool;
|
2021-09-06 18:29:10 +02:00
|
|
|
import java.util.concurrent.Phaser;
|
2021-09-07 02:36:11 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-09-07 11:39:13 +02:00
|
|
|
import java.util.concurrent.TimeoutException;
|
2021-10-26 00:02:08 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-09-06 15:06:51 +02:00
|
|
|
import org.apache.lucene.index.IndexWriter;
|
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
|
import org.apache.lucene.search.SearcherFactory;
|
|
|
|
import org.apache.lucene.search.SearcherManager;
|
|
|
|
import org.apache.lucene.search.similarities.Similarity;
|
2021-09-06 17:35:02 +02:00
|
|
|
import org.apache.lucene.store.AlreadyClosedException;
|
2021-09-06 15:06:51 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-11-09 02:14:21 +01:00
|
|
|
import org.warp.commonutils.type.ShortNamedThreadFactory;
|
2021-09-06 15:06:51 +02:00
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
import reactor.core.publisher.Sinks;
|
|
|
|
import reactor.core.publisher.Sinks.Empty;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
public class CachedIndexSearcherManager implements IndexSearcherManager {
|
2021-09-06 15:06:51 +02:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
private static final Logger logger = LogManager.getLogger(CachedIndexSearcherManager.class);
|
2021-12-16 16:14:44 +01:00
|
|
|
private final Executor SEARCH_EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
|
|
|
|
new ShortNamedThreadFactory("lucene-search").withGroup(new ThreadGroup("lucene-search")));
|
2021-11-09 02:14:21 +01:00
|
|
|
private final SearcherFactory SEARCHER_FACTORY = new ExecutorSearcherFactory(SEARCH_EXECUTOR);
|
2021-09-06 18:52:21 +02:00
|
|
|
|
2021-09-06 15:06:51 +02:00
|
|
|
private final SnapshotsManager snapshotsManager;
|
|
|
|
private final Similarity similarity;
|
|
|
|
private final SearcherManager searcherManager;
|
|
|
|
private final Duration queryRefreshDebounceTime;
|
2021-09-06 18:29:10 +02:00
|
|
|
private final Phaser activeSearchers = new Phaser(1);
|
2021-09-06 18:52:21 +02:00
|
|
|
private final Phaser activeRefreshes = new Phaser(1);
|
2021-09-06 15:06:51 +02:00
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
private final LoadingCache<LLSnapshot, Mono<Send<LLIndexSearcher>>> cachedSnapshotSearchers;
|
|
|
|
private final Mono<Send<LLIndexSearcher>> cachedMainSearcher;
|
2021-09-06 15:06:51 +02:00
|
|
|
|
2021-10-26 00:02:08 +02:00
|
|
|
private final AtomicBoolean closeRequested = new AtomicBoolean();
|
|
|
|
private final Empty<Void> closeRequestedMono = Sinks.empty();
|
2021-09-08 21:34:52 +02:00
|
|
|
private final Mono<Void> closeMono;
|
2021-09-06 15:06:51 +02:00
|
|
|
|
2021-09-06 18:24:36 +02:00
|
|
|
public CachedIndexSearcherManager(IndexWriter indexWriter,
|
2021-09-06 15:06:51 +02:00
|
|
|
SnapshotsManager snapshotsManager,
|
|
|
|
Similarity similarity,
|
|
|
|
boolean applyAllDeletes,
|
|
|
|
boolean writeAllDeletes,
|
|
|
|
Duration queryRefreshDebounceTime) throws IOException {
|
|
|
|
this.snapshotsManager = snapshotsManager;
|
|
|
|
this.similarity = similarity;
|
|
|
|
this.queryRefreshDebounceTime = queryRefreshDebounceTime;
|
|
|
|
|
2021-09-25 13:06:24 +02:00
|
|
|
this.searcherManager = new SearcherManager(indexWriter, applyAllDeletes, writeAllDeletes, SEARCHER_FACTORY);
|
2021-09-06 15:06:51 +02:00
|
|
|
|
2021-10-26 00:02:08 +02:00
|
|
|
Empty<Void> refresherClosed = Sinks.empty();
|
2021-09-06 15:06:51 +02:00
|
|
|
Mono
|
2021-09-06 18:52:21 +02:00
|
|
|
.fromRunnable(() -> {
|
|
|
|
try {
|
2021-09-08 21:34:52 +02:00
|
|
|
maybeRefresh();
|
2021-09-06 18:52:21 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error("Failed to refresh the searcher manager", ex);
|
|
|
|
}
|
|
|
|
})
|
2021-12-16 16:14:44 +01:00
|
|
|
.subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic()))
|
|
|
|
.publishOn(Schedulers.parallel())
|
2021-09-09 11:43:37 +02:00
|
|
|
.repeatWhen(s -> s.delayElements(queryRefreshDebounceTime))
|
2021-10-26 00:02:08 +02:00
|
|
|
.takeUntilOther(closeRequestedMono.asMono())
|
2021-09-06 15:06:51 +02:00
|
|
|
.doAfterTerminate(refresherClosed::tryEmitEmpty)
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
this.cachedSnapshotSearchers = CacheBuilder.newBuilder()
|
|
|
|
.expireAfterWrite(queryRefreshDebounceTime)
|
2021-09-06 15:08:07 +02:00
|
|
|
// Max 3 cached non-main index writers
|
|
|
|
.maximumSize(3)
|
2021-09-06 15:06:51 +02:00
|
|
|
.build(new CacheLoader<>() {
|
|
|
|
@Override
|
2021-09-18 18:34:21 +02:00
|
|
|
public Mono<Send<LLIndexSearcher>> load(@NotNull LLSnapshot snapshot) {
|
2021-09-06 18:24:36 +02:00
|
|
|
return CachedIndexSearcherManager.this.generateCachedSearcher(snapshot);
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.cachedMainSearcher = this.generateCachedSearcher(null);
|
2021-09-08 21:34:52 +02:00
|
|
|
|
|
|
|
this.closeMono = Mono
|
|
|
|
.fromRunnable(() -> {
|
|
|
|
logger.info("Closing IndexSearcherManager...");
|
2021-10-26 00:02:08 +02:00
|
|
|
this.closeRequested.set(true);
|
|
|
|
this.closeRequestedMono.tryEmitEmpty();
|
2021-09-08 21:34:52 +02:00
|
|
|
})
|
|
|
|
.then(refresherClosed.asMono())
|
|
|
|
.then(Mono.<Void>fromRunnable(() -> {
|
|
|
|
logger.info("Closed IndexSearcherManager");
|
|
|
|
logger.info("Closing refreshes...");
|
|
|
|
if (!activeRefreshes.isTerminated()) {
|
|
|
|
try {
|
2021-12-16 16:14:44 +01:00
|
|
|
//noinspection BlockingMethodInNonBlockingContext
|
2021-09-08 21:34:52 +02:00
|
|
|
activeRefreshes.awaitAdvanceInterruptibly(activeRefreshes.arrive(), 15, TimeUnit.SECONDS);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
if (ex instanceof TimeoutException) {
|
|
|
|
logger.error("Failed to terminate active refreshes: timeout");
|
|
|
|
} else {
|
|
|
|
logger.error("Failed to terminate active refreshes", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.info("Closed refreshes...");
|
|
|
|
logger.info("Closing active searchers...");
|
|
|
|
if (!activeSearchers.isTerminated()) {
|
|
|
|
try {
|
2021-12-16 16:14:44 +01:00
|
|
|
//noinspection BlockingMethodInNonBlockingContext
|
2021-09-08 21:34:52 +02:00
|
|
|
activeSearchers.awaitAdvanceInterruptibly(activeSearchers.arrive(), 15, TimeUnit.SECONDS);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
if (ex instanceof TimeoutException) {
|
|
|
|
logger.error("Failed to terminate active searchers: timeout");
|
|
|
|
} else {
|
|
|
|
logger.error("Failed to terminate active searchers", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.info("Closed active searchers");
|
|
|
|
cachedSnapshotSearchers.invalidateAll();
|
|
|
|
cachedSnapshotSearchers.cleanUp();
|
2021-12-16 16:14:44 +01:00
|
|
|
}).subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic())))
|
|
|
|
.publishOn(Schedulers.parallel())
|
|
|
|
.cache();
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
private Mono<Send<LLIndexSearcher>> generateCachedSearcher(@Nullable LLSnapshot snapshot) {
|
2021-12-12 16:19:50 +01:00
|
|
|
return Mono.fromCallable(() -> {
|
|
|
|
if (closeRequested.get()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
activeSearchers.register();
|
|
|
|
IndexSearcher indexSearcher;
|
|
|
|
boolean decRef;
|
|
|
|
if (snapshot == null) {
|
|
|
|
indexSearcher = searcherManager.acquire();
|
|
|
|
decRef = true;
|
|
|
|
} else {
|
|
|
|
indexSearcher = snapshotsManager.resolveSnapshot(snapshot).getIndexSearcher(SEARCH_EXECUTOR);
|
|
|
|
decRef = false;
|
|
|
|
}
|
|
|
|
indexSearcher.setSimilarity(similarity);
|
|
|
|
assert indexSearcher.getIndexReader().getRefCount() > 0;
|
|
|
|
return new LLIndexSearcher(indexSearcher, decRef, this::dropCachedIndexSearcher).send();
|
|
|
|
})
|
|
|
|
.doOnDiscard(Send.class, Send::close)
|
|
|
|
.doOnDiscard(Resource.class, Resource::close);
|
2021-09-18 18:34:21 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 19:17:33 +02:00
|
|
|
private void dropCachedIndexSearcher() {
|
2021-09-18 18:34:21 +02:00
|
|
|
// This shouldn't happen more than once per searcher.
|
|
|
|
activeSearchers.arriveAndDeregister();
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
@Override
|
2021-09-06 15:06:51 +02:00
|
|
|
public void maybeRefreshBlocking() throws IOException {
|
2021-09-06 17:35:02 +02:00
|
|
|
try {
|
2021-09-06 18:52:21 +02:00
|
|
|
activeRefreshes.register();
|
2021-09-06 17:35:02 +02:00
|
|
|
searcherManager.maybeRefreshBlocking();
|
|
|
|
} catch (AlreadyClosedException ignored) {
|
|
|
|
|
2021-09-06 18:52:21 +02:00
|
|
|
} finally {
|
|
|
|
activeRefreshes.arriveAndDeregister();
|
2021-09-06 17:35:02 +02:00
|
|
|
}
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
@Override
|
2021-09-06 15:06:51 +02:00
|
|
|
public void maybeRefresh() throws IOException {
|
2021-09-06 17:35:02 +02:00
|
|
|
try {
|
2021-09-06 18:52:21 +02:00
|
|
|
activeRefreshes.register();
|
2021-09-06 17:35:02 +02:00
|
|
|
searcherManager.maybeRefresh();
|
|
|
|
} catch (AlreadyClosedException ignored) {
|
|
|
|
|
2021-09-06 18:52:21 +02:00
|
|
|
} finally {
|
|
|
|
activeRefreshes.arriveAndDeregister();
|
2021-09-06 17:35:02 +02:00
|
|
|
}
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
@Override
|
|
|
|
public Mono<Send<LLIndexSearcher>> retrieveSearcher(@Nullable LLSnapshot snapshot) {
|
2021-09-06 15:06:51 +02:00
|
|
|
if (snapshot == null) {
|
|
|
|
return this.cachedMainSearcher;
|
|
|
|
} else {
|
|
|
|
return this.cachedSnapshotSearchers.getUnchecked(snapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 18:34:21 +02:00
|
|
|
@Override
|
2021-09-06 15:06:51 +02:00
|
|
|
public Mono<Void> close() {
|
2021-09-08 21:34:52 +02:00
|
|
|
return closeMono;
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|
2021-09-25 13:06:24 +02:00
|
|
|
|
2021-09-06 15:06:51 +02:00
|
|
|
}
|