Skip to content

Commit b7e7559

Browse files
s1monwtlrx
authored andcommitted
Build DocStats from SegmentInfos in ReadOnlyEngine (#34079)
This change is related to #33903 that ports the DocStats simplification to the master branch. This change builds the docStats in the ReadOnlyEngine from the last committed segment infos rather than the reader. Co-authored-by: Tanguy Leroux <tlrx.dev@gmail.com>
1 parent 35a8049 commit b7e7559

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.lucene.index.DirectoryReader;
2222
import org.apache.lucene.index.IndexCommit;
2323
import org.apache.lucene.index.IndexWriter;
24+
import org.apache.lucene.index.SegmentCommitInfo;
2425
import org.apache.lucene.index.SegmentInfos;
2526
import org.apache.lucene.index.SoftDeletesDirectoryReaderWrapper;
2627
import org.apache.lucene.search.IndexSearcher;
@@ -95,15 +96,15 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
9596
this.lastCommittedSegmentInfos = Lucene.readSegmentInfos(directory);
9697
this.translogStats = translogStats == null ? new TranslogStats(0, 0, 0, 0, 0) : translogStats;
9798
this.seqNoStats = seqNoStats == null ? buildSeqNoStats(lastCommittedSegmentInfos) : seqNoStats;
98-
reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), config.getShardId());
99+
reader = ElasticsearchDirectoryReader.wrap(open(directory), config.getShardId());
99100
if (config.getIndexSettings().isSoftDeleteEnabled()) {
100101
reader = new SoftDeletesDirectoryReaderWrapper(reader, Lucene.SOFT_DELETES_FIELD);
101102
}
102103
reader = readerWrapperFunction.apply(reader);
103104
this.indexCommit = reader.getIndexCommit();
104105
this.searcherManager = new SearcherManager(reader,
105106
new RamAccountingSearcherFactory(engineConfig.getCircuitBreakerService()));
106-
this.docsStats = docsStats(reader);
107+
this.docsStats = docsStats(lastCommittedSegmentInfos);
107108
this.indexWriterLock = indexWriterLock;
108109
success = true;
109110
} finally {
@@ -116,6 +117,28 @@ public ReadOnlyEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats
116117
}
117118
}
118119

120+
protected DirectoryReader open(final Directory directory) throws IOException {
121+
return DirectoryReader.open(directory);
122+
}
123+
124+
private DocsStats docsStats(final SegmentInfos lastCommittedSegmentInfos) {
125+
long numDocs = 0;
126+
long numDeletedDocs = 0;
127+
long sizeInBytes = 0;
128+
if (lastCommittedSegmentInfos != null) {
129+
for (SegmentCommitInfo segmentCommitInfo : lastCommittedSegmentInfos) {
130+
numDocs += segmentCommitInfo.info.maxDoc() - segmentCommitInfo.getDelCount() - segmentCommitInfo.getSoftDelCount();
131+
numDeletedDocs += segmentCommitInfo.getDelCount() + segmentCommitInfo.getSoftDelCount();
132+
try {
133+
sizeInBytes += segmentCommitInfo.sizeInBytes();
134+
} catch (IOException e) {
135+
throw new UncheckedIOException("Failed to get size for [" + segmentCommitInfo.info.name + "]", e);
136+
}
137+
}
138+
}
139+
return new DocsStats(numDocs, numDeletedDocs, sizeInBytes);
140+
}
141+
119142
@Override
120143
protected void closeNoLock(String reason, CountDownLatch closedLatch) {
121144
if (isClosed.compareAndSet(false, true)) {

0 commit comments

Comments
 (0)