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


##########
master/src/main/java/org/apache/celeborn/service/deploy/master/clustermeta/ha/HeartbeatAggregator.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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() {
+    flushSafely();
+    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)
+                    .addAllAppHeartbeats(drainedApps)
+                    .build())
+            .build();
+    long startNs = System.nanoTime();
+    ratisServer.submitRequest(batchRequest);

Review Comment:
   From the underlying implementation, submitRequest() appears to be 
synchronous since it waits on 
   
   `raftClientReply = server.submitClientRequestAsync(raftClientRequest).get();`
   
   If the Ratis submission takes a long time, the single flush thread will be 
blocked and subsequent flushes will be delayed. During this time, new 
worker/app heartbeats will continue accumulating in the pending maps.
   Should we consider putting an upper bound on the number of pending heartbeat 
entries to prevent unbounded memory growth in case the Ratis call is blocked 
for an extended period? If the limit is reached, we could drop subsequent 
heartbeats and emit a warning log so that this condition is visible. 



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