Skip to content

Commit

Permalink
Avoid null threadContext in ResultDeduplicator
Browse files Browse the repository at this point in the history
In elastic#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.
  • Loading branch information
DaveCTurner committed Feb 17, 2022
1 parent bf00ab3 commit 2e9fe90
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class ResultDeduplicator<T, R> {
private final ConcurrentMap<T, CompositeListener> requests = ConcurrentCollections.newConcurrentMap();

public ResultDeduplicator(ThreadContext threadContext) {
assert threadContext != null;
this.threadContext = threadContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2e9fe90

Please sign in to comment.