From 2e9fe90c60dc0a1271dbca458694f906b7af0f20 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 17 Feb 2022 10:11:22 +0000 Subject: [PATCH 1/2] Avoid null threadContext in ResultDeduplicator 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. --- .../java/org/elasticsearch/action/ResultDeduplicator.java | 1 + .../elasticsearch/repositories/RepositoriesServiceTests.java | 3 +++ .../repositories/blobstore/BlobStoreTestUtil.java | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/action/ResultDeduplicator.java b/server/src/main/java/org/elasticsearch/action/ResultDeduplicator.java index b63eeaf64e505..2a9887d8a30d7 100644 --- a/server/src/main/java/org/elasticsearch/action/ResultDeduplicator.java +++ b/server/src/main/java/org/elasticsearch/action/ResultDeduplicator.java @@ -28,6 +28,7 @@ public final class ResultDeduplicator { private final ConcurrentMap requests = ConcurrentCollections.newConcurrentMap(); public ResultDeduplicator(ThreadContext threadContext) { + assert threadContext != null; this.threadContext = threadContext; } diff --git a/server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java b/server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java index 04c8a08462896..d4ec767e5817c 100644 --- a/server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java +++ b/server/src/test/java/org/elasticsearch/repositories/RepositoriesServiceTests.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.component.LifecycleListener; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.MockBigArrays; +import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; import org.elasticsearch.index.store.Store; @@ -65,7 +66,9 @@ public class RepositoriesServiceTests extends ESTestCase { @Override public void setUp() throws Exception { super.setUp(); + ThreadContext threadContext = new ThreadContext(Settings.EMPTY); ThreadPool threadPool = mock(ThreadPool.class); + when(threadPool.getThreadContext()).thenReturn(threadContext); final TransportService transportService = new TransportService( Settings.EMPTY, mock(Transport.class), diff --git a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java index 47e12cd4934d4..c61bd0c2799ac 100644 --- a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java +++ b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java @@ -29,6 +29,8 @@ import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; import org.elasticsearch.common.blobstore.BlobPath; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshots; @@ -417,7 +419,9 @@ public static ClusterService mockClusterService(RepositoryMetadata metadata) { } private static ClusterService mockClusterService(ClusterState initialState) { + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); final ThreadPool threadPool = mock(ThreadPool.class); + when(threadPool.getThreadContext()).thenReturn(threadContext); when(threadPool.executor(ThreadPool.Names.SNAPSHOT)).thenReturn(new SameThreadExecutorService()); when(threadPool.generic()).thenReturn(new SameThreadExecutorService()); when(threadPool.info(ThreadPool.Names.SNAPSHOT)).thenReturn( From 37562ec178cce6ede409f6b84f96c66ab2792d06 Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 17 Feb 2022 10:33:41 +0000 Subject: [PATCH 2/2] And another --- .../repositories/encrypted/EncryptedRepositoryTests.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/repository-encrypted/src/test/java/org/elasticsearch/repositories/encrypted/EncryptedRepositoryTests.java b/x-pack/plugin/repository-encrypted/src/test/java/org/elasticsearch/repositories/encrypted/EncryptedRepositoryTests.java index cbdf72b1b9cc9..ee1056977e906 100644 --- a/x-pack/plugin/repository-encrypted/src/test/java/org/elasticsearch/repositories/encrypted/EncryptedRepositoryTests.java +++ b/x-pack/plugin/repository-encrypted/src/test/java/org/elasticsearch/repositories/encrypted/EncryptedRepositoryTests.java @@ -17,6 +17,7 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.core.Tuple; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.license.XPackLicenseState; @@ -75,7 +76,10 @@ public void setUpMocks() throws Exception { Settings.EMPTY ); ClusterApplierService clusterApplierService = mock(ClusterApplierService.class); - when(clusterApplierService.threadPool()).thenReturn(mock(ThreadPool.class)); + final var threadContext = new ThreadContext(Settings.EMPTY); + final var threadPool = mock(ThreadPool.class); + when(threadPool.getThreadContext()).thenReturn(threadContext); + when(clusterApplierService.threadPool()).thenReturn(threadPool); ClusterService clusterService = mock(ClusterService.class); when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService); this.encryptedRepository = new EncryptedRepository(