Skip to content

Commit 5083e20

Browse files
authored
Avoid null threadContext in ResultDeduplicator (#84093) (#84097)
In #84038 we added a dependency on having a valid `threadContext` in a repository, but some tests use mocking and may end up with a `null` here. This seems not to be a problem in recent branches but causes failures in 8.0. With this commit we ensure that we always have a valid `threadContext` to avoid any problems.
1 parent e6e7449 commit 5083e20

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

server/src/main/java/org/elasticsearch/action/ResultDeduplicator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class ResultDeduplicator<T, R> {
2828
private final ConcurrentMap<T, CompositeListener> requests = ConcurrentCollections.newConcurrentMap();
2929

3030
public ResultDeduplicator(ThreadContext threadContext) {
31+
assert threadContext != null;
3132
this.threadContext = threadContext;
3233
}
3334

server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.component.LifecycleListener;
3232
import org.elasticsearch.common.settings.Settings;
3333
import org.elasticsearch.common.util.MockBigArrays;
34+
import org.elasticsearch.common.util.concurrent.ThreadContext;
3435
import org.elasticsearch.index.shard.ShardId;
3536
import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;
3637
import org.elasticsearch.index.store.Store;
@@ -65,7 +66,9 @@ public class RepositoriesServiceTests extends ESTestCase {
6566
@Override
6667
public void setUp() throws Exception {
6768
super.setUp();
69+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
6870
ThreadPool threadPool = mock(ThreadPool.class);
71+
when(threadPool.getThreadContext()).thenReturn(threadContext);
6972
final TransportService transportService = new TransportService(
7073
Settings.EMPTY,
7174
mock(Transport.class),

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.elasticsearch.common.blobstore.BlobContainer;
3030
import org.elasticsearch.common.blobstore.BlobMetadata;
3131
import org.elasticsearch.common.blobstore.BlobPath;
32+
import org.elasticsearch.common.settings.Settings;
33+
import org.elasticsearch.common.util.concurrent.ThreadContext;
3234
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3335
import org.elasticsearch.core.TimeValue;
3436
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshots;
@@ -417,7 +419,9 @@ public static ClusterService mockClusterService(RepositoryMetadata metadata) {
417419
}
418420

419421
private static ClusterService mockClusterService(ClusterState initialState) {
422+
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
420423
final ThreadPool threadPool = mock(ThreadPool.class);
424+
when(threadPool.getThreadContext()).thenReturn(threadContext);
421425
when(threadPool.executor(ThreadPool.Names.SNAPSHOT)).thenReturn(new SameThreadExecutorService());
422426
when(threadPool.generic()).thenReturn(new SameThreadExecutorService());
423427
when(threadPool.info(ThreadPool.Names.SNAPSHOT)).thenReturn(

x-pack/plugin/repository-encrypted/src/test/java/org/elasticsearch/repositories/encrypted/EncryptedRepositoryTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.common.settings.SecureString;
1818
import org.elasticsearch.common.settings.Settings;
1919
import org.elasticsearch.common.util.BigArrays;
20+
import org.elasticsearch.common.util.concurrent.ThreadContext;
2021
import org.elasticsearch.core.Tuple;
2122
import org.elasticsearch.indices.recovery.RecoverySettings;
2223
import org.elasticsearch.license.XPackLicenseState;
@@ -75,7 +76,10 @@ public void setUpMocks() throws Exception {
7576
Settings.EMPTY
7677
);
7778
ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
78-
when(clusterApplierService.threadPool()).thenReturn(mock(ThreadPool.class));
79+
final var threadContext = new ThreadContext(Settings.EMPTY);
80+
final var threadPool = mock(ThreadPool.class);
81+
when(threadPool.getThreadContext()).thenReturn(threadContext);
82+
when(clusterApplierService.threadPool()).thenReturn(threadPool);
7983
ClusterService clusterService = mock(ClusterService.class);
8084
when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);
8185
this.encryptedRepository = new EncryptedRepository(

0 commit comments

Comments
 (0)