Cyrill commented on code in PR #7242: URL: https://github.com/apache/ignite-3/pull/7242#discussion_r2741282075
########## modules/raft/src/test/java/org/apache/ignite/internal/raft/client/LeaderAvailabilityStateTest.java: ########## @@ -0,0 +1,516 @@ +/* + * 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.ignite.internal.raft.client; + +import static org.apache.ignite.internal.testframework.IgniteTestUtils.deriveUuidFrom; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.ignite.internal.network.ClusterNodeImpl; +import org.apache.ignite.internal.network.InternalClusterNode; +import org.apache.ignite.internal.raft.client.LeaderAvailabilityState.State; +import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest; +import org.apache.ignite.network.NetworkAddress; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link LeaderAvailabilityState} state machine. + */ +public class LeaderAvailabilityStateTest extends BaseIgniteAbstractTest { + + /** Initial state should be WAITING_FOR_LEADER with term -1. */ + @Test + void testInitialState() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + + assertEquals(State.WAITING_FOR_LEADER, state.currentState()); + assertEquals(-1, state.currentTerm()); + } + + /** awaitLeader returns incomplete future when no leader elected. */ + @Test + void testAwaitLeaderWhenNoLeader() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + + CompletableFuture<Long> future = state.awaitLeader(); + + assertFalse(future.isDone(), "Future should not be completed when no leader"); + } + + /** awaitLeader returns completed future when leader is available. */ + @Test + void testAwaitLeaderWhenLeaderAvailable() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + state.onLeaderElected(leaderNode, 1); + + CompletableFuture<Long> future = state.awaitLeader(); + + assertTrue(future.isDone(), "Future should be completed when leader available"); + assertEquals(1L, future.join()); + } + + /** onLeaderElected transitions to LEADER_AVAILABLE state. */ + @Test + void testOnLeaderElectedTransition() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + state.onLeaderElected(leaderNode, 1); + + assertEquals(State.LEADER_AVAILABLE, state.currentState()); + assertEquals(1, state.currentTerm()); + } + + /** onLeaderElected completes waiting futures. */ + @Test + void testOnLeaderElectedCompletesWaiters() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + CompletableFuture<Long> waiter = state.awaitLeader(); + assertFalse(waiter.isDone()); + + state.onLeaderElected(leaderNode, 5); + + assertTrue(waiter.isDone()); + assertEquals(5L, waiter.join()); + } + + /** Stale term notifications are ignored. */ + @Test + void testStaleTermIgnored() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode1 = createNode("leader-1"); + InternalClusterNode leaderNode2 = createNode("leader-2"); + + state.onLeaderElected(leaderNode1, 5); + assertEquals(5, state.currentTerm()); + + // Stale notification with lower term should be ignored + state.onLeaderElected(leaderNode2, 3); + assertEquals(5, state.currentTerm()); + } + + /** Equal term notifications are ignored. */ + @Test + void testEqualTermIgnored() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader"); + + state.onLeaderElected(leaderNode, 5); + assertEquals(5, state.currentTerm()); + + // Same term should be ignored + state.onLeaderElected(leaderNode, 5); + assertEquals(5, state.currentTerm()); + } + + /** Higher term updates current term. */ + @Test + void testHigherTermUpdates() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode1 = createNode("leader-1"); + InternalClusterNode leaderNode2 = createNode("leader-2"); + + state.onLeaderElected(leaderNode1, 1); + assertEquals(1, state.currentTerm()); + + state.onLeaderElected(leaderNode2, 5); + assertEquals(5, state.currentTerm()); + } + + /** onGroupUnavailable transitions back to WAITING_FOR_LEADER. */ + @Test + void testOnGroupUnavailableTransition() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + state.onLeaderElected(leaderNode, 1); + assertEquals(State.LEADER_AVAILABLE, state.currentState()); + + state.onGroupUnavailable(1); + assertEquals(State.WAITING_FOR_LEADER, state.currentState()); + } + + /** onGroupUnavailable ignored if term changed. */ + @Test + void testOnGroupUnavailableIgnoredIfTermChanged() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + state.onLeaderElected(leaderNode, 1); + state.onLeaderElected(leaderNode, 2); + assertEquals(State.LEADER_AVAILABLE, state.currentState()); + assertEquals(2, state.currentTerm()); + + // Should be ignored because term changed from 1 to 2 + state.onGroupUnavailable(1); + assertEquals(State.LEADER_AVAILABLE, state.currentState()); + } + + /** onGroupUnavailable has no effect when already in WAITING_FOR_LEADER. */ + @Test + void testOnGroupUnavailableWhenAlreadyWaiting() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + + // Already in WAITING_FOR_LEADER, should have no effect + state.onGroupUnavailable(0); + assertEquals(State.WAITING_FOR_LEADER, state.currentState()); + } + + /** After onGroupUnavailable, new waiters get fresh future. */ + @Test + void testNewWaitersAfterUnavailable() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + state.onLeaderElected(leaderNode, 1); + CompletableFuture<Long> future1 = state.awaitLeader(); + assertTrue(future1.isDone()); + + state.onGroupUnavailable(1); + + CompletableFuture<Long> future2 = state.awaitLeader(); + assertFalse(future2.isDone(), "New waiter should get incomplete future after unavailable"); + } + + /** Multiple waiters are all completed on leader election. */ + @Test + void testMultipleWaitersCompleted() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader-node"); + + CompletableFuture<Long> waiter1 = state.awaitLeader(); + CompletableFuture<Long> waiter2 = state.awaitLeader(); + CompletableFuture<Long> waiter3 = state.awaitLeader(); + + assertFalse(waiter1.isDone()); + assertFalse(waiter2.isDone()); + assertFalse(waiter3.isDone()); + + state.onLeaderElected(leaderNode, 10); + + assertTrue(waiter1.isDone()); + assertTrue(waiter2.isDone()); + assertTrue(waiter3.isDone()); + + assertEquals(10L, waiter1.join()); + assertEquals(10L, waiter2.join()); + assertEquals(10L, waiter3.join()); + } + + /** Term 0 is accepted as first valid term. */ + @Test + void testTermZeroAccepted() { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + InternalClusterNode leaderNode = createNode("leader"); + + assertEquals(-1, state.currentTerm()); + + state.onLeaderElected(leaderNode, 0); + + assertEquals(0, state.currentTerm()); + assertEquals(State.LEADER_AVAILABLE, state.currentState()); + } + + /** Concurrent leader elections with different terms. */ + @Test + void testConcurrentLeaderElections() throws Exception { + LeaderAvailabilityState state = new LeaderAvailabilityState(); + int threadCount = 10; + CountDownLatch startLatch = new CountDownLatch(1); + CountDownLatch doneLatch = new CountDownLatch(threadCount); + AtomicLong maxTerm = new AtomicLong(-1); + + Thread[] threads = new Thread[threadCount]; + for (int i = 0; i < threadCount; i++) { + int term = i; + threads[i] = new Thread(() -> { + try { + startLatch.await(); + InternalClusterNode leader = createNode("leader-" + term); + state.onLeaderElected(leader, term); + maxTerm.updateAndGet(current -> Math.max(current, term)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + doneLatch.countDown(); + } + }); + threads[i].start(); + } + + startLatch.countDown(); + assertTrue(doneLatch.await(5, TimeUnit.SECONDS)); + + // The final term should be the maximum term that was set + assertEquals(threadCount - 1, state.currentTerm()); + } + + /** State machine handles rapid state transitions. */ + @Test + void testRapidStateTransitions() { 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
