codaitya commented on a change in pull request #446: URL: https://github.com/apache/lucene/pull/446#discussion_r756849130
########## File path: lucene/sandbox/src/java/org/apache/lucene/sandbox/index/MergeOnFlushTieredMergePolicy.java ########## @@ -0,0 +1,138 @@ +/* + * 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.ArrayList; +import java.util.List; +import org.apache.lucene.index.MergeTrigger; +import org.apache.lucene.index.SegmentCommitInfo; +import org.apache.lucene.index.SegmentInfos; +import org.apache.lucene.index.TieredMergePolicy; + +/** + * A simple extension to {@link TieredMergePolicy} to merge all tiny segments (or at least segments + * smaller than specified in setSmallSegmentThresholdMB) into one segment on commit. + */ +public class MergeOnFlushTieredMergePolicy extends TieredMergePolicy { + private double maxSegmentSizeAsProportionOfIndex = 1.0; + private long smallSegmentThresholdBytes = Units.mbToBytes(100.0); + private long hardMaxSegmentSizeBytes = 5 * 1024 * 1024 * 1024L; + + private static long indexSize(SegmentInfos infos) throws IOException { + long totalSize = 0; + for (SegmentCommitInfo sci : infos) { + totalSize += sci.sizeInBytes(); + } + return totalSize; + } + + public double getSmallSegmentThresholdMB() { + return Units.bytesToMB(smallSegmentThresholdBytes); + } + + /** + * @param smallSegmentThresholdMB all segments smaller than this will be merged into a single + * segment before commit completes. + */ + public void setSmallSegmentThresholdMB(double smallSegmentThresholdMB) { + this.smallSegmentThresholdBytes = Units.mbToBytes(smallSegmentThresholdMB); + } + + public double getMaxSegmentSizeAsProportionOfIndex() { + return maxSegmentSizeAsProportionOfIndex; + } + + public void setMaxSegmentSizeAsProportionOfIndex(double maxSegmentSizeAsProportionOfIndex) { + if (maxSegmentSizeAsProportionOfIndex < 0.0 || maxSegmentSizeAsProportionOfIndex > 1.0) { + throw new IllegalArgumentException( + "maxSegmentSizeAsProportionOfIndex must be between 0 and 1; got: " + + maxSegmentSizeAsProportionOfIndex); + } + this.maxSegmentSizeAsProportionOfIndex = maxSegmentSizeAsProportionOfIndex; + } + + @Override + public TieredMergePolicy setMaxMergedSegmentMB(double v) { + this.hardMaxSegmentSizeBytes = Units.mbToBytes(v); Review comment: @msokolov TMP.setMaxMergedSegmentMB sets its member variable `maxMergedSegmentBytes` while here we set it for MergeOnFlushTMP. We then use it later in f`indmerges` to calculate the size to be set for `maxSegmentSizeMB`. ``` maxSegmentSizeMB = Units.bytesToMB( Math.min( hardMaxSegmentSizeBytes, Math.max( Units.mbToBytes(getFloorSegmentMB()), (long) (getMaxSegmentSizeAsProportionOfIndex() * indexSize(infos))))); ``` @ uschindler good point about the chaining . I will update. -- 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