use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method schedule_partitionAware_runnable.
@Test
public void schedule_partitionAware_runnable() {
int delay = 1;
String completionLatchName = "completionLatch";
HazelcastInstance[] instances = createClusterWithCount(2);
ICountDownLatch completionLatch = instances[0].getCPSubsystem().getCountDownLatch(completionLatchName);
completionLatch.trySetCount(1);
IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
Runnable task = new PlainPartitionAwareRunnableTask(completionLatchName);
IScheduledFuture first = executorService.schedule(task, delay, SECONDS);
assertOpenEventually(completionLatch);
ScheduledTaskHandler handler = first.getHandler();
int expectedPartition = getPartitionIdFromPartitionAwareTask(instances[0], (PartitionAware) task);
assertEquals(expectedPartition, handler.getPartitionId());
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method capacity_whenPositiveLimit_afterDisposing_andReplicaPartitionPromotion.
/**
* Make sure disposal of SUSPENDED tasks doesn't release permits
*/
@Test
public void capacity_whenPositiveLimit_afterDisposing_andReplicaPartitionPromotion() throws ExecutionException, InterruptedException {
String schedulerName = ANY_EXECUTOR_NAME;
ScheduledExecutorConfig sec = new ScheduledExecutorConfig().setName(schedulerName).setDurability(1).setPoolSize(1).setCapacity(1).setCapacityPolicy(ScheduledExecutorConfig.CapacityPolicy.PER_PARTITION);
Config config = new Config().addScheduledExecutorConfig(sec);
HazelcastInstance[] instances = createClusterWithCount(2, config);
IScheduledExecutorService service = instances[0].getScheduledExecutorService(schedulerName);
String key = generateKeyOwnedBy(instances[0]);
int keyOwner = getNodeEngineImpl(instances[0]).getPartitionService().getPartitionId(key);
IScheduledFuture future = service.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
future.get();
assertCapacityReached(service, key, "Maximum capacity (1) of tasks reached " + "for partition (" + keyOwner + ") and scheduled executor (" + schedulerName + ").");
instances[0].getLifecycleService().shutdown();
waitAllForSafeState(instances[1]);
// Re-assign service & future
service = instances[1].getScheduledExecutorService(schedulerName);
future = service.getScheduledFuture(future.getHandler());
assertCapacityReached(service, key, "Maximum capacity (1) of tasks reached " + "for partition (" + keyOwner + ") and scheduled executor (" + schedulerName + ").");
future.dispose();
service.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
assertCapacityReached(service, key, "Maximum capacity (1) of tasks reached " + "for partition (" + keyOwner + ") and scheduled executor (" + schedulerName + ").");
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method schedule_andCancel_onMember.
@Test
public void schedule_andCancel_onMember() {
HazelcastInstance[] instances = createClusterWithCount(2);
Member localMember = instances[0].getCluster().getLocalMember();
ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
latch.trySetCount(1);
IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
IScheduledFuture future = executorService.scheduleOnMemberAtFixedRate(new ICountdownLatchRunnableTask("latch"), localMember, 1, 1, SECONDS);
sleepSeconds(5);
assertFalse(future.isCancelled());
assertFalse(future.isDone());
future.cancel(false);
assertTrue(future.isCancelled());
assertTrue(future.isDone());
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method schedule_testPartitionLostEvent.
public void schedule_testPartitionLostEvent(int replicaLostCount) {
int delay = 1;
HazelcastInstance[] instances = createClusterWithCount(1);
IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
final IScheduledFuture future = executorService.schedule(new PlainCallableTask(), delay, SECONDS);
// Used to make sure both futures (on the same handler) get the event.
// Catching possible equal/hashcode issues in the Map
final IScheduledFuture futureCopyInstance = (IScheduledFuture) ((List) executorService.getAllScheduledFutures().values().toArray()[0]).get(0);
ScheduledTaskHandler handler = future.getHandler();
int partitionOwner = handler.getPartitionId();
IPartitionLostEvent internalEvent = new PartitionLostEventImpl(partitionOwner, replicaLostCount, null);
((InternalPartitionServiceImpl) getNodeEngineImpl(instances[0]).getPartitionService()).onPartitionLost(internalEvent);
assertTrueEventually(() -> {
try {
future.get();
fail();
} catch (IllegalStateException ex) {
try {
futureCopyInstance.get();
fail();
} catch (IllegalStateException ex2) {
assertEquals(format("Partition %d, holding this scheduled task was lost along with all backups.", future.getHandler().getPartitionId()), ex.getMessage());
assertEquals(format("Partition %d, holding this scheduled task was lost along with all backups.", future.getHandler().getPartitionId()), ex2.getMessage());
}
}
});
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ScheduledExecutorServiceSlowTest method schedule_thenDisposeLeakTest.
@Test(timeout = 1800000)
public void schedule_thenDisposeLeakTest() {
Config config = new Config().addScheduledExecutorConfig(new ScheduledExecutorConfig().setName("s").setCapacity(10000));
HazelcastInstance[] instances = createClusterWithCount(2, config);
final IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
final AtomicBoolean running = new AtomicBoolean(true);
long counter = 0;
long limit = 2000000;
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
while (running.get()) {
for (Collection<IScheduledFuture<Object>> collection : executorService.getAllScheduledFutures().values()) {
for (IScheduledFuture future : collection) {
if (future.getStats().getTotalRuns() >= 1) {
future.dispose();
}
}
}
}
}
});
while (running.get()) {
try {
executorService.schedule(new PlainCallableTask(), 1, SECONDS);
Thread.yield();
if (counter++ % 1000 == 0) {
System.out.println("Tasks: " + counter);
}
if (counter >= limit) {
running.set(false);
}
} catch (Exception ex) {
ex.printStackTrace();
running.set(false);
}
}
// wait for running tasks to finish, keeping log clean of PassiveMode exceptions
sleepSeconds(5);
}
Aggregations