use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method scheduleAndGet_withCallable_durableAfterTaskCompletion.
@Test
public void scheduleAndGet_withCallable_durableAfterTaskCompletion() throws Exception {
int delay = 5;
double expectedResult = 25.0;
HazelcastInstance[] instances = createClusterWithCount(2);
String key = generateKeyOwnedBy(instances[1]);
IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
IScheduledFuture<Double> future = executorService.scheduleOnKeyOwner(new PlainCallableTask(), key, delay, SECONDS);
double resultFromOriginalTask = future.get();
instances[1].getLifecycleService().shutdown();
double resultFromMigratedTask = future.get();
assertEquals(expectedResult, resultFromOriginalTask, 0);
assertEquals(expectedResult, resultFromMigratedTask, 0);
assertTrue(future.isDone());
assertFalse(future.isCancelled());
}
use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method scheduleOnMember_whenAutoDisposable_thenGet.
@Test
public void scheduleOnMember_whenAutoDisposable_thenGet() throws Exception {
HazelcastInstance[] instances = createClusterWithCount(2);
Member localMember = instances[0].getCluster().getLocalMember();
IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
IScheduledFuture<Double> future = executorService.scheduleOnMember(autoDisposable(new PlainCallableTask()), localMember, 1, SECONDS);
assertTaskHasBeenDestroyedEventually(future);
}
use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method capacity_onMember_whenPositiveLimit.
@Test
public void capacity_onMember_whenPositiveLimit() {
String schedulerName = ANY_EXECUTOR_NAME;
ScheduledExecutorConfig sec = new ScheduledExecutorConfig().setName(schedulerName).setDurability(1).setPoolSize(1).setCapacity(10);
Config config = new Config().addScheduledExecutorConfig(sec);
HazelcastInstance[] instances = createClusterWithCount(1, config);
IScheduledExecutorService service = instances[0].getScheduledExecutorService(schedulerName);
Member member = instances[0].getCluster().getLocalMember();
List<IScheduledFuture> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
futures.add(service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS));
}
try {
service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS);
fail("Should have been rejected.");
} catch (RejectedExecutionException ex) {
assertEquals("Got wrong RejectedExecutionException", "Maximum capacity (10) of tasks reached for this member and scheduled executor (" + schedulerName + "). " + "Reminder, that tasks must be disposed if not needed.", ex.getMessage());
}
// Dispose all
for (IScheduledFuture future : futures) {
future.dispose();
}
// Re-schedule to verify capacity
for (int i = 0; i < 10; i++) {
service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS);
}
try {
service.scheduleOnMember(new PlainCallableTask(), member, 0, TimeUnit.SECONDS);
fail("Should have been rejected.");
} catch (RejectedExecutionException ex) {
assertEquals("Got wrong RejectedExecutionException", "Maximum capacity (10) of tasks reached for this member and scheduled executor (" + schedulerName + "). " + "Reminder, that tasks must be disposed if not needed.", ex.getMessage());
}
}
use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method capacity_whenPositiveLimit_onMember_andMigration.
@Test
public void capacity_whenPositiveLimit_onMember_andMigration() throws ExecutionException, InterruptedException {
String schedulerName = ANY_EXECUTOR_NAME;
ScheduledExecutorConfig sec = new ScheduledExecutorConfig().setName(schedulerName).setDurability(1).setPoolSize(1).setCapacity(3).setCapacityPolicy(ScheduledExecutorConfig.CapacityPolicy.PER_NODE);
Config config = new Config().addScheduledExecutorConfig(sec);
HazelcastInstance[] instances = createClusterWithCount(2, config);
IScheduledExecutorService serviceA = instances[0].getScheduledExecutorService(schedulerName);
IScheduledExecutorService serviceB = instances[1].getScheduledExecutorService(schedulerName);
// Fill-up both nodes
String key = generateKeyOwnedBy(instances[0]);
IScheduledFuture futureAA = serviceA.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
IScheduledFuture futureAB = serviceA.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
IScheduledFuture futureAC = serviceA.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
futureAA.get();
futureAB.get();
futureAC.get();
key = generateKeyOwnedBy(instances[1]);
IScheduledFuture futureBA = serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
IScheduledFuture futureBB = serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
futureBA.get();
futureBB.get();
// At this point both Node A has 5 tasks each.
// Accounting for both primary copies & replicas
// Node B has still availability for 1 more primary task
// Scheduling on different partition on Node A should be rejected
key = generateKeyOwnedBy(instances[0]);
assertCapacityReached(serviceA, key, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
instances[0].getLifecycleService().shutdown();
waitAllForSafeState(instances[1]);
// At this point Node B has 5 primary tasks.
// No new tasks should be allowed
// Re-fetch future from Node B
futureAA = serviceB.getScheduledFuture(futureAA.getHandler());
futureAB = serviceB.getScheduledFuture(futureAB.getHandler());
futureAC = serviceB.getScheduledFuture(futureAC.getHandler());
assertCapacityReached(serviceB, null, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
// Disposing node's A tasks will be enough to schedule ONE more,
// since Node B was already holding 2 tasks before the migration
futureAA.dispose();
assertCapacityReached(serviceB, null, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
futureAB.dispose();
assertCapacityReached(serviceB, null, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
futureAC.dispose();
// Now we should be able to schedule again ONE more
IScheduledFuture futureBC = serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
assertCapacityReached(serviceB, null, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
futureBA.dispose();
futureBB.dispose();
futureBC.dispose();
// Clean slate - can schedule 3 more
serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
serviceB.scheduleOnKeyOwner(new PlainCallableTask(), key, 0, TimeUnit.SECONDS);
assertCapacityReached(serviceB, null, "Maximum capacity (3) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
}
use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.
the class ScheduledExecutorServiceBasicTest method capacity_whenDefault.
@Test
public void capacity_whenDefault() {
String schedulerName = ANY_EXECUTOR_NAME;
HazelcastInstance[] instances = createClusterWithCount(1, null);
IScheduledExecutorService service = instances[0].getScheduledExecutorService(schedulerName);
List<IScheduledFuture> futures = new ArrayList<>();
for (int i = 0; i < 100; i++) {
futures.add(service.schedule(new PlainCallableTask(), 0, TimeUnit.SECONDS));
}
assertCapacityReached(service, null, "Maximum capacity (100) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
// Dispose all
for (IScheduledFuture future : futures) {
future.dispose();
}
// Re-schedule to verify capacity
for (int i = 0; i < 100; i++) {
service.schedule(new PlainCallableTask(), 0, TimeUnit.SECONDS);
}
assertCapacityReached(service, null, "Maximum capacity (100) of tasks reached " + "for this member and scheduled executor (" + schedulerName + ").");
}
Aggregations