diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterApplierService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterApplierService.java index 0e6bd3c71f63d..4a2c6e9424410 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterApplierService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterApplierService.java @@ -105,7 +105,7 @@ public class ClusterApplierService extends AbstractLifecycleComponent implements private final ClusterSettings clusterSettings; protected final ThreadPool threadPool; - protected final InternalThreadContextWrapper tcWrapper; + protected volatile InternalThreadContextWrapper tcWrapper; private volatile TimeValue slowTaskLoggingThreshold; @@ -141,7 +141,6 @@ public ClusterApplierService( ) { this.clusterSettings = clusterSettings; this.threadPool = threadPool; - this.tcWrapper = InternalThreadContextWrapper.from(threadPool.getThreadContext()); this.state = new AtomicReference<>(); this.nodeName = nodeName; @@ -176,6 +175,7 @@ protected synchronized void doStart() { Objects.requireNonNull(nodeConnectionsService, "please set the node connection service before starting"); Objects.requireNonNull(state.get(), "please set initial state before starting"); threadPoolExecutor = createThreadPoolExecutor(); + tcWrapper = InternalThreadContextWrapper.from(threadPool.getThreadContext()); } protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() { diff --git a/server/src/main/java/org/opensearch/cluster/service/MasterService.java b/server/src/main/java/org/opensearch/cluster/service/MasterService.java index 304fb67fe98ba..ca16036086d1b 100644 --- a/server/src/main/java/org/opensearch/cluster/service/MasterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/MasterService.java @@ -135,7 +135,7 @@ public class MasterService extends AbstractLifecycleComponent { private volatile TimeValue slowTaskLoggingThreshold; protected final ThreadPool threadPool; - protected final InternalThreadContextWrapper tcWrapper; + protected volatile InternalThreadContextWrapper tcWrapper; private volatile PrioritizedOpenSearchThreadPoolExecutor threadPoolExecutor; private volatile Batcher taskBatcher; @@ -171,7 +171,6 @@ public MasterService( ); this.stateStats = new ClusterStateStats(); this.threadPool = threadPool; - this.tcWrapper = InternalThreadContextWrapper.from(threadPool.getThreadContext()); this.clusterManagerMetrics = clusterManagerMetrics; } @@ -193,6 +192,7 @@ protected synchronized void doStart() { Objects.requireNonNull(clusterStateSupplier, "please set a cluster state supplier before starting"); threadPoolExecutor = createThreadPoolExecutor(); taskBatcher = new Batcher(logger, threadPoolExecutor, clusterManagerTaskThrottler); + tcWrapper = InternalThreadContextWrapper.from(threadPool.getThreadContext()); } protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() {