(index) Prevent index from attempting to answer queries when no index data is loaded

This improves query times, and gets rid of exceptions in the logs when one of the index nodes doesn't have any data loaded, yet is configured to answer queries.
This commit is contained in:
Viktor Lofgren 2024-01-17 21:14:57 +01:00
parent 321fa94b8f
commit aa2df327db
4 changed files with 24 additions and 5 deletions

View File

@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Executors;
import static nu.marginalia.index.forward.ForwardIndexParameters.*;
@ -114,4 +113,8 @@ public class ForwardIndexReader {
if (data != null)
data.close();
}
public boolean isLoaded() {
return data != null;
}
}

View File

@ -5,7 +5,6 @@ import com.google.inject.Singleton;
import nu.marginalia.index.IndexServicesFactory;
import nu.marginalia.index.query.*;
import nu.marginalia.index.query.filter.QueryFilterStepFromPredicate;
import nu.marginalia.index.svc.IndexSearchSetsService;
import nu.marginalia.service.control.ServiceEventLog;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@ -96,14 +95,20 @@ public class SearchIndex {
}
/** Returns true if the service has initialized */
public boolean isAvailable() {
return indexReader != null;
}
/** Stronger version of isAvailable() that also checks that the index is loaded */
public boolean isLoaded() {
return indexReader != null && indexReader.isLoaded();
}
public List<IndexQuery> createQueries(SearchIndexSearchTerms terms, IndexQueryParams params, LongPredicate includePred) {
if (!isAvailable()) {
if (!isLoaded()) {
logger.warn("Index reader not ready");
return Collections.emptyList();
}
@ -188,10 +193,10 @@ public class SearchIndex {
public long[] getTermMetadata(long termId, long[] docs) {
return indexReader.getMetadata(termId, docs);
}
public long getDocumentMetadata(long docId) {
return indexReader.getDocumentMetadata(docId);
}
public int getHtmlFeatures(long docId) {
return indexReader.getHtmlFeatures(docId);
}
@ -199,10 +204,10 @@ public class SearchIndex {
public int getTotalDocCount() {
return indexReader.totalDocCount();
}
public int getTermFrequency(long id) {
return (int) indexReader.numHits(id);
}
public int getTermFrequencyPrio(long id) {
return (int) indexReader.numHitsPrio(id);
}

View File

@ -93,4 +93,10 @@ public class SearchIndexReader {
}
logger.error("Failed to close index");
}
/** Returns true if index data is available */
public boolean isLoaded() {
// We only need to check one of the readers, as they are either all loaded or none are
return forwardIndexReader.isLoaded();
}
}

View File

@ -265,6 +265,11 @@ public class IndexQueryService extends IndexApiImplBase {
}
private SearchResultSet executeSearch(SearchParameters params) throws SQLException {
if (!index.isLoaded()) {
// Short-circuit if the index is not loaded, as we trivially know that there can be no results
return new SearchResultSet(List.of());
}
var rankingContext = createRankingContext(params.rankingParams, params.subqueries);
logger.info(queryMarker, "{}", params.queryParams);