-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Currently we set the defaults for ccsMinimizeRoundtrips, preFilterShardSize and requestCache on the HLRC SubmitAsyncSearchRequest in the constructor. This is no longer needed since we now only send the parameters along with the rest request that are supported (omitting e.g. ccsMinimizeRoundtrips) and the correct defaults are set on the client side. This change removes setting and sending these defaults where possible, leaving only the overwrite of batchedReduceSize with a default value of 5, since the default used in the vanilla SearchRequest is 512. However, we don't need to send this value along as a request parameter if its the default since the correct one will be set on the receiving end if no value is specified. Also adding tests for RestSubmitAsyncSearchAction that check the correct defaults are set when parameters are missing on the server side. Backport of #54200
- Loading branch information
Christoph Büscher
authored
Mar 26, 2020
1 parent
e6e27ff
commit 3664f10
Showing
5 changed files
with
139 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...search/src/test/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchActionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.search; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.client.node.NodeClient; | ||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
import org.elasticsearch.rest.RestController; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.rest.FakeRestChannel; | ||
import org.elasticsearch.test.rest.FakeRestRequest; | ||
import org.elasticsearch.usage.UsageService; | ||
import org.elasticsearch.xpack.core.search.action.SubmitAsyncSearchRequest; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class RestSubmitAsyncSearchActionTests extends ESTestCase { | ||
|
||
private RestSubmitAsyncSearchAction action; | ||
private ActionRequest lastCapturedRequest; | ||
private RestController controller; | ||
private NodeClient nodeClient; | ||
|
||
@Before | ||
public void setUpController() { | ||
nodeClient = new NodeClient(Settings.EMPTY, null) { | ||
|
||
@Override | ||
public <Request extends ActionRequest, Response extends ActionResponse> Task executeLocally(ActionType<Response> action, | ||
Request request, ActionListener<Response> listener) { | ||
lastCapturedRequest = request; | ||
return new Task(1L, "type", "action", "description", null, null); | ||
} | ||
}; | ||
nodeClient.initialize(new HashMap<>(), () -> "local", null); | ||
controller = new RestController(Collections.emptySet(), null, | ||
nodeClient, | ||
new NoneCircuitBreakerService(), | ||
new UsageService()); | ||
action = new RestSubmitAsyncSearchAction(); | ||
controller.registerHandler(action); | ||
} | ||
|
||
/** | ||
* Check that the appropriate defaults are set on the {@link SubmitAsyncSearchRequest} if | ||
* no parameters are specified on the rest request itself. | ||
*/ | ||
public void testRequestParameterDefaults() throws IOException { | ||
RestRequest submitAsyncRestRequest = new FakeRestRequest.Builder(xContentRegistry()) | ||
.withMethod(RestRequest.Method.POST) | ||
.withPath("/test_index/_async_search") | ||
.withContent(new BytesArray("{}"), XContentType.JSON) | ||
.build(); | ||
dispatchRequest(submitAsyncRestRequest); | ||
SubmitAsyncSearchRequest submitRequest = (SubmitAsyncSearchRequest) lastCapturedRequest; | ||
assertEquals(TimeValue.timeValueSeconds(1), submitRequest.getWaitForCompletionTimeout()); | ||
assertEquals(false, submitRequest.isKeepOnCompletion()); | ||
assertEquals(TimeValue.timeValueDays(5), submitRequest.getKeepAlive()); | ||
// check parameters we implicitly set in the SubmitAsyncSearchRequest ctor | ||
assertEquals(false, submitRequest.getSearchRequest().isCcsMinimizeRoundtrips()); | ||
assertEquals(5, submitRequest.getSearchRequest().getBatchedReduceSize()); | ||
assertEquals(true, submitRequest.getSearchRequest().requestCache()); | ||
assertEquals(1, submitRequest.getSearchRequest().getPreFilterShardSize().intValue()); | ||
} | ||
|
||
public void testParameters() throws IOException { | ||
String tvString = randomTimeValue(1, 100); | ||
doTestParameter("keep_alive", tvString, TimeValue.parseTimeValue(tvString, ""), SubmitAsyncSearchRequest::getKeepAlive); | ||
doTestParameter("wait_for_completion_timeout", tvString, TimeValue.parseTimeValue(tvString, ""), | ||
SubmitAsyncSearchRequest::getWaitForCompletionTimeout); | ||
boolean keepOnCompletion = randomBoolean(); | ||
doTestParameter("keep_on_completion", Boolean.toString(keepOnCompletion), keepOnCompletion, | ||
SubmitAsyncSearchRequest::isKeepOnCompletion); | ||
boolean requestCache = randomBoolean(); | ||
doTestParameter("request_cache", Boolean.toString(requestCache), requestCache, | ||
r -> r.getSearchRequest().requestCache()); | ||
Integer batchReduceSize = randomIntBetween(2, 50); | ||
doTestParameter("batched_reduce_size", Integer.toString(batchReduceSize), batchReduceSize, | ||
r -> r.getSearchRequest().getBatchedReduceSize()); | ||
} | ||
|
||
private <T> void doTestParameter(String paramName, String paramValue, T expectedValue, | ||
Function<SubmitAsyncSearchRequest, T> valueAccessor) { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put(paramName, paramValue); | ||
RestRequest submitAsyncRestRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) | ||
.withPath("/test_index/_async_search") | ||
.withParams(params) | ||
.withContent(new BytesArray("{}"), XContentType.JSON).build(); | ||
dispatchRequest(submitAsyncRestRequest); | ||
SubmitAsyncSearchRequest submitRequest = (SubmitAsyncSearchRequest) lastCapturedRequest; | ||
assertEquals(expectedValue, valueAccessor.apply(submitRequest)); | ||
} | ||
|
||
/** | ||
* Sends the given request to the test controller | ||
*/ | ||
protected void dispatchRequest(RestRequest request) { | ||
FakeRestChannel channel = new FakeRestChannel(request, false, 1); | ||
ThreadContext threadContext = new ThreadContext(Settings.EMPTY); | ||
controller.dispatchRequest(request, channel, threadContext); | ||
} | ||
} |