Search in sources :

Example 31 with IScheduledExecutorService

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

Example 32 with IScheduledExecutorService

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 33 with IScheduledExecutorService

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());
    }
}
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 34 with IScheduledExecutorService

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 + ").");
}
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 35 with IScheduledExecutorService

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 + ").");
}
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)

Aggregations

IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)94 HazelcastInstance (com.hazelcast.core.HazelcastInstance)83 Test (org.junit.Test)83 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)81 QuickTest (com.hazelcast.test.annotation.QuickTest)70 IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)38 ICountDownLatch (com.hazelcast.cp.ICountDownLatch)29 Config (com.hazelcast.config.Config)21 ScheduledExecutorConfig (com.hazelcast.config.ScheduledExecutorConfig)21 Member (com.hazelcast.cluster.Member)18 SlowTest (com.hazelcast.test.annotation.SlowTest)15 ArrayList (java.util.ArrayList)11 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)10 HashMap (java.util.HashMap)6 List (java.util.List)6 RootCauseMatcher (com.hazelcast.internal.util.RootCauseMatcher)4 ScheduledTaskStatistics (com.hazelcast.scheduledexecutor.ScheduledTaskStatistics)4 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 Address (com.hazelcast.cluster.Address)2 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)2