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


##########
raft/src/main/java/org/apache/kafka/raft/KafkaNetworkChannel.java:
##########
@@ -178,6 +182,7 @@ public void pollOnce() {
         requestThread.doWork();
     }
 
+    @SuppressWarnings("NPathComplexity")

Review Comment:
   Try using `if (...) { return } else if (...) { return } ...` and see if that 
reduces the path complexity so you can remove this suppression.
   
   You can also try using Java's new pattern matching and lambda syntax for 
switch statements. E.g.
   ```java
   return switch (requestData) {
       case VoterRequestData voterData -> new VoterRequest.Builder(voterData);
       ...
       default -> throw new IllegalArgumentException(...);
   }
   ```



##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -3266,14 +3304,45 @@ private long pollFollowerAsVoter(FollowerState state, 
long currentTimeMs) {
             backoffMs,
             Math.min(
                 state.remainingFetchTimeMs(currentTimeMs),
-                state.remainingUpdateVoterPeriodMs(currentTimeMs)
+                state.remainingUpdateVoterSetPeriodMs(currentTimeMs)
             )
         );
     }
 
     private long pollFollowerAsObserver(FollowerState state, long 
currentTimeMs) {
         if (state.hasFetchTimeoutExpired(currentTimeMs)) {
             return maybeSendFetchToAnyBootstrap(currentTimeMs);
+        } else if (partitionState.lastKraftVersion().isReconfigSupported() && 
canBecomeVoter &&
+            quorumConfig.autoJoin() && 
state.hasUpdateVoterSetPeriodExpired(currentTimeMs)) {
+            /* Only replicas that are always flushing and are configured to 
auto join should

Review Comment:
   Replace "are always flushing" with "can become a voter."



##########
raft/src/test/java/org/apache/kafka/raft/KafkaRaftClientAutoJoinTest.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var addRequest = context.assertSentAddVoterRequest(
+            newVoter,
+            context.client.quorum().localVoterNodeOrThrow().listeners()
+        );
+        context.deliverResponse(
+            addRequest.correlationId(),
+            addRequest.destination(),
+            RaftUtil.addVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending an add voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @Test
+    public void testObserverRemovesOldVoterAndAutoJoins() 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();
+
+        // advance time and complete a fetch to trigger the remove voter 
request
+        context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var removeVoterFetch = context.assertSentFetchRequest();
+        context.assertFetchRequestData(
+            removeVoterFetch,
+            epoch,
+            context.log.endOffset().offset(),
+            context.log.lastFetchedEpoch()
+        );
+
+        // deliver the fetch response with the updated voter set after 
removing the old voter
+        var localEndOffset = context.log.endOffset().offset();
+        context.deliverResponse(
+            removeVoterFetch.correlationId(),
+            removeVoterFetch.destination(),
+            context.fetchResponse(
+                epoch,
+                leader.id(),
+                MemoryRecords.withVotersRecord(
+                    localEndOffset,
+                    0,
+                    epoch,
+                    BufferSupplier.NO_CACHING.get(300),
+                    
VoterSetTest.voterSet(Stream.of(leader)).toVotersRecord((short) 0)),
+                localEndOffset + 1,
+                Errors.NONE
+            )
+        );
+        // poll kraft to update the replica's voter set
+        context.client.poll();
+
+        // advance time and complete a fetch to trigger the add voter request
+        context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var addVoterRequest = context.assertSentAddVoterRequest(
+            newFollowerKey,
+            context.client.quorum().localVoterNodeOrThrow().listeners()
+        );
+        context.deliverResponse(
+            addVoterRequest.correlationId(),
+            addVoterRequest.destination(),
+            RaftUtil.addVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending an add voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var addVoterFetch = context.assertSentFetchRequest();
+        context.assertFetchRequestData(
+            addVoterFetch,
+            epoch,
+            context.log.endOffset().offset(),
+            context.log.lastFetchedEpoch()
+        );
+
+        // deliver the fetch response with the updated voter set after adding 
the observer
+        localEndOffset = context.log.endOffset().offset();
+        context.deliverResponse(
+            addVoterFetch.correlationId(),
+            addVoterFetch.destination(),
+            context.fetchResponse(
+                epoch,
+                leader.id(),
+                MemoryRecords.withVotersRecord(
+                    localEndOffset,
+                    0,
+                    epoch,
+                    BufferSupplier.NO_CACHING.get(300),
+                    VoterSetTest.voterSet(Stream.of(leader, 
newFollowerKey)).toVotersRecord((short) 0)),
+                localEndOffset + 1,
+                Errors.NONE
+            )
+        );
+        // poll kraft to update the replica's voter set
+        context.client.poll();

Review Comment:
   How about adding this time advancement and showing that the replica sent a 
fetch request?
   ```java
           context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
           context.time.sleep(context.fetchTimeoutMs - 1);
   ```



##########
raft/src/test/java/org/apache/kafka/raft/KafkaRaftClientAutoJoinTest.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var addRequest = context.assertSentAddVoterRequest(
+            newVoter,
+            context.client.quorum().localVoterNodeOrThrow().listeners()
+        );
+        context.deliverResponse(
+            addRequest.correlationId(),
+            addRequest.destination(),
+            RaftUtil.addVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending an add voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @Test
+    public void testObserverRemovesOldVoterAndAutoJoins() 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();
+
+        // advance time and complete a fetch to trigger the remove voter 
request
+        context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var removeVoterFetch = context.assertSentFetchRequest();
+        context.assertFetchRequestData(
+            removeVoterFetch,
+            epoch,
+            context.log.endOffset().offset(),
+            context.log.lastFetchedEpoch()
+        );
+
+        // deliver the fetch response with the updated voter set after 
removing the old voter
+        var localEndOffset = context.log.endOffset().offset();
+        context.deliverResponse(
+            removeVoterFetch.correlationId(),
+            removeVoterFetch.destination(),
+            context.fetchResponse(
+                epoch,
+                leader.id(),
+                MemoryRecords.withVotersRecord(
+                    localEndOffset,
+                    0,
+                    epoch,
+                    BufferSupplier.NO_CACHING.get(300),
+                    
VoterSetTest.voterSet(Stream.of(leader)).toVotersRecord((short) 0)),
+                localEndOffset + 1,
+                Errors.NONE
+            )
+        );
+        // poll kraft to update the replica's voter set
+        context.client.poll();
+
+        // advance time and complete a fetch to trigger the add voter request
+        context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var addVoterRequest = context.assertSentAddVoterRequest(
+            newFollowerKey,
+            context.client.quorum().localVoterNodeOrThrow().listeners()
+        );
+        context.deliverResponse(
+            addVoterRequest.correlationId(),
+            addVoterRequest.destination(),
+            RaftUtil.addVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending an add voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var addVoterFetch = context.assertSentFetchRequest();
+        context.assertFetchRequestData(
+            addVoterFetch,
+            epoch,
+            context.log.endOffset().offset(),
+            context.log.lastFetchedEpoch()
+        );
+
+        // deliver the fetch response with the updated voter set after adding 
the observer
+        localEndOffset = context.log.endOffset().offset();
+        context.deliverResponse(
+            addVoterFetch.correlationId(),
+            addVoterFetch.destination(),
+            context.fetchResponse(
+                epoch,
+                leader.id(),
+                MemoryRecords.withVotersRecord(
+                    localEndOffset,
+                    0,
+                    epoch,
+                    BufferSupplier.NO_CACHING.get(300),
+                    VoterSetTest.voterSet(Stream.of(leader, 
newFollowerKey)).toVotersRecord((short) 0)),

Review Comment:
   Missing newline.
   ```java
                       VoterSetTest.voterSet(Stream.of(leader, 
newFollowerKey)).toVotersRecord((short) 0)
                   ),
   ```



##########
raft/src/test/java/org/apache/kafka/raft/KafkaRaftClientAutoJoinTest.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @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.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var addRequest = context.assertSentAddVoterRequest(
+            newVoter,
+            context.client.quorum().localVoterNodeOrThrow().listeners()
+        );
+        context.deliverResponse(
+            addRequest.correlationId(),
+            addRequest.destination(),
+            RaftUtil.addVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending an add voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var fetchRequest = context.assertSentFetchRequest();
+        context.assertFetchRequestData(fetchRequest, epoch, 0L, 0);
+    }
+
+    @Test
+    public void testObserverRemovesOldVoterAndAutoJoins() 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();
+
+        // advance time and complete a fetch to trigger the remove voter 
request
+        context.advanceTimeAndFetchToUpdateVoterSetTimer(epoch, leader.id());
+        context.time.sleep(context.fetchTimeoutMs - 1);
+        context.pollUntilRequest();
+        final var removeRequest = 
context.assertSentRemoveVoterRequest(oldFollower);
+        context.deliverResponse(
+            removeRequest.correlationId(),
+            removeRequest.destination(),
+            RaftUtil.removeVoterResponse(Errors.NONE, Errors.NONE.message())
+        );
+
+        // after sending a remove voter the next request should be a fetch
+        context.pollUntilRequest();
+        final var removeVoterFetch = context.assertSentFetchRequest();
+        context.assertFetchRequestData(
+            removeVoterFetch,
+            epoch,
+            context.log.endOffset().offset(),
+            context.log.lastFetchedEpoch()
+        );
+
+        // deliver the fetch response with the updated voter set after 
removing the old voter
+        var localEndOffset = context.log.endOffset().offset();
+        context.deliverResponse(
+            removeVoterFetch.correlationId(),
+            removeVoterFetch.destination(),
+            context.fetchResponse(
+                epoch,
+                leader.id(),
+                MemoryRecords.withVotersRecord(
+                    localEndOffset,
+                    0,
+                    epoch,
+                    BufferSupplier.NO_CACHING.get(300),
+                    
VoterSetTest.voterSet(Stream.of(leader)).toVotersRecord((short) 0)),

Review Comment:
   Missing newline.
   ```java
                       
VoterSetTest.voterSet(Stream.of(leader)).toVotersRecord((short) 0)
                   ),
   ```



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