-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Make list-tasks API cancellable #96283
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
Merged
DaveCTurner
merged 8 commits into
elastic:main
from
DaveCTurner:2023-05-23-ListTasks-cancellable
May 30, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e63d87f
Make list-tasks API cancellable
DaveCTurner 38b7942
Fix affected tests
DaveCTurner 82cf365
Mocks 👿
DaveCTurner b4701c8
Let request create task
DaveCTurner 8ea3205
Spotless
DaveCTurner aeac395
Fix REST test
DaveCTurner dd15059
Merge branch 'main' into 2023-05-23-ListTasks-cancellable
DaveCTurner 59016e0
Merge branch 'main' into 2023-05-23-ListTasks-cancellable
DaveCTurner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...javaRestTest/java/org/elasticsearch/action/support/tasks/RestListTasksCancellationIT.java
This file contains hidden or 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,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.action.support.tasks; | ||
|
||
import org.apache.http.client.methods.HttpGet; | ||
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction; | ||
import org.elasticsearch.action.admin.cluster.state.ClusterStateAction; | ||
import org.elasticsearch.action.support.PlainActionFuture; | ||
import org.elasticsearch.client.Cancellable; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.http.HttpSmokeTestCase; | ||
import org.elasticsearch.tasks.TaskManager; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; | ||
import static org.elasticsearch.test.TaskAssertions.awaitTaskWithPrefix; | ||
|
||
public class RestListTasksCancellationIT extends HttpSmokeTestCase { | ||
|
||
public void testListTasksCancellation() throws Exception { | ||
final Request clusterStateRequest = new Request(HttpGet.METHOD_NAME, "/_cluster/state"); | ||
clusterStateRequest.addParameter("wait_for_metadata_version", Long.toString(Long.MAX_VALUE)); | ||
clusterStateRequest.addParameter("wait_for_timeout", "1h"); | ||
|
||
final PlainActionFuture<Response> clusterStateFuture = new PlainActionFuture<>(); | ||
final Cancellable clusterStateCancellable = getRestClient().performRequestAsync( | ||
clusterStateRequest, | ||
wrapAsRestResponseListener(clusterStateFuture) | ||
); | ||
|
||
awaitTaskWithPrefix(ClusterStateAction.NAME); | ||
|
||
final Request tasksRequest = new Request(HttpGet.METHOD_NAME, "/_tasks"); | ||
tasksRequest.addParameter("actions", ClusterStateAction.NAME); | ||
tasksRequest.addParameter("wait_for_completion", Boolean.toString(true)); | ||
tasksRequest.addParameter("timeout", "1h"); | ||
|
||
final PlainActionFuture<Response> tasksFuture = new PlainActionFuture<>(); | ||
final Cancellable tasksCancellable = getRestClient().performRequestAsync(tasksRequest, wrapAsRestResponseListener(tasksFuture)); | ||
|
||
awaitTaskWithPrefix(ListTasksAction.NAME + "[n]"); | ||
|
||
tasksCancellable.cancel(); | ||
|
||
final var taskManagers = new ArrayList<TaskManager>(internalCluster().getNodeNames().length); | ||
for (final var transportService : internalCluster().getInstances(TransportService.class)) { | ||
taskManagers.add(transportService.getTaskManager()); | ||
} | ||
assertBusy( | ||
() -> assertFalse( | ||
taskManagers.stream() | ||
.flatMap(taskManager -> taskManager.getCancellableTasks().values().stream()) | ||
.anyMatch(t -> t.getAction().startsWith(ListTasksAction.NAME)) | ||
) | ||
); | ||
|
||
expectThrows(CancellationException.class, () -> tasksFuture.actionGet(10, TimeUnit.SECONDS)); | ||
clusterStateCancellable.cancel(); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's pity that
internalCluster().getInstances
returnsIterable
, but it's actually a list. Otherwise, we could perform a check just with the Streams API.