Skip to content

Commit d357f9f

Browse files
committed
Move up acquireSearcher logic to Engine (#33453)
By moving the logic to acquire the searcher up to the engine it's simpler to build new engines that are for instance read-only.
1 parent 23033e5 commit d357f9f

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

server/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.lucene.index.SegmentReader;
3434
import org.apache.lucene.index.Term;
3535
import org.apache.lucene.search.IndexSearcher;
36+
import org.apache.lucene.search.ReferenceManager;
3637
import org.apache.lucene.store.AlreadyClosedException;
3738
import org.apache.lucene.store.Directory;
3839
import org.apache.lucene.store.IOContext;
@@ -569,7 +570,31 @@ public final Searcher acquireSearcher(String source) throws EngineException {
569570
*
570571
* @see Searcher#close()
571572
*/
572-
public abstract Searcher acquireSearcher(String source, SearcherScope scope) throws EngineException;
573+
public Searcher acquireSearcher(String source, SearcherScope scope) throws EngineException {
574+
/* Acquire order here is store -> manager since we need
575+
* to make sure that the store is not closed before
576+
* the searcher is acquired. */
577+
if (store.tryIncRef() == false) {
578+
throw new AlreadyClosedException(shardId + " store is closed", failedEngine.get());
579+
}
580+
Releasable releasable = store::decRef;
581+
try {
582+
EngineSearcher engineSearcher = new EngineSearcher(source, getReferenceManager(scope), store, logger);
583+
releasable = null; // success - hand over the reference to the engine searcher
584+
return engineSearcher;
585+
} catch (AlreadyClosedException ex) {
586+
throw ex;
587+
} catch (Exception ex) {
588+
maybeFailEngine("acquire_searcher", ex);
589+
ensureOpen(ex); // throw EngineCloseException here if we are already closed
590+
logger.error(() -> new ParameterizedMessage("failed to acquire searcher, source {}", source), ex);
591+
throw new EngineException(shardId, "failed to acquire searcher, source " + source, ex);
592+
} finally {
593+
Releasables.close(releasable);
594+
}
595+
}
596+
597+
protected abstract ReferenceManager<IndexSearcher> getReferenceManager(SearcherScope scope);
573598

574599
public enum SearcherScope {
575600
EXTERNAL, INTERNAL

server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.index.engine;
2121

2222
import org.apache.logging.log4j.Logger;
23-
import org.apache.logging.log4j.message.ParameterizedMessage;
2423
import org.apache.lucene.document.Field;
2524
import org.apache.lucene.document.NumericDocValuesField;
2625
import org.apache.lucene.index.DirectoryReader;
@@ -53,7 +52,6 @@
5352
import org.elasticsearch.common.Nullable;
5453
import org.elasticsearch.common.SuppressForbidden;
5554
import org.elasticsearch.common.lease.Releasable;
56-
import org.elasticsearch.common.lease.Releasables;
5755
import org.elasticsearch.common.lucene.LoggerInfoStream;
5856
import org.elasticsearch.common.lucene.Lucene;
5957
import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader;
@@ -1527,19 +1525,11 @@ final void refresh(String source, SearcherScope scope) throws EngineException {
15271525
if (store.tryIncRef()) {
15281526
// increment the ref just to ensure nobody closes the store during a refresh
15291527
try {
1530-
switch (scope) {
1531-
case EXTERNAL:
1532-
// even though we maintain 2 managers we really do the heavy-lifting only once.
1533-
// the second refresh will only do the extra work we have to do for warming caches etc.
1534-
externalSearcherManager.maybeRefreshBlocking();
1535-
// the break here is intentional we never refresh both internal / external together
1536-
break;
1537-
case INTERNAL:
1538-
internalSearcherManager.maybeRefreshBlocking();
1539-
break;
1540-
default:
1541-
throw new IllegalArgumentException("unknown scope: " + scope);
1542-
}
1528+
// even though we maintain 2 managers we really do the heavy-lifting only once.
1529+
// the second refresh will only do the extra work we have to do for warming caches etc.
1530+
ReferenceManager<IndexSearcher> referenceManager = getReferenceManager(scope);
1531+
// it is intentional that we never refresh both internal / external together
1532+
referenceManager.maybeRefreshBlocking();
15431533
} finally {
15441534
store.decRef();
15451535
}
@@ -2090,37 +2080,14 @@ protected final void closeNoLock(String reason, CountDownLatch closedLatch) {
20902080
}
20912081

20922082
@Override
2093-
public Searcher acquireSearcher(String source, SearcherScope scope) {
2094-
/* Acquire order here is store -> manager since we need
2095-
* to make sure that the store is not closed before
2096-
* the searcher is acquired. */
2097-
if (store.tryIncRef() == false) {
2098-
throw new AlreadyClosedException(shardId + " store is closed", failedEngine.get());
2099-
}
2100-
Releasable releasable = store::decRef;
2101-
try {
2102-
final ReferenceManager<IndexSearcher> referenceManager;
2103-
switch (scope) {
2104-
case INTERNAL:
2105-
referenceManager = internalSearcherManager;
2106-
break;
2107-
case EXTERNAL:
2108-
referenceManager = externalSearcherManager;
2109-
break;
2110-
default:
2111-
throw new IllegalStateException("unknown scope: " + scope);
2112-
}
2113-
EngineSearcher engineSearcher = new EngineSearcher(source, referenceManager, store, logger);
2114-
releasable = null; // success - hand over the reference to the engine searcher
2115-
return engineSearcher;
2116-
} catch (AlreadyClosedException ex) {
2117-
throw ex;
2118-
} catch (Exception ex) {
2119-
ensureOpen(ex); // throw EngineCloseException here if we are already closed
2120-
logger.error(() -> new ParameterizedMessage("failed to acquire searcher, source {}", source), ex);
2121-
throw new EngineException(shardId, "failed to acquire searcher, source " + source, ex);
2122-
} finally {
2123-
Releasables.close(releasable);
2083+
protected final ReferenceManager<IndexSearcher> getReferenceManager(SearcherScope scope) {
2084+
switch (scope) {
2085+
case INTERNAL:
2086+
return internalSearcherManager;
2087+
case EXTERNAL:
2088+
return externalSearcherManager;
2089+
default:
2090+
throw new IllegalStateException("unknown scope: " + scope);
21242091
}
21252092
}
21262093

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ private void snapshotFile(final BlobStoreIndexShardSnapshot.FileInfo fileInfo) t
13401340
}
13411341

13421342
private void failStoreIfCorrupted(Exception e) {
1343-
if (e instanceof CorruptIndexException || e instanceof IndexFormatTooOldException || e instanceof IndexFormatTooNewException) {
1343+
if (Lucene.isCorruptionException(e)) {
13441344
try {
13451345
store.markStoreCorrupted((IOException) e);
13461346
} catch (IOException inner) {

0 commit comments

Comments
 (0)