diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java index dfec4c566cc..cfeff53f888 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java @@ -53,6 +53,7 @@ public class TestTieredMergePolicy extends BaseMergePolicyTestCase { int totalDelCount = 0; int totalMaxDoc = 0; long totalBytes = 0; + List segmentSizes = new ArrayList<>(); for (SegmentCommitInfo sci : infos) { totalDelCount += sci.getDelCount(); totalMaxDoc += sci.info.maxDoc(); @@ -60,6 +61,7 @@ public class TestTieredMergePolicy extends BaseMergePolicyTestCase { double liveRatio = 1 - (double) sci.getDelCount() / sci.info.maxDoc(); long weightedByteSize = (long) (liveRatio * byteSize); totalBytes += weightedByteSize; + segmentSizes.add(weightedByteSize); minSegmentBytes = Math.min(minSegmentBytes, weightedByteSize); } @@ -89,6 +91,25 @@ public class TestTieredMergePolicy extends BaseMergePolicyTestCase { } allowedSegCount = Math.max(allowedSegCount, tmp.getSegmentsPerTier()); + // It's ok to be over the allowed segment count if none of the most balanced merges are balanced + // enough + Collections.sort(segmentSizes); + boolean hasBalancedMerges = false; + for (int i = 0; i < segmentSizes.size() - mergeFactor; ++i) { + long maxMergeSegmentSize = segmentSizes.get(i + mergeFactor - 1); + if (maxMergeSegmentSize >= maxMergedSegmentBytes / 2) { + break; + } + long totalMergeSize = 0; + for (int j = 0; j < i + mergeFactor; ++j) { + totalMergeSize += segmentSizes.get(j); + } + if (maxMergedSegmentBytes * 1.5 <= totalMergeSize) { + hasBalancedMerges = true; + break; + } + } + int numSegments = infos.asList().size(); assertTrue( String.format( @@ -104,7 +125,7 @@ public class TestTieredMergePolicy extends BaseMergePolicyTestCase { totalBytes, delPercentage, tmp.getDeletesPctAllowed()), - numSegments <= allowedSegCount); + numSegments <= allowedSegCount || hasBalancedMerges == false); } @Override