CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/CachedIndexSearcherManager.java

207 lines
7.1 KiB
Java
Raw Normal View History

2021-09-06 15:06:51 +02:00
package it.cavallium.dbengine.database.disk;
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;
2022-03-16 13:47:56 +01:00
import io.netty5.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;
2022-02-25 15:46:32 +01:00
import java.util.concurrent.ExecutorService;
2021-11-09 02:14:21 +01:00
import java.util.concurrent.Executors;
2021-09-07 02:36:11 +02:00
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
2022-02-25 15:46:32 +01:00
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;
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;
2022-04-04 20:12:29 +02:00
import reactor.core.scheduler.Scheduler;
2021-09-06 15:06:51 +02:00
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
private static final Logger logger = LogManager.getLogger(CachedIndexSearcherManager.class);
2022-02-25 15:46:32 +01:00
private final ExecutorService searchExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(),
new ShortNamedThreadFactory("lucene-search")
.setDaemon(true).withGroup(new ThreadGroup("lucene-search"))
);
private final SearcherFactory SEARCHER_FACTORY = new ExecutorSearcherFactory(searchExecutor);
2021-09-06 18:52:21 +02:00
2021-09-06 15:06:51 +02:00
private final SnapshotsManager snapshotsManager;
2022-04-04 20:12:29 +02:00
private final Scheduler luceneHeavyTasksScheduler;
2021-09-06 15:06:51 +02:00
private final Similarity similarity;
private final SearcherManager searcherManager;
private final Duration queryRefreshDebounceTime;
2022-02-25 15:46:32 +01:00
private final AtomicLong activeSearchers = new AtomicLong(0);
private final AtomicLong activeRefreshes = new AtomicLong(0);
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
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,
2022-04-04 20:12:29 +02:00
Scheduler luceneHeavyTasksScheduler,
2021-09-06 15:06:51 +02:00
Similarity similarity,
boolean applyAllDeletes,
boolean writeAllDeletes,
Duration queryRefreshDebounceTime) throws IOException {
this.snapshotsManager = snapshotsManager;
2022-04-04 20:12:29 +02:00
this.luceneHeavyTasksScheduler = luceneHeavyTasksScheduler;
2021-09-06 15:06:51 +02:00
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
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);
}
})
2022-04-04 20:12:29 +02:00
.subscribeOn(luceneHeavyTasksScheduler)
.publishOn(Schedulers.parallel())
2021-09-09 11:43:37 +02:00
.repeatWhen(s -> s.delayElements(queryRefreshDebounceTime))
.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.debug("Closing IndexSearcherManager...");
this.closeRequested.set(true);
this.closeRequestedMono.tryEmitEmpty();
2021-09-08 21:34:52 +02:00
})
.then(refresherClosed.asMono())
.then(Mono.<Void>fromRunnable(() -> {
2022-05-01 17:36:29 +02:00
logger.debug("Closed IndexSearcherManager");
logger.debug("Closing refreshes...");
2022-02-25 15:46:32 +01:00
long initTime = System.nanoTime();
while (activeRefreshes.get() > 0 && (System.nanoTime() - initTime) <= 15000000000L) {
LockSupport.parkNanos(50000000);
2021-09-08 21:34:52 +02:00
}
2022-05-01 17:36:29 +02:00
logger.debug("Closed refreshes...");
logger.debug("Closing active searchers...");
2022-02-25 15:46:32 +01:00
initTime = System.nanoTime();
while (activeSearchers.get() > 0 && (System.nanoTime() - initTime) <= 15000000000L) {
LockSupport.parkNanos(50000000);
2021-09-08 21:34:52 +02:00
}
2022-05-01 17:36:29 +02:00
logger.debug("Closed active searchers");
logger.debug("Stopping searcher executor...");
2021-09-08 21:34:52 +02:00
cachedSnapshotSearchers.invalidateAll();
cachedSnapshotSearchers.cleanUp();
2022-02-25 15:46:32 +01:00
searchExecutor.shutdown();
try {
//noinspection BlockingMethodInNonBlockingContext
if (!searchExecutor.awaitTermination(15, TimeUnit.SECONDS)) {
searchExecutor.shutdownNow();
}
} catch (InterruptedException e) {
logger.error("Failed to stop executor", e);
}
2022-05-01 17:36:29 +02:00
logger.debug("Stopped searcher executor");
}).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;
}
2022-02-25 15:46:32 +01:00
activeSearchers.incrementAndGet();
2021-12-12 16:19:50 +01:00
IndexSearcher indexSearcher;
boolean decRef;
if (snapshot == null) {
indexSearcher = searcherManager.acquire();
decRef = true;
} else {
2022-02-25 15:46:32 +01:00
indexSearcher = snapshotsManager.resolveSnapshot(snapshot).getIndexSearcher(searchExecutor);
2021-12-12 16:19:50 +01:00
decRef = false;
}
indexSearcher.setSimilarity(similarity);
assert indexSearcher.getIndexReader().getRefCount() > 0;
return new LLIndexSearcher(indexSearcher, decRef, this::dropCachedIndexSearcher).send();
2022-01-26 14:22:54 +01:00
});
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.
2022-02-25 15:46:32 +01:00
activeSearchers.decrementAndGet();
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 {
2022-02-25 15:46:32 +01:00
activeRefreshes.incrementAndGet();
2021-09-06 17:35:02 +02:00
searcherManager.maybeRefreshBlocking();
} catch (AlreadyClosedException ignored) {
2021-09-06 18:52:21 +02:00
} finally {
2022-02-25 15:46:32 +01:00
activeRefreshes.decrementAndGet();
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 {
2022-02-25 15:46:32 +01:00
activeRefreshes.incrementAndGet();
2021-09-06 17:35:02 +02:00
searcherManager.maybeRefresh();
} catch (AlreadyClosedException ignored) {
2021-09-06 18:52:21 +02:00
} finally {
2022-02-25 15:46:32 +01:00
activeRefreshes.decrementAndGet();
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
}