Search in sources :

Example 21 with RaftAlgorithmConfig

use of com.hazelcast.config.cp.RaftAlgorithmConfig in project hazelcast by hazelcast.

the class XMLConfigBuilderTest method testCPSubsystemConfig.

@Override
@Test
public void testCPSubsystemConfig() {
    String xml = HAZELCAST_START_TAG + "<cp-subsystem>\n" + "  <cp-member-count>10</cp-member-count>\n" + "  <group-size>5</group-size>\n" + "  <session-time-to-live-seconds>15</session-time-to-live-seconds>\n" + "  <session-heartbeat-interval-seconds>3</session-heartbeat-interval-seconds>\n" + "  <missing-cp-member-auto-removal-seconds>120</missing-cp-member-auto-removal-seconds>\n" + "  <fail-on-indeterminate-operation-state>true</fail-on-indeterminate-operation-state>\n" + "  <persistence-enabled>true</persistence-enabled>\n" + "  <base-dir>/mnt/cp-data</base-dir>\n" + "  <data-load-timeout-seconds>30</data-load-timeout-seconds>\n" + "  <raft-algorithm>\n" + "    <leader-election-timeout-in-millis>500</leader-election-timeout-in-millis>\n" + "    <leader-heartbeat-period-in-millis>100</leader-heartbeat-period-in-millis>\n" + "    <max-missed-leader-heartbeat-count>3</max-missed-leader-heartbeat-count>\n" + "    <append-request-max-entry-count>25</append-request-max-entry-count>\n" + "    <commit-index-advance-count-to-snapshot>250</commit-index-advance-count-to-snapshot>\n" + "    <uncommitted-entry-count-to-reject-new-appends>75</uncommitted-entry-count-to-reject-new-appends>\n" + "    <append-request-backoff-timeout-in-millis>50</append-request-backoff-timeout-in-millis>\n" + "  </raft-algorithm>\n" + "  <semaphores>\n" + "    <semaphore>\n" + "      <name>sem1</name>\n" + "      <jdk-compatible>true</jdk-compatible>\n" + "      <initial-permits>1</initial-permits>\n" + "    </semaphore>\n" + "    <semaphore>\n" + "      <name>sem2</name>\n" + "      <jdk-compatible>false</jdk-compatible>\n" + "      <initial-permits>2</initial-permits>\n" + "    </semaphore>\n" + "  </semaphores>\n" + "  <locks>\n" + "    <fenced-lock>\n" + "      <name>lock1</name>\n" + "      <lock-acquire-limit>1</lock-acquire-limit>\n" + "    </fenced-lock>\n" + "    <fenced-lock>\n" + "      <name>lock2</name>\n" + "      <lock-acquire-limit>2</lock-acquire-limit>\n" + "    </fenced-lock>\n" + "  </locks>\n" + "</cp-subsystem>" + HAZELCAST_END_TAG;
    Config config = new InMemoryXmlConfig(xml);
    CPSubsystemConfig cpSubsystemConfig = config.getCPSubsystemConfig();
    assertEquals(10, cpSubsystemConfig.getCPMemberCount());
    assertEquals(5, cpSubsystemConfig.getGroupSize());
    assertEquals(15, cpSubsystemConfig.getSessionTimeToLiveSeconds());
    assertEquals(3, cpSubsystemConfig.getSessionHeartbeatIntervalSeconds());
    assertEquals(120, cpSubsystemConfig.getMissingCPMemberAutoRemovalSeconds());
    assertTrue(cpSubsystemConfig.isFailOnIndeterminateOperationState());
    assertTrue(cpSubsystemConfig.isPersistenceEnabled());
    assertEquals(new File("/mnt/cp-data").getAbsoluteFile(), cpSubsystemConfig.getBaseDir().getAbsoluteFile());
    assertEquals(30, cpSubsystemConfig.getDataLoadTimeoutSeconds());
    RaftAlgorithmConfig raftAlgorithmConfig = cpSubsystemConfig.getRaftAlgorithmConfig();
    assertEquals(500, raftAlgorithmConfig.getLeaderElectionTimeoutInMillis());
    assertEquals(100, raftAlgorithmConfig.getLeaderHeartbeatPeriodInMillis());
    assertEquals(3, raftAlgorithmConfig.getMaxMissedLeaderHeartbeatCount());
    assertEquals(25, raftAlgorithmConfig.getAppendRequestMaxEntryCount());
    assertEquals(250, raftAlgorithmConfig.getCommitIndexAdvanceCountToSnapshot());
    assertEquals(75, raftAlgorithmConfig.getUncommittedEntryCountToRejectNewAppends());
    assertEquals(50, raftAlgorithmConfig.getAppendRequestBackoffTimeoutInMillis());
    SemaphoreConfig semaphoreConfig1 = cpSubsystemConfig.findSemaphoreConfig("sem1");
    SemaphoreConfig semaphoreConfig2 = cpSubsystemConfig.findSemaphoreConfig("sem2");
    assertNotNull(semaphoreConfig1);
    assertNotNull(semaphoreConfig2);
    assertTrue(semaphoreConfig1.isJDKCompatible());
    assertFalse(semaphoreConfig2.isJDKCompatible());
    assertEquals(1, semaphoreConfig1.getInitialPermits());
    assertEquals(2, semaphoreConfig2.getInitialPermits());
    FencedLockConfig lockConfig1 = cpSubsystemConfig.findLockConfig("lock1");
    FencedLockConfig lockConfig2 = cpSubsystemConfig.findLockConfig("lock2");
    assertNotNull(lockConfig1);
    assertNotNull(lockConfig2);
    assertEquals(1, lockConfig1.getLockAcquireLimit());
    assertEquals(2, lockConfig2.getLockAcquireLimit());
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) FencedLockConfig(com.hazelcast.config.cp.FencedLockConfig) TokenIdentityConfig(com.hazelcast.config.security.TokenIdentityConfig) LdapAuthenticationConfig(com.hazelcast.config.security.LdapAuthenticationConfig) SemaphoreConfig(com.hazelcast.config.cp.SemaphoreConfig) CPSubsystemConfig(com.hazelcast.config.cp.CPSubsystemConfig) RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) SimpleAuthenticationConfig(com.hazelcast.config.security.SimpleAuthenticationConfig) KerberosIdentityConfig(com.hazelcast.config.security.KerberosIdentityConfig) KerberosAuthenticationConfig(com.hazelcast.config.security.KerberosAuthenticationConfig) RealmConfig(com.hazelcast.config.security.RealmConfig) FencedLockConfig(com.hazelcast.config.cp.FencedLockConfig) SemaphoreConfig(com.hazelcast.config.cp.SemaphoreConfig) File.createTempFile(java.io.File.createTempFile) File(java.io.File) CPSubsystemConfig(com.hazelcast.config.cp.CPSubsystemConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with RaftAlgorithmConfig

use of com.hazelcast.config.cp.RaftAlgorithmConfig in project hazelcast by hazelcast.

the class PersistenceTest method when_followerIsRestarted_then_itRestoresItsRaftStateWithSnapshot.

@Test
public void when_followerIsRestarted_then_itRestoresItsRaftStateWithSnapshot() throws ExecutionException, InterruptedException {
    final int committedEntryCountToSnapshot = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(committedEntryCountToSnapshot);
    group = new LocalRaftGroupBuilder(3, config).setAppendNopEntryOnLeaderElection(true).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    final RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i <= committedEntryCountToSnapshot; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl node : group.getNodes()) {
            assertTrue(getSnapshotEntry(node).index() > 0);
        }
    });
    RaftNodeImpl terminatedFollower = group.getAnyFollowerNode();
    RaftEndpoint terminatedEndpoint = terminatedFollower.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(terminatedFollower);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    group.terminateNode(terminatedEndpoint);
    leader.replicate(new ApplyRaftRunnable("val" + (committedEntryCountToSnapshot + 1))).get();
    final RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    assertEquals(new ArrayList<>(getCommittedGroupMembers(leader).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
    assertEquals(new ArrayList<>(getLastGroupMembers(leader).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    assertTrueEventually(() -> {
        assertEquals(leader.getLocalMember(), restartedNode.getLeader());
        assertEquals(getTerm(leader), getTerm(restartedNode));
        assertEquals(getCommitIndex(leader), getCommitIndex(restartedNode));
        assertEquals(getLastApplied(leader), getLastApplied(restartedNode));
        RaftDataService service = group.getService(restartedNode);
        Object[] values = service.valuesArray();
        assertThat(values, arrayWithSize(committedEntryCountToSnapshot + 2));
        for (int i = 0; i <= committedEntryCountToSnapshot + 1; i++) {
            assertEquals("val" + i, values[i]);
        }
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) InMemoryRaftStateStore(com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 23 with RaftAlgorithmConfig

use of com.hazelcast.config.cp.RaftAlgorithmConfig in project hazelcast by hazelcast.

the class PersistenceTest method when_leaderIsRestarted_then_itBecomesLeaderAndAppliesPreviouslyCommittedMemberListViaSnapshot.

@Test
public void when_leaderIsRestarted_then_itBecomesLeaderAndAppliesPreviouslyCommittedMemberListViaSnapshot() throws ExecutionException, InterruptedException {
    int committedEntryCountToSnapshot = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(committedEntryCountToSnapshot);
    group = new LocalRaftGroupBuilder(3, config).setAppendNopEntryOnLeaderElection(true).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl removedFollower = followers[0];
    final RaftNodeImpl runningFollower = followers[1];
    group.terminateNode(removedFollower.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val")).get();
    leader.replicateMembershipChange(removedFollower.getLocalMember(), REMOVE).get();
    while (getSnapshotEntry(leader).index() == 0) {
        leader.replicate(new ApplyRaftRunnable("val")).get();
    }
    RaftEndpoint terminatedEndpoint = leader.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(leader);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    // Block voting between followers
    // to avoid a leader election before leader restarts.
    blockVotingBetweenFollowers();
    group.terminateNode(terminatedEndpoint);
    RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    assertSame(restartedNode, newLeader);
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(runningFollower), getCommitIndex(restartedNode));
        assertEquals(new ArrayList<>(getCommittedGroupMembers(runningFollower).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
        assertEquals(new ArrayList<>(getLastGroupMembers(runningFollower).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) InMemoryRaftStateStore(com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with RaftAlgorithmConfig

use of com.hazelcast.config.cp.RaftAlgorithmConfig in project hazelcast by hazelcast.

the class PersistenceTest method when_followerIsRestarted_then_itAppliesPreviouslyCommittedMemberListViaSnapshot.

@Test
public void when_followerIsRestarted_then_itAppliesPreviouslyCommittedMemberListViaSnapshot() throws ExecutionException, InterruptedException {
    int committedEntryCountToSnapshot = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(committedEntryCountToSnapshot).setLeaderHeartbeatPeriodInMillis(SECONDS.toMillis(30));
    group = new LocalRaftGroupBuilder(3, config).setAppendNopEntryOnLeaderElection(true).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    final RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl removedFollower = followers[0];
    RaftNodeImpl terminatedFollower = followers[1];
    group.terminateNode(removedFollower.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val")).get();
    leader.replicateMembershipChange(removedFollower.getLocalMember(), REMOVE).get();
    while (getSnapshotEntry(terminatedFollower).index() == 0) {
        leader.replicate(new ApplyRaftRunnable("val")).get();
    }
    RaftEndpoint terminatedEndpoint = terminatedFollower.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(terminatedFollower);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    group.terminateNode(terminatedEndpoint);
    final RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(leader), getCommitIndex(restartedNode));
        assertEquals(getLastApplied(leader), getLastApplied(restartedNode));
        assertEquals(new ArrayList<>(getCommittedGroupMembers(leader).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
        assertEquals(new ArrayList<>(getLastGroupMembers(leader).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) InMemoryRaftStateStore(com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 25 with RaftAlgorithmConfig

use of com.hazelcast.config.cp.RaftAlgorithmConfig in project hazelcast by hazelcast.

the class SnapshotTest method when_leaderMissesInstallSnapshotResponse_then_itAdvancesMatchIndexWithNextInstallSnapshotResponse.

@Test
public void when_leaderMissesInstallSnapshotResponse_then_itAdvancesMatchIndexWithNextInstallSnapshotResponse() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount).setAppendRequestBackoffTimeoutInMillis(1000);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl slowFollower = followers[1];
    // the leader cannot send AppendEntriesRPC to the follower
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    // the follower cannot send append response to the leader after installing the snapshot
    group.dropMessagesToMember(slowFollower.getLocalMember(), leader.getLocalMember(), AppendSuccessResponse.class);
    for (int i = 0; i < entryCount; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
    leader.replicate(new ApplyRaftRunnable("valFinal")).get();
    group.resetAllRulesFrom(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodesExcept(slowFollower.getLocalMember())) {
            assertEquals(entryCount + 1, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(entryCount + 1, service.size());
            for (int i = 0; i < entryCount; i++) {
                assertEquals(("val" + i), service.get(i + 1));
            }
            assertEquals("valFinal", service.get(51));
        }
        assertEquals(entryCount, getCommitIndex(slowFollower));
        RaftDataService service = group.getService(slowFollower);
        assertEquals(entryCount, service.size());
        for (int i = 0; i < entryCount; i++) {
            assertEquals(("val" + i), service.get(i + 1));
        }
    });
    group.resetAllRulesFrom(slowFollower.getLocalMember());
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNode raftNode : group.getNodesExcept(leader.getLocalMember())) {
            assertEquals(commitIndex, getMatchIndex(leader, raftNode.getLocalMember()));
        }
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)32 QuickTest (com.hazelcast.test.annotation.QuickTest)28 Test (org.junit.Test)28 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)27 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)23 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)14 LocalRaftGroupBuilder (com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder)9 RestoredRaftState (com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState)8 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)7 InMemoryRaftStateStore (com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore)6 CPSubsystemConfig (com.hazelcast.config.cp.CPSubsystemConfig)4 FencedLockConfig (com.hazelcast.config.cp.FencedLockConfig)4 SemaphoreConfig (com.hazelcast.config.cp.SemaphoreConfig)4 StaleAppendRequestException (com.hazelcast.cp.exception.StaleAppendRequestException)3 KerberosAuthenticationConfig (com.hazelcast.config.security.KerberosAuthenticationConfig)2 KerberosIdentityConfig (com.hazelcast.config.security.KerberosIdentityConfig)2 LdapAuthenticationConfig (com.hazelcast.config.security.LdapAuthenticationConfig)2 RealmConfig (com.hazelcast.config.security.RealmConfig)2 SimpleAuthenticationConfig (com.hazelcast.config.security.SimpleAuthenticationConfig)2 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)2