Skip to content

Commit 4b8c8f8

Browse files
committed
Make GET /_cat/segments cancellable (#69020)
A small followup to #67413 and #68965: the underlying actions of the `GET /_cat/segments` API are now cancellable, so we may as well cancel them if needed.
1 parent f9eb059 commit 4b8c8f8

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesSegmentsRestCancellationIT.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ protected boolean addMockInternalEngine() {
6464
return false;
6565
}
6666

67-
public void testClusterStateRestCancellation() throws Exception {
67+
public void testIndicesSegmentsRestCancellation() throws Exception {
68+
runTest(new Request(HttpGet.METHOD_NAME, "/_segments"));
69+
}
70+
71+
public void testCatSegmentsRestCancellation() throws Exception {
72+
runTest(new Request(HttpGet.METHOD_NAME, "/_cat/segments"));
73+
}
74+
75+
private void runTest(Request request) throws Exception {
6876

6977
createIndex("test", Settings.builder().put(BLOCK_SEARCHER_SETTING.getKey(), true).build());
7078
ensureGreen("test");
@@ -89,11 +97,9 @@ public void testClusterStateRestCancellation() throws Exception {
8997
releasables.add(searcherBlock::release);
9098
}
9199

92-
final Request indicesSegments = new Request(HttpGet.METHOD_NAME, "/_segments");
93-
94100
final PlainActionFuture<Void> future = new PlainActionFuture<>();
95101
logger.info("--> sending indices segments request");
96-
final Cancellable cancellable = getRestClient().performRequestAsync(indicesSegments, new ResponseListener() {
102+
final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() {
97103
@Override
98104
public void onSuccess(Response response) {
99105
future.onResponse(null);

server/src/main/java/org/elasticsearch/rest/action/cat/RestSegmentsAction.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.elasticsearch.rest.RestRequest;
2424
import org.elasticsearch.rest.RestResponse;
2525
import org.elasticsearch.rest.action.RestActionListener;
26+
import org.elasticsearch.rest.action.RestCancellableNodeClient;
2627
import org.elasticsearch.rest.action.RestResponseListener;
28+
import org.elasticsearch.tasks.TaskCancelledException;
2729

2830
import java.util.List;
2931
import java.util.Map;
@@ -60,14 +62,19 @@ protected RestChannelConsumer doCatRequest(final RestRequest request, final Node
6062
clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
6163
clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices);
6264

63-
return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
65+
final RestCancellableNodeClient cancelClient = new RestCancellableNodeClient(client, request.getHttpChannel());
66+
67+
return channel -> cancelClient.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
6468
@Override
6569
public void processResponse(final ClusterStateResponse clusterStateResponse) {
6670
final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest();
6771
indicesSegmentsRequest.indices(indices);
68-
client.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {
72+
cancelClient.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {
6973
@Override
7074
public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentResponse) throws Exception {
75+
if (request.getHttpChannel().isOpen() == false) {
76+
throw new TaskCancelledException("response channel [" + request.getHttpChannel() + "] closed");
77+
}
7178
final Map<String, IndexSegments> indicesSegments = indicesSegmentResponse.getIndices();
7279
Table tab = buildTable(request, clusterStateResponse, indicesSegments);
7380
return RestTable.buildResponse(tab, channel);

0 commit comments

Comments
 (0)