Search in sources :

Example 21 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class LeaderStateTest method test_matchIndex.

@Test
public void test_matchIndex() {
    Map<RaftEndpoint, Long> indices = new HashMap<RaftEndpoint, Long>();
    for (RaftEndpoint endpoint : remoteEndpoints) {
        long index = 1 + RandomPicker.getInt(100);
        state.getFollowerState(endpoint).matchIndex(index);
        indices.put(endpoint, index);
    }
    for (RaftEndpoint endpoint : remoteEndpoints) {
        long index = indices.get(endpoint);
        assertEquals(index, state.getFollowerState(endpoint).matchIndex());
    }
    long[] matchIndices = state.matchIndices();
    assertEquals(indices.size() + 1, matchIndices.length);
    for (int i = 0; i < matchIndices.length - 1; i++) {
        long index = matchIndices[i];
        assertTrue(indices.containsValue(index));
    }
}
Also used : HashMap(java.util.HashMap) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class LeaderStateTest method test_initialState.

@Test
public void test_initialState() {
    for (RaftEndpoint endpoint : remoteEndpoints) {
        FollowerState followerState = state.getFollowerState(endpoint);
        assertEquals(0, followerState.matchIndex());
        assertEquals(lastLogIndex + 1, followerState.nextIndex());
    }
    long[] matchIndices = state.matchIndices();
    assertEquals(remoteEndpoints.size() + 1, matchIndices.length);
    for (long index : matchIndices) {
        assertEquals(0, index);
    }
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 23 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class RaftSessionServiceTest method testLeaderFailureShiftsSessionExpirationTimes.

@Test
public void testLeaderFailureShiftsSessionExpirationTimes() throws ExecutionException, InterruptedException, UnknownHostException {
    SessionResponse response = invocationManager.<SessionResponse>invoke(groupId, newCreateSessionOp()).get();
    CPSessionInfo[] sessions = new CPSessionInfo[instances.length];
    assertTrueEventually(() -> {
        for (int i = 0; i < instances.length; i++) {
            RaftSessionService service = getNodeEngineImpl(instances[i]).getService(RaftSessionService.SERVICE_NAME);
            RaftSessionRegistry registry = service.getSessionRegistryOrNull(groupId);
            assertNotNull(registry);
            CPSessionInfo session = registry.getSession(response.getSessionId());
            assertNotNull(session);
            sessions[i] = session;
        }
    });
    RaftEndpoint leaderEndpoint = getLeaderMember(getRaftNode(instances[0], groupId));
    HazelcastInstance leader = getInstance(leaderEndpoint);
    leader.getLifecycleService().terminate();
    assertTrueEventually(() -> {
        for (int i = 0; i < instances.length; i++) {
            Node node;
            try {
                node = getNode(instances[i]);
            } catch (IllegalArgumentException ignored) {
                continue;
            }
            RaftSessionService service = node.nodeEngine.getService(RaftSessionService.SERVICE_NAME);
            RaftSessionRegistry registry = service.getSessionRegistryOrNull(groupId);
            assertNotNull(registry);
            CPSessionInfo session = registry.getSession(response.getSessionId());
            assertNotNull(session);
            assertTrue(session.version() > sessions[i].version());
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Node(com.hazelcast.instance.impl.Node) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class RaftStateTest method test_majority.

private void test_majority(int count) {
    members = new HashSet<RaftEndpoint>();
    members.add(localMember);
    for (int i = 1; i < count; i++) {
        members.add(newRaftMember(1000 + i));
    }
    state = newRaftState(groupId, localMember, members, 100);
    assertEquals(majority(count), state.majority());
}
Also used : TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 25 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.

the class RaftStateTest method toLeader_fromCandidate.

@Test
public void toLeader_fromCandidate() {
    state.toCandidate(false);
    int term = state.term();
    RaftLog log = state.log();
    log.appendEntries(new LogEntry(term, 1, null), new LogEntry(term, 2, null), new LogEntry(term, 3, null));
    long lastLogIndex = log.lastLogOrSnapshotIndex();
    state.toLeader();
    assertEquals(RaftRole.LEADER, state.role());
    assertEquals(localMember, state.leader());
    assertNull(state.candidateState());
    LeaderState leaderState = state.leaderState();
    assertNotNull(leaderState);
    for (RaftEndpoint endpoint : state.remoteMembers()) {
        FollowerState followerState = leaderState.getFollowerState(endpoint);
        assertEquals(0, followerState.matchIndex());
        assertEquals(lastLogIndex + 1, followerState.nextIndex());
    }
    long[] matchIndices = leaderState.matchIndices();
    assertEquals(state.remoteMembers().size() + 1, matchIndices.length);
    for (long index : matchIndices) {
        assertEquals(0, index);
    }
}
Also used : TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) TestRaftEndpoint(com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)57 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)14 QuickTest (com.hazelcast.test.annotation.QuickTest)14 Test (org.junit.Test)14 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)8 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)7 ArrayList (java.util.ArrayList)7 CPMember (com.hazelcast.cp.CPMember)6 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)6 UUID (java.util.UUID)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 CPGroupId (com.hazelcast.cp.CPGroupId)5 TestRaftEndpoint (com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint)5 LinkedHashSet (java.util.LinkedHashSet)4 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)2 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 CannotCreateRaftGroupException (com.hazelcast.cp.internal.exception.CannotCreateRaftGroupException)2 RaftStateStore (com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2