Skip to content

Commit e8b8010

Browse files
committed
Add estimatedByteSizes to merges kicked off by IndexWriter.addIndexes(CodecReader[]) (backport of #15120).
1 parent 7982423 commit e8b8010

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

lucene/CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ Changes in Runtime Behavior
180180

181181
Bug Fixes
182182
---------------------
183+
184+
* GITHUB#15120: Add estimatedByteSizes to merges kicked off by
185+
IndexWriter.addIndexes(CodecReader[]) (Craig Perkins)
186+
183187
* GITHUB*15025: flush segments in addIndexes, not in addIndexesReaderMerge;
184188
reduces extra needless merge activity
185189

lucene/core/src/java/org/apache/lucene/index/IndexWriter.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.Closeable;
2323
import java.io.IOException;
24+
import java.io.UncheckedIOException;
2425
import java.time.Instant;
2526
import java.util.ArrayDeque;
2627
import java.util.ArrayList;
@@ -3321,6 +3322,11 @@ public AddIndexesMergeSource(IndexWriter writer) {
33213322
}
33223323

33233324
public void registerMerge(MergePolicy.OneMerge merge) {
3325+
try {
3326+
addEstimatedBytesToMerge(merge);
3327+
} catch (IOException e) {
3328+
throw new UncheckedIOException(e);
3329+
}
33243330
synchronized (IndexWriter.this) {
33253331
pendingAddIndexesMerges.add(merge);
33263332
}
@@ -4825,6 +4831,21 @@ private void abortOneMerge(MergePolicy.OneMerge merge) throws IOException {
48254831
closeMergeReaders(merge, true, false);
48264832
}
48274833

4834+
/** Compute {@code estimatedMergeBytes} and {@code totalMergeBytes} for a merge. */
4835+
void addEstimatedBytesToMerge(MergePolicy.OneMerge merge) throws IOException {
4836+
assert merge.estimatedMergeBytes == 0;
4837+
assert merge.totalMergeBytes == 0;
4838+
for (SegmentCommitInfo info : merge.segments) {
4839+
if (info.info.maxDoc() > 0) {
4840+
final int delCount = numDeletedDocs(info);
4841+
assert delCount <= info.info.maxDoc();
4842+
final double delRatio = ((double) delCount) / info.info.maxDoc();
4843+
merge.estimatedMergeBytes += (long) (info.sizeInBytes() * (1.0 - delRatio));
4844+
merge.totalMergeBytes += info.sizeInBytes();
4845+
}
4846+
}
4847+
}
4848+
48284849
/**
48294850
* Checks whether this merge involves any segments already participating in a merge. If not, this
48304851
* merge is "registered", meaning we record that its segments are now participating in a merge,
@@ -4916,17 +4937,7 @@ private synchronized boolean registerMerge(MergePolicy.OneMerge merge) throws IO
49164937
mergingSegments.add(info);
49174938
}
49184939

4919-
assert merge.estimatedMergeBytes == 0;
4920-
assert merge.totalMergeBytes == 0;
4921-
for (SegmentCommitInfo info : merge.segments) {
4922-
if (info.info.maxDoc() > 0) {
4923-
final int delCount = numDeletedDocs(info);
4924-
assert delCount <= info.info.maxDoc();
4925-
final double delRatio = ((double) delCount) / info.info.maxDoc();
4926-
merge.estimatedMergeBytes += (long) (info.sizeInBytes() * (1.0 - delRatio));
4927-
merge.totalMergeBytes += info.sizeInBytes();
4928-
}
4929-
}
4940+
addEstimatedBytesToMerge(merge);
49304941

49314942
// Merge is now registered
49324943
merge.registerDone = true;

lucene/core/src/test/org/apache/lucene/index/TestIndexWriterMerging.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,35 @@ public void run() {
465465

466466
directory.close();
467467
}
468+
469+
public void testAddEstimatedBytesToMerge() throws IOException {
470+
try (Directory dir = newDirectory();
471+
IndexWriter writer =
472+
new IndexWriter(
473+
dir,
474+
newIndexWriterConfig(new MockAnalyzer(random()))
475+
.setMergePolicy(NoMergePolicy.INSTANCE))) {
476+
477+
Document doc = new Document();
478+
doc.add(newTextField("field", "content", Field.Store.YES));
479+
480+
for (int i = 0; i < 10; i++) {
481+
482+
writer.addDocument(doc);
483+
}
484+
writer.flush();
485+
486+
// Create a merge with the segments
487+
SegmentInfos segmentInfos = writer.cloneSegmentInfos();
488+
MergePolicy.OneMerge merge = new MergePolicy.OneMerge(segmentInfos.asList());
489+
490+
writer.addEstimatedBytesToMerge(merge);
491+
492+
assertTrue(merge.estimatedMergeBytes > 0);
493+
494+
assertTrue(merge.totalMergeBytes > 0);
495+
496+
assertTrue(merge.estimatedMergeBytes <= merge.totalMergeBytes);
497+
}
498+
}
468499
}

0 commit comments

Comments
 (0)