AmandeepSingh285 commented on code in PR #3810:
URL: https://github.com/apache/celeborn/pull/3810#discussion_r3836124195


##########
master/src/main/java/org/apache/celeborn/service/deploy/master/clustermeta/ha/HeartbeatAggregator.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.celeborn.service.deploy.master.clustermeta.ha;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.common.CelebornConf;
+import org.apache.celeborn.common.client.MasterClient;
+import org.apache.celeborn.common.util.ThreadUtils;
+import org.apache.celeborn.service.deploy.master.clustermeta.ResourceProtos;
+import 
org.apache.celeborn.service.deploy.master.clustermeta.ResourceProtos.ResourceRequest;
+import 
org.apache.celeborn.service.deploy.master.clustermeta.ResourceProtos.Type;
+
+public class HeartbeatAggregator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HeartbeatAggregator.class);
+
+  private final HARaftServer ratisServer;
+  private final long batchIntervalMs;
+
+  private final ReentrantLock pendingLock = new ReentrantLock();
+  private Map<String, ResourceProtos.WorkerHeartbeatRequest> workerHeartbeats 
= new HashMap<>();
+  private Map<String, ResourceProtos.AppHeartbeatRequest> appHeartbeats = new 
HashMap<>();
+
+  private final ScheduledExecutorService flushExecutor;
+
+  public HeartbeatAggregator(HARaftServer ratisServer, CelebornConf conf) {
+    this.ratisServer = ratisServer;
+    this.batchIntervalMs = conf.masterHaHeartbeatBatchIntervalMs();
+    this.flushExecutor =
+        
ThreadUtils.newDaemonSingleThreadScheduledExecutor("master-heartbeat-aggregator");
+    this.flushExecutor.scheduleWithFixedDelay(
+        this::flushSafely, batchIntervalMs, batchIntervalMs, 
TimeUnit.MILLISECONDS);
+    LOG.info("HeartbeatAggregator started, flush interval {} ms.", 
batchIntervalMs);
+  }
+
+  public void offerWorkerHeartbeat(ResourceProtos.WorkerHeartbeatRequest 
heartbeat) {
+    pendingLock.lock();
+    try {
+      workerHeartbeats.put(workerKey(heartbeat), heartbeat);
+    } finally {
+      pendingLock.unlock();
+    }
+  }
+
+  public void offerAppHeartbeat(ResourceProtos.AppHeartbeatRequest heartbeat) {
+    pendingLock.lock();
+    try {
+      appHeartbeats.put(heartbeat.getAppId(), heartbeat);
+    } finally {
+      pendingLock.unlock();
+    }
+  }
+
+  private static String workerKey(ResourceProtos.WorkerHeartbeatRequest 
heartbeat) {
+    return heartbeat.getHost()
+        + ":"
+        + heartbeat.getRpcPort()
+        + ":"
+        + heartbeat.getPushPort()
+        + ":"
+        + heartbeat.getFetchPort()
+        + ":"
+        + heartbeat.getReplicatePort();
+  }
+
+  public void stop() {
+    flushExecutor.shutdownNow();
+  }
+
+  private void flushSafely() {
+    try {
+      flush();
+    } catch (Throwable t) {
+      // Dropped batches self-heal next interval.
+      LOG.error("Failed to flush aggregated heartbeats, dropping this batch.", 
t);
+    }
+  }
+
+  private void flush() {
+    List<ResourceProtos.WorkerHeartbeatRequest> drainedWorkers;
+    List<ResourceProtos.AppHeartbeatRequest> drainedApps;
+    pendingLock.lock();
+    try {
+      if (workerHeartbeats.isEmpty() && appHeartbeats.isEmpty()) {
+        return;
+      }
+      drainedWorkers = new ArrayList<>(workerHeartbeats.values());
+      workerHeartbeats.clear();
+      drainedApps = new ArrayList<>(appHeartbeats.values());
+      appHeartbeats.clear();
+    } finally {
+      pendingLock.unlock();
+    }
+
+    if (!ratisServer.isLeader()) {
+      return;
+    }
+
+    ResourceRequest batchRequest =
+        ResourceRequest.newBuilder()
+            .setCmdType(Type.BatchHeartbeat)
+            .setRequestId(MasterClient.genRequestId())
+            .setBatchHeartbeatRequest(
+                ResourceProtos.BatchHeartbeatRequest.newBuilder()
+                    .addAllWorkerHeartbeats(drainedWorkers)

Review Comment:
   If the active leader dies before the batched worker heartbeat is committed 
to Ratis, the new leader keeps the last applied worker snapshot. Load-aware 
slot allocation then ranks disks from stale flush/fetch times, usable space, 
and active slots, so traffic can keep landing on workers that have since become 
hot or unhealthy.
   highWorkload is applied only when that heartbeat is applied. If the lost 
heartbeat was the one that marked the worker overloaded, the new leader will 
still treat it as available and offer new slots until the next heartbeat (~30s).
   The same gap exists for decommission: the worker may already be draining 
from an event it received, but the status it reported in the lost heartbeat 
never reaches the new leader. Until the next heartbeat applies, the master can 
still give applications slots on a worker that is leaving the cluster.
   
   The impact of losing a worker/app heartbeat after this change could be 
significantly higher, as losing the subsequent heartbeat would further extend 
the period during which the leader operates on stale worker state.



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