Skip to content

Commit f2a558d

Browse files
authored
Cut over SearchResponse and SearchTemplateResponse to Writeable (#41855)
Relates to #34389
1 parent 932c2b6 commit f2a558d

File tree

14 files changed

+99
-67
lines changed

14 files changed

+99
-67
lines changed

client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/NoopSearchAction.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,23 @@
2020

2121
import org.elasticsearch.action.Action;
2222
import org.elasticsearch.action.search.SearchResponse;
23+
import org.elasticsearch.common.io.stream.Writeable;
2324

2425
public class NoopSearchAction extends Action<SearchResponse> {
2526
public static final NoopSearchAction INSTANCE = new NoopSearchAction();
2627
public static final String NAME = "mock:data/read/search";
2728

28-
public NoopSearchAction() {
29+
private NoopSearchAction() {
2930
super(NAME);
3031
}
3132

3233
@Override
3334
public SearchResponse newResponse() {
34-
return new SearchResponse();
35+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
36+
}
37+
38+
@Override
39+
public Writeable.Reader<SearchResponse> getResponseReader() {
40+
return SearchResponse::new;
3541
}
3642
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public static class Item implements Writeable {
4949

5050
private Item(StreamInput in) throws IOException {
5151
if (in.readBoolean()) {
52-
this.response = new SearchTemplateResponse();
53-
response.readFrom(in);
52+
this.response = new SearchTemplateResponse(in);
5453
this.exception = null;
5554
} else {
5655
exception = in.readException();

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.script.mustache;
2121

2222
import org.elasticsearch.action.Action;
23+
import org.elasticsearch.common.io.stream.Writeable;
2324

2425
public class SearchTemplateAction extends Action<SearchTemplateResponse> {
2526

@@ -32,6 +33,11 @@ private SearchTemplateAction() {
3233

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

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public class SearchTemplateResponse extends ActionResponse implements StatusToXC
4848
SearchTemplateResponse() {
4949
}
5050

51+
SearchTemplateResponse(StreamInput in) throws IOException {
52+
super(in);
53+
source = in.readOptionalBytesReference();
54+
response = in.readOptionalWriteable(SearchResponse::new);
55+
}
56+
5157
public BytesReference getSource() {
5258
return source;
5359
}
@@ -81,10 +87,8 @@ public void writeTo(StreamOutput out) throws IOException {
8187
}
8288

8389
@Override
84-
public void readFrom(StreamInput in) throws IOException {
85-
super.readFrom(in);
86-
source = in.readOptionalBytesReference();
87-
response = in.readOptionalStreamable(SearchResponse::new);
90+
public void readFrom(StreamInput in) {
91+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
8892
}
8993

9094
public static SearchTemplateResponse fromXContent(XContentParser parser) throws IOException {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public Item(SearchResponse response, Exception exception) {
7070

7171
Item(StreamInput in) throws IOException{
7272
if (in.readBoolean()) {
73-
this.response = new SearchResponse();
74-
this.response.readFrom(in);
73+
this.response = new SearchResponse(in);
7574
this.exception = null;
7675
} else {
7776
this.exception = in.readException();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.search;
2121

2222
import org.elasticsearch.action.Action;
23+
import org.elasticsearch.common.io.stream.Writeable;
2324

2425
public class SearchAction extends Action<SearchResponse> {
2526

@@ -32,6 +33,11 @@ private SearchAction() {
3233

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

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

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,33 @@ public class SearchResponse extends ActionResponse implements StatusToXContentOb
6666
private static final ParseField TERMINATED_EARLY = new ParseField("terminated_early");
6767
private static final ParseField NUM_REDUCE_PHASES = new ParseField("num_reduce_phases");
6868

69-
private SearchResponseSections internalResponse;
70-
71-
private String scrollId;
72-
73-
private int totalShards;
74-
75-
private int successfulShards;
76-
77-
private int skippedShards;
78-
79-
private ShardSearchFailure[] shardFailures;
80-
81-
private Clusters clusters;
82-
83-
private long tookInMillis;
84-
85-
public SearchResponse() {
69+
private final SearchResponseSections internalResponse;
70+
private final String scrollId;
71+
private final int totalShards;
72+
private final int successfulShards;
73+
private final int skippedShards;
74+
private final ShardSearchFailure[] shardFailures;
75+
private final Clusters clusters;
76+
private final long tookInMillis;
77+
78+
public SearchResponse(StreamInput in) throws IOException {
79+
super(in);
80+
internalResponse = new InternalSearchResponse(in);
81+
totalShards = in.readVInt();
82+
successfulShards = in.readVInt();
83+
int size = in.readVInt();
84+
if (size == 0) {
85+
shardFailures = ShardSearchFailure.EMPTY_ARRAY;
86+
} else {
87+
shardFailures = new ShardSearchFailure[size];
88+
for (int i = 0; i < shardFailures.length; i++) {
89+
shardFailures[i] = readShardSearchFailure(in);
90+
}
91+
}
92+
clusters = new Clusters(in);
93+
scrollId = in.readOptionalString();
94+
tookInMillis = in.readVLong();
95+
skippedShards = in.readVInt();
8696
}
8797

8898
public SearchResponse(SearchResponseSections internalResponse, String scrollId, int totalShards, int successfulShards,
@@ -193,10 +203,6 @@ public String getScrollId() {
193203
return scrollId;
194204
}
195205

196-
public void scrollId(String scrollId) {
197-
this.scrollId = scrollId;
198-
}
199-
200206
/**
201207
* If profiling was enabled, this returns an object containing the profile results from
202208
* each shard. If profiling was not enabled, this will return null
@@ -355,24 +361,8 @@ static SearchResponse innerFromXContent(XContentParser parser) throws IOExceptio
355361
}
356362

357363
@Override
358-
public void readFrom(StreamInput in) throws IOException {
359-
super.readFrom(in);
360-
internalResponse = new InternalSearchResponse(in);
361-
totalShards = in.readVInt();
362-
successfulShards = in.readVInt();
363-
int size = in.readVInt();
364-
if (size == 0) {
365-
shardFailures = ShardSearchFailure.EMPTY_ARRAY;
366-
} else {
367-
shardFailures = new ShardSearchFailure[size];
368-
for (int i = 0; i < shardFailures.length; i++) {
369-
shardFailures[i] = readShardSearchFailure(in);
370-
}
371-
}
372-
clusters = new Clusters(in);
373-
scrollId = in.readOptionalString();
374-
tookInMillis = in.readVLong();
375-
skippedShards = in.readVInt();
364+
public void readFrom(StreamInput in) {
365+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
376366
}
377367

378368
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.search;
2121

2222
import org.elasticsearch.action.Action;
23+
import org.elasticsearch.common.io.stream.Writeable;
2324

2425
public class SearchScrollAction extends Action<SearchResponse> {
2526

@@ -32,6 +33,11 @@ private SearchScrollAction() {
3233

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

server/src/test/java/org/elasticsearch/action/search/MultiSearchActionTookTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.common.UUIDs;
3333
import org.elasticsearch.common.settings.Settings;
3434
import org.elasticsearch.common.util.concurrent.AtomicArray;
35+
import org.elasticsearch.search.internal.InternalSearchResponse;
3536
import org.elasticsearch.tasks.Task;
3637
import org.elasticsearch.tasks.TaskManager;
3738
import org.elasticsearch.test.ESTestCase;
@@ -154,7 +155,8 @@ public void search(final SearchRequest request, final ActionListener<SearchRespo
154155
requests.add(request);
155156
commonExecutor.execute(() -> {
156157
counter.decrementAndGet();
157-
listener.onResponse(new SearchResponse());
158+
listener.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, 0, 0L,
159+
ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY));
158160
});
159161
}
160162
};

server/src/test/java/org/elasticsearch/action/search/SearchAsyncActionTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.index.shard.ShardId;
3333
import org.elasticsearch.search.SearchPhaseResult;
3434
import org.elasticsearch.search.internal.AliasFilter;
35+
import org.elasticsearch.search.internal.InternalSearchResponse;
3536
import org.elasticsearch.test.ESTestCase;
3637
import org.elasticsearch.transport.Transport;
3738
import org.elasticsearch.transport.TransportException;
@@ -406,13 +407,17 @@ static GroupShardsIterator<SearchShardIterator> getShardsIter(String index, Orig
406407
}
407408

408409
public static class TestSearchResponse extends SearchResponse {
409-
public final Set<ShardId> queried = new HashSet<>();
410+
final Set<ShardId> queried = new HashSet<>();
411+
412+
TestSearchResponse() {
413+
super(InternalSearchResponse.empty(), null, 0, 0, 0, 0L, ShardSearchFailure.EMPTY_ARRAY, Clusters.EMPTY);
414+
}
410415
}
411416

412417
public static class TestSearchPhaseResult extends SearchPhaseResult {
413418
final DiscoveryNode node;
414419

415-
public TestSearchPhaseResult(long id, DiscoveryNode node) {
420+
TestSearchPhaseResult(long id, DiscoveryNode node) {
416421
this.requestId = id;
417422
this.node = node;
418423
}
@@ -427,7 +432,7 @@ public static final class MockConnection implements Transport.Connection {
427432

428433
private final DiscoveryNode node;
429434

430-
public MockConnection(DiscoveryNode node) {
435+
MockConnection(DiscoveryNode node) {
431436
this.node = node;
432437
}
433438

@@ -438,7 +443,7 @@ public DiscoveryNode getNode() {
438443

439444
@Override
440445
public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options)
441-
throws IOException, TransportException {
446+
throws TransportException {
442447
throw new UnsupportedOperationException();
443448
}
444449

0 commit comments

Comments
 (0)