jsancio commented on code in PR #15671: URL: https://github.com/apache/kafka/pull/15671#discussion_r1581908701
########## raft/src/main/java/org/apache/kafka/raft/internals/VoterSetHistory.java: ########## @@ -0,0 +1,102 @@ +/* + * 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 both generating a new snapshot at a given offset or when evaulating + * the latest set of voters. + */ +final public class VoterSetHistory implements History<VoterSet> { + private final Optional<VoterSet> staticVoterSet; + private final History<VoterSet> votersHistory = new TreeMapHistory<>(); + + VoterSetHistory(Optional<VoterSet> staticVoterSet) { + this.staticVoterSet = staticVoterSet; + } + + @Override + public void addAt(long offset, VoterSet voters) { + Optional<History.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. + * + * @param offset the offset (inclusive) + * @return the voter set if one exist, otherwise {@code Optional.empty()} + */ + @Override + public Optional<VoterSet> valueAtOrBefore(long offset) { + return votersHistory.valueAtOrBefore(offset); + } + + @Override + public Optional<History.Entry<VoterSet>> lastEntry() { + Optional<History.Entry<VoterSet>> result = votersHistory.lastEntry(); + if (result.isPresent()) return result; + + return staticVoterSet.map(value -> new History.Entry<>(-1, value)); Review Comment: Hmm. I only implemented this so that `VoterSetHistory` can implement `History`. This method is not used by `KRaftControlRecordStateMachine` and I don't think we'll need it in the future. I'll just remove this method and remove`implements History`. -- 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