Search in sources :

Example 16 with IScheduledFuture

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());
    }
}
Also used : IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) ArrayList(java.util.ArrayList) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 17 with IScheduledFuture

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 + ").");
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with IScheduledFuture

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 + ").");
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ArrayList(java.util.ArrayList) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 19 with IScheduledFuture

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;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) HashMap(java.util.HashMap) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member)

Example 20 with IScheduledFuture

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;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) TaskDefinition(com.hazelcast.scheduledexecutor.impl.TaskDefinition) HashMap(java.util.HashMap) Member(com.hazelcast.core.Member) Callable(java.util.concurrent.Callable)

Aggregations

IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)48 IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)39 HazelcastInstance (com.hazelcast.core.HazelcastInstance)35 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)34 Test (org.junit.Test)34 QuickTest (com.hazelcast.test.annotation.QuickTest)27 Config (com.hazelcast.config.Config)16 ScheduledExecutorConfig (com.hazelcast.config.ScheduledExecutorConfig)16 Member (com.hazelcast.cluster.Member)14 ICountDownLatch (com.hazelcast.cp.ICountDownLatch)14 HashMap (java.util.HashMap)12 ArrayList (java.util.ArrayList)10 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)7 SlowTest (com.hazelcast.test.annotation.SlowTest)7 Member (com.hazelcast.core.Member)5 TaskDefinition (com.hazelcast.scheduledexecutor.impl.TaskDefinition)5 List (java.util.List)5 Map (java.util.Map)5 Nonnull (javax.annotation.Nonnull)4 LinkedList (java.util.LinkedList)3