cameronlee314 commented on a change in pull request #1151: [SAMZA-2198] Fix
deadlock in container signal handler
URL: https://github.com/apache/samza/pull/1151#discussion_r324293136
##########
File path:
samza-core/src/main/java/org/apache/samza/runtime/ContainerLaunchUtil.java
##########
@@ -142,19 +150,59 @@ public void beforeStart() {
public void afterStart() {
log.info("Container Started");
listener.afterStart();
+ addShutdownHook();
}
@Override
public void afterStop() {
log.info("Container Stopped");
listener.afterStop();
+ removeShutdownHook();
+ shutdownLatch.countDown();
}
@Override
public void afterFailure(Throwable t) {
log.info("Container Failed");
containerRunnerException = t;
listener.afterFailure(t);
+ removeShutdownHook();
+ shutdownLatch.countDown();
+ }
+
+ private void removeShutdownHook() {
+ try {
+ if (shutdownHookThread != null) {
+ Runtime.getRuntime().removeShutdownHook(shutdownHookThread);
+ log.info("Removed Samza container shutdown hook");
+ }
+ } catch (IllegalStateException e) {
+ // Thrown when then JVM is already shutting down, so safe to
ignore.
+ }
+ }
+
+ private void addShutdownHook() {
+ shutdownHookThread = new Thread("Samza Container Shutdown Hook
Thread") {
+ @Override
+ public void run() {
+ long shutdownMs = taskConfig.getShutdownMs();
+ log.info("Attempting to shutdown container from inside
shutdownHook, will wait upto {} ms.", shutdownMs);
+ try {
+ container.shutdown();
+ boolean hasShutdown = shutdownLatch.await(shutdownMs,
TimeUnit.MILLISECONDS);
+ if (hasShutdown) {
+ log.info("Shutdown complete");
+ } else {
+ log.error("Did not shut down within {} ms, exiting.",
shutdownMs);
+ ThreadUtil.logThreadDump("Thread dump from Samza
Container Shutdown Hook.");
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
Review comment:
Please use the `log` here instead of `printStackTrace`. Maybe do the same
thing here as if it didn't shut down in time.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services