Search in sources :

Example 31 with RaftNodeImpl

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

the class LocalRaftIntegration method send.

@Override
public boolean send(VoteRequest request, RaftEndpoint target) {
    assertNotEquals(localEndpoint, target);
    RaftNodeImpl node = nodes.get(target);
    if (node == null) {
        return false;
    }
    if (shouldDrop(request, target)) {
        return true;
    }
    node.handleVoteRequest(alterMessageIfNeeded(request, target));
    return true;
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)

Example 32 with RaftNodeImpl

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

the class RaftSessionServiceTest method testSnapshotRestore.

@Test
public void testSnapshotRestore() throws ExecutionException, InterruptedException, UnknownHostException {
    HazelcastInstance leader = getLeaderInstance(instances, groupId);
    HazelcastInstance follower = getRandomFollowerInstance(instances, groupId);
    // the follower falls behind the leader. It neither append entries nor installs snapshots.
    dropOperationsBetween(leader, follower, RaftServiceDataSerializerHook.F_ID, asList(RaftServiceDataSerializerHook.APPEND_REQUEST_OP, RaftServiceDataSerializerHook.INSTALL_SNAPSHOT_OP));
    SessionResponse response = invocationManager.<SessionResponse>invoke(groupId, newCreateSessionOp()).get();
    spawn(() -> {
        for (int i = 0; i < 30; i++) {
            invocationManager.invoke(groupId, new HeartbeatSessionOp(response.getSessionId())).joinInternal();
            sleepAtLeastSeconds(5);
        }
    });
    for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
        invocationManager.invoke(groupId, new RaftTestApplyOp("value" + i)).get();
    }
    RaftNodeImpl leaderRaftNode = (RaftNodeImpl) ((RaftService) getNodeEngineImpl(leader).getService(RaftService.SERVICE_NAME)).getRaftNode(groupId);
    RaftNodeImpl followerRaftNode = (RaftNodeImpl) ((RaftService) getNodeEngineImpl(follower).getService(RaftService.SERVICE_NAME)).getRaftNode(groupId);
    // the leader takes a snapshot
    long[] leaderSnapshotIndex = new long[1];
    assertTrueEventually(() -> {
        long idx = getSnapshotEntry(leaderRaftNode).index();
        assertTrue(idx > 0);
        leaderSnapshotIndex[0] = idx;
    });
    // the follower doesn't have it since its raft log is still empty
    assertTrueAllTheTime(() -> assertEquals(0, getSnapshotEntry(followerRaftNode).index()), 10);
    resetPacketFiltersFrom(leader);
    // the follower installs the snapshot after it hears from the leader
    assertTrueEventually(() -> assertTrue(getSnapshotEntry(followerRaftNode).index() > 0));
    assertTrueEventually(() -> {
        RaftSessionService sessionService = getNodeEngineImpl(follower).getService(RaftSessionService.SERVICE_NAME);
        RaftSessionRegistry registry = sessionService.getSessionRegistryOrNull(groupId);
        assertNotNull(registry.getSession(response.getSessionId()));
    });
    // the follower disconnects from the leader again
    dropOperationsBetween(leader, follower, RaftServiceDataSerializerHook.F_ID, asList(RaftServiceDataSerializerHook.APPEND_REQUEST_OP, RaftServiceDataSerializerHook.INSTALL_SNAPSHOT_OP));
    for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
        invocationManager.invoke(groupId, new HeartbeatSessionOp(response.getSessionId())).get();
    }
    // the leader takes a new snapshot
    assertTrueEventually(() -> assertTrue(getSnapshotEntry(leaderRaftNode).index() > leaderSnapshotIndex[0]));
    resetPacketFiltersFrom(leader);
    // the follower installs the new snapshot after it hears from the leader
    assertTrueEventually(() -> {
        CPSessionInfo leaderSession = getSession(leader, groupId, response.getSessionId());
        CPSessionInfo followerSession = getSession(follower, groupId, response.getSessionId());
        assertNotNull(leaderSession);
        assertNotNull(followerSession);
        assertEquals(leaderSession.version(), followerSession.version());
    });
}
Also used : RaftTestApplyOp(com.hazelcast.cp.internal.RaftTestApplyOp) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) HeartbeatSessionOp(com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp) 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 33 with RaftNodeImpl

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

the class LocalRaftGroup method destroy.

public void destroy() {
    for (int i = 0; i < nodes.length; i++) {
        RaftNodeImpl node = nodes[i];
        LocalRaftIntegration integration = integrations[i];
        if (integration.isShutdown()) {
            continue;
        }
        node.forceSetTerminatedStatus().joinInternal();
        integration.shutdown();
    }
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 34 with RaftNodeImpl

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

the class LocalRaftGroup method waitUntilLeaderElected.

public RaftNodeImpl waitUntilLeaderElected() {
    RaftNodeImpl[] leaderRef = new RaftNodeImpl[1];
    assertTrueEventually(() -> {
        RaftNodeImpl leaderNode = getLeaderNode();
        assertNotNull(leaderNode);
        int leaderTerm = getTerm(leaderNode);
        for (RaftNodeImpl raftNode : nodes) {
            if (integrations[getIndexOf(raftNode.getLocalMember())].isShutdown()) {
                continue;
            }
            assertEquals(leaderNode.getLocalMember(), getLeaderMember(raftNode));
            assertEquals(leaderTerm, getTerm(raftNode));
        }
        leaderRef[0] = leaderNode;
    });
    return leaderRef[0];
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 35 with RaftNodeImpl

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

the class LocalRaftGroup method createNewRaftNode.

public RaftNodeImpl createNewRaftNode() {
    int oldSize = this.integrations.length;
    int newSize = oldSize + 1;
    RaftEndpoint[] endpoints = new RaftEndpoint[newSize];
    LocalRaftIntegration[] integrations = new LocalRaftIntegration[newSize];
    RaftNodeImpl[] nodes = new RaftNodeImpl[newSize];
    System.arraycopy(this.members, 0, endpoints, 0, oldSize);
    System.arraycopy(this.integrations, 0, integrations, 0, oldSize);
    System.arraycopy(this.nodes, 0, nodes, 0, oldSize);
    LocalRaftIntegration integration = createNewLocalRaftIntegration();
    createdNodeCount++;
    integrations[oldSize] = integration;
    RaftEndpoint endpoint = integration.getLocalEndpoint();
    endpoints[oldSize] = endpoint;
    RaftStateStore raftStateStore = raftStateStoreFactory.apply(endpoint, raftAlgorithmConfig);
    RaftNodeImpl node = newRaftNode(groupId, endpoint, asList(initialMembers), raftAlgorithmConfig, integration, raftStateStore);
    nodes[oldSize] = node;
    this.members = endpoints;
    this.integrations = integrations;
    this.nodes = nodes;
    node.start();
    initDiscovery();
    return node;
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftStateStore(com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore) NopRaftStateStore(com.hazelcast.cp.internal.raft.impl.persistence.NopRaftStateStore) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Aggregations

RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)38 HazelcastInstance (com.hazelcast.core.HazelcastInstance)18 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)17 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)14 Test (org.junit.Test)14 CPGroupId (com.hazelcast.cp.CPGroupId)9 CPMember (com.hazelcast.cp.CPMember)9 CPGroup (com.hazelcast.cp.CPGroup)8 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance)7 QuickTest (com.hazelcast.test.annotation.QuickTest)7 SlowTest (com.hazelcast.test.annotation.SlowTest)7 ArrayList (java.util.ArrayList)5 Config (com.hazelcast.config.Config)4 MigrationEndpoint (com.hazelcast.internal.partition.MigrationEndpoint)4 TestHazelcastInstanceFactory.initOrCreateConfig (com.hazelcast.test.TestHazelcastInstanceFactory.initOrCreateConfig)4 ResourceRegistry (com.hazelcast.cp.internal.datastructures.spi.blocking.ResourceRegistry)3 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 RaftStateStore (com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore)3 RestoreSnapshotOp (com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp)3 List (java.util.List)3