@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { String scrollIds = request.param("scroll_id"); ClearScrollRequest clearRequest = new ClearScrollRequest(); clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds))); request.withContentOrSourceParamParserOrNull((xContentParser -> { if (xContentParser != null) { // NOTE: if rest request with xcontent body has request parameters, these parameters does not override xcontent value clearRequest.setScrollIds(null); try { buildFromContent(xContentParser, clearRequest); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse request body", e); } } })); return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<>(channel)); }
public static void buildFromContent(XContentParser parser, ClearScrollRequest clearScrollRequest) throws IOException { if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new IllegalArgumentException("Malformed content, must start with an object"); } else { XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token.isValue() == false) { throw new IllegalArgumentException("scroll_id array element should only contain scroll_id"); } clearScrollRequest.addScrollId(parser.text()); } } else { throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] "); } } } }
@Override public void clearScroll(String scrollId, Runnable onCompletion) { ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId(scrollId); /* * Unwrap the client so we don't set our task as the parent. If we *did* set our ID then the clear scroll would be cancelled as * if this task is cancelled. But we want to clear the scroll regardless of whether or not the main request was cancelled. */ client.unwrap().clearScroll(clearScrollRequest, new ActionListener<ClearScrollResponse>() { @Override public void onResponse(ClearScrollResponse response) { logger.debug("Freed [{}] contexts", response.getNumFreed()); onCompletion.run(); } @Override public void onFailure(Exception e) { logger.warn((Supplier<?>) () -> new ParameterizedMessage("Failed to clear scroll [{}]", scrollId), e); onCompletion.run(); } }); }
@Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { String scrollIds = request.param("scroll_id"); ClearScrollRequest clearRequest = new ClearScrollRequest(); clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds))); if (RestActions.hasBodyContent(request)) { XContentType type = RestActions.guessBodyContentType(request); if (type == null) { scrollIds = RestActions.getRestContent(request).toUtf8(); clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds))); } else { // NOTE: if rest request with xcontent body has request parameters, these parameters does not override xcontent value clearRequest.setScrollIds(null); buildFromContent(RestActions.getRestContent(request), clearRequest); } } client.clearScroll(clearRequest, new RestStatusToXContentListener<ClearScrollResponse>(channel)); }
public static void buildFromContent(BytesReference content, ClearScrollRequest clearScrollRequest) { try (XContentParser parser = XContentHelper.createParser(content)) { if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new IllegalArgumentException("Malformed content, must start with an object"); } else { XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token.isValue() == false) { throw new IllegalArgumentException("scroll_id array element should only contain scroll_id"); } clearScrollRequest.addScrollId(parser.text()); } } else { throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] "); } } } } catch (IOException e) { throw new IllegalArgumentException("Failed to parse request body", e); } }
public void execute(ClearScrollRequest request, final ActionListener<ClearScrollResponse> listener) { logger.debug("clear scroll request {}", request); try { RequestUriBuilder uriBuilder = new RequestUriBuilder() .addEndpoint("_search/scroll"); uriBuilder.addQueryParameter("scroll_id", Strings.collectionToCommaDelimitedString(request.getScrollIds())); httpClient.getHttpClient().submit(HttpClientRequest.createDelete(uriBuilder.toString())) .flatMap(HANDLES_404) .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ClearScrollResponse>>() { @Override public Observable<ClearScrollResponse> call(final HttpClientResponse<ByteBuf> response) { return response.getContent().flatMap(new Func1<ByteBuf, Observable<ClearScrollResponse>>() { @Override public Observable<ClearScrollResponse> call(ByteBuf byteBuf) { return ClearScrollResponse.parse(response.getStatus().code()); } }); } }) .single() .subscribe(new ListenerCompleterObserver<>(listener)); } catch (Exception e) { listener.onFailure(e); } }
public void testParseClearScrollRequest() throws Exception { XContentParser content = createParser(XContentFactory.jsonBuilder() .startObject() .array("scroll_id", "value_1", "value_2") .endObject()); ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); RestClearScrollAction.buildFromContent(content, clearScrollRequest); assertThat(clearScrollRequest.scrollIds(), contains("value_1", "value_2")); }
public void testParseClearScrollRequestWithUnknownParamThrowsException() throws Exception { XContentParser invalidContent = createParser(XContentFactory.jsonBuilder() .startObject() .array("scroll_id", "value_1", "value_2") .field("unknown", "keyword") .endObject()); ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); Exception e = expectThrows(IllegalArgumentException.class, () -> RestClearScrollAction.buildFromContent(invalidContent, clearScrollRequest)); assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]")); }
public void testSetsParentId() { TaskId[] parentTaskId = new TaskId[] {new TaskId(randomAsciiOfLength(3), randomLong())}; // This mock will do nothing but verify that parentTaskId is set on all requests sent to it. NoOpClient mock = new NoOpClient(getTestName()) { @Override protected < Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder> > void doExecute( Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) { assertEquals(parentTaskId[0], request.getParentTask()); super.doExecute(action, request, listener); } }; try (ParentTaskAssigningClient client = new ParentTaskAssigningClient(mock, parentTaskId[0])) { // All of these should have the parentTaskId set client.bulk(new BulkRequest()); client.search(new SearchRequest()); client.clearScroll(new ClearScrollRequest()); // Now lets verify that unwrapped calls don't have the parentTaskId set parentTaskId[0] = TaskId.EMPTY_TASK_ID; client.unwrap().bulk(new BulkRequest()); client.unwrap().search(new SearchRequest()); client.unwrap().clearScroll(new ClearScrollRequest()); } }
public void sendFreeContext(DiscoveryNode node, long contextId, ClearScrollRequest request, final ActionListener<SearchFreeContextResponse> listener) { transportService.sendRequest(node, FREE_CONTEXT_SCROLL_ACTION_NAME, new ScrollFreeContextRequest(request, contextId), new ActionListenerResponseHandler<SearchFreeContextResponse>(listener) { @Override public SearchFreeContextResponse newInstance() { return new SearchFreeContextResponse(); } }); }
public void sendClearAllScrollContexts(DiscoveryNode node, ClearScrollRequest request, final ActionListener<TransportResponse> listener) { transportService.sendRequest(node, CLEAR_SCROLL_CONTEXTS_ACTION_NAME, new ClearScrollContextsRequest(request), new ActionListenerResponseHandler<TransportResponse>(listener) { @Override public TransportResponse newInstance() { return TransportResponse.Empty.INSTANCE; } }); }
@After public void clear_scrolls() { if(!scrollIds.isEmpty()) { ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.scrollIds(scrollIds); transportClient.clearScroll(clearScrollRequest).actionGet(); scrollIds.clear(); } }
@Test public void should_clear_scroll() throws IOException, ExecutionException, InterruptedException { SearchRequest searchRequest = new SearchRequest(THE_INDEX).types(THE_TYPE).source(new SearchSourceBuilder().query(matchAllQuery())).scroll("1m"); SearchResponse searchResponse = httpClient.search(searchRequest).get(); String scrollId = searchResponse.getScrollId(); Assertions.assertThat(scrollId).isNotEmpty(); ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId(scrollId); ClearScrollResponse response = httpClient.clearScroll(clearScrollRequest).get(); Assertions.assertThat(response.isSucceeded()).isTrue(); }
@Test public void should_return_failed_if_scroll_does_not_exists() throws IOException, ExecutionException, InterruptedException { ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId("cXVlcnlUaGVuRmV0Y2g7NTs2OmZXdGUtMmVkVEpteHBDa2RFSVNoZGc7ODpmV3RlLTJlZFRKbXhwQ2tkRUlTaGRnOzk6Zld0ZS0yZWRUSm14cENrZEVJU2hkZzs3OmZXdGUtMmVkVEpteHBDa2RFSVNoZGc7MTA6Zld0ZS0yZWRUSm14cENrZEVJU2hkZzswOw"); ClearScrollResponse response = httpClient.clearScroll(clearScrollRequest).get(); Assertions.assertThat(response.isSucceeded()).isFalse(); }
ActionResponse execute(final ActionRequest request) throws IOException { if (request instanceof BulkRequest) { return bulk((BulkRequest) request); } else if (request instanceof IndexRequest) { return index((IndexRequest) request); } else if (request instanceof DeleteRequest) { return delete((DeleteRequest) request); } else if (request instanceof ClearScrollRequest) { return clearScroll((ClearScrollRequest) request); } else if (request instanceof SearchScrollRequest) { return searchScroll((SearchScrollRequest) request); } return null; }
@Override public void closeNow() { final String scrollId = _searchResponse.getScrollId(); final ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId(scrollId); try { _client.execute(clearScrollRequest); } catch (IOException e) { logger.warn("Could not clear scroll.", e); } }
@Override public void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener) { execute(ClearScrollAction.INSTANCE, request, listener); }
@Override public ActionFuture<ClearScrollResponse> clearScroll(ClearScrollRequest request) { return execute(ClearScrollAction.INSTANCE, request); }
ScrollFreeContextRequest(ClearScrollRequest request, long id) { this((TransportRequest) request, id); }
@Override public void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener) { // TODO Auto-generated method stub }
@Override public ActionFuture<ClearScrollResponse> clearScroll(ClearScrollRequest request) { // TODO Auto-generated method stub return null; }
public void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener) { clearScrollActionHandler.execute(request, listener); }
public Future<ClearScrollResponse> clearScroll(ClearScrollRequest request) { PlainActionFuture<ClearScrollResponse> future = PlainActionFuture.newFuture(); clearScroll(request, future); return future; }
/** * Clears the search contexts associated with specified scroll ids. */ ActionFuture<ClearScrollResponse> clearScroll(ClearScrollRequest request);
/** * Clears the search contexts associated with specified scroll ids. */ void clearScroll(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener);