Search in sources :

Example 41 with IScheduledFuture

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

Example 42 with IScheduledFuture

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

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

Example 44 with IScheduledFuture

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());
            }
        }
    });
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) InternalPartitionServiceImpl(com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl) IPartitionLostEvent(com.hazelcast.internal.partition.IPartitionLostEvent) PartitionLostEventImpl(com.hazelcast.internal.partition.PartitionLostEventImpl)

Example 45 with IScheduledFuture

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);
}
Also used : IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Collection(java.util.Collection) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

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