Skip to content

Commit bf5d9a5

Browse files
committed
Responses can use Writeable.Reader interface (#34655)
In order to remove Streamable from the codebase, Response objects need to be read using the Writeable.Reader interface which this change enables. This change enables the use of Writeable.Reader by adding the `Action#getResponseReader` method. The default implementation simply uses the existing `newResponse` method and the readFrom method. As responses are migrated to the Writeable.Reader interface, Action classes can be updated to throw an UnsupportedOperationException when `newResponse` is called and override the `getResponseReader` method. Relates #34389
1 parent f2280c4 commit bf5d9a5

File tree

55 files changed

+507
-347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+507
-347
lines changed

modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4ScheduledPingTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.elasticsearch.cluster.node.DiscoveryNode;
2222
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
23+
import org.elasticsearch.common.io.stream.StreamInput;
2324
import org.elasticsearch.common.lease.Releasables;
2425
import org.elasticsearch.common.network.NetworkService;
2526
import org.elasticsearch.common.settings.Settings;
@@ -103,7 +104,7 @@ public void testScheduledPing() throws Exception {
103104
TransportRequest.Empty.INSTANCE, TransportRequestOptions.builder().withCompress(randomBoolean()).build(),
104105
new TransportResponseHandler<TransportResponse.Empty>() {
105106
@Override
106-
public TransportResponse.Empty newInstance() {
107+
public TransportResponse.Empty read(StreamInput in) {
107108
return TransportResponse.Empty.INSTANCE;
108109
}
109110

server/src/main/java/org/elasticsearch/action/ActionListenerResponseHandler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
package org.elasticsearch.action;
2121

22+
import org.elasticsearch.common.io.stream.StreamInput;
23+
import org.elasticsearch.common.io.stream.Writeable;
2224
import org.elasticsearch.threadpool.ThreadPool;
2325
import org.elasticsearch.transport.TransportResponseHandler;
2426
import org.elasticsearch.transport.TransportException;
2527
import org.elasticsearch.transport.TransportResponse;
2628

29+
import java.io.IOException;
2730
import java.util.Objects;
28-
import java.util.function.Supplier;
2931

3032
/**
3133
* A simple base class for action response listeners, defaulting to using the SAME executor (as its
@@ -34,11 +36,11 @@
3436
public class ActionListenerResponseHandler<Response extends TransportResponse> implements TransportResponseHandler<Response> {
3537

3638
private final ActionListener<? super Response> listener;
37-
private final Supplier<Response> responseSupplier;
39+
private final Writeable.Reader<Response> reader;
3840

39-
public ActionListenerResponseHandler(ActionListener<? super Response> listener, Supplier<Response> responseSupplier) {
41+
public ActionListenerResponseHandler(ActionListener<? super Response> listener, Writeable.Reader<Response> reader) {
4042
this.listener = Objects.requireNonNull(listener);
41-
this.responseSupplier = Objects.requireNonNull(responseSupplier);
43+
this.reader = Objects.requireNonNull(reader);
4244
}
4345

4446
@Override
@@ -52,12 +54,12 @@ public void handleException(TransportException e) {
5254
}
5355

5456
@Override
55-
public Response newInstance() {
56-
return responseSupplier.get();
57+
public String executor() {
58+
return ThreadPool.Names.SAME;
5759
}
5860

5961
@Override
60-
public String executor() {
61-
return ThreadPool.Names.SAME;
62+
public Response read(StreamInput in) throws IOException {
63+
return reader.read(in);
6264
}
6365
}

server/src/main/java/org/elasticsearch/action/ActionResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
*/
3131
public abstract class ActionResponse extends TransportResponse {
3232

33+
public ActionResponse() {
34+
}
35+
36+
public ActionResponse(StreamInput in) throws IOException {
37+
super(in);
38+
}
39+
3340
@Override
3441
public void readFrom(StreamInput in) throws IOException {
3542
super.readFrom(in);

server/src/main/java/org/elasticsearch/action/GenericAction.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action;
2121

22+
import org.elasticsearch.common.io.stream.Writeable;
2223
import org.elasticsearch.common.settings.Settings;
2324
import org.elasticsearch.transport.TransportRequestOptions;
2425

@@ -45,9 +46,23 @@ public String name() {
4546

4647
/**
4748
* Creates a new response instance.
49+
* @deprecated Implement {@link #getResponseReader()} instead and make this method throw an
50+
* {@link UnsupportedOperationException}
4851
*/
52+
@Deprecated
4953
public abstract Response newResponse();
5054

55+
/**
56+
* Get a reader that can create a new instance of the class from a {@link org.elasticsearch.common.io.stream.StreamInput}
57+
*/
58+
public Writeable.Reader<Response> getResponseReader() {
59+
return in -> {
60+
Response response = newResponse();
61+
response.readFrom(in);
62+
return response;
63+
};
64+
}
65+
5166
/**
5267
* Optional request options for the action.
5368
*/

server/src/main/java/org/elasticsearch/action/TransportActionNodeProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public void execute(final DiscoveryNode node, final Request request, final Actio
4848
return;
4949
}
5050
transportService.sendRequest(node, action.name(), request, transportOptions,
51-
new ActionListenerResponseHandler<>(listener, action::newResponse));
51+
new ActionListenerResponseHandler<>(listener, action.getResponseReader()));
5252
}
5353
}

server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/get/TransportGetTaskAction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.cluster.node.DiscoveryNode;
3333
import org.elasticsearch.cluster.service.ClusterService;
3434
import org.elasticsearch.common.inject.Inject;
35+
import org.elasticsearch.common.io.stream.StreamInput;
3536
import org.elasticsearch.common.settings.Settings;
3637
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
3738
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@@ -124,8 +125,10 @@ private void runOnNodeWithTaskIfPossible(Task thisTask, GetTaskRequest request,
124125
transportService.sendRequest(node, GetTaskAction.NAME, nodeRequest, builder.build(),
125126
new TransportResponseHandler<GetTaskResponse>() {
126127
@Override
127-
public GetTaskResponse newInstance() {
128-
return new GetTaskResponse();
128+
public GetTaskResponse read(StreamInput in) throws IOException {
129+
GetTaskResponse response = new GetTaskResponse();
130+
response.readFrom(in);
131+
return response;
129132
}
130133

131134
@Override

server/src/main/java/org/elasticsearch/action/admin/cluster/shards/ClusterSearchShardsAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.action.Action;
2323
import org.elasticsearch.client.ElasticsearchClient;
24+
import org.elasticsearch.common.io.stream.Writeable;
2425

2526
public class ClusterSearchShardsAction extends Action<ClusterSearchShardsRequest, ClusterSearchShardsResponse, ClusterSearchShardsRequestBuilder> {
2627

@@ -33,7 +34,12 @@ private ClusterSearchShardsAction() {
3334

3435
@Override
3536
public ClusterSearchShardsResponse newResponse() {
36-
return new ClusterSearchShardsResponse();
37+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
38+
}
39+
40+
@Override
41+
public Writeable.Reader<ClusterSearchShardsResponse> getResponseReader() {
42+
return ClusterSearchShardsResponse::new;
3743
}
3844

3945
@Override

server/src/main/java/org/elasticsearch/action/admin/cluster/shards/ClusterSearchShardsResponse.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,12 @@ public class ClusterSearchShardsResponse extends ActionResponse implements ToXCo
3939
public static final ClusterSearchShardsResponse EMPTY = new ClusterSearchShardsResponse(new ClusterSearchShardsGroup[0],
4040
new DiscoveryNode[0], Collections.emptyMap());
4141

42-
private ClusterSearchShardsGroup[] groups;
43-
private DiscoveryNode[] nodes;
44-
private Map<String, AliasFilter> indicesAndFilters;
42+
private final ClusterSearchShardsGroup[] groups;
43+
private final DiscoveryNode[] nodes;
44+
private final Map<String, AliasFilter> indicesAndFilters;
4545

46-
public ClusterSearchShardsResponse() {
47-
48-
}
49-
50-
public ClusterSearchShardsResponse(ClusterSearchShardsGroup[] groups, DiscoveryNode[] nodes,
51-
Map<String, AliasFilter> indicesAndFilters) {
52-
this.groups = groups;
53-
this.nodes = nodes;
54-
this.indicesAndFilters = indicesAndFilters;
55-
}
56-
57-
public ClusterSearchShardsGroup[] getGroups() {
58-
return groups;
59-
}
60-
61-
public DiscoveryNode[] getNodes() {
62-
return nodes;
63-
}
64-
65-
public Map<String, AliasFilter> getIndicesAndFilters() {
66-
return indicesAndFilters;
67-
}
68-
69-
@Override
70-
public void readFrom(StreamInput in) throws IOException {
71-
super.readFrom(in);
46+
public ClusterSearchShardsResponse(StreamInput in) throws IOException {
47+
super(in);
7248
groups = new ClusterSearchShardsGroup[in.readVInt()];
7349
for (int i = 0; i < groups.length; i++) {
7450
groups[i] = ClusterSearchShardsGroup.readSearchShardsGroupResponse(in);
@@ -85,9 +61,16 @@ public void readFrom(StreamInput in) throws IOException {
8561
AliasFilter aliasFilter = new AliasFilter(in);
8662
indicesAndFilters.put(index, aliasFilter);
8763
}
64+
} else {
65+
indicesAndFilters = null;
8866
}
8967
}
9068

69+
@Override
70+
public void readFrom(StreamInput in) throws IOException {
71+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
72+
}
73+
9174
@Override
9275
public void writeTo(StreamOutput out) throws IOException {
9376
super.writeTo(out);
@@ -108,6 +91,25 @@ public void writeTo(StreamOutput out) throws IOException {
10891
}
10992
}
11093

94+
public ClusterSearchShardsResponse(ClusterSearchShardsGroup[] groups, DiscoveryNode[] nodes,
95+
Map<String, AliasFilter> indicesAndFilters) {
96+
this.groups = groups;
97+
this.nodes = nodes;
98+
this.indicesAndFilters = indicesAndFilters;
99+
}
100+
101+
public ClusterSearchShardsGroup[] getGroups() {
102+
return groups;
103+
}
104+
105+
public DiscoveryNode[] getNodes() {
106+
return nodes;
107+
}
108+
109+
public Map<String, AliasFilter> getIndicesAndFilters() {
110+
return indicesAndFilters;
111+
}
112+
111113
@Override
112114
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
113115
builder.startObject();

server/src/main/java/org/elasticsearch/action/admin/cluster/shards/TransportClusterSearchShardsAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
import org.elasticsearch.cluster.routing.ShardRouting;
3333
import org.elasticsearch.cluster.service.ClusterService;
3434
import org.elasticsearch.common.inject.Inject;
35+
import org.elasticsearch.common.io.stream.StreamInput;
3536
import org.elasticsearch.common.settings.Settings;
3637
import org.elasticsearch.index.shard.ShardId;
3738
import org.elasticsearch.indices.IndicesService;
3839
import org.elasticsearch.search.internal.AliasFilter;
3940
import org.elasticsearch.threadpool.ThreadPool;
4041
import org.elasticsearch.transport.TransportService;
4142

43+
import java.io.IOException;
4244
import java.util.HashMap;
4345
import java.util.HashSet;
4446
import java.util.Map;
@@ -72,7 +74,12 @@ protected ClusterBlockException checkBlock(ClusterSearchShardsRequest request, C
7274

7375
@Override
7476
protected ClusterSearchShardsResponse newResponse() {
75-
return new ClusterSearchShardsResponse();
77+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
78+
}
79+
80+
@Override
81+
protected ClusterSearchShardsResponse read(StreamInput in) throws IOException {
82+
return new ClusterSearchShardsResponse(in);
7683
}
7784

7885
@Override

server/src/main/java/org/elasticsearch/action/ingest/IngestActionForwarder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IngestActionForwarder(TransportService transportService) {
4949

5050
public void forwardIngestRequest(Action<?, ?, ?> action, ActionRequest request, ActionListener<?> listener) {
5151
transportService.sendRequest(randomIngestNode(), action.name(), request,
52-
new ActionListenerResponseHandler(listener, action::newResponse));
52+
new ActionListenerResponseHandler(listener, action.getResponseReader()));
5353
}
5454

5555
private DiscoveryNode randomIngestNode() {

server/src/main/java/org/elasticsearch/action/resync/TransportResyncReplicationAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.cluster.routing.ShardRouting;
3333
import org.elasticsearch.cluster.service.ClusterService;
3434
import org.elasticsearch.common.inject.Inject;
35+
import org.elasticsearch.common.io.stream.StreamInput;
3536
import org.elasticsearch.common.settings.Settings;
3637
import org.elasticsearch.index.engine.Engine;
3738
import org.elasticsearch.index.seqno.SequenceNumbers;
@@ -45,6 +46,7 @@
4546
import org.elasticsearch.transport.TransportResponseHandler;
4647
import org.elasticsearch.transport.TransportService;
4748

49+
import java.io.IOException;
4850
import java.util.function.Consumer;
4951
import java.util.function.Supplier;
5052

@@ -151,8 +153,10 @@ public void sync(ResyncReplicationRequest request, Task parentTask, String prima
151153
transportOptions,
152154
new TransportResponseHandler<ResyncReplicationResponse>() {
153155
@Override
154-
public ResyncReplicationResponse newInstance() {
155-
return newResponseInstance();
156+
public ResyncReplicationResponse read(StreamInput in) throws IOException {
157+
ResyncReplicationResponse response = newResponseInstance();
158+
response.readFrom(in);
159+
return response;
156160
}
157161

158162
@Override

server/src/main/java/org/elasticsearch/action/search/MultiSearchResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public MultiSearchResponse(Item[] items) {
133133
this.items = items;
134134
}
135135

136+
MultiSearchResponse(StreamInput in) throws IOException {
137+
readFrom(in);
138+
}
139+
136140
@Override
137141
public Iterator<Item> iterator() {
138142
return Arrays.stream(items).iterator();

0 commit comments

Comments
 (0)