cmccabe commented on a change in pull request #10949:
URL: https://github.com/apache/kafka/pull/10949#discussion_r661869382



##########
File path: metadata/src/main/java/org/apache/kafka/image/ClusterDelta.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.image;
+
+import org.apache.kafka.common.metadata.BrokerRegistrationChangeRecord;
+import org.apache.kafka.common.metadata.FenceBrokerRecord;
+import org.apache.kafka.common.metadata.RegisterBrokerRecord;
+import org.apache.kafka.common.metadata.UnfenceBrokerRecord;
+import org.apache.kafka.common.metadata.UnregisterBrokerRecord;
+import org.apache.kafka.metadata.BrokerRegistration;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+
+
+/**
+ * Represents changes to the cluster in the metadata image.
+ */
+public final class ClusterDelta {
+    private final ClusterImage image;
+    private final HashMap<Integer, Optional<BrokerRegistration>> 
changedBrokers = new HashMap<>();
+
+    public ClusterDelta(ClusterImage image) {
+        this.image = image;
+    }
+
+    public HashMap<Integer, Optional<BrokerRegistration>> changedBrokers() {
+        return changedBrokers;
+    }
+
+    public BrokerRegistration broker(int nodeId) {
+        Optional<BrokerRegistration> result = changedBrokers.get(nodeId);
+        if (result != null) {
+            return result.orElse(null);
+        }
+        return image.broker(nodeId);
+    }
+
+    public void finishSnapshot() {
+        for (Integer brokerId : image.brokers().keySet()) {
+            if (!changedBrokers.containsKey(brokerId)) {
+                changedBrokers.put(brokerId, Optional.empty());
+            }
+        }
+    }
+
+    public void replay(RegisterBrokerRecord record) {
+        BrokerRegistration broker = BrokerRegistration.fromRecord(record);
+        changedBrokers.put(broker.id(), Optional.of(broker));
+    }
+
+    public void replay(UnregisterBrokerRecord record) {
+        changedBrokers.put(record.brokerId(), Optional.empty());
+    }
+
+    public void replay(FenceBrokerRecord record) {
+        BrokerRegistration broker = broker(record.id());
+        if (broker == null) {
+            throw new RuntimeException("Tried to fence broker " + record.id() +
+                ", but that broker was not registered.");
+        }
+        changedBrokers.put(record.id(), 
Optional.of(broker.cloneWithFencing(true)));
+    }
+
+    public void replay(UnfenceBrokerRecord record) {
+        BrokerRegistration broker = broker(record.id());
+        if (broker == null) {
+            throw new RuntimeException("Tried to unfence broker " + 
record.id() +
+                ", but that broker was not registered.");
+        }
+        changedBrokers.put(record.id(), 
Optional.of(broker.cloneWithFencing(false)));
+    }
+
+    public void replay(BrokerRegistrationChangeRecord record) {
+        BrokerRegistration broker = broker(record.brokerId());
+        if (broker == null) {
+            throw new RuntimeException("Tried to change broker " + 
record.brokerId() +
+                ", but that broker was not registered.");
+        }
+        if (record.fenced() < 0) {

Review comment:
       Let's validate the epoch for each




-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to