SzyWilliam commented on code in PR #14866:
URL: https://github.com/apache/iotdb/pull/14866#discussion_r1972707247


##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/load/cache/detector/PhiAccrualDetector.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.iotdb.confignode.manager.load.cache.detector;
+
+import org.apache.iotdb.confignode.manager.load.cache.AbstractHeartbeatSample;
+import org.apache.iotdb.confignode.manager.load.cache.IFailureDetector;
+import org.apache.iotdb.confignode.manager.load.cache.node.NodeHeartbeatSample;
+
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+import org.apache.tsfile.utils.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PhiAccrualDetector implements IFailureDetector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PhiAccrualDetector.class);
+  private final long threshold;
+  private final long acceptableHeartbeatPauseNs;
+  private final long firstHeartbeatEstimateNs;
+  private final long minHeartbeatStdNs;
+
+  public PhiAccrualDetector(
+      long threshold,
+      long acceptableHeartbeatPauseNs,
+      long firstHeartbeatEstimateNs,
+      long minHeartbeatStdNs) {
+    this.threshold = threshold;
+    this.acceptableHeartbeatPauseNs = acceptableHeartbeatPauseNs;
+    this.firstHeartbeatEstimateNs = firstHeartbeatEstimateNs;
+    this.minHeartbeatStdNs = minHeartbeatStdNs;
+  }
+
+  @Override
+  public boolean isAvailable(List<AbstractHeartbeatSample> history) {
+    if (history.isEmpty()) {
+      /* We haven't received the first heartbeat reply. We cannot decide the 
node availability. */
+      return true;
+    }
+    final PhiAccrual phiAccrual = create(history);
+    final boolean isAvailable = phiAccrual.phi() < (double) this.threshold;
+    if (!isAvailable) {
+      // log the status change and dump the heartbeat history for analysis use
+      final StringBuilder builder = new StringBuilder();
+      builder.append("[");
+      for (double interval : phiAccrual.heartbeatIntervals) {
+        final long msInterval = (long) interval / 1000_000;
+        builder.append(msInterval).append(", ");
+      }
+      builder.append(phiAccrual.timeElapsedSinceLastHeartbeat / 1000_000);
+      builder.append("]");
+      LOGGER.info(String.format("Node Down, heartbeat history (ms): %s", 
builder));
+    }
+
+    return isAvailable;
+  }
+
+  PhiAccrual create(List<AbstractHeartbeatSample> history) {
+    final int size = history.size();
+
+    final List<Double> heartbeatIntervals = new ArrayList<>();
+    /*
+     * During cold start, the heartbeat history may contain not enough samples 
for wise decisions.
+     * Therefore, we manually add data samples for better estimation.
+     * 1. mean = heartbeat interval (1000ms)
+     * 2. std = mean / 4
+     */
+    if (size <= 2) {

Review Comment:
   Use fixed detector as fallback in the initial 60s



-- 
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]

Reply via email to