From c00640fba25712c25cbe45cd96eaad41f224f738 Mon Sep 17 00:00:00 2001 From: Andrey Ershov Date: Thu, 23 May 2019 17:44:03 -0400 Subject: [PATCH] Use GetSnapshotsResponse.Response in GroupedActionListener --- .../snapshots/get/GetSnapshotsResponse.java | 29 ++++++++++-------- .../get/TransportGetSnapshotsAction.java | 30 ++++--------------- .../get/GetSnapshotsResponseTests.java | 10 +++---- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java index 2ad49c3c2f06c..6a40cc779beb5 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -53,17 +54,13 @@ public class GetSnapshotsResponse extends ActionResponse implements ToXContentOb (p, c) -> Response.fromXContent(p), new ParseField("responses")); } - public GetSnapshotsResponse(Map> successfulResponses, Map failedResponses) { - this.successfulResponses = successfulResponses; - this.failedResponses = failedResponses; - } - private static class Response { - String repository; - List snapshots; - ElasticsearchException error; + public static class Response { + private String repository; + private List snapshots; + private ElasticsearchException error; - static final ConstructingObjectParser RESPONSE_PARSER = + private static final ConstructingObjectParser RESPONSE_PARSER = new ConstructingObjectParser<>(Response.class.getName(), true, (args) -> new Response((String) args[0], (List) args[1], (ElasticsearchException) args[2])); @@ -76,13 +73,21 @@ private static class Response { (p, c) -> ElasticsearchException.fromXContent(p), new ParseField("error")); } - Response(String repository, List snapshots, ElasticsearchException error) { + private Response(String repository, List snapshots, ElasticsearchException error) { this.repository = repository; this.snapshots = snapshots; this.error = error; } - static Response fromXContent(XContentParser parser) throws IOException { + public static Response snapshots(String repository, List snapshots) { + return new Response(repository, snapshots, null); + } + + public static Response error(String repository, ElasticsearchException error) { + return new Response(repository, null, error); + } + + private static Response fromXContent(XContentParser parser) throws IOException { return RESPONSE_PARSER.parse(parser, null); } } @@ -90,7 +95,7 @@ static Response fromXContent(XContentParser parser) throws IOException { private Map> successfulResponses = Collections.emptyMap(); private Map failedResponses = Collections.emptyMap(); - private GetSnapshotsResponse(List responses) { + public GetSnapshotsResponse(Collection responses) { this.successfulResponses = new HashMap<>(); this.failedResponses = new HashMap<>(); for (Response response : responses) { diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java index 384004016d83c..a8b4b275e2083 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java @@ -106,42 +106,22 @@ protected void masterOperation(final GetSnapshotsRequest request, final ClusterS private void getMultipleReposSnapshotInfo(List repos, String[] snapshots, boolean ignoreUnavailable, boolean verbose, ActionListener listener) { - GroupedActionListener, ElasticsearchException>>> groupedActionListener = + final GroupedActionListener groupedActionListener = new GroupedActionListener<>( ActionListener.map(listener, responses -> { assert repos.size() == responses.size(); - - Map> successfulResponses = new HashMap<>(); - Map failedResponses = new HashMap<>(); - - Iterator, ElasticsearchException>>> it = responses.iterator(); - - while (it.hasNext()) { - Tuple, ElasticsearchException>> response = it.next(); - String repo = response.v1(); - Tuple, ElasticsearchException> result = response.v2(); - if (result.v1() != null) { - assert result.v2() == null; - successfulResponses.put(repo, result.v1()); - } else { - assert result.v2() != null; - failedResponses.put(repo, result.v2()); - } - } - - return new GetSnapshotsResponse(successfulResponses, failedResponses); + return new GetSnapshotsResponse(responses); }), repos.size()); // run concurrently for all repos on GENERIC thread pool for (final RepositoryMetaData repo : repos) { threadPool.executor(ThreadPool.Names.GENERIC).execute( () -> { - // Unfortunately, there is no Either in Java, so we use Tuple with only one value set try { - groupedActionListener.onResponse(Tuple.tuple(repo.name(), - Tuple.tuple(getSingleRepoSnapshotInfo(repo.name(), snapshots, ignoreUnavailable, verbose), null))); + groupedActionListener.onResponse(GetSnapshotsResponse.Response.snapshots( + repo.name(), getSingleRepoSnapshotInfo(repo.name(), snapshots, ignoreUnavailable, verbose))); } catch (ElasticsearchException e) { - groupedActionListener.onResponse(Tuple.tuple(repo.name(), Tuple.tuple(null, e))); + groupedActionListener.onResponse(GetSnapshotsResponse.Response.error(repo.name(), e)); } }); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java index 5a70f7bf1ee96..2f2dd3fa0d1db 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java @@ -36,7 +36,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -86,22 +85,21 @@ private List createSnapshotInfos() { private GetSnapshotsResponse createTestInstance() { Set repositories = new HashSet<>(); - Map> successfulResponses = new HashMap<>(); - Map failedResponses = new HashMap<>(); + List responses = new ArrayList<>(); for (int i = 0; i < randomIntBetween(0, 5); i++) { String repository = randomValueOtherThanMany(r -> repositories.contains(r), () -> randomAlphaOfLength(10)); repositories.add(repository); - successfulResponses.put(repository, createSnapshotInfos()); + responses.add(GetSnapshotsResponse.Response.snapshots(repository, createSnapshotInfos())); } for (int i = 0; i < randomIntBetween(0, 5); i++) { String repository = randomValueOtherThanMany(r -> repositories.contains(r), () -> randomAlphaOfLength(10)); repositories.add(repository); - failedResponses.put(repository, new ElasticsearchException(randomAlphaOfLength(10))); + responses.add(GetSnapshotsResponse.Response.error(repository, new ElasticsearchException(randomAlphaOfLength(10)))); } - return new GetSnapshotsResponse(successfulResponses, failedResponses); + return new GetSnapshotsResponse(responses); } public void testSerialization() throws IOException {