Apache Lucene open-source search software
SOLR-7381: MDC keys are now exposed in thread names automatically so that a thread dump can give hints on what the thread was doing
Shalin Shekhar Mangar382f9e45e07a
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6226fdcfc9e..162c45778e9 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -133,7 +133,9 @@ Other Changes* SOLR-7381: Improve logging by adding node name in MDC in SolrCloud mode and adding MDC toall thread pools. A new MDCAwareThreadPoolExecutor is introduced and usages ofExecutors#newFixedThreadPool, #newSingleThreadExecutor, #newCachedThreadPool as well as- ThreadPoolExecutor directly is now forbidden in Solr. (shalin)+ ThreadPoolExecutor directly is now forbidden in Solr. MDC keys are now exposed in thread+ names automatically so that a thread dump can give hints on what the thread was doing.+ (shalin)* SOLR-7384: Fix spurious failures in FullSolrCloudDistribCmdsTest. (shalin)diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java b/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.javaindex 6a5bf314a66..d26c15691e3 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java@@ -33,6 +33,7 @@ import org.apache.solr.core.CloudConfig;import org.apache.solr.update.UpdateShardHandler;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import java.io.Closeable;import java.util.ArrayList;@@ -245,14 +246,19 @@ public class OverseerAutoReplicaFailoverThread implements Runnable, Closeable {// need an async request - full shard goes down leader electionfinal String coreName = badReplica.replica.getStr(ZkStateReader.CORE_NAME_PROP);log.debug("submit call to {}", createUrl);- updateExecutor.submit(new Callable<Boolean>() {-- @Override- public Boolean call() {- return createSolrCore(collection, createUrl, dataDir, ulogDir, coreNodeName, coreName);- }- });-+ MDC.put("OverseerAutoReplicaFailoverThread.createUrl", createUrl);+ try {+ updateExecutor.submit(new Callable<Boolean>() {++ @Override+ public Boolean call() {+ return createSolrCore(collection, createUrl, dataDir, ulogDir, coreNodeName, coreName);+ }+ });+ } finally {+ MDC.remove("OverseerAutoReplicaFailoverThread.createUrl");+ }+// wait to see state for core we just createdboolean success = ClusterStateUtil.waitToSeeLiveReplica(zkStateReader, collection, coreNodeName, createUrl, 30000);if (!success) {diff --git a/solr/core/src/java/org/apache/solr/core/ZkContainer.java b/solr/core/src/java/org/apache/solr/core/ZkContainer.javaindex 5fb6186fe47..fef266452ab 100644--- a/solr/core/src/java/org/apache/solr/core/ZkContainer.java+++ b/solr/core/src/java/org/apache/solr/core/ZkContainer.java@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeoutException;+import org.apache.log4j.MDC;import org.apache.solr.cloud.CurrentCoreDescriptorProvider;import org.apache.solr.cloud.SolrZkServer;import org.apache.solr.cloud.ZkController;@@ -36,11 +37,14 @@ import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.ZkConfigManager;import org.apache.solr.common.cloud.ZooKeeperException;import org.apache.solr.common.util.ExecutorUtil;+import org.apache.solr.logging.MDCUtils;import org.apache.solr.util.DefaultSolrThreadFactory;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import static org.apache.solr.common.cloud.ZkStateReader.CORE_NAME_PROP;+public class ZkContainer {protected static Logger log = LoggerFactory.getLogger(ZkContainer.class);@@ -195,14 +199,19 @@ public class ZkContainer {SolrException.log(log, "", e);}}-+};if (zkController != null) {- if (background) {- coreZkRegister.execute(thread);- } else {- thread.run();+ MDCUtils.setCore(core.getName());+ try {+ if (background) {+ coreZkRegister.execute(thread);+ } else {+ thread.run();+ }+ } finally {+ MDC.remove(CORE_NAME_PROP);}}}diff --git a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.javaindex 7f4f386bcdd..af56cc1d1f2 100644--- a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java@@ -89,6 +89,7 @@ import org.apache.solr.util.RefCounted;import org.apache.solr.util.plugin.SolrCoreAware;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import static org.apache.solr.common.params.CommonParams.NAME;@@ -426,8 +427,17 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw}RestoreCore restoreCore = new RestoreCore(core, location, name);- restoreFuture = restoreExecutor.submit(restoreCore);- currentRestoreName = name;+ try {+ MDC.put("RestoreCore.core", core.getName());+ MDC.put("RestoreCore.backupLocation", location);+ MDC.put("RestoreCore.backupName", name);+ restoreFuture = restoreExecutor.submit(restoreCore);+ currentRestoreName = name;+ } finally {+ MDC.remove("RestoreCore.core");+ MDC.remove("RestoreCore.backupLocation");+ MDC.remove("RestoreCore.backupName");+ }}private NamedList<Object> getRestoreStatus() {diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex 6902ba177d5..c107bc0a2fa 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -84,6 +84,7 @@ import org.apache.solr.util.RefCounted;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import static org.apache.solr.common.cloud.DocCollection.DOC_ROUTER;import static org.apache.solr.common.params.CommonParams.NAME;@@ -191,7 +192,16 @@ public class CoreAdminHandler extends RequestHandlerBase {handleRequestInternal(req, rsp, action);} else {ParallelCoreAdminHandlerThread parallelHandlerThread = new ParallelCoreAdminHandlerThread(req, rsp, action, taskObject);- parallelExecutor.execute(parallelHandlerThread);+ try {+ MDC.put("CoreAdminHandler.asyncId", taskId);+ if (action != null) {+ MDC.put("CoreAdminHandler.action", action.name());+ }+ parallelExecutor.execute(parallelHandlerThread);+ } finally {+ MDC.remove("CoreAdminHandler.asyncId");+ MDC.remove("CoreAdminHandler.action");+ }}}diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.javaindex 2a89ab12f40..c36fa4c1a2e 100644--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java@@ -60,6 +60,7 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.common.base.Strings;+import org.slf4j.MDC;public class HttpShardHandler extends ShardHandler {@@ -239,7 +240,18 @@ public class HttpShardHandler extends ShardHandler {}};- pending.add( completionService.submit(task) );+ try {+ if (shard != null) {+ MDC.put("ShardRequest.shards", shard);+ }+ if (urls != null && !urls.isEmpty()) {+ MDC.put("ShardRequest.urlList", urls.toString());+ }+ pending.add( completionService.submit(task) );+ } finally {+ MDC.remove("ShardRequest.shards");+ MDC.remove("ShardRequest.urlList");+ }}/**diff --git a/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java b/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.javaindex 6b564533695..b6ccdb7a38c 100644--- a/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java+++ b/solr/core/src/java/org/apache/solr/update/processor/DistributedUpdateProcessor.java@@ -72,6 +72,7 @@ import org.apache.solr.update.VersionInfo;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import java.io.IOException;import java.util.ArrayList;@@ -933,7 +934,12 @@ public class DistributedUpdateProcessor extends UpdateRequestProcessor {maxTries,cloudDesc.getCoreNodeName()); // core node name of current leaderExecutorService executor = coreContainer.getUpdateShardHandler().getUpdateExecutor();- executor.execute(lirThread);+ try {+ MDC.put("DistributedUpdateProcessor.replicaUrlToRecover", error.req.node.getNodeProps().getCoreUrl());+ executor.execute(lirThread);+ } finally {+ MDC.remove("DistributedUpdateProcessor.replicaUrlToRecover");+ }}if (replicationTracker != null) {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.javaindex 7beb1678222..b0e8e10d646 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java@@ -54,6 +54,7 @@ import org.apache.solr.common.util.StrUtils;import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import java.io.IOException;import java.net.ConnectException;@@ -110,7 +111,7 @@ public class CloudSolrClient extends SolrClient {private boolean parallelUpdates = true;private ExecutorService threadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrjNamedThreadFactory(- "CloudSolrServer ThreadPool"));+ "CloudSolrClient ThreadPool"));private String idField = "id";public static final String STATE_VERSION = "_stateVer_";private final Set<String> NON_ROUTABLE_PARAMS;@@ -591,12 +592,17 @@ public class CloudSolrClient extends SolrClient {for (final Map.Entry<String, LBHttpSolrClient.Req> entry : routes.entrySet()) {final String url = entry.getKey();final LBHttpSolrClient.Req lbRequest = entry.getValue();- responseFutures.put(url, threadPool.submit(new Callable<NamedList<?>>() {- @Override- public NamedList<?> call() throws Exception {- return lbClient.request(lbRequest).getResponse();- }- }));+ try {+ MDC.put("CloudSolrClient.url", url);+ responseFutures.put(url, threadPool.submit(new Callable<NamedList<?>>() {+ @Override+ public NamedList<?> call() throws Exception {+ return lbClient.request(lbRequest).getResponse();+ }+ }));+ } finally {+ MDC.remove("CloudSolrClient.url");+ }}for (final Map.Entry<String, Future<NamedList<?>>> entry: responseFutures.entrySet()) {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.javaindex 65508e26955..667016b48c3 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/ConcurrentUpdateSolrClient.java@@ -42,6 +42,7 @@ import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SolrjNamedThreadFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import java.io.IOException;import java.io.OutputStream;@@ -343,9 +344,14 @@ public class ConcurrentUpdateSolrClient extends SolrClient {if (runners.isEmpty() || (queue.remainingCapacity() < queue.size() && runners.size() < threadCount)){// We need more runners, so start a new one.- Runner r = new Runner();- runners.add(r);- scheduler.execute(r);+ MDC.put("ConcurrentUpdateSolrClient.url", client.getBaseURL());+ try {+ Runner r = new Runner();+ runners.add(r);+ scheduler.execute(r);+ } finally {+ MDC.remove("ConcurrentUpdateSolrClient.url");+ }} else {// break out of the retry loop if we added the element to the queue// successfully, *and*@@ -402,9 +408,14 @@ public class ConcurrentUpdateSolrClient extends SolrClient {if (queueSize > 0) {log.warn("No more runners, but queue still has "+queueSize+" adding more runners to process remaining requests on queue");- Runner r = new Runner();- runners.add(r);- scheduler.execute(r);+ MDC.put("ConcurrentUpdateSolrClient.url", client.getBaseURL());+ try {+ Runner r = new Runner();+ runners.add(r);+ scheduler.execute(r);+ } finally {+ MDC.remove("ConcurrentUpdateSolrClient.url");+ }}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.javaindex 4eef33faa02..04fb70eee16 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java@@ -57,6 +57,7 @@ import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SolrjNamedThreadFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import org.slf4j.MDC;import java.io.IOException;import java.io.InputStream;@@ -262,6 +263,7 @@ public class HttpSolrClient extends SolrClient {final HttpRequestBase method = createMethod(request, null);ExecutorService pool = ExecutorUtil.newMDCAwareFixedThreadPool(1, new SolrjNamedThreadFactory("httpUriRequest"));try {+ MDC.put("HttpSolrClient.url", baseUrl);mrr.future = pool.submit(new Callable<NamedList<Object>>(){@Override@@ -271,6 +273,7 @@ public class HttpSolrClient extends SolrClient {} finally {pool.shutdown();+ MDC.remove("HttpSolrClient.url");}assert method != null;mrr.httpUriRequest = method;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.javaindex 88dcd0b7098..6dc2f353bf6 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/LBHttpSolrClient.java@@ -27,6 +27,7 @@ import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SolrjNamedThreadFactory;import org.apache.solr.common.SolrException;+import org.slf4j.MDC;import java.io.IOException;import java.lang.ref.WeakReference;@@ -320,11 +321,16 @@ public class LBHttpSolrClient extends SolrClient {continue;}rsp.server = serverStr;- HttpSolrClient client = makeSolrClient(serverStr);+ try {+ MDC.put("LBHttpSolrClient.url", serverStr);+ HttpSolrClient client = makeSolrClient(serverStr);- ex = doRequest(client, req, rsp, isUpdate, false, null);- if (ex == null) {- return rsp; // SUCCESS+ ex = doRequest(client, req, rsp, isUpdate, false, null);+ if (ex == null) {+ return rsp; // SUCCESS+ }+ } finally {+ MDC.remove("LBHttpSolrClient.url");}}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java b/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.javaindex 0d5e704c647..f6f7cf52be6 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/ExecutorUtil.java@@ -106,6 +106,8 @@ public class ExecutorUtil {public static class MDCAwareThreadPoolExecutor extends ThreadPoolExecutor {+ private static final int MAX_THREAD_NAME_LEN = 512;+public MDCAwareThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);}@@ -125,12 +127,18 @@ public class ExecutorUtil {@Overridepublic void execute(final Runnable command) {final Map<String, String> submitterContext = MDC.getCopyOfContextMap();+ String ctxStr = submitterContext != null && !submitterContext.isEmpty() ?+ submitterContext.toString().replace("/", "//") : "";+ final String submitterContextStr = ctxStr.length() <= MAX_THREAD_NAME_LEN ? ctxStr : ctxStr.substring(0, MAX_THREAD_NAME_LEN);super.execute(new Runnable() {@Overridepublic void run() {Map<String, String> threadContext = MDC.getCopyOfContextMap();- if (submitterContext != null) {+ final Thread currentThread = Thread.currentThread();+ final String oldName = currentThread.getName();+ if (submitterContext != null && !submitterContext.isEmpty()) {MDC.setContextMap(submitterContext);+ currentThread.setName(oldName + "-processing-" + submitterContextStr);} else {MDC.clear();}@@ -142,6 +150,7 @@ public class ExecutorUtil {} else {MDC.clear();}+ currentThread.setName(oldName);}}});