Skip to content

Commit c00640f

Browse files
author
Andrey Ershov
committed
Use GetSnapshotsResponse.Response in GroupedActionListener
1 parent 845ed5f commit c00640f

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.IOException;
3535
import java.util.ArrayList;
36+
import java.util.Collection;
3637
import java.util.Collections;
3738
import java.util.HashMap;
3839
import java.util.List;
@@ -53,17 +54,13 @@ public class GetSnapshotsResponse extends ActionResponse implements ToXContentOb
5354
(p, c) -> Response.fromXContent(p), new ParseField("responses"));
5455
}
5556

56-
public GetSnapshotsResponse(Map<String, List<SnapshotInfo>> successfulResponses, Map<String, ElasticsearchException> failedResponses) {
57-
this.successfulResponses = successfulResponses;
58-
this.failedResponses = failedResponses;
59-
}
6057

61-
private static class Response {
62-
String repository;
63-
List<SnapshotInfo> snapshots;
64-
ElasticsearchException error;
58+
public static class Response {
59+
private String repository;
60+
private List<SnapshotInfo> snapshots;
61+
private ElasticsearchException error;
6562

66-
static final ConstructingObjectParser<Response, Void> RESPONSE_PARSER =
63+
private static final ConstructingObjectParser<Response, Void> RESPONSE_PARSER =
6764
new ConstructingObjectParser<>(Response.class.getName(), true,
6865
(args) -> new Response((String) args[0],
6966
(List<SnapshotInfo>) args[1], (ElasticsearchException) args[2]));
@@ -76,21 +73,29 @@ private static class Response {
7673
(p, c) -> ElasticsearchException.fromXContent(p), new ParseField("error"));
7774
}
7875

79-
Response(String repository, List<SnapshotInfo> snapshots, ElasticsearchException error) {
76+
private Response(String repository, List<SnapshotInfo> snapshots, ElasticsearchException error) {
8077
this.repository = repository;
8178
this.snapshots = snapshots;
8279
this.error = error;
8380
}
8481

85-
static Response fromXContent(XContentParser parser) throws IOException {
82+
public static Response snapshots(String repository, List<SnapshotInfo> snapshots) {
83+
return new Response(repository, snapshots, null);
84+
}
85+
86+
public static Response error(String repository, ElasticsearchException error) {
87+
return new Response(repository, null, error);
88+
}
89+
90+
private static Response fromXContent(XContentParser parser) throws IOException {
8691
return RESPONSE_PARSER.parse(parser, null);
8792
}
8893
}
8994

9095
private Map<String, List<SnapshotInfo>> successfulResponses = Collections.emptyMap();
9196
private Map<String, ElasticsearchException> failedResponses = Collections.emptyMap();
9297

93-
private GetSnapshotsResponse(List<Response> responses) {
98+
public GetSnapshotsResponse(Collection<Response> responses) {
9499
this.successfulResponses = new HashMap<>();
95100
this.failedResponses = new HashMap<>();
96101
for (Response response : responses) {

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,42 +106,22 @@ protected void masterOperation(final GetSnapshotsRequest request, final ClusterS
106106

107107
private void getMultipleReposSnapshotInfo(List<RepositoryMetaData> repos, String[] snapshots, boolean ignoreUnavailable,
108108
boolean verbose, ActionListener<GetSnapshotsResponse> listener) {
109-
GroupedActionListener<Tuple<String, Tuple<List<SnapshotInfo>, ElasticsearchException>>> groupedActionListener =
109+
final GroupedActionListener<GetSnapshotsResponse.Response> groupedActionListener =
110110
new GroupedActionListener<>(
111111
ActionListener.map(listener, responses -> {
112112
assert repos.size() == responses.size();
113-
114-
Map<String, List<SnapshotInfo>> successfulResponses = new HashMap<>();
115-
Map<String, ElasticsearchException> failedResponses = new HashMap<>();
116-
117-
Iterator<Tuple<String, Tuple<List<SnapshotInfo>, ElasticsearchException>>> it = responses.iterator();
118-
119-
while (it.hasNext()) {
120-
Tuple<String, Tuple<List<SnapshotInfo>, ElasticsearchException>> response = it.next();
121-
String repo = response.v1();
122-
Tuple<List<SnapshotInfo>, ElasticsearchException> result = response.v2();
123-
if (result.v1() != null) {
124-
assert result.v2() == null;
125-
successfulResponses.put(repo, result.v1());
126-
} else {
127-
assert result.v2() != null;
128-
failedResponses.put(repo, result.v2());
129-
}
130-
}
131-
132-
return new GetSnapshotsResponse(successfulResponses, failedResponses);
113+
return new GetSnapshotsResponse(responses);
133114
}), repos.size());
134115

135116
// run concurrently for all repos on GENERIC thread pool
136117
for (final RepositoryMetaData repo : repos) {
137118
threadPool.executor(ThreadPool.Names.GENERIC).execute(
138119
() -> {
139-
// Unfortunately, there is no Either in Java, so we use Tuple with only one value set
140120
try {
141-
groupedActionListener.onResponse(Tuple.tuple(repo.name(),
142-
Tuple.tuple(getSingleRepoSnapshotInfo(repo.name(), snapshots, ignoreUnavailable, verbose), null)));
121+
groupedActionListener.onResponse(GetSnapshotsResponse.Response.snapshots(
122+
repo.name(), getSingleRepoSnapshotInfo(repo.name(), snapshots, ignoreUnavailable, verbose)));
143123
} catch (ElasticsearchException e) {
144-
groupedActionListener.onResponse(Tuple.tuple(repo.name(), Tuple.tuple(null, e)));
124+
groupedActionListener.onResponse(GetSnapshotsResponse.Response.error(repo.name(), e));
145125
}
146126
});
147127
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.ArrayList;
3737
import java.util.Arrays;
3838
import java.util.Collections;
39-
import java.util.HashMap;
4039
import java.util.HashSet;
4140
import java.util.List;
4241
import java.util.Map;
@@ -86,22 +85,21 @@ private List<SnapshotInfo> createSnapshotInfos() {
8685

8786
private GetSnapshotsResponse createTestInstance() {
8887
Set<String> repositories = new HashSet<>();
89-
Map<String, List<SnapshotInfo>> successfulResponses = new HashMap<>();
90-
Map<String, ElasticsearchException> failedResponses = new HashMap<>();
88+
List<GetSnapshotsResponse.Response> responses = new ArrayList<>();
9189

9290
for (int i = 0; i < randomIntBetween(0, 5); i++) {
9391
String repository = randomValueOtherThanMany(r -> repositories.contains(r), () -> randomAlphaOfLength(10));
9492
repositories.add(repository);
95-
successfulResponses.put(repository, createSnapshotInfos());
93+
responses.add(GetSnapshotsResponse.Response.snapshots(repository, createSnapshotInfos()));
9694
}
9795

9896
for (int i = 0; i < randomIntBetween(0, 5); i++) {
9997
String repository = randomValueOtherThanMany(r -> repositories.contains(r), () -> randomAlphaOfLength(10));
10098
repositories.add(repository);
101-
failedResponses.put(repository, new ElasticsearchException(randomAlphaOfLength(10)));
99+
responses.add(GetSnapshotsResponse.Response.error(repository, new ElasticsearchException(randomAlphaOfLength(10))));
102100
}
103101

104-
return new GetSnapshotsResponse(successfulResponses, failedResponses);
102+
return new GetSnapshotsResponse(responses);
105103
}
106104

107105
public void testSerialization() throws IOException {

0 commit comments

Comments
 (0)