diff --git a/server/src/main/java/org/elasticsearch/action/AliasesRequest.java b/server/src/main/java/org/elasticsearch/action/AliasesRequest.java index e85f38c777b77..bf7ceb28d5023 100644 --- a/server/src/main/java/org/elasticsearch/action/AliasesRequest.java +++ b/server/src/main/java/org/elasticsearch/action/AliasesRequest.java @@ -33,9 +33,9 @@ public interface AliasesRequest extends IndicesRequest.Replaceable { String[] aliases(); /** - * Replaces the aliases that the action relates to + * Replaces current aliases with the provided aliases. * - * This is an internal method. + * Sometimes aliases expressions need to be resolved to concrete aliases prior to executing the transport action. */ void replaceAliases(String... aliases); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java index 3961311f1e269..728033c39375e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java @@ -98,9 +98,9 @@ public void replaceAliases(String... aliases) { } /** - * Returns aliases originally specified by the user + * Returns the aliases as was originally specified by the user */ - String[] getOriginalAliases() { + public String[] getOriginalAliases() { return originalAliases; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java index 078c7ec1e5257..1bacd652ee734 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java @@ -27,7 +27,6 @@ import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -64,17 +63,24 @@ protected GetAliasesResponse newResponse() { @Override protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener listener) { String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request); - ImmutableOpenMap> result = state.metaData().findAliases(request.aliases(), concreteIndices); + ImmutableOpenMap> aliases = state.metaData().findAliases(request.aliases(), concreteIndices); + listener.onResponse(new GetAliasesResponse(postProcess(request, concreteIndices, aliases))); + } - // in case all aliases are requested then it is desired to return the concrete index with no aliases (#25114): - boolean aliasesProvided = Strings.isAllOrWildcard(request.getOriginalAliases()); - ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(result); + /** + * Fills alias result with empty entries for requested indices when no specific aliases were requested. + */ + static ImmutableOpenMap> postProcess(GetAliasesRequest request, String[] concreteIndices, + ImmutableOpenMap> aliases) { + boolean noAliasesSpecified = request.getOriginalAliases() == null || request.getOriginalAliases().length == 0; + ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(aliases); for (String index : concreteIndices) { - if (result.get(index) == null && aliasesProvided) { - mapBuilder.put(index, Collections.emptyList()); + if (aliases.get(index) == null && noAliasesSpecified) { + List previous = mapBuilder.put(index, Collections.emptyList()); + assert previous == null; } } - - listener.onResponse(new GetAliasesResponse(mapBuilder.build())); + return mapBuilder.build(); } + } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 632ea9e1f861d..4ed2adc9a1c9f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -263,7 +263,7 @@ public ImmutableOpenMap> findAliases(final String[] return ImmutableOpenMap.of(); } - boolean matchAllAliases = Strings.isAllOrWildcard(aliases); + boolean matchAllAliases = matchAllAliases(aliases); ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(); for (String index : concreteIndices) { IndexMetaData indexMetaData = indices.get(index); @@ -284,6 +284,15 @@ public ImmutableOpenMap> findAliases(final String[] return mapBuilder.build(); } + private static boolean matchAllAliases(final String[] aliases) { + for (String alias : aliases) { + if (alias.equals(ALL)) { + return true; + } + } + return aliases.length == 0; + } + /** * Checks if at least one of the specified aliases exists in the specified concrete indices. Wildcards are supported in the * alias names for partial matches. diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java index 8a1e4e74e819e..0d6d46e95b602 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java @@ -77,6 +77,10 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + // The TransportGetAliasesAction was improved do the same post processing as is happening here. + // We can't remove this logic yet to support mixed clusters. We should be able to remove this logic here + // in when 8.0 becomes the new version in the master branch. + final boolean namesProvided = request.hasParam("name"); final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name"); final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesActionTests.java new file mode 100644 index 0000000000000..d445a63aea170 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesActionTests.java @@ -0,0 +1,64 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.action.admin.indices.alias.get; + +import org.elasticsearch.cluster.metadata.AliasMetaData; +import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.test.ESTestCase; + +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.Matchers.equalTo; + +public class TransportGetAliasesActionTests extends ESTestCase { + + public void testPostProcess() { + GetAliasesRequest request = new GetAliasesRequest(); + ImmutableOpenMap> aliases = ImmutableOpenMap.>builder() + .fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build())) + .build(); + ImmutableOpenMap> result = + TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases); + assertThat(result.size(), equalTo(3)); + assertThat(result.get("a").size(), equalTo(0)); + assertThat(result.get("b").size(), equalTo(1)); + assertThat(result.get("c").size(), equalTo(0)); + + request = new GetAliasesRequest(); + request.replaceAliases("y", "z"); + aliases = ImmutableOpenMap.>builder() + .fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build())) + .build(); + result = TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases); + assertThat(result.size(), equalTo(3)); + assertThat(result.get("a").size(), equalTo(0)); + assertThat(result.get("b").size(), equalTo(1)); + assertThat(result.get("c").size(), equalTo(0)); + + request = new GetAliasesRequest("y", "z"); + aliases = ImmutableOpenMap.>builder() + .fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build())) + .build(); + result = TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases); + assertThat(result.size(), equalTo(1)); + assertThat(result.get("b").size(), equalTo(1)); + } + +}