cmccabe commented on code in PR #17502:
URL: https://github.com/apache/kafka/pull/17502#discussion_r1811521396


##########
metadata/src/main/java/org/apache/kafka/controller/BrokerHeartbeatManager.java:
##########
@@ -82,23 +80,16 @@ static class BrokerHeartbeatState {
          */
         private long controlledShutdownOffset;
 
-        /**
-         * The previous entry in the unfenced list, or null if the broker is 
not in that list.
-         */
-        private BrokerHeartbeatState prev;
-
-        /**
-         * The next entry in the unfenced list, or null if the broker is not 
in that list.
-         */
-        private BrokerHeartbeatState next;
-
-        BrokerHeartbeatState(int id) {
+        BrokerHeartbeatState(
+            int id,
+            boolean fenced,
+            long metadataOffset,
+            long controlledShutdownOffset

Review Comment:
   `controlledShutdownOffset` is an existing feature of the heartbeat manager, 
not added by this change. It is the offset at which the broker should complete 
its controlled shutdown. Used to prevent the broker shutting down before all 
the other brokers have fetched enough metadata.



##########
metadata/src/main/java/org/apache/kafka/controller/BrokerHeartbeatTracker.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.kafka.controller;
+
+import org.apache.kafka.common.utils.Time;
+
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.concurrent.ConcurrentHashMap;
+
+class BrokerHeartbeatTracker {
+    /**
+     * The clock to use.
+     */
+    private final Time time;
+
+    /**
+     * The broker session timeout in nanoseconds.
+     */
+    private final long sessionTimeoutNs;
+
+    /**
+     * Maps a broker ID and epoch to the last contact time in monotonic 
nanoseconds.
+     */
+    private final ConcurrentHashMap<BrokerIdAndEpoch, Long> contactTimes;
+
+    BrokerHeartbeatTracker(Time time, long sessionTimeoutNs) {
+        this.time = time;
+        this.sessionTimeoutNs = sessionTimeoutNs;
+        this.contactTimes = new ConcurrentHashMap<>();
+    }
+
+    Time time() {
+        return time;
+    }
+
+    /**
+     * Update the contact time for the given broker ID and epoch to be the 
current time.
+     *
+     * @param idAndEpoch    The broker ID and epoch.
+     */
+    void updateContactTime(BrokerIdAndEpoch idAndEpoch) {
+        updateContactTime(idAndEpoch, time.nanoseconds());
+    }
+
+    /**
+     * Update the contact time for the given broker ID and epoch to be the 
given time.
+     *
+     * @param idAndEpoch    The broker ID and epoch.
+     * @param timeNs        The monotonic time in nanoseconds.
+     */
+    void updateContactTime(BrokerIdAndEpoch idAndEpoch, long timeNs) {
+        contactTimes.put(idAndEpoch, timeNs);
+    }
+
+    /**
+     * Get the contact time for the given broker ID and epoch.
+     *
+     * @param idAndEpoch    The broker ID and epoch.
+     * @return              The contact time, or Optional.empty if none is 
known.
+     */
+    OptionalLong contactTime(BrokerIdAndEpoch idAndEpoch) {
+        Long value = contactTimes.get(idAndEpoch);
+        if (value == null) return OptionalLong.empty();
+        return OptionalLong.of(value);
+    }
+
+    /**
+     * Remove either one or zero expired brokers from the map.
+     *
+     * @return      The expired broker that was removed, or Optional.empty if 
there was none.
+     */
+    Optional<BrokerIdAndEpoch> maybeRemoveExpired() {
+        return maybeRemoveExpired(time.nanoseconds());
+    }
+
+    /**
+     * Remove either one or zero expired brokers from the map.
+     *
+     * @param nowNs The current time in monotonic nanoseconds.
+     *
+     * @return      The expired broker that was removed, or Optional.empty if 
there was none.
+     */
+    Optional<BrokerIdAndEpoch> maybeRemoveExpired(long nowNs) {
+        Iterator<Entry<BrokerIdAndEpoch, Long>> iterator =
+            contactTimes.entrySet().iterator();
+        while (iterator.hasNext()) {

Review Comment:
   correct



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