codaitya commented on a change in pull request #446: URL: https://github.com/apache/lucene/pull/446#discussion_r816915526
########## File path: lucene/sandbox/src/test/org/apache/lucene/sandbox/index/TestMergeOnFlushMergePolicy.java ########## @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.sandbox.index; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.MergePolicy; +import org.apache.lucene.index.MergeTrigger; +import org.apache.lucene.index.SegmentCommitInfo; +import org.apache.lucene.index.SegmentInfos; +import org.apache.lucene.tests.index.BaseMergePolicyTestCase; +import org.apache.lucene.tests.util.NullInfoStream; +import org.apache.lucene.tests.util.TestUtil; +import org.apache.lucene.util.InfoStream; +import org.apache.lucene.util.Version; +import org.junit.Ignore; + +/** Test for {@link MergeOnFlushMergePolicy}. */ +public class TestMergeOnFlushMergePolicy extends BaseMergePolicyTestCase { + + @Override + protected MergePolicy mergePolicy() { + Random r = random(); + MergePolicy mergePolicy = newMergePolicy(); + MergeOnFlushMergePolicy mergeOnFlushPolicy = new MergeOnFlushMergePolicy(mergePolicy); + mergeOnFlushPolicy.setMaxCFSSegmentSizeMB(mergePolicy.getMaxCFSSegmentSizeMB()); + mergeOnFlushPolicy.setNoCFSRatio(mergePolicy.getNoCFSRatio()); + mergeOnFlushPolicy.setSmallSegmentThresholdMB(TestUtil.nextInt(r, 1, 100)); + return mergeOnFlushPolicy; + } + + @Override + protected void assertSegmentInfos(MergePolicy policy, SegmentInfos infos) {} + + @Override + public void testForceMergeNotNeeded() throws IOException { + super.testForceMergeNotNeeded(); + } + + @Override + protected void assertMerge(MergePolicy policy, MergePolicy.MergeSpecification merge) {} + + public void testFindCommitMerges() throws IOException { + MergeOnFlushMergePolicy mergePolicy = (MergeOnFlushMergePolicy) mergePolicy(); + double smallSegmentThresholdMB = mergePolicy.getSmallSegmentThresholdMB(); + Random r = random(); + + for (int j = 0; j < 10_000; j++) { + SegmentInfos segmentInfos = new SegmentInfos(Version.LATEST.major); + int numSegs = random().nextInt(50); + Set<SegmentCommitInfo> mergingSegments = new HashSet<>(); + Set<SegmentCommitInfo> smallSegments = new HashSet<>(); + for (int i = 0; i < numSegs; i++) { + SegmentCommitInfo sci = + makeSegmentCommitInfo( + "_" + i, + TestUtil.nextInt(r, 10, 100), + r.nextInt(10), + r.nextDouble() * 2.0 * smallSegmentThresholdMB, + IndexWriter.SOURCE_FLUSH); + if (sci.sizeInBytes() < MergeOnFlushMergePolicy.Units.mbToBytes(smallSegmentThresholdMB)) { + smallSegments.add(sci); + } + if (r.nextBoolean()) { + mergingSegments.add(sci); + } + segmentInfos.add(sci); + } + MergePolicy.MergeContext context = new MergingMockMergeContext(mergingSegments); + MergePolicy.MergeSpecification mergeSpecification; + + mergeSpecification = + mergePolicy.findFullFlushMerges(MergeTrigger.COMMIT, segmentInfos, context); + + if (mergeSpecification == null) { + // If we didn't compute a merge, then we have at most one small segment not already + // participating in a merge. + boolean foundNonMergingSmallSegment = false; + for (SegmentCommitInfo smallSegment : smallSegments) { + if (!mergingSegments.contains(smallSegment)) { + assertFalse( + "If no merges, then at most one small segment is not already merging", + foundNonMergingSmallSegment); + foundNonMergingSmallSegment = true; + } + } + } else { + for (MergePolicy.OneMerge oneMerge : mergeSpecification.merges) { + for (SegmentCommitInfo sci : oneMerge.segments) { + assertTrue("Merges only contain small segments", smallSegments.contains(sci)); + assertFalse( + "findFullFlushMerges must not return already merging segments", + mergingSegments.contains(sci)); + } + } + } + } + } + + @Override + @Ignore // This test takes > 2 minutes on a 2015 Macbook Pro Review comment: Thanks for pointing to this. What I find is that there are some instances when `newMergePolicy()` creates a `TieredMergePolicy` which sometimes gets initialized with a low value of `MaxMergedSegmentMB` making this test (specially testSimulateAppendOnly) slow. So if I set this to a high enough value, this test becomes super fast. When I look at the existing test in `TestTieredMergePolicy` I see this lines [link](https://github.com/apache/lucene/blob/main/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java#L884) ``` // Avoid low values of the max merged segment size which prevent this merge policy from scaling // well mergePolicy.setMaxMergedSegmentMB(TestUtil.nextInt(random(), 1024, 10 * 1024)); ``` So maybe I should follow a similar approach here, by checking if the mergepolicy created is an instance of `TieredMergePolicy` and then overriding `setMaxMergedSegmentMB` ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org