JAkutenshi commented on code in PR #7242:
URL: https://github.com/apache/ignite-3/pull/7242#discussion_r2743024139


##########
modules/raft/src/test/java/org/apache/ignite/internal/raft/client/RaftGroupServiceTest.java:
##########
@@ -680,6 +680,51 @@ public void testRetryOnErrorWithTimeout(RaftError error) {
         assertThat(response, willThrow(TimeoutException.class, "Send with 
retry timed out"));
     }
 
+    /**
+     * Tests that UNKNOWN/EINTERNAL/ENOENT errors retry on the same peer (the 
leader) for ActionRequests.
+     * This is because these errors are transient and the leader is likely to 
recover.
+     */
+    @ParameterizedTest
+    @EnumSource(names = {"UNKNOWN", "EINTERNAL", "ENOENT"})
+    public void testRetryOnTransientErrorRetriesOnSamePeer(RaftError error) {
+        Peer leaderPeer = NODES.get(0);
+
+        // First call returns error, second call succeeds - both to the same 
leader.
+        when(messagingService.invoke(
+                argThat((InternalClusterNode node) -> node != null && 
node.name().equals(leaderPeer.consistentId())),
+                any(ReadActionRequest.class),
+                anyLong())
+        )
+                .thenReturn(completedFuture(FACTORY.errorResponse()
+                        .errorCode(error.getNumber())
+                        .build()))
+                
.thenReturn(completedFuture(FACTORY.actionResponse().result(null).build()));
+
+        RaftGroupService service = 
startRaftGroupServiceWithRefreshLeader(NODES);
+
+        assertThat(service.leader(), is(leaderPeer));
+
+        CompletableFuture<Object> response = 
service.run(mock(ReadCommand.class));
+
+        assertThat(response, willBe(nullValue()));
+
+        // Verify that only the leader was called (twice: once with error, 
once with success).
+        verify(messagingService, atLeastOnce()).invoke(
+                argThat((InternalClusterNode target) -> target != null && 
target.name().equals(leaderPeer.consistentId())),
+                any(ReadActionRequest.class),
+                anyLong()
+        );
+
+        // Verify that other peers were NOT called.
+        for (Peer otherPeer : NODES.subList(1, NODES.size())) {
+            verify(messagingService, org.mockito.Mockito.never()).invoke(

Review Comment:
   Resolved.



##########
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. */

Review Comment:
   Resolved.



##########
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);

Review Comment:
   Resolved.



##########
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());

Review Comment:
   Resolved.



##########
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());

Review Comment:
   Resolved.



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

Reply via email to