소스 검색

HADOOP-12423. Handle failure of registering shutdownhook by ShutdownHookManager in static block (Contributed by Abhishek Agarwal)

(cherry picked from commit 446987e20aacd870e1bc3f950b2bd4bbda1f9571)
(cherry picked from commit c13929b385cf2eddcf580b242ffef1f88b7c5d40)

Conflicts:
	hadoop-common-project/hadoop-common/CHANGES.txt
Vinayakumar B 9 년 전
부모
커밋
a02fab85ee

+ 3 - 0
hadoop-common-project/hadoop-common/CHANGES.txt

@@ -946,6 +946,9 @@ Release 2.8.0 - UNRELEASED
 
     HADOOP-12712. Fix some cmake plugin and native build warnings (cmccabe)
 
+    HADOOP-12423. Handle failure of registering shutdownhook by
+    ShutdownHookManager in static block (Abhishek Agarwal via vinayakumarb)
+
 Release 2.7.3 - UNRELEASED
 
   INCOMPATIBLE CHANGES

+ 18 - 13
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java

@@ -44,22 +44,27 @@ public class ShutdownHookManager {
   private static final Log LOG = LogFactory.getLog(ShutdownHookManager.class);
 
   static {
-    Runtime.getRuntime().addShutdownHook(
-      new Thread() {
-        @Override
-        public void run() {
-          MGR.shutdownInProgress.set(true);
-          for (Runnable hook: MGR.getShutdownHooksInOrder()) {
-            try {
-              hook.run();
-            } catch (Throwable ex) {
-              LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() +
-                       "' failed, " + ex.toString(), ex);
+    try {
+      Runtime.getRuntime().addShutdownHook(
+        new Thread() {
+          @Override
+          public void run() {
+            MGR.shutdownInProgress.set(true);
+            for (Runnable hook: MGR.getShutdownHooksInOrder()) {
+              try {
+                hook.run();
+              } catch (Throwable ex) {
+                LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() +
+                         "' failed, " + ex.toString(), ex);
+              }
             }
           }
         }
-      }
-    );
+      );
+    } catch (IllegalStateException ex) {
+      // JVM is being shut down. Ignore
+      LOG.warn("Failed to add the ShutdownHook", ex);
+    }
   }
 
   /**