@Test public void testCallbackWhenHeartbeatDead() throws InterruptedException { ContainerHeartbeatClient mockClient = mock(ContainerHeartbeatClient.class); CountDownLatch countDownLatch = new CountDownLatch(1); Runnable onExpired = () -> { countDownLatch.countDown(); }; ContainerHeartbeatMonitor monitor = new ContainerHeartbeatMonitor(onExpired, mockClient); ContainerHeartbeatResponse response = new ContainerHeartbeatResponse(false); when(mockClient.requestHeartbeat()).thenReturn(response); monitor.start(); boolean success = countDownLatch.await(2, TimeUnit.SECONDS); Assert.assertTrue(success); }
/** * Creates a new container heartbeat monitor if possible. * @param container the container to monitor * @return a new {@link ContainerHeartbeatMonitor} instance, or null if could not create one */ private static ContainerHeartbeatMonitor createContainerHeartbeatMonitor(SamzaContainer container) { String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL()); String executionEnvContainerId = System.getenv(ShellCommandConfig.ENV_EXECUTION_ENV_CONTAINER_ID()); if (executionEnvContainerId != null) { log.info("Got execution environment container id: {}", executionEnvContainerId); return new ContainerHeartbeatMonitor(() -> { try { container.shutdown(); containerRunnerException = new SamzaException("Container shutdown due to expired heartbeat"); } catch (Exception e) { log.error("Heartbeat monitor failed to shutdown the container gracefully. Exiting process.", e); System.exit(1); } }, new ContainerHeartbeatClient(coordinatorUrl, executionEnvContainerId)); } else { log.warn("Execution environment container id not set. Container heartbeat monitor will not be created"); return null; } } }
/** * Creates a new container heartbeat monitor if possible. * @param container the container to monitor * @return a new {@link ContainerHeartbeatMonitor} instance, or null if could not create one */ private static ContainerHeartbeatMonitor createContainerHeartbeatMonitor(SamzaContainer container) { String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL()); String executionEnvContainerId = System.getenv(ShellCommandConfig.ENV_EXECUTION_ENV_CONTAINER_ID()); if (executionEnvContainerId != null) { log.info("Got execution environment container id: {}", executionEnvContainerId); return new ContainerHeartbeatMonitor(() -> { try { container.shutdown(); containerRunnerException = new SamzaException("Container shutdown due to expired heartbeat"); } catch (Exception e) { log.error("Heartbeat monitor failed to shutdown the container gracefully. Exiting process.", e); System.exit(1); } }, new ContainerHeartbeatClient(coordinatorUrl, executionEnvContainerId)); } else { log.warn("Execution environment container id not set. Container heartbeat monitor will not be created"); return null; } } }
@Test public void testDoesNotCallbackWhenHeartbeatAlive() throws InterruptedException { ContainerHeartbeatClient client = mock(ContainerHeartbeatClient.class); CountDownLatch countDownLatch = new CountDownLatch(1); Runnable onExpired = () -> { countDownLatch.countDown(); }; ContainerHeartbeatMonitor monitor = new ContainerHeartbeatMonitor(onExpired, client); ContainerHeartbeatResponse response = new ContainerHeartbeatResponse(true); when(client.requestHeartbeat()).thenReturn(response); monitor.start(); boolean success = countDownLatch.await(2, TimeUnit.SECONDS); Assert.assertFalse(success); Assert.assertEquals(1, countDownLatch.getCount()); } }
/** * Creates a new container heartbeat monitor if possible. * @param container the container to monitor * @return a new {@link ContainerHeartbeatMonitor} instance, or null if could not create one */ private static ContainerHeartbeatMonitor createContainerHeartbeatMonitor(SamzaContainer container) { String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL()); String executionEnvContainerId = System.getenv(ShellCommandConfig.ENV_EXECUTION_ENV_CONTAINER_ID()); if (executionEnvContainerId != null) { log.info("Got execution environment container id: {}", executionEnvContainerId); return new ContainerHeartbeatMonitor(() -> { try { container.shutdown(); containerRunnerException = new SamzaException("Container shutdown due to expired heartbeat"); } catch (Exception e) { log.error("Heartbeat monitor failed to shutdown the container gracefully. Exiting process.", e); System.exit(1); } }, new ContainerHeartbeatClient(coordinatorUrl, executionEnvContainerId)); } else { log.warn("Execution environment container id not set. Container heartbeat monitor will not be created"); return null; } } }
/** * Creates a new container heartbeat monitor if possible. * @param container the container to monitor * @return a new {@link ContainerHeartbeatMonitor} instance, or null if could not create one */ private static ContainerHeartbeatMonitor createContainerHeartbeatMonitor(SamzaContainer container) { String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL()); String executionEnvContainerId = System.getenv(ShellCommandConfig.ENV_EXECUTION_ENV_CONTAINER_ID()); if (executionEnvContainerId != null) { log.info("Got execution environment container id: {}", executionEnvContainerId); return new ContainerHeartbeatMonitor(() -> { try { container.shutdown(); containerRunnerException = new SamzaException("Container shutdown due to expired heartbeat"); } catch (Exception e) { log.error("Heartbeat monitor failed to shutdown the container gracefully. Exiting process.", e); System.exit(1); } }, new ContainerHeartbeatClient(coordinatorUrl, executionEnvContainerId)); } else { log.warn("Execution environment container id not set. Container heartbeat monitor will not be created"); return null; } } }
/** * Creates a new container heartbeat monitor if possible. * @param container the container to monitor * @return a new {@link ContainerHeartbeatMonitor} instance, or null if could not create one */ private static ContainerHeartbeatMonitor createContainerHeartbeatMonitor(SamzaContainer container) { String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL()); String executionEnvContainerId = System.getenv(ShellCommandConfig.ENV_EXECUTION_ENV_CONTAINER_ID()); if (executionEnvContainerId != null) { log.info("Got execution environment container id: {}", executionEnvContainerId); return new ContainerHeartbeatMonitor(() -> { try { container.shutdown(); containerRunnerException = new SamzaException("Container shutdown due to expired heartbeat"); } catch (Exception e) { log.error("Heartbeat monitor failed to shutdown the container gracefully. Exiting process.", e); System.exit(1); } }, new ContainerHeartbeatClient(coordinatorUrl, executionEnvContainerId)); } else { log.warn("Execution environment container id not set. Container heartbeat monitor will not be created"); return null; } } }