ASTERIXDB-1611: Improve Thread Names

Executor threads are now named as follows:

- "Executor-<thread number>:<nodeId|ClusterController>"

Change-Id: Iea54740c51a9ac9388337cf88260a8482ff4a334
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1226
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: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
index f6dfe4a..9120aa5 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
@@ -95,7 +95,7 @@
             LOGGER.info("Starting Asterix cluster controller");
         }
 
-        appCtx.setThreadFactory(new AsterixThreadFactory(new LifeCycleComponentManager()));
+        appCtx.setThreadFactory(new AsterixThreadFactory(appCtx.getThreadFactory(), new LifeCycleComponentManager()));
         GlobalRecoveryManager.instantiate((HyracksConnection) getNewHyracksClientConnection());
         ILibraryManager libraryManager = new ExternalLibraryManager();
         AsterixResourceIdManager resourceIdManager = new AsterixResourceIdManager();
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
index c678bf3..f26afa8 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
@@ -95,7 +95,8 @@
             throw e;
         }
 
-        ncAppCtx.setThreadFactory(new AsterixThreadFactory(ncAppCtx.getLifeCycleComponentManager()));
+        ncAppCtx.setThreadFactory(new AsterixThreadFactory(ncAppCtx.getThreadFactory(),
+                ncAppCtx.getLifeCycleComponentManager()));
         ncApplicationContext = ncAppCtx;
         nodeId = ncApplicationContext.getNodeId();
         if (LOGGER.isLoggable(Level.INFO)) {
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/api/AsterixThreadFactory.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/api/AsterixThreadFactory.java
index 6b4bf72..e215d6a 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/api/AsterixThreadFactory.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/api/AsterixThreadFactory.java
@@ -24,22 +24,19 @@
 
 public class AsterixThreadFactory implements ThreadFactory {
 
+    private final ThreadFactory delegate;
     private final ILifeCycleComponentManager lccm;
 
-    public AsterixThreadFactory(ILifeCycleComponentManager lifeCycleComponentManager) {
+    public AsterixThreadFactory(ThreadFactory delegate, ILifeCycleComponentManager lifeCycleComponentManager) {
+        this.delegate = delegate;
         this.lccm = lifeCycleComponentManager;
     }
 
     @Override
-    public Thread newThread(Runnable r) {
-        Thread t;
-        if ((r instanceof Thread)) {
-            t = (Thread) r;
-        } else {
-            t = new Thread(r);
-        }
-        t.setUncaughtExceptionHandler(lccm);
-        return t;
+    public Thread newThread(Runnable runnable) {
+        Thread thread = delegate.newThread(runnable);
+        thread.setUncaughtExceptionHandler(lccm);
+        return thread;
     }
 
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-cc/src/main/java/org/apache/hyracks/control/cc/application/CCApplicationContext.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-cc/src/main/java/org/apache/hyracks/control/cc/application/CCApplicationContext.java
index ad3ffc1..dfce7b8 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-cc/src/main/java/org/apache/hyracks/control/cc/application/CCApplicationContext.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-cc/src/main/java/org/apache/hyracks/control/cc/application/CCApplicationContext.java
@@ -38,6 +38,7 @@
 import org.apache.hyracks.control.cc.ClusterControllerService;
 import org.apache.hyracks.control.common.application.ApplicationContext;
 import org.apache.hyracks.control.common.context.ServerContext;
+import org.apache.hyracks.control.common.utils.HyracksThreadFactory;
 import org.apache.hyracks.control.common.work.IResultCallback;
 
 public class CCApplicationContext extends ApplicationContext implements ICCApplicationContext {
@@ -55,7 +56,7 @@
 
     public CCApplicationContext(ClusterControllerService ccs, ServerContext serverCtx, ICCContext ccContext,
             IApplicationConfig appConfig) throws IOException {
-        super(serverCtx, appConfig);
+        super(serverCtx, appConfig, new HyracksThreadFactory("ClusterController"));
         this.ccContext = ccContext;
         this.ccs = ccs;
         initPendingNodeIds = new HashSet<>();
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/application/ApplicationContext.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/application/ApplicationContext.java
index 23d287c..06bcda3 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/application/ApplicationContext.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/application/ApplicationContext.java
@@ -18,7 +18,6 @@
  */
 package org.apache.hyracks.control.common.application;
 
-import java.io.IOException;
 import java.io.Serializable;
 import java.util.concurrent.ThreadFactory;
 
@@ -30,22 +29,17 @@
 import org.apache.hyracks.control.common.context.ServerContext;
 
 public abstract class ApplicationContext implements IApplicationContext {
-    protected ServerContext serverCtx;
+    protected final ServerContext serverCtx;
+    protected final IApplicationConfig appConfig;
+    protected ThreadFactory threadFactory;
     protected Serializable distributedState;
     protected IMessageBroker messageBroker;
-    protected final IApplicationConfig appConfig;
     protected IJobSerializerDeserializerContainer jobSerDeContainer = new JobSerializerDeserializerContainer();
-    protected ThreadFactory threadFactory = new ThreadFactory() {
-        public Thread newThread(Runnable r) {
-            Thread t = new Thread(r);
-            t.setDaemon(true);
-            return t;
-        }
-    };
 
-    public ApplicationContext(ServerContext serverCtx, IApplicationConfig appConfig) {
+    public ApplicationContext(ServerContext serverCtx, IApplicationConfig appConfig, ThreadFactory threadFactory) {
         this.serverCtx = serverCtx;
         this.appConfig = appConfig;
+        this.threadFactory = threadFactory;
     }
 
     @Override
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/HyracksThreadFactory.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/HyracksThreadFactory.java
new file mode 100644
index 0000000..52631bf
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/utils/HyracksThreadFactory.java
@@ -0,0 +1,38 @@
+/*
+ * 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.common.utils;
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class HyracksThreadFactory implements ThreadFactory {
+    private final String identifier;
+    private final AtomicInteger threadId = new AtomicInteger();
+
+    public HyracksThreadFactory(String identifier) {
+        this.identifier = identifier;
+    }
+
+    @Override
+    public Thread newThread(Runnable runnable) {
+        Thread t = new Thread(runnable, "Executor-" + threadId.incrementAndGet() + ":" + identifier);
+        t.setDaemon(true);
+        return t;
+    }
+}
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/application/NCApplicationContext.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/application/NCApplicationContext.java
index a2c3f4c..44549b4 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/application/NCApplicationContext.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/application/NCApplicationContext.java
@@ -32,6 +32,7 @@
 import org.apache.hyracks.api.service.IControllerService;
 import org.apache.hyracks.control.common.application.ApplicationContext;
 import org.apache.hyracks.control.common.context.ServerContext;
+import org.apache.hyracks.control.common.utils.HyracksThreadFactory;
 import org.apache.hyracks.control.nc.NodeControllerService;
 import org.apache.hyracks.control.nc.resources.memory.MemoryManager;
 
@@ -48,7 +49,7 @@
     public NCApplicationContext(NodeControllerService ncs, ServerContext serverCtx, IHyracksRootContext rootCtx,
             String nodeId, MemoryManager memoryManager, ILifeCycleComponentManager lifeCyclecomponentManager,
             IApplicationConfig appConfig) throws IOException {
-        super(serverCtx, appConfig);
+        super(serverCtx, appConfig, new HyracksThreadFactory(nodeId));
         this.lccm = lifeCyclecomponentManager;
         this.nodeId = nodeId;
         this.rootCtx = rootCtx;