private Task<Boolean> checkAlive(final String handle, final SimpleStreamProxy subscriber) { final ActorRuntime runtime = ActorRuntime.getRuntime(); final NodeAddress r = await(runtime.locateActor((Addressable) subscriber, false)); if (r == null) { state().subscribers.remove(handle); return Task.fromValue(Boolean.FALSE); } return Task.fromValue(Boolean.TRUE); }
private void registerLocalTimer(final ReminderEntry reminderEntry) { // adjusting start date. long dueTime = reminderEntry.getStartAt().getTime() - ActorRuntime.getRuntime().clock().millis(); if (dueTime < 0) { dueTime = reminderEntry.getPeriod() + (dueTime % reminderEntry.getPeriod()); } final Registration localData = registerTimer(() -> callRemainder(reminderEntry), dueTime, reminderEntry.getPeriod(), TimeUnit.MILLISECONDS); local.put(reminderEntry, localData); }
protected Task<Void> deactivate(final T actor) { await(Task.allOf(runtime.getAllExtensions(LifetimeExtension.class).stream().map(v -> v.preDeactivation(actor)))); try { await(actor.deactivateAsync()); } catch (final Throwable ex) { getLogger().error("Error on actor " + reference + " deactivation", ex); } clearTimers(); await(clearStreamSubscriptions()); await(Task.allOf(runtime.getAllExtensions(LifetimeExtension.class).stream().map(v -> v.postDeactivation(actor)))); return Task.done(); }
@SuppressWarnings("unchecked") public static <T> T cast(Class<T> remoteInterface, Actor actor) { return (T) Proxy.newProxyInstance(DefaultDescriptorFactory.class.getClassLoader(), new Class[]{ remoteInterface }, (proxy, method, args) -> { // TODO: throw proper exceptions for the expected error scenarios (non task return), final int methodId = instance.dynamicReferenceFactory.getMethodId(method); return ActorRuntime.getRuntime() .invoke(RemoteReference.from(actor), method, method.isAnnotationPresent(OneWay.class), methodId, args); }); } }
public static Task<Void> flushAll(Actor actor) { final List<NodeAddress> nodes = ActorRuntime.getRuntime().getAllNodes(); return Task.allOf(nodes.stream() .map(nodeAddress -> DefaultDescriptorFactory.observerRef(nodeAddress, ExecutionCacheFlushObserver.class, "").flush(actor))); } }
protected Task<T> activate() lastAccess = runtime.clock().millis(); if (key == reference) if (!Objects.equals(runtime.getLocalAddress(), await(runtime.locateActor(reference, true)))) final Object newInstance = runtime.getFirstExtension(ActorConstructionExtension.class).newInstance(concreteClass); if (!AbstractActor.class.isInstance(newInstance)) actor.activation = this; await(Task.allOf(runtime.getAllExtensions(LifetimeExtension.class).stream().map(v -> v.preActivation(actor)))); await(Task.allOf(runtime.getAllExtensions(LifetimeExtension.class).stream().map(v -> v.postActivation(actor)))); return Task.fromValue((T) actor);
@Override public Object copy(Kryo kryo, Object original) { if (original instanceof RemoteReference) { final RemoteReference<?> remoteReference = (RemoteReference<?>) original; if (RemoteReference.getRuntime(remoteReference) != null) { return DefaultDescriptorFactory.get().getReference(null, RemoteReference.getAddress(remoteReference), RemoteReference.getInterfaceClass(remoteReference), RemoteReference.getId(remoteReference)); } return original; } return ActorRuntime.getRuntime().registerObserver(null, (ActorObserver) original); }
if (Objects.equals(message.getToNode(), runtime.getLocalAddress())) runtime.bind(); try
@SuppressWarnings("unchecked") default <T extends ActorExtension> T getFirstExtension(Class<T> itemType) { final List<ActorExtension> extensions = getExtensions(); if (extensions == null || extensions.isEmpty()) { return null; } for (ActorExtension extension : extensions) { if (itemType.isInstance(extension)) { return (T) extension; } } return null; }
public void setRuntime(final ActorRuntime runtime) { this.runtime = runtime; this.lastAccess = runtime.clock().millis(); }
private <R> Task<R> doRun(final TaskFunction<LocalObjects.LocalObjectEntry<T>, R> function) { runtime.bind(); final ActorTaskContext actorTaskContext = ActorTaskContext.pushNew(); try { // using await makes the actorTaskContext.pop() run in the wrong thread. // the the internal par is separated return doRunInternal(function, actorTaskContext); } finally { actorTaskContext.pop(); } }
@SuppressWarnings("unchecked") public Task<Void> unsubscribe(String handle) { StreamSubscription subscription = observerMap.remove(handle); // no more messages if (subscription != null && subscription.valid) { subscription.valid = false; await(subscription.handle); await(runtime.getStream(subscription.provider, subscription.dataClass, subscription.streamId) .unsubscribe(subscription.handle.join())); observerMap.remove(handle); } return Task.done(); }
/** * Requests the global actor count across the cluster. * * @return A task indicating containing the total actor count across the cluster. */ static Task<Long> getClusterActorCount() { final List<Task<Long>> countList = ActorRuntime.getRuntime().getAllNodes().stream() .map(address -> { final RuntimeActions runtimeActions = DefaultDescriptorFactory.observerRef(address, RuntimeActions.class, ""); return runtimeActions.getActorCount(); }) .collect(Collectors.toList()); await(Task.allOf(countList)); final Long actorCount = countList.stream() .mapToLong(Task<Long>::join) .sum(); return Task.fromValue(actorCount); }
@SuppressWarnings("unchecked") public static <T> T cast(Class<T> remoteInterface, Actor actor) { return (T) Proxy.newProxyInstance(DefaultDescriptorFactory.class.getClassLoader(), new Class[]{ remoteInterface }, (proxy, method, args) -> { // TODO: throw proper exceptions for the expected error scenarios (non task return), final int methodId = instance.dynamicReferenceFactory.getMethodId(method); return ActorRuntime.getRuntime() .invoke(RemoteReference.from(actor), method, method.isAnnotationPresent(OneWay.class), methodId, args); }); } }
@SuppressWarnings("unchecked") default <T extends ActorExtension> List<T> getAllExtensions(Class<T> itemType) { final List<ActorExtension> extensions = getExtensions(); if (extensions == null || extensions.isEmpty()) { return Collections.emptyList(); } List<T> result = new ArrayList<>(); for (ActorExtension extension : extensions) { if (itemType.isInstance(extension)) { result.add((T) extension); } } return result; }
@Override public void updateLastAccessTime() { lastAccess = runtime.clock().millis(); }
private <R> Task<R> doRunInternal(final TaskFunction<LocalObjects.LocalObjectEntry<T>, R> function, final ActorTaskContext actorTaskContext) { if (actor == null && !isDeactivated()) { this.actor = await(activate()); runtime.bind(); } actorTaskContext.setActor(this.getObject()); return function.apply(this); }
@SuppressWarnings("unchecked") public Task<String> subscribe(String provider, Class dataClass, String streamId) { String handle = IdUtils.urlSafeString(128); StreamSubscription subscription = new StreamSubscription(); subscription.provider = provider; subscription.dataClass = dataClass; subscription.streamId = streamId; subscription.handle = runtime.getStream(provider, dataClass, streamId).subscribe(new AsyncObserver() { @Override public Task<Void> onNext(final Object data, final StreamSequenceToken sequenceToken) { if (subscription.valid && peer.getPipeline().isActive()) { return peer.getPipeline().write(new Invocation()); } return Task.done(); } }, null); observerMap.put(handle, subscription); await(subscription.handle); return Task.fromValue(handle); }
/** * Requests the deactivation of an actor * * @param actor the actor which you want to deactivate. * @return A task indicating the state of the request. Immediately resolved if actor is not activated. */ static Task deactivate(final Actor actor) { final NodeAddress address = await(ActorRuntime.getRuntime().locateActor(RemoteReference.from(actor), false)); if(address != null) { final RuntimeActions runtimeActions = DefaultDescriptorFactory.observerRef(address, RuntimeActions.class, ""); return runtimeActions.deactivateActor(actor); } return Task.done(); }
public static Task<Void> flushAll(Actor actor) { final List<NodeAddress> nodes = ActorRuntime.getRuntime().getAllNodes(); return Task.allOf(nodes.stream() .map(nodeAddress -> DefaultDescriptorFactory.observerRef(nodeAddress, ExecutionCacheFlushObserver.class, "").flush(actor))); } }