dengziming commented on code in PR #12195:
URL: https://github.com/apache/kafka/pull/12195#discussion_r883213806


##########
metadata/src/main/java/org/apache/kafka/controller/ClusterControlManager.java:
##########
@@ -391,41 +393,60 @@ public void replay(UnregisterBrokerRecord record) {
     }
 
     public void replay(FenceBrokerRecord record) {
-        int brokerId = record.id();
-        BrokerRegistration registration = brokerRegistrations.get(brokerId);
-        if (registration == null) {
-            throw new RuntimeException(String.format("Unable to replay %s: no 
broker " +
-                "registration found for that id", record.toString()));
-        } else if (registration.epoch() !=  record.epoch()) {
-            throw new RuntimeException(String.format("Unable to replay %s: no 
broker " +
-                "registration with that epoch found", record.toString()));
-        } else {
-            if (heartbeatManager != null) heartbeatManager.register(brokerId, 
true);
-            brokerRegistrations.put(brokerId, 
registration.cloneWithFencing(true));
-            updateMetrics(registration, brokerRegistrations.get(brokerId));
-            log.info("Fenced broker: {}", record);
-        }
+        replayRegistrationChange(record, record.id(), record.epoch(), 
Optional.of(true));
     }
 
     public void replay(UnfenceBrokerRecord record) {
-        int brokerId = record.id();
-        BrokerRegistration registration = brokerRegistrations.get(brokerId);
-        if (registration == null) {
+        replayRegistrationChange(record, record.id(), record.epoch(), 
Optional.of(false));
+    }
+
+    public void replay(BrokerRegistrationChangeRecord record) {
+        replayRegistrationChange(record, record.brokerId(), 
record.brokerEpoch(), getFencingChange(record.fenced()));
+    }
+
+    private Optional<Boolean> getFencingChange(byte value) {
+        switch (value) {
+            case -1:
+                return Optional.of(false);
+            case 0:
+                return Optional.empty();
+            case 1:
+                return Optional.of(true);
+            default:
+                throw new RuntimeException(String.format("Invalid value for 
fenced: %d", value));
+        }
+    }
+
+    private void replayRegistrationChange(
+        ApiMessage record,
+        int brokerId,
+        long brokerEpoch,
+        Optional<Boolean> fenced
+    ) {
+        BrokerRegistration curRegistration = brokerRegistrations.get(brokerId);
+        if (curRegistration == null) {
             throw new RuntimeException(String.format("Unable to replay %s: no 
broker " +
                 "registration found for that id", record.toString()));
-        } else if (registration.epoch() !=  record.epoch()) {
+        } else if (curRegistration.epoch() != brokerEpoch) {
             throw new RuntimeException(String.format("Unable to replay %s: no 
broker " +
                 "registration with that epoch found", record.toString()));
         } else {
-            if (heartbeatManager != null) heartbeatManager.register(brokerId, 
false);
-            brokerRegistrations.put(brokerId, 
registration.cloneWithFencing(false));
-            updateMetrics(registration, brokerRegistrations.get(brokerId));
-            log.info("Unfenced broker: {}", record);
-        }
-        if (readyBrokersFuture.isPresent()) {
-            if (readyBrokersFuture.get().check()) {
-                readyBrokersFuture.get().future.complete(null);
-                readyBrokersFuture = Optional.empty();
+            BrokerRegistration nextRegistration = curRegistration;
+            if (fenced.isPresent()) {
+                nextRegistration = 
nextRegistration.cloneWithFencing(fenced.get());
+            }
+            if (!curRegistration.equals(nextRegistration)) {
+                brokerRegistrations.put(brokerId, nextRegistration);
+                updateMetrics(curRegistration, nextRegistration);
+            } else {
+                log.info("Ignoring no-op registration change for {}", 
curRegistration);
+            }
+            if (heartbeatManager != null) heartbeatManager.register(brokerId, 
nextRegistration.fenced());

Review Comment:
   it seems `heartbeatManager` will be null in a standby controller.



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