Search in sources :

Example 1 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ClientScheduledExecutorProxy method getAllScheduledFutures.

@Override
public <V> Map<Member, List<IScheduledFuture<V>>> getAllScheduledFutures() {
    ClientMessage request = ScheduledExecutorGetAllScheduledFuturesCodec.encodeRequest(getName());
    final ClientInvocationFuture future = new ClientInvocation(getClient(), request).invoke();
    ClientMessage response;
    try {
        response = future.get();
    } catch (Exception e) {
        throw rethrow(e);
    }
    Collection<Map.Entry<Member, List<ScheduledTaskHandler>>> urnsPerMember = ScheduledExecutorGetAllScheduledFuturesCodec.decodeResponse(response).handlers;
    Map<Member, List<IScheduledFuture<V>>> tasksMap = new HashMap<Member, List<IScheduledFuture<V>>>();
    for (Map.Entry<Member, List<ScheduledTaskHandler>> entry : urnsPerMember) {
        List<IScheduledFuture<V>> memberTasks = new ArrayList<IScheduledFuture<V>>();
        for (ScheduledTaskHandler scheduledTaskHandler : entry.getValue()) {
            memberTasks.add(new ClientScheduledFutureProxy(scheduledTaskHandler, getContext()));
        }
        tasksMap.put(entry.getKey(), memberTasks);
    }
    return tasksMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Member(com.hazelcast.core.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ClientScheduledExecutorProxy method scheduleOnMembers.

@Override
public <V> Map<Member, IScheduledFuture<V>> scheduleOnMembers(Callable<V> command, Collection<Member> members, long delay, TimeUnit unit) {
    checkNotNull(command, "Command is null");
    checkNotNull(members, "Members is null");
    checkNotNull(unit, "Unit is null");
    String name = extractNameOrGenerateOne(command);
    Map<Member, IScheduledFuture<V>> futures = new HashMap<Member, IScheduledFuture<V>>();
    for (Member member : members) {
        TaskDefinition definition = new TaskDefinition(TaskDefinition.Type.SINGLE_RUN, name, command, delay, unit);
        futures.put(member, (IScheduledFuture<V>) 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)

Example 3 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ClientScheduledExecutorProxy method getAllScheduledFutures.

@Nonnull
@Override
public <V> Map<Member, List<IScheduledFuture<V>>> getAllScheduledFutures() {
    ClientMessage request = ScheduledExecutorGetAllScheduledFuturesCodec.encodeRequest(getName());
    ClientInvocationFuture future = new ClientInvocation(getClient(), request, getName()).invoke();
    ClientMessage response;
    try {
        response = future.get();
    } catch (Exception e) {
        throw rethrow(e);
    }
    Collection<ScheduledTaskHandler> urnsPerMember = ScheduledExecutorGetAllScheduledFuturesCodec.decodeResponse(response);
    Map<Member, List<IScheduledFuture<V>>> tasksMap = new HashMap<>();
    for (ScheduledTaskHandler scheduledTaskHandler : urnsPerMember) {
        UUID memberUuid = scheduledTaskHandler.getUuid();
        Member member = getContext().getClusterService().getMember(memberUuid);
        tasksMap.compute(member, (BiFunctionEx<Member, List<IScheduledFuture<V>>, List<IScheduledFuture<V>>>) (m, iScheduledFutures) -> {
            if (iScheduledFutures == null) {
                iScheduledFutures = new LinkedList<>();
            }
            iScheduledFutures.add(new ClientScheduledFutureProxy(scheduledTaskHandler, getContext()));
            return iScheduledFutures;
        });
    }
    return tasksMap;
}
Also used : ScheduledExecutorShutdownCodec(com.hazelcast.client.impl.protocol.codec.ScheduledExecutorShutdownCodec) AutoDisposableTask(com.hazelcast.scheduledexecutor.AutoDisposableTask) FutureUtil(com.hazelcast.internal.util.FutureUtil) Member(com.hazelcast.cluster.Member) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ExceptionUtil.rethrow(com.hazelcast.internal.util.ExceptionUtil.rethrow) Level(java.util.logging.Level) BiFunctionEx(com.hazelcast.function.BiFunctionEx) Future(java.util.concurrent.Future) ExceptionUtil.sneakyThrow(com.hazelcast.internal.util.ExceptionUtil.sneakyThrow) PartitionAware(com.hazelcast.partition.PartitionAware) ILogger(com.hazelcast.logging.ILogger) ClientContext(com.hazelcast.client.impl.spi.ClientContext) Map(java.util.Map) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) UuidUtil(com.hazelcast.internal.util.UuidUtil) ClientMessageDecoder(com.hazelcast.client.impl.clientside.ClientMessageDecoder) LinkedList(java.util.LinkedList) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) Nonnull(javax.annotation.Nonnull) ScheduledExecutorGetAllScheduledFuturesCodec(com.hazelcast.client.impl.protocol.codec.ScheduledExecutorGetAllScheduledFuturesCodec) Logger(com.hazelcast.logging.Logger) AbstractTaskDecorator(com.hazelcast.scheduledexecutor.impl.AbstractTaskDecorator) Collection(java.util.Collection) FutureUtil.waitWithDeadline(com.hazelcast.internal.util.FutureUtil.waitWithDeadline) Data(com.hazelcast.internal.serialization.Data) TaskDefinition(com.hazelcast.scheduledexecutor.impl.TaskDefinition) UUID(java.util.UUID) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) ScheduledTaskHandlerImpl(com.hazelcast.scheduledexecutor.impl.ScheduledTaskHandlerImpl) ScheduledExecutorSubmitToPartitionCodec(com.hazelcast.client.impl.protocol.codec.ScheduledExecutorSubmitToPartitionCodec) TimeUnit(java.util.concurrent.TimeUnit) NamedTask(com.hazelcast.scheduledexecutor.NamedTask) List(java.util.List) SplitBrainProtectionException(com.hazelcast.splitbrainprotection.SplitBrainProtectionException) ClientDelegatingFuture(com.hazelcast.client.impl.ClientDelegatingFuture) ScheduledRunnableAdapter(com.hazelcast.scheduledexecutor.impl.ScheduledRunnableAdapter) ScheduledExecutorSubmitToMemberCodec(com.hazelcast.client.impl.protocol.codec.ScheduledExecutorSubmitToMemberCodec) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) Collections(java.util.Collections) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HashMap(java.util.HashMap) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) SplitBrainProtectionException(com.hazelcast.splitbrainprotection.SplitBrainProtectionException) LinkedList(java.util.LinkedList) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) Nonnull(javax.annotation.Nonnull)

Example 4 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ScheduledExecutorServiceSlowTest method scheduleRandomPartitions_getAllScheduled_durable.

@Test
public void scheduleRandomPartitions_getAllScheduled_durable() throws Exception {
    ScheduledExecutorConfig scheduledExecutorConfig = new ScheduledExecutorConfig().setName("s").setDurability(2);
    Config config = new Config().setProperty("hazelcast.partition.count", "10").addScheduledExecutorConfig(scheduledExecutorConfig);
    HazelcastInstance[] instances = createClusterWithCount(4, config);
    IScheduledExecutorService s = getScheduledExecutor(instances, "s");
    int expectedTotal = 11;
    IScheduledFuture[] futures = new IScheduledFuture[expectedTotal];
    for (int i = 0; i < expectedTotal; i++) {
        futures[i] = s.schedule(new PlainCallableTask(i), 0, SECONDS);
    }
    instances[1].shutdown();
    assertEquals(expectedTotal, countScheduledTasksOn(s), 0);
    // verify all tasks
    for (int i = 0; i < expectedTotal; i++) {
        assertEquals(25.0 + i, futures[i].get());
    }
}
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) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 5 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ScheduledExecutorServiceSlowTest method stats_longRunningTask_durable.

@Test
public void stats_longRunningTask_durable() throws Exception {
    HazelcastInstance[] instances = createClusterWithCount(4);
    String key = generateKeyOwnedBy(instances[1]);
    ICountDownLatch firstLatch = instances[0].getCPSubsystem().getCountDownLatch("firstLatch");
    firstLatch.trySetCount(2);
    ICountDownLatch lastLatch = instances[0].getCPSubsystem().getCountDownLatch("lastLatch");
    lastLatch.trySetCount(6);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
    IScheduledFuture future = executorService.scheduleOnKeyOwnerAtFixedRate(new ICountdownLatchRunnableTask("firstLatch", "lastLatch"), key, 0, 10, SECONDS);
    firstLatch.await(12, SECONDS);
    instances[1].getLifecycleService().shutdown();
    lastLatch.await(70, SECONDS);
    // wait for run-cycle to finish before cancelling, in order for stats to get updated
    sleepSeconds(4);
    future.cancel(false);
    ScheduledTaskStatistics stats = future.getStats();
    assertEquals(6, stats.getTotalRuns(), 1);
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledTaskStatistics(com.hazelcast.scheduledexecutor.ScheduledTaskStatistics) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) 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