@Override public void onTerminated(Runnable runnable, Executor executor) { delegate.onTerminated(runnable, executor); }
@Override public void onTerminated(Runnable runnable, Executor executor) { delegate.onTerminated(runnable, executor); }
twillController.onTerminated(new Runnable() { @Override public void run() {
/** * Adds a listener to the given TwillController to delete local temp files when the program has started/terminated. * The local temp files could be removed once the program is started, since Twill would keep the files in * HDFS and no long needs the local temp files once program is started. * * @return The same TwillController instance. */ private TwillController addCleanupListener(TwillController controller, final Program program, final File tempDir) { final AtomicBoolean deleted = new AtomicBoolean(false); Runnable cleanup = () -> { if (!deleted.compareAndSet(false, true)) { return; } LOG.debug("Cleanup tmp files for {}: {}", program.getId(), tempDir); deleteDirectory(tempDir); }; controller.onRunning(cleanup, Threads.SAME_THREAD_EXECUTOR); controller.onTerminated(cleanup, Threads.SAME_THREAD_EXECUTOR); return controller; }
twillController.onTerminated(new Runnable() { @Override public void run() {
/** * Adds a listener to the given TwillController to delete local temp files when the program has started/terminated. * The local temp files could be removed once the program is started, since Twill would keep the files in * HDFS and no long needs the local temp files once program is started. * * @return The same TwillController instance. */ private TwillController addCleanupListener(TwillController controller, final Program program, final File tempDir) { final AtomicBoolean deleted = new AtomicBoolean(false); Runnable cleanup = () -> { if (!deleted.compareAndSet(false, true)) { return; } LOG.debug("Cleanup tmp files for {}: {}", program.getId(), tempDir); deleteDirectory(tempDir); }; controller.onRunning(cleanup, Threads.SAME_THREAD_EXECUTOR); controller.onTerminated(cleanup, Threads.SAME_THREAD_EXECUTOR); return controller; }
private void initOnTerminatingThread(final Cluster cluster, final TwillController controller) { if (controller == null || cluster.getRunId() == null) { // nothing we can do here return; } // if termRunnable is not there most likely it was never created (process restarted) // or we try to stop before start - unlikely though // create new one OnTerminatingRunnable onTerminating = new OnTerminatingRunnable(cluster, controller); OnTerminatingRunnable onTerminatingPrev = terminatingThreads.putIfAbsent(cluster.getRunId(), onTerminating); if (onTerminatingPrev == null) { logger.info("Cluster with ID: {} is adding onTerminatingThread for runId: ", cluster.getId(), cluster.getRunId()); controller.onTerminated(onTerminating, Threads.SAME_THREAD_EXECUTOR); } }
controller.onTerminated(new Runnable() { @Override public void run() {
controller.onTerminated(cleanup, Threads.SAME_THREAD_EXECUTOR); return controller; } catch (Exception e) {
@Ignore @Test public void testDistributedShell() throws InterruptedException { TwillRunner twillRunner = getTwillRunner(); TwillController controller = twillRunner.prepare(new DistributedShell("pwd", "ls -al")) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out))) .start(); final CountDownLatch stopLatch = new CountDownLatch(1); controller.onTerminated(new Runnable() { @Override public void run() { stopLatch.countDown(); } }, Threads.SAME_THREAD_EXECUTOR); Assert.assertTrue(stopLatch.await(10, TimeUnit.SECONDS)); } }
}).when(twillController).onTerminated(any(Runnable.class), any(Executor.class));
}).when(twillController).onTerminated(any(Runnable.class), any(Executor.class));
}).when(twillController).onTerminated(any(Runnable.class), any(Executor.class));
@Test public void testFailureComplete() throws TimeoutException, ExecutionException, InterruptedException { TwillRunner twillRunner = getTwillRunner(); // Start the app with an invalid ClassLoader. This will cause the AM fails to start. TwillController controller = twillRunner.prepare(new SleepTask(), ResourceSpecification.Builder.with() .setVirtualCores(1) .setMemory(512, ResourceSpecification.SizeUnit.MEGA) .setInstances(1).build()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .setClassLoader("InvalidClassLoader") .start(); final CountDownLatch terminateLatch = new CountDownLatch(1); controller.onTerminated(new Runnable() { @Override public void run() { terminateLatch.countDown(); } }, Threads.SAME_THREAD_EXECUTOR); Assert.assertTrue(terminateLatch.await(2, TimeUnit.MINUTES)); Assert.assertEquals(ServiceController.TerminationStatus.FAILED, controller.getTerminationStatus()); } }