Search in sources :

Example 1 with Cancellable

use of akka.actor.Cancellable in project controller by opendaylight.

the class EntityOwnershipShard method scheduleOwnerSelection.

/**
 * Schedule a new owner selection job. Cancelling any outstanding job if it has not been cancelled.
 */
private void scheduleOwnerSelection(final YangInstanceIdentifier entityPath, final Collection<String> allCandidates, final EntityOwnerSelectionStrategy strategy) {
    cancelOwnerSelectionTask(entityPath);
    LOG.debug("{}: Scheduling owner selection after {} ms", persistenceId(), strategy.getSelectionDelayInMillis());
    final Cancellable lastScheduledTask = context().system().scheduler().scheduleOnce(FiniteDuration.apply(strategy.getSelectionDelayInMillis(), TimeUnit.MILLISECONDS), self(), new SelectOwner(entityPath, allCandidates, strategy), context().system().dispatcher(), self());
    entityToScheduledOwnershipTask.put(entityPath, lastScheduledTask);
}
Also used : Cancellable(akka.actor.Cancellable) SelectOwner(org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner)

Example 2 with Cancellable

use of akka.actor.Cancellable in project flink by apache.

the class ActorSystemScheduledExecutorAdapter method scheduleAtFixedRate.

@Override
@Nonnull
public ScheduledFuture<?> scheduleAtFixedRate(@Nonnull Runnable command, long initialDelay, long period, @Nonnull TimeUnit unit) {
    ScheduledFutureTask<Void> scheduledFutureTask = new ScheduledFutureTask<>(command, triggerTime(unit.toNanos(initialDelay)), unit.toNanos(period));
    Cancellable cancellable = actorSystem.scheduler().schedule(new FiniteDuration(initialDelay, unit), new FiniteDuration(period, unit), ClassLoadingUtils.withContextClassLoader(scheduledFutureTask, flinkClassLoader), actorSystem.dispatcher());
    scheduledFutureTask.setCancellable(cancellable);
    return scheduledFutureTask;
}
Also used : Cancellable(akka.actor.Cancellable) FiniteDuration(scala.concurrent.duration.FiniteDuration) Nonnull(javax.annotation.Nonnull)

Example 3 with Cancellable

use of akka.actor.Cancellable in project SSM by Intel-bigdata.

the class AgentUtils method repeatActionUntil.

public static Cancellable repeatActionUntil(ActorSystem system, FiniteDuration initialDelay, FiniteDuration interval, FiniteDuration timeout, Runnable action, Runnable onTimeout) {
    final Scheduler scheduler = system.scheduler();
    final ExecutionContextExecutor dispatcher = system.dispatcher();
    final Cancellable run = scheduler.schedule(initialDelay, interval, action, dispatcher);
    final Cancellable cancelRun = scheduler.scheduleOnce(timeout, new Runnable() {

        @Override
        public void run() {
            run.cancel();
        }
    }, dispatcher);
    final Cancellable fail = scheduler.scheduleOnce(timeout, onTimeout, dispatcher);
    return new Cancellable() {

        @Override
        public boolean cancel() {
            return run.cancel() && cancelRun.cancel() && fail.cancel();
        }

        @Override
        public boolean isCancelled() {
            return run.isCancelled() && cancelRun.isCancelled() && fail.isCancelled();
        }
    };
}
Also used : Scheduler(akka.actor.Scheduler) Cancellable(akka.actor.Cancellable) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor)

Example 4 with Cancellable

use of akka.actor.Cancellable in project controller by opendaylight.

the class ShardManager method sendResponse.

private void sendResponse(final ShardInformation shardInformation, final boolean doWait, final boolean wantShardReady, final Supplier<Object> messageSupplier) {
    if (!shardInformation.isShardInitialized() || wantShardReady && !shardInformation.isShardReadyWithLeaderId()) {
        if (doWait) {
            final ActorRef sender = getSender();
            final ActorRef self = self();
            Runnable replyRunnable = () -> sender.tell(messageSupplier.get(), self);
            OnShardInitialized onShardInitialized = wantShardReady ? new OnShardReady(replyRunnable) : new OnShardInitialized(replyRunnable);
            shardInformation.addOnShardInitialized(onShardInitialized);
            FiniteDuration timeout = shardInformation.getDatastoreContext().getShardInitializationTimeout().duration();
            if (shardInformation.isShardInitialized()) {
                // If the shard is already initialized then we'll wait enough time for the shard to
                // elect a leader, ie 2 times the election timeout.
                timeout = FiniteDuration.create(shardInformation.getDatastoreContext().getShardRaftConfig().getElectionTimeOutInterval().toMillis() * 2, TimeUnit.MILLISECONDS);
            }
            LOG.debug("{}: Scheduling {} ms timer to wait for shard {}", persistenceId(), timeout.toMillis(), shardInformation.getShardName());
            Cancellable timeoutSchedule = getContext().system().scheduler().scheduleOnce(timeout, getSelf(), new ShardNotInitializedTimeout(shardInformation, onShardInitialized, sender), getContext().dispatcher(), getSelf());
            onShardInitialized.setTimeoutSchedule(timeoutSchedule);
        } else if (!shardInformation.isShardInitialized()) {
            LOG.debug("{}: Returning NotInitializedException for shard {}", persistenceId(), shardInformation.getShardName());
            getSender().tell(createNotInitializedException(shardInformation.getShardId()), getSelf());
        } else {
            LOG.debug("{}: Returning NoShardLeaderException for shard {}", persistenceId(), shardInformation.getShardName());
            getSender().tell(createNoShardLeaderException(shardInformation.getShardId()), getSelf());
        }
        return;
    }
    getSender().tell(messageSupplier.get(), getSelf());
}
Also used : ActorRef(akka.actor.ActorRef) Cancellable(akka.actor.Cancellable) FiniteDuration(scala.concurrent.duration.FiniteDuration)

Example 5 with Cancellable

use of akka.actor.Cancellable in project controller by opendaylight.

the class EntityOwnershipShard method onSelectOwner.

private void onSelectOwner(final SelectOwner selectOwner) {
    LOG.debug("{}: onSelectOwner: {}", persistenceId(), selectOwner);
    String currentOwner = getCurrentOwner(selectOwner.getEntityPath());
    if (Strings.isNullOrEmpty(currentOwner)) {
        writeNewOwner(selectOwner.getEntityPath(), newOwner(currentOwner, selectOwner.getAllCandidates(), selectOwner.getOwnerSelectionStrategy()));
        Cancellable cancellable = entityToScheduledOwnershipTask.get(selectOwner.getEntityPath());
        if (cancellable != null) {
            if (!cancellable.isCancelled()) {
                cancellable.cancel();
            }
            entityToScheduledOwnershipTask.remove(selectOwner.getEntityPath());
        }
    }
}
Also used : Cancellable(akka.actor.Cancellable)

Aggregations

Cancellable (akka.actor.Cancellable)8 Nonnull (javax.annotation.Nonnull)4 FiniteDuration (scala.concurrent.duration.FiniteDuration)2 ActorRef (akka.actor.ActorRef)1 Scheduler (akka.actor.Scheduler)1 SelectOwner (org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner)1 ExecutionContextExecutor (scala.concurrent.ExecutionContextExecutor)1