34
34
import org .apache .lucene .util .ArrayUtil ;
35
35
import org .elasticsearch .common .bytes .BytesReference ;
36
36
import org .elasticsearch .common .lucene .Lucene ;
37
+ import org .elasticsearch .core .internal .io .IOUtils ;
37
38
import org .elasticsearch .index .VersionType ;
38
39
import org .elasticsearch .index .fieldvisitor .FieldsVisitor ;
39
40
import org .elasticsearch .index .mapper .IdFieldMapper ;
49
50
import java .util .Comparator ;
50
51
import java .util .List ;
51
52
import java .util .Objects ;
53
+ import java .util .concurrent .atomic .AtomicBoolean ;
52
54
53
55
/**
54
56
* A {@link Translog.Snapshot} from changes in a Lucene index
@@ -88,6 +90,12 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
88
90
if (searchBatchSize < 0 ) {
89
91
throw new IllegalArgumentException ("Search_batch_size must not be negative [" + searchBatchSize + "]" );
90
92
}
93
+ final AtomicBoolean closed = new AtomicBoolean ();
94
+ this .onClose = () -> {
95
+ if (closed .compareAndSet (false , true )) {
96
+ IOUtils .close (engineSearcher );
97
+ }
98
+ };
91
99
this .mapperService = mapperService ;
92
100
this .searchBatchSize = searchBatchSize ;
93
101
this .fromSeqNo = fromSeqNo ;
@@ -98,10 +106,8 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
98
106
this .indexSearcher .setQueryCache (null );
99
107
this .parallelArray = new ParallelArray (searchBatchSize );
100
108
final TopDocs topDocs = searchOperations (null );
101
-
102
109
this .totalHits = Math .toIntExact (topDocs .totalHits );
103
110
this .scoreDocs = topDocs .scoreDocs ;
104
- this .onClose = engineSearcher ;
105
111
fillParallelArray (scoreDocs , parallelArray );
106
112
}
107
113
0 commit comments