use of com.hazelcast.scheduledexecutor.IScheduledFuture 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.IScheduledFuture 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.IScheduledFuture 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 + ").");
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project Payara by payara.
the class ClusterExecutionService method schedule.
/**
* Schedules a Callable object to be run in the future on the specified Member
* @param <V> The type of the result
* @param memberUUIDs The members to schedule the task on
* @param callable The Callable Object
* @param delay The delay before running the task
* @param unit The time unit of the delay
* @return A Future containing the result
*/
public <V extends Serializable> Map<UUID, ScheduledTaskFuture<V>> schedule(Collection<UUID> memberUUIDs, Callable<V> callable, long delay, TimeUnit unit) {
HashMap<UUID, ScheduledTaskFuture<V>> result = new HashMap<>(2);
if (hzCore.isEnabled()) {
Collection<Member> toSubmitTo = selectMembers(memberUUIDs);
IScheduledExecutorService scheduledExecutorService = hzCore.getInstance().getScheduledExecutorService(HazelcastCore.SCHEDULED_CLUSTER_EXECUTOR_SERVICE_NAME);
Map<Member, IScheduledFuture<V>> schedule = scheduledExecutorService.scheduleOnMembers(callable, toSubmitTo, delay, unit);
for (Entry<Member, IScheduledFuture<V>> entry : schedule.entrySet()) {
Member member = entry.getKey();
result.put(member.getUuid(), new ScheduledTaskFuture<>(entry.getValue()));
}
}
return result;
}
use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.
the class ClientScheduledExecutorProxy method scheduleOnMembersAtFixedRate.
@Override
public Map<Member, IScheduledFuture<?>> scheduleOnMembersAtFixedRate(Runnable command, Collection<Member> members, long initialDelay, long period, TimeUnit unit) {
checkNotNull(command, "Command is null");
checkNotNull(members, "Members is null");
checkNotNull(unit, "Unit is null");
String name = extractNameOrGenerateOne(command);
Callable adapter = createScheduledRunnableAdapter(command);
Map<Member, IScheduledFuture<?>> futures = new HashMap<Member, IScheduledFuture<?>>();
for (Member member : members) {
TaskDefinition definition = new TaskDefinition(TaskDefinition.Type.AT_FIXED_RATE, name, adapter, initialDelay, period, unit);
futures.put(member, scheduleOnMember(name, member, definition));
}
return futures;
}
Aggregations