Skip to content

Commit 7fde7a0

Browse files
committed
Ensure LuceneChangesSnapshot close once
1 parent 4363363 commit 7fde7a0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.lucene.util.ArrayUtil;
3535
import org.elasticsearch.common.bytes.BytesReference;
3636
import org.elasticsearch.common.lucene.Lucene;
37+
import org.elasticsearch.core.internal.io.IOUtils;
3738
import org.elasticsearch.index.VersionType;
3839
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
3940
import org.elasticsearch.index.mapper.IdFieldMapper;
@@ -49,6 +50,7 @@
4950
import java.util.Comparator;
5051
import java.util.List;
5152
import java.util.Objects;
53+
import java.util.concurrent.atomic.AtomicBoolean;
5254

5355
/**
5456
* A {@link Translog.Snapshot} from changes in a Lucene index
@@ -88,6 +90,12 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
8890
if (searchBatchSize < 0) {
8991
throw new IllegalArgumentException("Search_batch_size must not be negative [" + searchBatchSize + "]");
9092
}
93+
final AtomicBoolean closed = new AtomicBoolean();
94+
this.onClose = () -> {
95+
if (closed.compareAndSet(false, true)) {
96+
IOUtils.close(engineSearcher);
97+
}
98+
};
9199
this.mapperService = mapperService;
92100
this.searchBatchSize = searchBatchSize;
93101
this.fromSeqNo = fromSeqNo;
@@ -98,10 +106,8 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
98106
this.indexSearcher.setQueryCache(null);
99107
this.parallelArray = new ParallelArray(searchBatchSize);
100108
final TopDocs topDocs = searchOperations(null);
101-
102109
this.totalHits = Math.toIntExact(topDocs.totalHits);
103110
this.scoreDocs = topDocs.scoreDocs;
104-
this.onClose = engineSearcher;
105111
fillParallelArray(scoreDocs, parallelArray);
106112
}
107113

0 commit comments

Comments
 (0)