Search in sources :

Example 1 with RestoreSnapshotOp

use of com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp in project hazelcast by hazelcast.

the class SemaphoreAdvancedTest method testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot.

@Test
public void testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot() throws ExecutionException, InterruptedException {
    semaphore.init(1);
    spawn(() -> {
        try {
            semaphore.tryAcquire(2, 10, MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    CPGroupId groupId = getGroupId();
    assertTrueEventually(() -> {
        HazelcastInstance leader = leaderInstanceOf(groupId);
        SemaphoreService service = getNodeEngineImpl(leader).getService(SemaphoreService.SERVICE_NAME);
        SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
        assertFalse(registry.getWaitTimeouts().isEmpty());
    });
    for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
        semaphore.acquire();
        semaphore.release();
    }
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftNodeImpl raftNode = getRaftNode(instance, groupId);
            assertNotNull(raftNode);
            LogEntry snapshotEntry = getSnapshotEntry(raftNode);
            assertTrue(snapshotEntry.index() > 0);
            List<RestoreSnapshotOp> ops = (List<RestoreSnapshotOp>) snapshotEntry.operation();
            for (RestoreSnapshotOp op : ops) {
                if (op.getServiceName().equals(SemaphoreService.SERVICE_NAME)) {
                    ResourceRegistry registry = (ResourceRegistry) op.getSnapshot();
                    assertFalse(registry.getWaitTimeouts().isEmpty());
                    return;
                }
            }
            fail();
        }
    });
    instances[1].shutdown();
    HazelcastInstance newInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
    newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
    assertTrueEventually(() -> {
        SemaphoreService service = getNodeEngineImpl(newInstance).getService(SemaphoreService.SERVICE_NAME);
        SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertFalse(registry.getWaitTimeouts().isEmpty());
        assertEquals(1, registry.availablePermits(objectName));
    });
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RestoreSnapshotOp(com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp) List(java.util.List) ResourceRegistry(com.hazelcast.cp.internal.datastructures.spi.blocking.ResourceRegistry) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with RestoreSnapshotOp

use of com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp in project hazelcast by hazelcast.

the class NodeEngineRaftIntegration method restoreSnapshot.

@Override
public void restoreSnapshot(Object op, long commitIndex) {
    ILogger logger = nodeEngine.getLogger(getClass());
    List<RestoreSnapshotOp> snapshotOps = (List<RestoreSnapshotOp>) op;
    for (RestoreSnapshotOp snapshotOp : snapshotOps) {
        Object result = runOperation(snapshotOp, commitIndex);
        if (result instanceof Throwable) {
            logger.severe("Restore of " + snapshotOp + " failed...", (Throwable) result);
        }
    }
}
Also used : RestoreSnapshotOp(com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp) ILogger(com.hazelcast.logging.ILogger) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with RestoreSnapshotOp

use of com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp in project hazelcast by hazelcast.

the class NodeEngineRaftIntegration method takeSnapshot.

@Override
public Object takeSnapshot(long commitIndex) {
    try {
        List<RestoreSnapshotOp> snapshotOps = new ArrayList<>();
        for (ServiceInfo serviceInfo : nodeEngine.getServiceInfos(SnapshotAwareService.class)) {
            SnapshotAwareService service = serviceInfo.getService();
            Object snapshot = service.takeSnapshot(groupId, commitIndex);
            if (snapshot != null) {
                snapshotOps.add(new RestoreSnapshotOp(serviceInfo.getName(), snapshot));
            }
        }
        return snapshotOps;
    } catch (Throwable t) {
        return t;
    }
}
Also used : ServiceInfo(com.hazelcast.spi.impl.servicemanager.ServiceInfo) ArrayList(java.util.ArrayList) SnapshotAwareService(com.hazelcast.cp.internal.raft.SnapshotAwareService) RestoreSnapshotOp(com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp)

Example 4 with RestoreSnapshotOp

use of com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp in project hazelcast by hazelcast.

the class FencedLockAdvancedTest method testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot.

@Test
public void testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot() throws ExecutionException, InterruptedException {
    long fence = this.lock.lockAndGetFence();
    assertTrue(fence > 0);
    spawn(() -> {
        lock.tryLock(10, MINUTES);
    });
    CPGroupId groupId = this.lock.getGroupId();
    assertTrueEventually(() -> {
        HazelcastInstance leader = getLeaderInstance(instances, groupId);
        LockService service = getNodeEngineImpl(leader).getService(LockService.SERVICE_NAME);
        ResourceRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertFalse(registry.getWaitTimeouts().isEmpty());
    });
    spawn(() -> {
        for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
            lock.isLocked();
        }
    });
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftNodeImpl raftNode = getRaftNode(instance, groupId);
            assertNotNull(raftNode);
            LogEntry snapshotEntry = getSnapshotEntry(raftNode);
            assertTrue(snapshotEntry.index() > 0);
            List<RestoreSnapshotOp> ops = (List<RestoreSnapshotOp>) snapshotEntry.operation();
            for (RestoreSnapshotOp op : ops) {
                if (op.getServiceName().equals(LockService.SERVICE_NAME)) {
                    ResourceRegistry registry = (ResourceRegistry) op.getSnapshot();
                    assertFalse(registry.getWaitTimeouts().isEmpty());
                    return;
                }
            }
            fail();
        }
    });
    HazelcastInstance instanceToShutdown = (instances[0] == proxyInstance) ? instances[1] : instances[0];
    instanceToShutdown.shutdown();
    HazelcastInstance newInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
    newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
    assertTrueEventually(() -> {
        RaftNodeImpl raftNode = getRaftNode(newInstance, groupId);
        assertNotNull(raftNode);
        assertTrue(getSnapshotEntry(raftNode).index() > 0);
        LockService service = getNodeEngineImpl(newInstance).getService(LockService.SERVICE_NAME);
        LockRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertFalse(registry.getWaitTimeouts().isEmpty());
        LockOwnershipState ownership = registry.getLockOwnershipState(objectName);
        assertTrue(ownership.isLocked());
        assertTrue(ownership.getLockCount() > 0);
        assertEquals(fence, ownership.getFence());
    });
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RestoreSnapshotOp(com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp) ResourceRegistry(com.hazelcast.cp.internal.datastructures.spi.blocking.ResourceRegistry) CPGroupId(com.hazelcast.cp.CPGroupId) HazelcastInstance(com.hazelcast.core.HazelcastInstance) List(java.util.List) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with RestoreSnapshotOp

use of com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp in project hazelcast by hazelcast.

the class CountDownLatchAdvancedTest method testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot.

@Test
public void testNewRaftGroupMemberSchedulesTimeoutsWithSnapshot() throws ExecutionException, InterruptedException {
    latch.trySetCount(1);
    spawn(() -> {
        try {
            latch.await(10, MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    CPGroupId groupId = getGroupId(latch);
    assertTrueEventually(() -> {
        HazelcastInstance leader = getLeaderInstance(instances, groupId);
        CountDownLatchService service = getNodeEngineImpl(leader).getService(CountDownLatchService.SERVICE_NAME);
        ResourceRegistry registry = service.getRegistryOrNull(groupId);
        assertFalse(registry.getWaitTimeouts().isEmpty());
    });
    for (int i = 0; i < LOG_ENTRY_COUNT_TO_SNAPSHOT; i++) {
        latch.trySetCount(1);
    }
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftNodeImpl raftNode = getRaftNode(instance, groupId);
            assertNotNull(raftNode);
            LogEntry snapshotEntry = getSnapshotEntry(raftNode);
            assertTrue(snapshotEntry.index() > 0);
            List<RestoreSnapshotOp> ops = (List<RestoreSnapshotOp>) snapshotEntry.operation();
            for (RestoreSnapshotOp op : ops) {
                if (op.getServiceName().equals(CountDownLatchService.SERVICE_NAME)) {
                    ResourceRegistry registry = (ResourceRegistry) op.getSnapshot();
                    assertFalse(registry.getWaitTimeouts().isEmpty());
                    return;
                }
            }
            fail();
        }
    });
    instances[1].shutdown();
    HazelcastInstance newInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
    newInstance.getCPSubsystem().getCPSubsystemManagementService().promoteToCPMember().toCompletableFuture().get();
    assertTrueEventually(() -> {
        CountDownLatchService service = getNodeEngineImpl(newInstance).getService(CountDownLatchService.SERVICE_NAME);
        CountDownLatchRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertFalse(registry.getWaitTimeouts().isEmpty());
        Assert.assertEquals(1, registry.getRemainingCount(objectName));
    });
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RestoreSnapshotOp(com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp) List(java.util.List) ResourceRegistry(com.hazelcast.cp.internal.datastructures.spi.blocking.ResourceRegistry) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

RestoreSnapshotOp (com.hazelcast.cp.internal.raftop.snapshot.RestoreSnapshotOp)5 List (java.util.List)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 CPGroupId (com.hazelcast.cp.CPGroupId)3 ResourceRegistry (com.hazelcast.cp.internal.datastructures.spi.blocking.ResourceRegistry)3 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)3 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 SnapshotAwareService (com.hazelcast.cp.internal.raft.SnapshotAwareService)1 ILogger (com.hazelcast.logging.ILogger)1 ServiceInfo (com.hazelcast.spi.impl.servicemanager.ServiceInfo)1