richardstartin commented on a change in pull request #7956:
URL: https://github.com/apache/pinot/pull/7956#discussion_r774501653



##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/realtime/segment/SegmentFlushThresholdComputer.java
##########
@@ -0,0 +1,184 @@
+/**
+ * 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.pinot.controller.helix.core.realtime.segment;
+
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.metadata.segment.SegmentZKMetadata;
+import org.apache.pinot.common.utils.LLCSegmentName;
+import org.apache.pinot.spi.stream.PartitionGroupMetadata;
+import org.apache.pinot.spi.stream.PartitionLevelStreamConfig;
+import org.apache.pinot.spi.utils.TimeUtils;
+
+import static 
org.apache.pinot.controller.helix.core.realtime.segment.SegmentSizeBasedFlushThresholdUpdater.LOGGER;
+
+
+public class SegmentFlushThresholdComputer {
+  public static final int MINIMUM_NUM_ROWS_THRESHOLD = 10_000;
+  static final double CURRENT_SEGMENT_RATIO_WEIGHT = 0.1;
+  static final double PREVIOUS_SEGMENT_RATIO_WEIGHT = 0.9;
+  static final double ROWS_MULTIPLIER_WHEN_TIME_THRESHOLD_HIT = 1.1;
+
+  // num rows to segment size ratio of last committed segment for this table
+  private double _latestSegmentRowsToSizeRatio;
+  private final Clock _clock;
+
+  public SegmentFlushThresholdComputer() {
+    this(new SystemClock(), 0);
+  }
+
+  public SegmentFlushThresholdComputer(Clock clock, double 
latestSegmentRowsToSizeRatio) {
+    this._clock = clock;
+    this._latestSegmentRowsToSizeRatio = latestSegmentRowsToSizeRatio;
+  }
+
+  public SegmentFlushThresholdComputer(Clock clock) {
+    this(clock, 0);
+  }
+
+  double getLatestSegmentRowsToSizeRatio() {
+    return _latestSegmentRowsToSizeRatio;
+  }
+
+  public int computeThreshold(PartitionLevelStreamConfig streamConfig,
+      CommittingSegmentDescriptor committingSegmentDescriptor,
+      @Nullable SegmentZKMetadata committingSegmentZKMetadata,
+      List<PartitionGroupMetadata> partitionGroupMetadataList, String 
newSegmentName) {
+    final long desiredSegmentSizeBytes = 
streamConfig.getFlushThresholdSegmentSizeBytes();
+    final long optimalSegmentSizeBytesMin = desiredSegmentSizeBytes / 2;
+    final double optimalSegmentSizeBytesMax = desiredSegmentSizeBytes * 1.5;
+
+    if (committingSegmentZKMetadata == null) { // first segment of the 
partition, hence committing segment is null
+      if (_latestSegmentRowsToSizeRatio > 0) { // new partition group added 
case
+        long targetSegmentNumRows = (long) (desiredSegmentSizeBytes * 
_latestSegmentRowsToSizeRatio);
+        targetSegmentNumRows = capNumRowsIfOverflow(targetSegmentNumRows);
+        LOGGER.info(
+            "Committing segment zk metadata is not available, using prev ratio 
{}, setting rows threshold for {} as {}",
+            _latestSegmentRowsToSizeRatio, newSegmentName, 
targetSegmentNumRows);
+        return (int) targetSegmentNumRows;
+      } else {
+        final int autotuneInitialRows = 
streamConfig.getFlushAutotuneInitialRows();
+        LOGGER.info(
+            "Committing segment zk metadata is not available, setting 
threshold for {} as {}", newSegmentName,
+            autotuneInitialRows);
+        return autotuneInitialRows;
+      }
+    }
+
+    final long committingSegmentSizeBytes = 
committingSegmentDescriptor.getSegmentSizeBytes();
+    if (committingSegmentSizeBytes <= 0) { // repair segment case
+      final int targetNumRows = 
committingSegmentZKMetadata.getSizeThresholdToFlushSegment();
+      LOGGER.info(
+          "Committing segment size is not available, setting thresholds from 
previous segment for {} as {}",
+          newSegmentName, targetNumRows);
+      return targetNumRows;
+    }
+
+    final long timeConsumed = _clock.currentTimeMillis() - 
committingSegmentZKMetadata.getCreationTime();
+    final long numRowsConsumed = committingSegmentZKMetadata.getTotalDocs();
+    final int numRowsThreshold = 
committingSegmentZKMetadata.getSizeThresholdToFlushSegment();
+    LOGGER.info(
+        "{}: Data from committing segment: Time {}  numRows {} threshold {} 
segmentSize(bytes) {}",
+        newSegmentName, TimeUtils.convertMillisToPeriod(timeConsumed), 
numRowsConsumed, numRowsThreshold,
+        committingSegmentSizeBytes);
+
+    double currentRatio = (double) numRowsConsumed / 
committingSegmentSizeBytes;
+    // Compute segment size to rows ratio only from the lowest available 
partition id.
+    // If we consider all partitions then it is likely that we will assign a 
much higher weight to the most
+    // recent trend in the table (since it is usually true that all partitions 
of the same table follow more or
+    // less same characteristics at any one point in time).
+    // However, when we start a new table or change controller mastership, we 
can have any partition completing first.
+    // It is best to learn the ratio as quickly as we can, so we allow any 
partition to supply the value.
+    int smallestAvailablePartitionGroupId =
+        
partitionGroupMetadataList.stream().mapToInt(PartitionGroupMetadata::getPartitionGroupId).min()
+            .orElseGet(() -> 0);

Review comment:
       I know this is just code moved from one place to another but 
`.orElse(0)` would be better




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to