/** * Tries to shut down the given actor gracefully. * * @param actorRef specifying the actor to shut down * @param timeout for the graceful shut down * @return A future that finishes with {@code true} iff. the actor could be stopped gracefully * or {@code actorRef} was {@code null}. */ private CompletableFuture<Boolean> stopActor(@Nullable final ActorRef actorRef, FiniteDuration timeout) { if (actorRef == null) { return CompletableFuture.completedFuture(true); } return FutureUtils.toJava(Patterns.gracefulStop(actorRef, timeout)) .exceptionally( (Throwable throwable) -> { // The actor did not stop gracefully in time, try to directly stop it actorSystem.stop(actorRef); log.warn("Could not stop actor {} gracefully.", actorRef.path(), throwable); return false; } ); }
@Override protected void shutdownApplication(ApplicationStatus finalStatus, String optionalDiagnostics) { // first, de-register from YARN FinalApplicationStatus yarnStatus = getYarnStatus(finalStatus); LOG.info("Unregistering application from the YARN Resource Manager"); try { resourceManagerClient.unregisterApplicationMaster(yarnStatus, optionalDiagnostics, ""); } catch (Throwable t) { LOG.error("Could not unregister the application master.", t); } // now shut down all our components try { resourceManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Asynchronous Resource Manager Client", t); } try { nodeManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Node Manager Client", t); } // stop the actor after finishing processing the stop message getContext().system().stop(getSelf()); }
public void stopNode(String actorName) { ActorRef actorRef = actorRefs.get(actorName); for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) { if (entry.getKey().endsWith(actorName)) { actorSystem.stop(entry.getValue()); } } actorSystem.stop(actorRef); actorRefs.remove(actorName); allPeers.remove(actorName); }
public void stopNode(String actorName) { ActorRef actorRef = actorRefs.get(actorName); for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) { if (entry.getKey().endsWith(actorName)) { actorSystem.stop(entry.getValue()); } } actorSystem.stop(actorRef); actorRefs.remove(actorName); allPeers.remove(actorName); }
ActorConfig.createAndGetActorSystem().stop(executionManager);
@Override public void dispose() { HashSet<WorkerRunnable> set; synchronized (this) { set = tasks; tasks = null; } if (set != null) { for (WorkerRunnable wr : set) { wr.delete(); } } if (ctx != null) { ctx.stop(actor); } }
public void disconnect() { dataBus.disconnect(); workerActorSystem.stop(worker); workerActorSystem.shutdown(); dataBus = null; }
@Override protected void shutdownApplication(ApplicationStatus finalStatus, String optionalDiagnostics) { // first, de-register from YARN FinalApplicationStatus yarnStatus = getYarnStatus(finalStatus); LOG.info("Unregistering application from the YARN Resource Manager"); try { resourceManagerClient.unregisterApplicationMaster(yarnStatus, optionalDiagnostics, ""); } catch (Throwable t) { LOG.error("Could not unregister the application master.", t); } // now shut down all our components try { resourceManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Asynchronous Resource Manager Client", t); } try { nodeManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Node Manager Client", t); } // stop the actor after finishing processing the stop message getContext().system().stop(getSelf()); }
@Override protected void shutdownApplication(ApplicationStatus finalStatus, String optionalDiagnostics) { // first, de-register from YARN FinalApplicationStatus yarnStatus = getYarnStatus(finalStatus); LOG.info("Unregistering application from the YARN Resource Manager"); try { resourceManagerClient.unregisterApplicationMaster(yarnStatus, optionalDiagnostics, ""); } catch (Throwable t) { LOG.error("Could not unregister the application master.", t); } // now shut down all our components try { resourceManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Asynchronous Resource Manager Client", t); } try { nodeManagerClient.stop(); } catch (Throwable t) { LOG.error("Could not cleanly shut down the Node Manager Client", t); } // stop the actor after finishing processing the stop message getContext().system().stop(getSelf()); }
public class LightswitchTest { @Test public void testLightswitch() { ActorSystem system = ActorSystem.create("lightswitchtest");//should make this static if you're going to test a lot of things, actor systems are a bit expensive new JavaTestKit(system) {{ //there's that static initializer again ActorRef lightswitch = system.actorOf(Props.create(Lightswitch.class)); //here is our lightswitch. It's an actor ref, a reference to an actor that will be created on //our behalf of type Lightswitch. We can't, as mentioned earlier, actually touch the instance //of Lightswitch, but we can send messages to it via this reference. lightswitch.tell( //using the reference to our actor, tell it new PowerOn(), //to "Power On," using our message type getRef()); //and giving it an actor to call back (in this case, the JavaTestKit itself) //because it is asynchronous, the tell will return immediately. Somewhere off in the distance, on another thread, our lightbulb is receiving its message expectMsgEquals(LightswitchState.on); //we block until the lightbulb sends us back a message with its current state ("on.") //If our actor is broken, this call will timeout and fail. lightswitch.tell(new PowerOff(), getRef()); expectMsgEquals(LightswitchState.off); system.stop(lightswitch); //switch works, kill the instance, leave the system up for further use }}; } }
@After public void teardown() { actorSystem.stop(mockActor.ref()); }
@After public void teardown() { actorSystem.stop(mockActor.ref()); }
@Test public void testCommandStashing() { system.stop(mockedActor); mockedActor = system.actorOf(MockedActor.props(id, initialBehavior)); final InternalCommand<BackendInfo> cmd = mock(InternalCommand.class); when(cmd.execute(any())).thenReturn(initialBehavior); //send messages before recovery is completed mockedActor.tell(cmd, ActorRef.noSender()); mockedActor.tell(cmd, ActorRef.noSender()); mockedActor.tell(cmd, ActorRef.noSender()); //complete recovery handleRecovery(null); verify(cmd, timeout(1000).times(3)).execute(initialBehavior); }
@Test public void testRecoveryAfterRestartSaveSnapshotFail() { system.stop(mockedActor); mockedActor = system.actorOf(MockedActor.props(id, initialBehavior)); probe.watch(mockedActor); probe.expectMsgClass(MockedSnapshotStore.LoadRequest.class); probe.reply(Optional.empty()); probe.expectMsgClass(MockedSnapshotStore.SaveRequest.class); probe.reply(new RuntimeException("save failed")); probe.expectMsgClass(MockedSnapshotStore.DeleteByMetadataRequest.class); probe.expectTerminated(mockedActor, TIMEOUT); }
@Test public void testRecoveryAfterRestartDeleteSnapshotsFail() { system.stop(mockedActor); mockedActor = system.actorOf(MockedActor.props(id, initialBehavior)); probe.watch(mockedActor); probe.expectMsgClass(MockedSnapshotStore.LoadRequest.class); probe.reply(Optional.empty()); probe.expectMsgClass(MockedSnapshotStore.SaveRequest.class); probe.reply(Void.TYPE); probe.expectMsgClass(MockedSnapshotStore.DeleteByCriteriaRequest.class); probe.reply(new RuntimeException("delete failed")); //actor shouldn't terminate probe.expectNoMessage(); }
ActorConfig.createAndGetActorSystem().stop(executionManager);
@Test public void testRecoveryAfterRestart() { system.stop(mockedActor); mockedActor = system.actorOf(MockedActor.props(id, initialBehavior)); final MockedSnapshotStore.SaveRequest newSaveRequest = handleRecovery(new SelectedSnapshot(saveRequest.getMetadata(), saveRequest.getSnapshot())); Assert.assertEquals(MEMBER_1_FRONTEND_TYPE_1, newSaveRequest.getMetadata().persistenceId()); }
@Test public void testRecoveryAfterRestartFrontendIdMismatch() { system.stop(mockedActor); //start actor again mockedActor = system.actorOf(MockedActor.props(id, initialBehavior)); probe.expectMsgClass(MockedSnapshotStore.LoadRequest.class); //offer snapshot with incorrect client id final SnapshotMetadata metadata = saveRequest.getMetadata(); final FrontendIdentifier anotherFrontend = FrontendIdentifier.create(MemberName.forName("another"), FrontendType.forName("type-2")); final ClientIdentifier incorrectClientId = ClientIdentifier.create(anotherFrontend, 0); probe.watch(mockedActor); probe.reply(Optional.of(new SelectedSnapshot(metadata, incorrectClientId))); //actor should be stopped probe.expectTerminated(mockedActor, TIMEOUT); }