Skip to content

Commit 4eb05f4

Browse files
committed
iter
1 parent 342610c commit 4eb05f4

File tree

6 files changed

+97
-14
lines changed

6 files changed

+97
-14
lines changed

server/src/main/java/org/elasticsearch/action/AliasesRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public interface AliasesRequest extends IndicesRequest.Replaceable {
3333
String[] aliases();
3434

3535
/**
36-
* Replaces the aliases that the action relates to
36+
* Replaces current aliases with the provided aliases.
3737
*
38-
* This is an internal method.
38+
* Sometimes aliases expressions need to be resolved to concrete aliases prior to executing the transport action.
3939
*/
4040
void replaceAliases(String... aliases);
4141

server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public void replaceAliases(String... aliases) {
9898
}
9999

100100
/**
101-
* Returns aliases originally specified by the user
101+
* Returns the aliases as was originally specified by the user
102102
*/
103-
String[] getOriginalAliases() {
103+
public String[] getOriginalAliases() {
104104
return originalAliases;
105105
}
106106

server/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.cluster.metadata.AliasMetaData;
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2929
import org.elasticsearch.cluster.service.ClusterService;
30-
import org.elasticsearch.common.Strings;
3130
import org.elasticsearch.common.collect.ImmutableOpenMap;
3231
import org.elasticsearch.common.inject.Inject;
3332
import org.elasticsearch.common.settings.Settings;
@@ -64,17 +63,24 @@ protected GetAliasesResponse newResponse() {
6463
@Override
6564
protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<GetAliasesResponse> listener) {
6665
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request);
67-
ImmutableOpenMap<String, List<AliasMetaData>> result = state.metaData().findAliases(request.aliases(), concreteIndices);
66+
ImmutableOpenMap<String, List<AliasMetaData>> aliases = state.metaData().findAliases(request.aliases(), concreteIndices);
67+
listener.onResponse(new GetAliasesResponse(postProcess(request, concreteIndices, aliases)));
68+
}
6869

69-
// in case all aliases are requested then it is desired to return the concrete index with no aliases (#25114):
70-
boolean aliasesProvided = Strings.isAllOrWildcard(request.getOriginalAliases());
71-
ImmutableOpenMap.Builder<String, List<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder(result);
70+
/**
71+
* Fills alias result with empty entries for requested indices when no specific aliases were requested.
72+
*/
73+
static ImmutableOpenMap<String, List<AliasMetaData>> postProcess(GetAliasesRequest request, String[] concreteIndices,
74+
ImmutableOpenMap<String, List<AliasMetaData>> aliases) {
75+
boolean noAliasesSpecified = request.getOriginalAliases() == null || request.getOriginalAliases().length == 0;
76+
ImmutableOpenMap.Builder<String, List<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder(aliases);
7277
for (String index : concreteIndices) {
73-
if (result.get(index) == null && aliasesProvided) {
74-
mapBuilder.put(index, Collections.emptyList());
78+
if (aliases.get(index) == null && noAliasesSpecified) {
79+
List<AliasMetaData> previous = mapBuilder.put(index, Collections.emptyList());
80+
assert previous == null;
7581
}
7682
}
77-
78-
listener.onResponse(new GetAliasesResponse(mapBuilder.build()));
83+
return mapBuilder.build();
7984
}
85+
8086
}

server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[]
263263
return ImmutableOpenMap.of();
264264
}
265265

266-
boolean matchAllAliases = Strings.isAllOrWildcard(aliases);
266+
boolean matchAllAliases = matchAllAliases(aliases);
267267
ImmutableOpenMap.Builder<String, List<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder();
268268
for (String index : concreteIndices) {
269269
IndexMetaData indexMetaData = indices.get(index);
@@ -284,6 +284,15 @@ public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[]
284284
return mapBuilder.build();
285285
}
286286

287+
private static boolean matchAllAliases(final String[] aliases) {
288+
for (String alias : aliases) {
289+
if (alias.equals(ALL)) {
290+
return true;
291+
}
292+
}
293+
return aliases.length == 0;
294+
}
295+
287296
/**
288297
* Checks if at least one of the specified aliases exists in the specified concrete indices. Wildcards are supported in the
289298
* alias names for partial matches.

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public String getName() {
7777

7878
@Override
7979
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
80+
// The TransportGetAliasesAction was improved do the same post processing as is happening here.
81+
// We can't remove this logic yet to support mixed clusters. We should be able to remove this logic here
82+
// in when 8.0 becomes the new version in the master branch.
83+
8084
final boolean namesProvided = request.hasParam("name");
8185
final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
8286
final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.action.admin.indices.alias.get;
20+
21+
import org.elasticsearch.cluster.metadata.AliasMetaData;
22+
import org.elasticsearch.common.collect.ImmutableOpenMap;
23+
import org.elasticsearch.test.ESTestCase;
24+
25+
import java.util.Collections;
26+
import java.util.List;
27+
28+
import static org.hamcrest.Matchers.equalTo;
29+
30+
public class TransportGetAliasesActionTests extends ESTestCase {
31+
32+
public void testPostProcess() {
33+
GetAliasesRequest request = new GetAliasesRequest();
34+
ImmutableOpenMap<String, List<AliasMetaData>> aliases = ImmutableOpenMap.<String, List<AliasMetaData>>builder()
35+
.fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build()))
36+
.build();
37+
ImmutableOpenMap<String, List<AliasMetaData>> result =
38+
TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases);
39+
assertThat(result.size(), equalTo(3));
40+
assertThat(result.get("a").size(), equalTo(0));
41+
assertThat(result.get("b").size(), equalTo(1));
42+
assertThat(result.get("c").size(), equalTo(0));
43+
44+
request = new GetAliasesRequest();
45+
request.replaceAliases("y", "z");
46+
aliases = ImmutableOpenMap.<String, List<AliasMetaData>>builder()
47+
.fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build()))
48+
.build();
49+
result = TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases);
50+
assertThat(result.size(), equalTo(3));
51+
assertThat(result.get("a").size(), equalTo(0));
52+
assertThat(result.get("b").size(), equalTo(1));
53+
assertThat(result.get("c").size(), equalTo(0));
54+
55+
request = new GetAliasesRequest("y", "z");
56+
aliases = ImmutableOpenMap.<String, List<AliasMetaData>>builder()
57+
.fPut("b", Collections.singletonList(new AliasMetaData.Builder("y").build()))
58+
.build();
59+
result = TransportGetAliasesAction.postProcess(request, new String[]{"a", "b", "c"}, aliases);
60+
assertThat(result.size(), equalTo(1));
61+
assertThat(result.get("b").size(), equalTo(1));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)