Remove Redundant Shutdown Hook

Currently, when we start a NodeControllerService, we add
its shutdown hook to the runtime. The NCDriver does that as well.
This change removes the NCDriver's addition of the shutdown hook
and moves the NCShutdown hook to its own class.

Change-Id: Ie9b4e2c870feb36fb25f35034dcfe3e315b9d183
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1323
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCDriver.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCDriver.java
index 2d0a49d..cbd8edd 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCDriver.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCDriver.java
@@ -39,23 +39,12 @@
                 System.exit(1);
             }
             ncConfig.loadConfigAndApplyDefaults();
-
             final NodeControllerService nService = new NodeControllerService(ncConfig);
             if (LOGGER.isLoggable(Level.SEVERE)) {
                 LOGGER.severe("Setting uncaught exception handler " + nService.getLifeCycleComponentManager());
             }
             Thread.currentThread().setUncaughtExceptionHandler(nService.getLifeCycleComponentManager());
             nService.start();
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-                @Override
-                public void run() {
-                    try {
-                        nService.stop();
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                }
-            });
             while (true) {
                 Thread.sleep(10000);
             }
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCShutdownHook.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCShutdownHook.java
new file mode 100644
index 0000000..a8b9461
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NCShutdownHook.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hyracks.control.nc;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Shutdown hook that invokes {@link NodeControllerService#stop() stop} method.
+ */
+public class NCShutdownHook extends Thread {
+    private static final Logger LOGGER = Logger.getLogger(NCShutdownHook.class.getName());
+    private final NodeControllerService nodeControllerService;
+    public NCShutdownHook(NodeControllerService nodeControllerService) {
+        this.nodeControllerService = nodeControllerService;
+    }
+
+    @Override
+    public void run() {
+        LOGGER.info("Shutdown hook in progress");
+        try {
+            nodeControllerService.stop();
+        } catch (Exception e) {
+            LOGGER.log(Level.WARNING, "Exception in executing shutdown hook", e);
+        }
+    }
+}
\ No newline at end of file
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NodeControllerService.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NodeControllerService.java
index ed46b53..540b4d0 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NodeControllerService.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/NodeControllerService.java
@@ -79,16 +79,16 @@
 import org.apache.hyracks.control.nc.partitions.PartitionManager;
 import org.apache.hyracks.control.nc.resources.memory.MemoryManager;
 import org.apache.hyracks.control.nc.runtime.RootHyracksContext;
+import org.apache.hyracks.control.nc.task.ShutdownTask;
+import org.apache.hyracks.control.nc.task.ThreadDumpTask;
 import org.apache.hyracks.control.nc.work.AbortTasksWork;
 import org.apache.hyracks.control.nc.work.ApplicationMessageWork;
 import org.apache.hyracks.control.nc.work.BuildJobProfilesWork;
 import org.apache.hyracks.control.nc.work.CleanupJobletWork;
 import org.apache.hyracks.control.nc.work.DeployBinaryWork;
 import org.apache.hyracks.control.nc.work.ReportPartitionAvailabilityWork;
-import org.apache.hyracks.control.nc.task.ShutdownTask;
 import org.apache.hyracks.control.nc.work.StartTasksWork;
 import org.apache.hyracks.control.nc.work.StateDumpWork;
-import org.apache.hyracks.control.nc.task.ThreadDumpTask;
 import org.apache.hyracks.control.nc.work.UnDeployBinaryWork;
 import org.apache.hyracks.ipc.api.IIPCHandle;
 import org.apache.hyracks.ipc.api.IIPCI;
@@ -328,7 +328,7 @@
         }
 
         //add JVM shutdown hook
-        Runtime.getRuntime().addShutdownHook(new JVMShutdownHook(this));
+        Runtime.getRuntime().addShutdownHook(new NCShutdownHook(this));
     }
 
     private void startApplication() throws Exception {
@@ -599,26 +599,4 @@
     public MessagingNetworkManager getMessagingNetworkManager() {
         return messagingNetManager;
     }
-
-    /**
-     * Shutdown hook that invokes {@link NodeControllerService#stop() stop} method.
-     */
-    private static class JVMShutdownHook extends Thread {
-
-        private final NodeControllerService nodeControllerService;
-
-        public JVMShutdownHook(NodeControllerService ncAppEntryPoint) {
-            this.nodeControllerService = ncAppEntryPoint;
-        }
-
-        @Override
-        public void run() {
-            LOGGER.info("Shutdown hook in progress");
-            try {
-                nodeControllerService.stop();
-            } catch (Exception e) {
-                LOGGER.log(Level.WARNING, "Exception in executing shutdown hook", e);
-            }
-        }
-    }
 }