Skip to content

Commit

Permalink
Ensure LuceneChangesSnapshot close once
Browse files Browse the repository at this point in the history
  • Loading branch information
dnhatn committed Jul 13, 2018
1 parent 4363363 commit 7fde7a0
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.lucene.util.ArrayUtil;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.fieldvisitor.FieldsVisitor;
import org.elasticsearch.index.mapper.IdFieldMapper;
Expand All @@ -49,6 +50,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* A {@link Translog.Snapshot} from changes in a Lucene index
Expand Down Expand Up @@ -88,6 +90,12 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
if (searchBatchSize < 0) {
throw new IllegalArgumentException("Search_batch_size must not be negative [" + searchBatchSize + "]");
}
final AtomicBoolean closed = new AtomicBoolean();
this.onClose = () -> {
if (closed.compareAndSet(false, true)) {
IOUtils.close(engineSearcher);
}
};
this.mapperService = mapperService;
this.searchBatchSize = searchBatchSize;
this.fromSeqNo = fromSeqNo;
Expand All @@ -98,10 +106,8 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
this.indexSearcher.setQueryCache(null);
this.parallelArray = new ParallelArray(searchBatchSize);
final TopDocs topDocs = searchOperations(null);

this.totalHits = Math.toIntExact(topDocs.totalHits);
this.scoreDocs = topDocs.scoreDocs;
this.onClose = engineSearcher;
fillParallelArray(scoreDocs, parallelArray);
}

Expand Down

0 comments on commit 7fde7a0

Please sign in to comment.