From 8044ca48d041f59a66ccd597a1cf7e9832a60146 Mon Sep 17 00:00:00 2001
From: Madhan Neethiraj <madhan@apache.org>
Date: Mon, 12 Sep 2016 15:46:17 -0700
Subject: [PATCH] ATLAS-1162: shutdown hooks to register with ShutdownHookManager, instead of System.Runtime

---
 addons/falcon-bridge/src/main/java/org/apache/atlas/falcon/hook/FalconHook.java | 10 ++++++++--
 addons/hive-bridge/src/main/java/org/apache/atlas/hive/hook/HiveHook.java       |  9 +++++++--
 common/src/main/java/org/apache/atlas/AtlasConstants.java                       |  1 +
 notification/src/main/java/org/apache/atlas/hook/AtlasHook.java                 |  4 ++--
 release-log.txt                                                                 |  1 +
 webapp/src/main/java/org/apache/atlas/Atlas.java                                | 11 ++++++++---
 6 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/addons/falcon-bridge/src/main/java/org/apache/atlas/falcon/hook/FalconHook.java b/addons/falcon-bridge/src/main/java/org/apache/atlas/falcon/hook/FalconHook.java
index d724f57..b6312d7 100644
--- a/addons/falcon-bridge/src/main/java/org/apache/atlas/falcon/hook/FalconHook.java
+++ b/addons/falcon-bridge/src/main/java/org/apache/atlas/falcon/hook/FalconHook.java
@@ -21,6 +21,7 @@ package org.apache.atlas.falcon.hook;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
+import org.apache.atlas.AtlasConstants;
 import org.apache.atlas.falcon.bridge.FalconBridge;
 import org.apache.atlas.falcon.event.FalconEvent;
 import org.apache.atlas.falcon.publisher.FalconEventPublisher;
@@ -32,6 +33,7 @@ import org.apache.atlas.typesystem.Referenceable;
 import org.apache.falcon.entity.store.ConfigurationStore;
 import org.apache.falcon.entity.v0.feed.Feed;
 import org.apache.falcon.entity.v0.process.Process;
+import org.apache.hadoop.util.ShutdownHookManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -90,19 +92,23 @@ public class FalconHook extends AtlasHook implements FalconEventPublisher {
                     new LinkedBlockingQueue<Runnable>(queueSize),
                     new ThreadFactoryBuilder().setNameFormat("Atlas Logger %d").build());
 
-            Runtime.getRuntime().addShutdownHook(new Thread() {
+            ShutdownHookManager.get().addShutdownHook(new Thread() {
                 @Override
                 public void run() {
                     try {
+                        LOG.info("==> Shutdown of Atlas Falcon Hook");
+
                         executor.shutdown();
                         executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
                         executor = null;
                     } catch (InterruptedException ie) {
                         LOG.info("Interrupt received in shutdown.");
+                    } finally {
+                        LOG.info("<== Shutdown of Atlas Falcon Hook");
                     }
                     // shutdown client
                 }
-            });
+            }, AtlasConstants.ATLAS_SHUTDOWN_HOOK_PRIORITY);
 
             STORE = ConfigurationStore.get();
 
diff --git a/addons/hive-bridge/src/main/java/org/apache/atlas/hive/hook/HiveHook.java b/addons/hive-bridge/src/main/java/org/apache/atlas/hive/hook/HiveHook.java
index fdd0199..a3464a0 100755
--- a/addons/hive-bridge/src/main/java/org/apache/atlas/hive/hook/HiveHook.java
+++ b/addons/hive-bridge/src/main/java/org/apache/atlas/hive/hook/HiveHook.java
@@ -50,6 +50,7 @@ import org.apache.hadoop.hive.ql.metadata.Partition;
 import org.apache.hadoop.hive.ql.metadata.Table;
 import org.apache.hadoop.hive.ql.plan.HiveOperation;
 import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.util.ShutdownHookManager;
 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -122,19 +123,23 @@ public class HiveHook extends AtlasHook implements ExecuteWithHookContext {
                         new LinkedBlockingQueue<Runnable>(queueSize),
                         new ThreadFactoryBuilder().setNameFormat("Atlas Logger %d").build());
 
-                Runtime.getRuntime().addShutdownHook(new Thread() {
+                ShutdownHookManager.get().addShutdownHook(new Thread() {
                     @Override
                     public void run() {
                         try {
+                            LOG.info("==> Shutdown of Atlas Hive Hook");
+
                             executor.shutdown();
                             executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
                             executor = null;
                         } catch (InterruptedException ie) {
                             LOG.info("Interrupt received in shutdown.");
+                        } finally {
+                            LOG.info("<== Shutdown of Atlas Hive Hook");
                         }
                         // shutdown client
                     }
-                });
+                }, AtlasConstants.ATLAS_SHUTDOWN_HOOK_PRIORITY);
             }
 
             setupOperationMap();
diff --git a/common/src/main/java/org/apache/atlas/AtlasConstants.java b/common/src/main/java/org/apache/atlas/AtlasConstants.java
index cee85b4..17ffbd7 100644
--- a/common/src/main/java/org/apache/atlas/AtlasConstants.java
+++ b/common/src/main/java/org/apache/atlas/AtlasConstants.java
@@ -32,5 +32,6 @@ public final class AtlasConstants {
     public static final String DEFAULT_APP_PORT_STR = "21000";
     public static final String ATLAS_REST_ADDRESS_KEY = "atlas.rest.address";
     public static final String DEFAULT_ATLAS_REST_ADDRESS = "http://localhost:21000";
+    public static final int ATLAS_SHUTDOWN_HOOK_PRIORITY = 30;
 
 }
diff --git a/notification/src/main/java/org/apache/atlas/hook/AtlasHook.java b/notification/src/main/java/org/apache/atlas/hook/AtlasHook.java
index 93a10b4..04ee9c0 100644
--- a/notification/src/main/java/org/apache/atlas/hook/AtlasHook.java
+++ b/notification/src/main/java/org/apache/atlas/hook/AtlasHook.java
@@ -131,9 +131,9 @@ public abstract class AtlasHook {
             } catch (Exception e) {
                 numRetries++;
                 if (numRetries < maxRetries) {
-                    LOG.error("Notification send retry failed");
+                    LOG.error("Failed to send notification - attempt #" + numRetries + "; error=" + e.getMessage());
                     try {
-                        LOG.info("Sleeping for {} ms before retry", notificationRetryInterval);
+                        LOG.debug("Sleeping for {} ms before retry", notificationRetryInterval);
                         Thread.sleep(notificationRetryInterval);
                     } catch (InterruptedException ie){
                         LOG.error("Notification hook thread sleep interrupted");
diff --git a/release-log.txt b/release-log.txt
index 19fd159..e37e65f 100644
--- a/release-log.txt
+++ b/release-log.txt
@@ -10,6 +10,7 @@ ATLAS-1060 Add composite indexes for exact match performance improvements for al
 ATLAS-1127 Modify creation and modification timestamps to Date instead of Long(sumasai)
 
 ALL CHANGES:
+ATLAS-1162 Register shutdown hooks with Hadoop's ShutdownHookManager, instead of directly with Java Runtime (mneethiraj via sumasai)
 ATLAS-1098 Atlas allows creation of tag with name "isa" which causes exceptions during search (apoorvnaik via kevalbhatt)
 ATLAS-1160 Update Atlas hive hook to read configuration from atlas-application.properties instead of hive-site.xml (mneethiraj via kevalbhatt)
 ATLAS-1154 Errors in Eclipse with web.xml (davidrad via dkantor)
diff --git a/webapp/src/main/java/org/apache/atlas/Atlas.java b/webapp/src/main/java/org/apache/atlas/Atlas.java
index dd43c6d..14c43cc 100755
--- a/webapp/src/main/java/org/apache/atlas/Atlas.java
+++ b/webapp/src/main/java/org/apache/atlas/Atlas.java
@@ -30,6 +30,7 @@ import org.apache.commons.cli.ParseException;
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.util.ShutdownHookManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,16 +53,20 @@ public final class Atlas {
     private static EmbeddedServer server;
 
     static {
-        Runtime.getRuntime().addShutdownHook(new Thread() {
+        ShutdownHookManager.get().addShutdownHook(new Thread() {
             @Override
             public void run() {
                 try {
+                    LOG.info("==> Shutdown of Atlas");
+
                     shutdown();
                 } catch (Exception e) {
-                    LOG.debug("Failed to shutdown", e);
+                    LOG.error("Failed to shutdown", e);
+                } finally {
+                    LOG.info("<== Shutdown of Atlas");
                 }
             }
-        });
+        }, AtlasConstants.ATLAS_SHUTDOWN_HOOK_PRIORITY);
     }
 
     private static void shutdown() {
--
libgit2 0.27.1