kevin-wu24 commented on code in PR #19589: URL: https://github.com/apache/kafka/pull/19589#discussion_r2152693217
########## raft/src/test/java/org/apache/kafka/raft/KafkaRaftClientAutoJoinTest.java: ########## @@ -0,0 +1,307 @@ +/* + * 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; + +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.common.record.MemoryRecords; +import org.apache.kafka.common.utils.BufferSupplier; +import org.apache.kafka.server.common.KRaftVersion; + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Stream; + +import static org.apache.kafka.raft.KafkaRaftClientTest.replicaKey; +import static org.apache.kafka.raft.RaftClientTestContext.RaftProtocol.KIP_595_PROTOCOL; +import static org.apache.kafka.raft.RaftClientTestContext.RaftProtocol.KIP_853_PROTOCOL; + +public class KafkaRaftClientAutoJoinTest { + @Test + public void testAutoRemoveOldVoter() throws Exception { + final var leader = replicaKey(randomReplicaId(), true); + final var oldFollower = replicaKey(leader.id() + 1, true); + final var newFollowerKey = replicaKey(oldFollower.id(), true); + final int epoch = 1; + final var context = new RaftClientTestContext.Builder( + newFollowerKey.id(), + newFollowerKey.directoryId().get() + ) + .withRaftProtocol(KIP_853_PROTOCOL) + .withStartingVoters( + VoterSetTest.voterSet(Stream.of(leader, oldFollower)), KRaftVersion.KRAFT_VERSION_1 + ) + .withElectedLeader(epoch, leader.id()) + .withAutoJoin(true) + .withCanBecomeVoter(true) + .build(); + + context.advanceTimeAndCompleteFetch(epoch, leader.id(), true); + + // the next request should be a remove voter request + pollAndDeliverRemoveVoter(context, oldFollower); + + // after sending a remove voter the next request should be a fetch + context.advanceTimeAndCompleteFetch(epoch, leader.id(), true); + + // the replica should send remove voter again because the fetch did not update the voter set + pollAndDeliverRemoveVoter(context, oldFollower); + } + + @Test + public void testAutoAddNewVoter() throws Exception { + final var leader = replicaKey(randomReplicaId(), true); + final var follower = replicaKey(leader.id() + 1, true); + final var newVoter = replicaKey(follower.id() + 1, true); + final int epoch = 1; + final var context = new RaftClientTestContext.Builder( + newVoter.id(), + newVoter.directoryId().get() + ) + .withRaftProtocol(KIP_853_PROTOCOL) + .withStartingVoters( + VoterSetTest.voterSet(Stream.of(leader, follower)), KRaftVersion.KRAFT_VERSION_1 + ) + .withElectedLeader(epoch, leader.id()) + .withAutoJoin(true) + .withCanBecomeVoter(true) + .build(); + + context.advanceTimeAndCompleteFetch(epoch, leader.id(), true); + + // the next request should be an add voter request + pollAndSendAddVoter(context, newVoter); + + // expire the add voter request, the next request should be a fetch + context.advanceTimeAndCompleteFetch(epoch, leader.id(), true); + + // the replica should send add voter again because the completed fetch + // did not update the voter set, and its timer has expired + pollAndSendAddVoter(context, newVoter); + + // verify the replica can perform a fetch to commit the new voter set while the add + // voter request is still in-flight + pollAndDeliverFetchToUpdateVoterSet(context, epoch, + VoterSetTest.voterSet(Stream.of(leader, newVoter))); Review Comment: We can deliver addVoterResponse before the voter set is committed after https://github.com/apache/kafka/pull/19982 is merged -- 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