Search in sources :

Example 1 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class CountDownLatchLongAwaitTest method testLongAwait.

private void testLongAwait(HazelcastInstance instance) throws ExecutionException, InterruptedException {
    ICountDownLatch latch = instance.getCPSubsystem().getCountDownLatch(proxyName);
    latch.trySetCount(1);
    Future<Boolean> f = spawn(() -> latch.await(5, TimeUnit.MINUTES));
    assertTrueEventually(() -> {
        CountDownLatchService service = getNodeEngineImpl(instance).getService(CountDownLatchService.SERVICE_NAME);
        assertFalse(service.getLiveOperations(groupId).isEmpty());
    });
    assertTrueAllTheTime(() -> {
        CountDownLatchService service = getNodeEngineImpl(instance).getService(CountDownLatchService.SERVICE_NAME);
        assertFalse(service.getLiveOperations(groupId).isEmpty());
    }, callTimeoutSeconds + 5);
    latch.countDown();
    assertCompletesEventually(f);
    assertTrue(f.get());
}
Also used : ICountDownLatch(com.hazelcast.cp.ICountDownLatch)

Example 2 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class EntryProcessorOffloadableTest method testHeartBeatsComingWhenEntryProcessorOffloaded.

/**
 * <pre>
 * Given: Operation heartbeats are sent four times per {@link ClusterProperty#OPERATION_CALL_TIMEOUT_MILLIS}
 *        (see {@link InvocationMonitor#getHeartbeatBroadcastPeriodMillis()})
 * When: An offloaded EntryProcessor takes a long time to run.
 * Then: Heartbeats are still coming while the task is offloaded.
 * </pre>
 */
@Test
@Category(SlowTest.class)
public void testHeartBeatsComingWhenEntryProcessorOffloaded() {
    /* Shut down the cluster since we want to use a different
         * OPERATION_CALL_TIMEOUT_MILLIS value in this test. */
    shutdownNodeFactory();
    int heartbeatsIntervalSec = 15;
    Config config = getConfig();
    config.getProperties().setProperty(ClusterProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), String.valueOf(heartbeatsIntervalSec * 4 * 1000));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    instances = factory.newInstances(config);
    final String key = generateKeyOwnedBy(instances[1]);
    String offloadableStartTimeRefName = "offloadableStartTimeRefName";
    String exitLatchName = "exitLatchName";
    IAtomicLong offloadableStartedTimeRef = instances[0].getCPSubsystem().getAtomicLong(offloadableStartTimeRefName);
    ICountDownLatch exitLatch = instances[0].getCPSubsystem().getCountDownLatch(exitLatchName);
    exitLatch.trySetCount(1);
    TimestampedSimpleValue givenValue = new TimestampedSimpleValue(1);
    final IMap<String, TimestampedSimpleValue> map = instances[0].getMap(MAP_NAME);
    map.put(key, givenValue);
    final Address instance1Address = instances[1].getCluster().getLocalMember().getAddress();
    final List<Long> heartBeatTimestamps = new LinkedList<>();
    Thread hbMonitorThread = new Thread(() -> {
        NodeEngine nodeEngine = getNodeEngineImpl(instances[0]);
        OperationServiceImpl osImpl = (OperationServiceImpl) nodeEngine.getOperationService();
        Map<Address, AtomicLong> heartBeats = osImpl.getInvocationMonitor().getHeartbeatPerMember();
        long lastbeat = Long.MIN_VALUE;
        while (!Thread.currentThread().isInterrupted()) {
            AtomicLong timestamp = heartBeats.get(instance1Address);
            if (timestamp != null) {
                long newlastbeat = timestamp.get();
                if (lastbeat != newlastbeat) {
                    lastbeat = newlastbeat;
                    long offloadableStartTime = offloadableStartedTimeRef.get();
                    if (offloadableStartTime != 0 && offloadableStartTime < newlastbeat) {
                        heartBeatTimestamps.add(newlastbeat);
                        exitLatch.countDown();
                    }
                }
            }
            HazelcastTestSupport.sleepMillis(100);
        }
    });
    final int secondsToRun = 55;
    try {
        hbMonitorThread.start();
        map.executeOnKey(key, new TimeConsumingOffloadableTask(secondsToRun, offloadableStartTimeRefName, exitLatchName));
    } finally {
        hbMonitorThread.interrupt();
    }
    int heartBeatCount = 0;
    TimestampedSimpleValue updatedValue = map.get(key);
    for (long heartBeatTimestamp : heartBeatTimestamps) {
        if (heartBeatTimestamp > updatedValue.processStart && heartBeatTimestamp < updatedValue.processEnd) {
            heartBeatCount++;
        }
    }
    assertTrue("Heartbeats should be received while offloadable entry processor is running. " + "Observed: " + heartBeatTimestamps + " EP start: " + updatedValue.processStart + " end: " + updatedValue.processEnd, heartBeatCount > 0);
}
Also used : Address(com.hazelcast.cluster.Address) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) LinkedList(java.util.LinkedList) NodeEngine(com.hazelcast.spi.impl.NodeEngine) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IAtomicLong(com.hazelcast.cp.IAtomicLong) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 3 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceSlowTest method stats_longRunningTask_durable.

@Test
public void stats_longRunningTask_durable() throws Exception {
    HazelcastInstance[] instances = createClusterWithCount(4);
    String key = generateKeyOwnedBy(instances[1]);
    ICountDownLatch firstLatch = instances[0].getCPSubsystem().getCountDownLatch("firstLatch");
    firstLatch.trySetCount(2);
    ICountDownLatch lastLatch = instances[0].getCPSubsystem().getCountDownLatch("lastLatch");
    lastLatch.trySetCount(6);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
    IScheduledFuture future = executorService.scheduleOnKeyOwnerAtFixedRate(new ICountdownLatchRunnableTask("firstLatch", "lastLatch"), key, 0, 10, SECONDS);
    firstLatch.await(12, SECONDS);
    instances[1].getLifecycleService().shutdown();
    lastLatch.await(70, SECONDS);
    // wait for run-cycle to finish before cancelling, in order for stats to get updated
    sleepSeconds(4);
    future.cancel(false);
    ScheduledTaskStatistics stats = future.getStats();
    assertEquals(6, stats.getTotalRuns(), 1);
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledTaskStatistics(com.hazelcast.scheduledexecutor.ScheduledTaskStatistics) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 4 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceSlowTest method schedule_withStatefulRunnable_durable.

@Test
public void schedule_withStatefulRunnable_durable() throws Exception {
    HazelcastInstance[] instances = createClusterWithCount(4);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
    int waitStateSyncPeriodToAvoidPassiveState = 2000;
    String key = generateKeyOwnedBy(instances[1]);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    IAtomicLong runC = instances[0].getCPSubsystem().getAtomicLong("runC");
    IAtomicLong loadC = instances[0].getCPSubsystem().getAtomicLong("loadC");
    latch.trySetCount(1);
    IScheduledFuture future = executorService.scheduleOnKeyOwnerAtFixedRate(new StatefulRunnableTask("latch", "runC", "loadC"), key, 10, 10, SECONDS);
    // wait for task to get scheduled and start
    latch.await(11, SECONDS);
    Thread.sleep(waitStateSyncPeriodToAvoidPassiveState);
    instances[1].getLifecycleService().shutdown();
    // reset latch - task should be running on a replica now
    latch.trySetCount(7);
    latch.await(70, SECONDS);
    future.cancel(false);
    assertEquals(getPartitionService(instances[0]).getPartitionId(key), future.getHandler().getPartitionId());
    assertEquals(8, runC.get(), 1);
    assertEquals(1, loadC.get());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) IAtomicLong(com.hazelcast.cp.IAtomicLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 5 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method schedule_withStatefulRunnable.

@Test
public void schedule_withStatefulRunnable() {
    HazelcastInstance[] instances = createClusterWithCount(4);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    executorService.schedule(new StatefulRunnableTask("latch", "runC", "loadC"), 2, SECONDS);
    assertOpenEventually(latch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ICountDownLatch (com.hazelcast.cp.ICountDownLatch)32 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)31 Test (org.junit.Test)31 HazelcastInstance (com.hazelcast.core.HazelcastInstance)30 IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)27 QuickTest (com.hazelcast.test.annotation.QuickTest)21 IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)14 SlowTest (com.hazelcast.test.annotation.SlowTest)11 IAtomicLong (com.hazelcast.cp.IAtomicLong)3 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)3 Member (com.hazelcast.cluster.Member)2 RootCauseMatcher (com.hazelcast.internal.util.RootCauseMatcher)2 ScheduledTaskStatistics (com.hazelcast.scheduledexecutor.ScheduledTaskStatistics)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 Address (com.hazelcast.cluster.Address)1 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)1 Config (com.hazelcast.config.Config)1 MapConfig (com.hazelcast.config.MapConfig)1 CPSubsystem (com.hazelcast.cp.CPSubsystem)1 CountDownLatchService (com.hazelcast.cp.internal.datastructures.countdownlatch.CountDownLatchService)1