|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.datastreams; |
| 11 | + |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.client.methods.HttpPost; |
| 14 | +import org.elasticsearch.action.datastreams.GetDataStreamAction; |
| 15 | +import org.elasticsearch.action.support.CancellableActionTestPlugin; |
| 16 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 17 | +import org.elasticsearch.action.support.RefCountingListener; |
| 18 | +import org.elasticsearch.action.support.SubscribableListener; |
| 19 | +import org.elasticsearch.client.Request; |
| 20 | +import org.elasticsearch.client.Response; |
| 21 | +import org.elasticsearch.common.Strings; |
| 22 | +import org.elasticsearch.common.network.NetworkModule; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.plugins.Plugin; |
| 25 | +import org.elasticsearch.rest.root.MainRestPlugin; |
| 26 | +import org.elasticsearch.test.ESIntegTestCase; |
| 27 | +import org.elasticsearch.test.rest.ObjectPath; |
| 28 | +import org.elasticsearch.transport.netty4.Netty4Plugin; |
| 29 | + |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | +import java.util.concurrent.CancellationException; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; |
| 37 | +import static org.elasticsearch.test.TaskAssertions.assertAllTasksHaveFinished; |
| 38 | +import static org.hamcrest.Matchers.greaterThan; |
| 39 | +import static org.hamcrest.Matchers.oneOf; |
| 40 | + |
| 41 | +public class DataStreamRestActionCancellationIT extends ESIntegTestCase { |
| 42 | + |
| 43 | + @Override |
| 44 | + protected boolean addMockHttpTransport() { |
| 45 | + return false; // enable http |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 50 | + return Settings.builder() |
| 51 | + .put(super.nodeSettings(nodeOrdinal, otherSettings)) |
| 52 | + .put(NetworkModule.TRANSPORT_TYPE_KEY, Netty4Plugin.NETTY_TRANSPORT_NAME) |
| 53 | + .put(NetworkModule.HTTP_TYPE_KEY, Netty4Plugin.NETTY_HTTP_TRANSPORT_NAME) |
| 54 | + .build(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 59 | + return List.of(getTestTransportPlugin(), MainRestPlugin.class, CancellableActionTestPlugin.class, DataStreamsPlugin.class); |
| 60 | + } |
| 61 | + |
| 62 | + public void testGetDataStreamCancellation() { |
| 63 | + runRestActionCancellationTest(new Request(HttpGet.METHOD_NAME, "/_data_stream"), GetDataStreamAction.NAME); |
| 64 | + runRestActionCancellationTest(new Request(HttpGet.METHOD_NAME, "/_data_stream?verbose"), GetDataStreamAction.NAME); |
| 65 | + } |
| 66 | + |
| 67 | + private void runRestActionCancellationTest(Request request, String actionName) { |
| 68 | + final var node = usually() ? internalCluster().getRandomNodeName() : internalCluster().startCoordinatingOnlyNode(Settings.EMPTY); |
| 69 | + |
| 70 | + try ( |
| 71 | + var restClient = createRestClient(node); |
| 72 | + var capturingAction = CancellableActionTestPlugin.capturingActionOnNode(actionName, node) |
| 73 | + ) { |
| 74 | + final var responseFuture = new PlainActionFuture<Response>(); |
| 75 | + final var restInvocation = restClient.performRequestAsync(request, wrapAsRestResponseListener(responseFuture)); |
| 76 | + |
| 77 | + if (randomBoolean()) { |
| 78 | + // cancel by aborting the REST request |
| 79 | + capturingAction.captureAndCancel(restInvocation::cancel); |
| 80 | + expectThrows(ExecutionException.class, CancellationException.class, () -> responseFuture.get(10, TimeUnit.SECONDS)); |
| 81 | + } else { |
| 82 | + // cancel via the task management API |
| 83 | + final var cancelFuture = new PlainActionFuture<Void>(); |
| 84 | + capturingAction.captureAndCancel( |
| 85 | + () -> SubscribableListener |
| 86 | + |
| 87 | + .<ObjectPath>newForked( |
| 88 | + l -> restClient.performRequestAsync( |
| 89 | + getListTasksRequest(node, actionName), |
| 90 | + wrapAsRestResponseListener(l.map(ObjectPath::createFromResponse)) |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + .<Void>andThen((l, listTasksResponse) -> { |
| 95 | + final var taskCount = listTasksResponse.evaluateArraySize("tasks"); |
| 96 | + assertThat(taskCount, greaterThan(0)); |
| 97 | + try (var listeners = new RefCountingListener(l)) { |
| 98 | + for (int i = 0; i < taskCount; i++) { |
| 99 | + final var taskPrefix = "tasks." + i + "."; |
| 100 | + assertTrue(listTasksResponse.evaluate(taskPrefix + "cancellable")); |
| 101 | + assertFalse(listTasksResponse.evaluate(taskPrefix + "cancelled")); |
| 102 | + restClient.performRequestAsync( |
| 103 | + getCancelTaskRequest( |
| 104 | + listTasksResponse.evaluate(taskPrefix + "node"), |
| 105 | + listTasksResponse.evaluate(taskPrefix + "id") |
| 106 | + ), |
| 107 | + wrapAsRestResponseListener(listeners.acquire(DataStreamRestActionCancellationIT::assertOK)) |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + .addListener(cancelFuture) |
| 114 | + ); |
| 115 | + cancelFuture.get(10, TimeUnit.SECONDS); |
| 116 | + expectThrows(Exception.class, () -> responseFuture.get(10, TimeUnit.SECONDS)); |
| 117 | + } |
| 118 | + |
| 119 | + assertAllTasksHaveFinished(actionName); |
| 120 | + } catch (Exception e) { |
| 121 | + fail(e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static Request getListTasksRequest(String taskNode, String actionName) { |
| 126 | + final var listTasksRequest = new Request(HttpGet.METHOD_NAME, "/_tasks"); |
| 127 | + listTasksRequest.addParameter("nodes", taskNode); |
| 128 | + listTasksRequest.addParameter("actions", actionName); |
| 129 | + listTasksRequest.addParameter("group_by", "none"); |
| 130 | + return listTasksRequest; |
| 131 | + } |
| 132 | + |
| 133 | + private static Request getCancelTaskRequest(String taskNode, int taskId) { |
| 134 | + final var cancelTaskRequest = new Request(HttpPost.METHOD_NAME, Strings.format("/_tasks/%s:%d/_cancel", taskNode, taskId)); |
| 135 | + cancelTaskRequest.addParameter("wait_for_completion", null); |
| 136 | + return cancelTaskRequest; |
| 137 | + } |
| 138 | + |
| 139 | + public static void assertOK(Response response) { |
| 140 | + assertThat(response.getStatusLine().getStatusCode(), oneOf(200, 201)); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments