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


##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSet.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * 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.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.feature.SupportedVersionRange;
+import org.apache.kafka.common.message.VotersRecord;
+import org.apache.kafka.common.utils.Utils;
+
+/**
+ * A type for representing the set of voters for a topic partition.
+ *
+ * It encapsulates static information like a voter's endpoint and their 
supported kraft.version.
+ *
+ * It providees functionality for converting to and from {@code VotersRecord} 
and for converting
+ * from the static configuration.
+ */
+final public class VoterSet {
+    private final Map<Integer, VoterNode> voters;

Review Comment:
   Yes. That is a sufficient condition but I decided to implement a stricter 
condition that the ids need be unique. This means that with this 
implementation, if they want to replace a directory id, they need to first 
remove the failed replica key (id, uuid) and then add the new replica key (id, 
uuid').
   
   There are two reasons why I decide to keep the ids unique:
   1. It makes it easier and safer to implement the feature for automatically 
having new controllers join the quorum. I was concerned that the set of voters 
would become unavailable if there was a race where new directory id kept 
joining the cluster. In this example the cluster would be come unavailable and 
the user would not be able to mitigate it: [(1, uuid1), (2, uuid2), (3, uuid3), 
(3, uuid3'), (3, uuid3''), (3, uuid3''')].
   2. Connection management is easier to implement. A lot of code in Kafka 
(e.g. `o.a.k.c.Node`) assumes that ids are unique and they can be used, along 
with the listener name, to identify an endpoint. I think it would be a big 
effort to extend this to identify endpoint by the replica key. Another example 
is `NodeEndpoints` in `FetchResponse`. That map is index by replica id.
   
   The main disadvantage of this implementation is that it would make it 
difficult to design and implement the ability for dynamically "altering the 
metadata/kraft log directory" like Kafka does for regular topic partitions. But 
I think we can discuss that if we ever want to implement that feature in the 
future.



##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSet.java:
##########
@@ -0,0 +1,377 @@
+/*
+ * 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.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.feature.SupportedVersionRange;
+import org.apache.kafka.common.message.VotersRecord;
+import org.apache.kafka.common.utils.Utils;
+
+/**
+ * A type for representing the set of voters for a topic partition.
+ *
+ * It encapsulates static information like a voter's endpoint and their 
supported kraft.version.
+ *
+ * It providees functionality for converting to and from {@code VotersRecord} 
and for converting
+ * from the static configuration.
+ */
+final public class VoterSet {
+    private final Map<Integer, VoterNode> voters;
+
+    VoterSet(Map<Integer, VoterNode> voters) {
+        if (voters.isEmpty()) {
+            throw new IllegalArgumentException("Voters cannot be empty");
+        }
+
+        this.voters = voters;
+    }
+
+    /**
+     * Returns the socket address for a given voter at a given listener.
+     *
+     * @param voter the id of the voter
+     * @param listener the name of the listener
+     * @return the socket address if it exists, otherwise {@code 
Optional.empty()}
+     */
+    public Optional<InetSocketAddress> voterAddress(int voter, String 
listener) {
+        return Optional.ofNullable(voters.get(voter))
+            .flatMap(voterNode -> voterNode.address(listener));
+    }
+
+    /**
+     * Returns all of the voter ids.
+     */
+    public Set<Integer> voterIds() {
+        return voters.keySet();
+    }
+
+    /**
+     * Adds a voter to the voter set.
+     *
+     * This object is immutable. A new voter set is returned if the voter was 
added.
+     *
+     * A new voter can be added to a voter set if its id doesn't already exist 
in the voter set.
+     *
+     * @param voter the new voter to add
+     * @return a new voter set if the voter was added, otherwise {@code 
Optional.empty()}
+     */
+    public Optional<VoterSet> addVoter(VoterNode voter) {
+        if (voters.containsKey(voter.voterKey().id())) {
+            return Optional.empty();
+        }
+
+        HashMap<Integer, VoterNode> newVoters = new HashMap<>(voters);
+        newVoters.put(voter.voterKey().id(), voter);
+
+        return Optional.of(new VoterSet(newVoters));
+    }
+
+    /**
+     * Remove a voter from the voter set.
+     *
+     * This object is immutable. A new voter set is returned if the voter was 
removed.
+     *
+     * A voter can be removed from the voter set if its id and directory id 
match.
+     *
+     * @param voterKey the voter key
+     * @return a new voter set if the voter was removed, otherwise {@code 
Optional.empty()}
+     */
+    public Optional<VoterSet> removeVoter(VoterKey voterKey) {
+        VoterNode oldVoter = voters.get(voterKey.id());
+        if (oldVoter != null && Objects.equals(oldVoter.voterKey(), voterKey)) 
{
+            HashMap<Integer, VoterNode> newVoters = new HashMap<>(voters);
+            newVoters.remove(voterKey.id());
+
+            return Optional.of(new VoterSet(newVoters));
+        }
+
+        return Optional.empty();
+    }
+
+    /**
+     * Converts a voter set to a voters record for a given version.
+     *
+     * @param version the version of the voters record
+     */
+    public VotersRecord toVotersRecord(short version) {
+        Function<VoterNode, VotersRecord.Voter> voterConvertor = voter -> {
+            Iterator<VotersRecord.Endpoint> endpoints = voter
+                .listeners()
+                .entrySet()
+                .stream()
+                .map(entry ->
+                    new VotersRecord.Endpoint()
+                        .setName(entry.getKey())
+                        .setHost(entry.getValue().getHostString())
+                        .setPort(entry.getValue().getPort())
+                )
+                .iterator();
+
+            VotersRecord.KRaftVersionFeature kraftVersionFeature = new 
VotersRecord.KRaftVersionFeature()
+                .setMinSupportedVersion(voter.supportedKRaftVersion().min())
+                .setMaxSupportedVersion(voter.supportedKRaftVersion().max());
+
+            return new VotersRecord.Voter()
+                .setVoterId(voter.voterKey().id())
+                
.setVoterDirectoryId(voter.voterKey().directoryId().orElse(Uuid.ZERO_UUID))
+                .setEndpoints(new VotersRecord.EndpointCollection(endpoints))
+                .setKRaftVersionFeature(kraftVersionFeature);
+        };
+
+        List<VotersRecord.Voter> voterRecordVoters = voters
+            .values()
+            .stream()
+            .map(voterConvertor)
+            .collect(Collectors.toList());
+
+        return new VotersRecord()
+            .setVersion(version)
+            .setVoters(voterRecordVoters);
+    }
+
+    /**
+     * Determines if two sets of voters have an overlapping majority.
+     *
+     * An overlapping majority means that for all majorities in {@code this} 
set of voters and for
+     * all majority in {@code that} set of voters, they have at least one 
voter in common.
+     *
+     * If this function returns true is means that one of the voter set 
commits an offset, it means
+     * that the other voter set cannot commit a conflicting offset.

Review Comment:
   I agree. I fixed it.



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