jsancio commented on code in PR #15671:
URL: https://github.com/apache/kafka/pull/15671#discussion_r1595916951


##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSetHistory.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.raft.internals;
+
+import java.util.Optional;
+
+/**
+ * A type for storing the historical value of the set of voters.
+ *
+ * This type can be use to keep track in-memory the sets for voters stored in 
the latest snapshot
+ * and log. This is useful when generating a new snapshot at a given offset or 
when evaulating
+ * the latest set of voters.
+ */
+final public class VoterSetHistory {
+    private final Optional<VoterSet> staticVoterSet;
+    private final LogHistory<VoterSet> votersHistory = new 
TreeMapLogHistory<>();
+
+    VoterSetHistory(Optional<VoterSet> staticVoterSet) {
+        this.staticVoterSet = staticVoterSet;
+    }
+
+    /**
+     * Add a new value at a given offset.
+     *
+     * The provided {@code offset} must be greater than or equal to 0 and must 
be greater than the
+     * offset of all previous calls to this method.
+     *
+     * @param offset the offset
+     * @param value the value to store

Review Comment:
   Fixed.



##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSetHistory.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.raft.internals;
+
+import java.util.Optional;
+
+/**
+ * A type for storing the historical value of the set of voters.
+ *
+ * This type can be use to keep track in-memory the sets for voters stored in 
the latest snapshot
+ * and log. This is useful when generating a new snapshot at a given offset or 
when evaulating
+ * the latest set of voters.
+ */
+final public class VoterSetHistory {
+    private final Optional<VoterSet> staticVoterSet;
+    private final LogHistory<VoterSet> votersHistory = new 
TreeMapLogHistory<>();
+
+    VoterSetHistory(Optional<VoterSet> staticVoterSet) {
+        this.staticVoterSet = staticVoterSet;
+    }
+
+    /**
+     * Add a new value at a given offset.
+     *
+     * The provided {@code offset} must be greater than or equal to 0 and must 
be greater than the
+     * offset of all previous calls to this method.
+     *
+     * @param offset the offset
+     * @param value the value to store
+     * @throws IllegalArgumentException if the offset is not greater than all 
previous offsets
+     */
+    public void addAt(long offset, VoterSet voters) {
+        Optional<LogHistory.Entry<VoterSet>> lastEntry = 
votersHistory.lastEntry();
+        if (lastEntry.isPresent() && lastEntry.get().offset() >= 0) {
+            // If the last voter set comes from the replicated log then the 
majorities must overlap.
+            // This ignores the static voter set and the bootstrapped voter 
set since they come from
+            // the configuration and the KRaft leader never guaranteed that 
they are the same across
+            // all replicas.
+            VoterSet lastVoterSet = lastEntry.get().value();
+            if (!lastVoterSet.hasOverlappingMajority(voters)) {
+                throw new IllegalArgumentException(
+                    String.format(
+                        "Last voter set %s doesn't have an overlapping 
majority with the new voter set %s",
+                        lastVoterSet,
+                        voters
+                    )
+                );
+            }
+        }
+
+        votersHistory.addAt(offset, voters);
+    }
+
+    /**
+     * Computes the value of the voter set at a given offset.
+     *
+     * This function will only return values provided through {@code addAt} 
and it would never
+     * include the {@code staticVoterSet} provided through the constructoer.

Review Comment:
   Fixed.



##########
raft/src/main/java/org/apache/kafka/raft/ReplicatedLog.java:
##########
@@ -236,14 +235,14 @@ default long truncateToEndOffset(OffsetAndEpoch 
endOffset) {
      * snapshot already exists or it is less than log start offset then return 
an
      * {@link Optional#empty()}.
      *
-     * Snapshots created using this method will be validated against the 
existing snapshots
-     * and the replicated log.
+     * The snapshot id will be validated against the existing snapshots and 
the log. The snapshot id
+     * must not alread exist, it must be greater than the log start offset, it 
must be less than

Review Comment:
   Fixed.



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