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());
}
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);
}
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);
}
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());
}
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);
}
Aggregations