Skip to content

Commit e441032

Browse files
author
Andrey Ershov
committed
Fix GetRepositoriesResponse streamable and get rid of rid of readFrom
method in GetSnapshotsResponse
1 parent e2f2e9a commit e441032

File tree

7 files changed

+76
-52
lines changed

7 files changed

+76
-52
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesAction.java

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

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

2425
/**
2526
* Get repositories action
@@ -35,7 +36,12 @@ private GetRepositoriesAction() {
3536

3637
@Override
3738
public GetRepositoriesResponse newResponse() {
38-
return new GetRepositoriesResponse();
39+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
40+
}
41+
42+
@Override
43+
public Writeable.Reader<GetRepositoriesResponse> getResponseReader() {
44+
return GetRepositoriesResponse::new;
3945
}
4046
}
4147

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.elasticsearch.common.xcontent.XContentParser;
3030

3131
import java.io.IOException;
32-
import java.util.Collections;
3332
import java.util.List;
3433

3534
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
@@ -41,10 +40,6 @@ public class GetRepositoriesResponse extends ActionResponse implements ToXConten
4140

4241
private final RepositoriesMetaData repositories;
4342

44-
GetRepositoriesResponse() {
45-
repositories = new RepositoriesMetaData(Collections.emptyList());
46-
}
47-
4843
GetRepositoriesResponse(RepositoriesMetaData repositories) {
4944
this.repositories = repositories;
5045
}

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
3232
import org.elasticsearch.cluster.service.ClusterService;
3333
import org.elasticsearch.common.inject.Inject;
34+
import org.elasticsearch.common.io.stream.StreamInput;
3435
import org.elasticsearch.common.regex.Regex;
3536
import org.elasticsearch.repositories.RepositoryMissingException;
3637
import org.elasticsearch.threadpool.ThreadPool;
3738
import org.elasticsearch.transport.TransportService;
3839

40+
import java.io.IOException;
3941
import java.util.ArrayList;
4042
import java.util.Collections;
4143
import java.util.LinkedHashSet;
@@ -62,7 +64,12 @@ protected String executor() {
6264

6365
@Override
6466
protected GetRepositoriesResponse newResponse() {
65-
return new GetRepositoriesResponse();
67+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
68+
}
69+
70+
@Override
71+
protected GetRepositoriesResponse read(StreamInput in) throws IOException {
72+
return new GetRepositoriesResponse(in);
6673
}
6774

6875
@Override

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsAction.java

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

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

2425
/**
2526
* Get snapshots action
@@ -35,7 +36,12 @@ private GetSnapshotsAction() {
3536

3637
@Override
3738
public GetSnapshotsResponse newResponse() {
38-
return new GetSnapshotsResponse();
39+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
40+
}
41+
42+
@Override
43+
public Writeable.Reader<GetSnapshotsResponse> getResponseReader() {
44+
return GetSnapshotsResponse::new;
3945
}
4046
}
4147

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,40 @@ public class GetSnapshotsResponse extends ActionResponse implements ToXContentOb
5555
(p, c) -> Response.fromXContent(p), new ParseField("responses"));
5656
}
5757

58+
public GetSnapshotsResponse(StreamInput in) throws IOException {
59+
if (in.getVersion().onOrAfter(GetSnapshotsRequest.MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
60+
int successfulSize = in.readVInt();
61+
Map<String, List<SnapshotInfo>> successfulResponses = new HashMap<>(successfulSize);
62+
for (int i = 0; i < successfulSize; i++) {
63+
String repository = in.readString();
64+
int size = in.readVInt();
65+
List<SnapshotInfo> snapshotInfos = new ArrayList<>(size);
66+
for (int j = 0; j < size; j++) {
67+
snapshotInfos.add(new SnapshotInfo(in));
68+
}
69+
successfulResponses.put(repository, snapshotInfos);
70+
}
71+
72+
int failedSize = in.readVInt();
73+
Map<String, ElasticsearchException> failedResponses = new HashMap<>(failedSize);
74+
for (int i = 0; i < failedSize; i++) {
75+
String repository = in.readString();
76+
ElasticsearchException error = in.readException();
77+
failedResponses.put(repository, error);
78+
}
79+
this.successfulResponses = Collections.unmodifiableMap(successfulResponses);
80+
this.failedResponses = Collections.unmodifiableMap(failedResponses);
81+
} else {
82+
int size = in.readVInt();
83+
List<SnapshotInfo> snapshots = new ArrayList<>(size);
84+
for (int i = 0; i < size; i++) {
85+
snapshots.add(new SnapshotInfo(in));
86+
}
87+
this.successfulResponses = Collections.singletonMap("unknown", snapshots);
88+
this.failedResponses = Collections.emptyMap();
89+
}
90+
}
91+
5892

5993
public static class Response {
6094
private String repository;
@@ -93,26 +127,23 @@ private static Response fromXContent(XContentParser parser) throws IOException {
93127
}
94128
}
95129

96-
private Map<String, List<SnapshotInfo>> successfulResponses = Collections.emptyMap();
97-
private Map<String, ElasticsearchException> failedResponses = Collections.emptyMap();
130+
private final Map<String, List<SnapshotInfo>> successfulResponses;
131+
private final Map<String, ElasticsearchException> failedResponses;
98132

99133
public GetSnapshotsResponse(Collection<Response> responses) {
100-
this.successfulResponses = new HashMap<>();
101-
this.failedResponses = new HashMap<>();
134+
Map<String, List<SnapshotInfo>> successfulResponses = new HashMap<>();
135+
Map<String, ElasticsearchException> failedResponses = new HashMap<>();
102136
for (Response response : responses) {
103137
if (response.snapshots != null) {
104138
assert response.error == null;
105-
this.successfulResponses.put(response.repository, response.snapshots);
139+
successfulResponses.put(response.repository, response.snapshots);
106140
} else {
107141
assert response.snapshots == null;
108-
this.failedResponses.put(response.repository, response.error);
142+
failedResponses.put(response.repository, response.error);
109143
}
110144
}
111-
successfulResponses = Collections.unmodifiableMap(successfulResponses);
112-
failedResponses = Collections.unmodifiableMap(failedResponses);
113-
}
114-
115-
public GetSnapshotsResponse() {
145+
this.successfulResponses = Collections.unmodifiableMap(successfulResponses);
146+
this.failedResponses = Collections.unmodifiableMap(failedResponses);
116147
}
117148

118149
/**
@@ -229,37 +260,7 @@ public void writeTo(StreamOutput out) throws IOException {
229260

230261
@Override
231262
public void readFrom(StreamInput in) throws IOException {
232-
super.readFrom(in);
233-
if (in.getVersion().onOrAfter(GetSnapshotsRequest.MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
234-
int successfulSize = in.readVInt();
235-
successfulResponses = new HashMap<>(successfulSize);
236-
for (int i = 0; i < successfulSize; i++) {
237-
String repository = in.readString();
238-
int size = in.readVInt();
239-
List<SnapshotInfo> snapshotInfos = new ArrayList<>(size);
240-
for (int j = 0; j < size; j++) {
241-
snapshotInfos.add(new SnapshotInfo(in));
242-
}
243-
successfulResponses.put(repository, snapshotInfos);
244-
}
245-
246-
int failedSize = in.readVInt();
247-
failedResponses = new HashMap<>(failedSize);
248-
for (int i = 0; i < failedSize; i++) {
249-
String repository = in.readString();
250-
ElasticsearchException error = in.readException();
251-
failedResponses.put(repository, error);
252-
}
253-
successfulResponses = Collections.unmodifiableMap(successfulResponses);
254-
failedResponses = Collections.unmodifiableMap(failedResponses);
255-
} else {
256-
int size = in.readVInt();
257-
List<SnapshotInfo> snapshots = new ArrayList<>(size);
258-
for (int i = 0; i < size; i++) {
259-
snapshots.add(new SnapshotInfo(in));
260-
}
261-
successfulResponses = Collections.singletonMap("unknown", snapshots);
262-
}
263+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
263264
}
264265

265266
public static GetSnapshotsResponse fromXContent(XContentParser parser) throws IOException {

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
3838
import org.elasticsearch.cluster.service.ClusterService;
3939
import org.elasticsearch.common.inject.Inject;
40+
import org.elasticsearch.common.io.stream.StreamInput;
4041
import org.elasticsearch.common.regex.Regex;
4142
import org.elasticsearch.repositories.IndexId;
4243
import org.elasticsearch.repositories.RepositoryData;
@@ -47,6 +48,7 @@
4748
import org.elasticsearch.threadpool.ThreadPool;
4849
import org.elasticsearch.transport.TransportService;
4950

51+
import java.io.IOException;
5052
import java.util.ArrayList;
5153
import java.util.Collections;
5254
import java.util.HashMap;
@@ -78,7 +80,12 @@ protected String executor() {
7880

7981
@Override
8082
protected GetSnapshotsResponse newResponse() {
81-
return new GetSnapshotsResponse();
83+
throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable");
84+
}
85+
86+
@Override
87+
protected GetSnapshotsResponse read(StreamInput in) throws IOException {
88+
return new GetSnapshotsResponse(in);
8289
}
8390

8491
@Override

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ private GetSnapshotsResponse doParseInstance(XContentParser parser) throws IOExc
5959
}
6060

6161
private GetSnapshotsResponse copyInstance(GetSnapshotsResponse instance, Version version) throws IOException {
62-
return copyStreamable(instance, new NamedWriteableRegistry(Collections.emptyList()), () -> new GetSnapshotsResponse(), version);
62+
return copyInstance(instance, new NamedWriteableRegistry(Collections.emptyList()), (out, value) -> value.writeTo(out),
63+
in -> new GetSnapshotsResponse(in), version);
64+
6365
}
6466

6567
private void assertEqualInstances(GetSnapshotsResponse expectedInstance, GetSnapshotsResponse newInstance) {

0 commit comments

Comments
 (0)