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


##########
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();

Review Comment:
   Good catch. `Master.stop()` stops the aggregator before the raft server and 
before leadereship transfer, so a final flush can still commit. Added a 
best-effort `flushSafely()` in `stop()`: on the leader it commits the pending 
batch synchronously; on a non-leader it's a no-op.



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