Skip to content

Commit 1d3db4c

Browse files
committed
making RestGetSampleStatsAction cancellable
1 parent 863e510 commit 1d3db4c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/sampling/GetSampleStatsAction.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@
2222
import org.elasticsearch.common.io.stream.StreamInput;
2323
import org.elasticsearch.common.io.stream.StreamOutput;
2424
import org.elasticsearch.common.io.stream.Writeable;
25+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2526
import org.elasticsearch.ingest.SamplingService;
2627
import org.elasticsearch.tasks.CancellableTask;
2728
import org.elasticsearch.tasks.Task;
2829
import org.elasticsearch.tasks.TaskId;
2930
import org.elasticsearch.transport.AbstractTransportRequest;
30-
import org.elasticsearch.xcontent.ToXContentObject;
31-
import org.elasticsearch.xcontent.XContentBuilder;
31+
import org.elasticsearch.xcontent.ToXContent;
3232

3333
import java.io.IOException;
34+
import java.util.Iterator;
3435
import java.util.List;
3536
import java.util.Map;
3637
import java.util.Objects;
3738

39+
import static org.elasticsearch.common.xcontent.ChunkedToXContentHelper.chunk;
40+
3841
public class GetSampleStatsAction extends ActionType<GetSampleStatsAction.Response> {
3942

4043
public static final GetSampleStatsAction INSTANCE = new GetSampleStatsAction();
@@ -131,7 +134,7 @@ public int hashCode() {
131134
}
132135
}
133136

134-
public static class Response extends BaseNodesResponse<GetSampleStatsAction.NodeResponse> implements Writeable, ToXContentObject {
137+
public static class Response extends BaseNodesResponse<GetSampleStatsAction.NodeResponse> implements Writeable, ChunkedToXContent {
135138
final int maxSize;
136139

137140
public Response(StreamInput in) throws IOException {
@@ -192,8 +195,8 @@ public int hashCode() {
192195
}
193196

194197
@Override
195-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
196-
return getSampleStats().toXContent(builder, params);
198+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
199+
return chunk(getSampleStats());
197200
}
198201
}
199202

server/src/main/java/org/elasticsearch/action/admin/indices/sampling/RestGetSampleStatsAction.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import org.elasticsearch.client.internal.node.NodeClient;
1414
import org.elasticsearch.rest.BaseRestHandler;
1515
import org.elasticsearch.rest.RestRequest;
16-
import org.elasticsearch.rest.action.RestToXContentListener;
16+
import org.elasticsearch.rest.action.RestCancellableNodeClient;
17+
import org.elasticsearch.rest.action.RestRefCountedChunkedToXContentListener;
1718

1819
import java.io.IOException;
1920
import java.util.Arrays;
@@ -44,6 +45,10 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
4445
);
4546
}
4647
GetSampleStatsAction.Request getSampleStatsRequest = new GetSampleStatsAction.Request(indexNames[0]);
47-
return channel -> client.execute(GetSampleStatsAction.INSTANCE, getSampleStatsRequest, new RestToXContentListener<>(channel));
48+
return channel -> new RestCancellableNodeClient(client, request.getHttpChannel()).execute(
49+
GetSampleStatsAction.INSTANCE,
50+
getSampleStatsRequest,
51+
new RestRefCountedChunkedToXContentListener<>(channel)
52+
);
4853
}
4954
}

0 commit comments

Comments
 (0)