Skip to content

Commit

Permalink
RestController not using thread context directly from thread pool (el…
Browse files Browse the repository at this point in the history
…astic#74293)

At the moment thread context is passed via dispatchRequest but in some
places thread context is fetched directly from thread pool
This is not a problem in production, because thread pool is initialized
with the same thread context as the one passed to dispatchRequest via
AbstractHttpServerTransport.
It might be harder to understand though and might cause problems in
testing in smaller units.
  • Loading branch information
pgomulka committed Jun 23, 2021
1 parent 02a3310 commit 654f50c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
}
}

private void dispatchRequest(RestRequest request, RestChannel channel, RestHandler handler) throws Exception {
private void dispatchRequest(RestRequest request, RestChannel channel, RestHandler handler,
ThreadContext threadContext)
throws Exception {
final int contentLength = request.contentLength();
if (contentLength > 0) {
final XContentType xContentType = request.getXContentType();
Expand Down Expand Up @@ -254,7 +256,6 @@ private void dispatchRequest(RestRequest request, RestChannel channel, RestHandl
request.ensureSafeBuffers();
}

final ThreadContext threadContext = client.threadPool().getThreadContext();
if (handler.allowSystemIndexAccessByDefault() == false) {
// The ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER indicates that the request is coming from an Elastic product and
// therefore we should allow a subset of external system index access.
Expand Down Expand Up @@ -352,7 +353,7 @@ private void tryAllHandlers(final RestRequest request, final RestChannel channel
return;
}
} else {
dispatchRequest(request, channel, handler);
dispatchRequest(request, channel, handler, threadContext);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
import org.elasticsearch.rest.RestController;
Expand Down Expand Up @@ -65,8 +64,10 @@ protected RestController controller() {
*/
protected void dispatchRequest(RestRequest request) {
FakeRestChannel channel = new FakeRestChannel(request, false, 1);
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
controller.dispatchRequest(request, channel, threadContext);
ThreadContext threadContext = verifyingClient.threadPool().getThreadContext();
try(ThreadContext.StoredContext ignore = threadContext.stashContext()) {
controller.dispatchRequest(request, channel, threadContext);
}
}

/**
Expand Down

0 comments on commit 654f50c

Please sign in to comment.