Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ThreadContext.stashAndMergeHeaders package-private #14983

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Bump `org.apache.commons:commons-lang3` from 3.14.0 to 3.15.0 ([#14861](https://github.com/opensearch-project/OpenSearch/pull/14861))

### Changed
- Make ThreadContext.stashAndMergeHeaders package-private ([#14983](https://github.com/opensearch-project/OpenSearch/pull/14983))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,13 @@
import org.opensearch.client.AdminClient;
import org.opensearch.client.Client;
import org.opensearch.client.ClusterAdminClient;
import org.opensearch.client.FilterClient;
import org.opensearch.client.IndicesAdminClient;
import org.opensearch.client.OpenSearchClient;
import org.opensearch.cluster.metadata.IndexMetadata.APIBlock;
import org.opensearch.common.Nullable;
import org.opensearch.common.action.ActionFuture;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.util.concurrent.StashAndMergeHeadersFilterClient;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.bytes.BytesReference;
Expand Down Expand Up @@ -2140,18 +2139,6 @@

@Override
public Client filterWithHeader(Map<String, String> headers) {
return new FilterClient(this) {
@Override
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
ActionType<Response> action,
Request request,
ActionListener<Response> listener
) {
ThreadContext threadContext = threadPool().getThreadContext();
try (ThreadContext.StoredContext ctx = threadContext.stashAndMergeHeaders(headers)) {
super.doExecute(action, request, listener);
}
}
};
return new StashAndMergeHeadersFilterClient(this, headers);

Check warning on line 2142 in server/src/main/java/org/opensearch/client/support/AbstractClient.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/client/support/AbstractClient.java#L2142

Added line #L2142 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.util.concurrent;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionType;
import org.opensearch.client.Client;
import org.opensearch.client.FilterClient;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.action.ActionResponse;

import java.util.Map;

/**
* This class is used for simulating request headers for testing. Used in TasksIT to
* assert that request headers are carried to task info
*
* @opensearch.internal
*/
public class StashAndMergeHeadersFilterClient extends FilterClient {
private Map<String, String> headers;

public StashAndMergeHeadersFilterClient(Client in, Map<String, String> headers) {
super(in);
this.headers = headers;
}

Check warning on line 32 in server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java#L30-L32

Added lines #L30 - L32 were not covered by tests

@Override
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
ActionType<Response> action,
Request request,
ActionListener<Response> listener
) {
ThreadContext threadContext = threadPool().getThreadContext();
try (ThreadContext.StoredContext ctx = threadContext.stashAndMergeHeaders(headers)) {
super.doExecute(action, request, listener);

Check warning on line 42 in server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java#L40-L42

Added lines #L40 - L42 were not covered by tests
}
}

Check warning on line 44 in server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/util/concurrent/StashAndMergeHeadersFilterClient.java#L44

Added line #L44 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public StoredContext stashWithOrigin(String origin) {
* The removed context can be restored when closing the returned {@link StoredContext}. The merge strategy is that headers
* that are already existing are preserved unless they are defaults.
*/
public StoredContext stashAndMergeHeaders(Map<String, String> headers) {
StoredContext stashAndMergeHeaders(Map<String, String> headers) {
final ThreadContextStruct context = threadLocal.get();
Map<String, String> newHeader = new HashMap<>(headers);
newHeader.putAll(context.requestHeaders);
Expand Down
Loading