Search in sources :

Example 1 with HeartbeatSessionOp

use of com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp 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 2 with HeartbeatSessionOp

use of com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp in project hazelcast by hazelcast.

the class RaftSessionServiceTest method testSessionHeartbeat.

@Test
public void testSessionHeartbeat() 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;
        }
    });
    invocationManager.invoke(groupId, new HeartbeatSessionOp(response.getSessionId())).get();
    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);
            assertTrue(session.version() > sessions[i].version());
        }
    });
}
Also used : 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 3 with HeartbeatSessionOp

use of com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp in project hazelcast by hazelcast.

the class RaftSessionServiceTest method testHeartbeatFailsAfterSessionClose.

@Test
public void testHeartbeatFailsAfterSessionClose() throws ExecutionException, InterruptedException, UnknownHostException {
    SessionResponse response = invocationManager.<SessionResponse>invoke(groupId, newCreateSessionOp()).get();
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService service = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            RaftSessionRegistry registry = service.getSessionRegistryOrNull(groupId);
            assertNotNull(registry);
            assertNotNull(registry.getSession(response.getSessionId()));
        }
    });
    invocationManager.invoke(groupId, new CloseSessionOp(response.getSessionId())).get();
    exception.expectCause(isA(SessionExpiredException.class));
    invocationManager.invoke(groupId, new HeartbeatSessionOp(response.getSessionId())).get();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CloseSessionOp(com.hazelcast.cp.internal.session.operation.CloseSessionOp) HeartbeatSessionOp(com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

HeartbeatSessionOp (com.hazelcast.cp.internal.session.operation.HeartbeatSessionOp)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)2 RaftTestApplyOp (com.hazelcast.cp.internal.RaftTestApplyOp)1 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)1 CloseSessionOp (com.hazelcast.cp.internal.session.operation.CloseSessionOp)1