Move IndicesAliasesRequest#concreteAliases to TransportIndicesAliasesAction (#24400)

This method has to do with how the transport action may or may not resolve wildcards expressions to aliases names. It is only needed in TransportIndicesAliasesAction and for this reason it should be a private method in it rather than part of a request class which is also part of the Java API and later in the high level REST client.
This commit is contained in:
Luca Cavanna 2017-05-01 19:59:06 +02:00 committed by GitHub
parent 38273709b5
commit 91fbb0ba28
3 changed files with 34 additions and 33 deletions

View File

@ -19,19 +19,15 @@
package org.elasticsearch.action.admin.indices.alias; package org.elasticsearch.action.admin.indices.alias;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.AliasesRequest; import org.elasticsearch.action.AliasesRequest;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.cluster.metadata.AliasAction; import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
@ -106,18 +102,21 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
public static AliasActions add() { public static AliasActions add() {
return new AliasActions(AliasActions.Type.ADD); return new AliasActions(AliasActions.Type.ADD);
} }
/** /**
* Build a new {@link AliasAction} to remove aliases. * Build a new {@link AliasAction} to remove aliases.
*/ */
public static AliasActions remove() { public static AliasActions remove() {
return new AliasActions(AliasActions.Type.REMOVE); return new AliasActions(AliasActions.Type.REMOVE);
} }
/** /**
* Build a new {@link AliasAction} to remove aliases. * Build a new {@link AliasAction} to remove an index.
*/ */
public static AliasActions removeIndex() { public static AliasActions removeIndex() {
return new AliasActions(AliasActions.Type.REMOVE_INDEX); return new AliasActions(AliasActions.Type.REMOVE_INDEX);
} }
private static ObjectParser<AliasActions, Void> parser(String name, Supplier<AliasActions> supplier) { private static ObjectParser<AliasActions, Void> parser(String name, Supplier<AliasActions> supplier) {
ObjectParser<AliasActions, Void> parser = new ObjectParser<>(name, supplier); ObjectParser<AliasActions, Void> parser = new ObjectParser<>(name, supplier);
parser.declareString((action, index) -> { parser.declareString((action, index) -> {
@ -402,24 +401,6 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
return INDICES_OPTIONS; return INDICES_OPTIONS;
} }
public String[] concreteAliases(MetaData metaData, String concreteIndex) {
if (expandAliasesWildcards()) {
//for DELETE we expand the aliases
String[] indexAsArray = {concreteIndex};
ImmutableOpenMap<String, List<AliasMetaData>> aliasMetaData = metaData.findAliases(aliases, indexAsArray);
List<String> finalAliases = new ArrayList<>();
for (ObjectCursor<List<AliasMetaData>> curAliases : aliasMetaData.values()) {
for (AliasMetaData aliasMeta: curAliases.value) {
finalAliases.add(aliasMeta.alias());
}
}
return finalAliases.toArray(new String[finalAliases.size()]);
} else {
//for add we just return the current aliases
return aliases;
}
}
@Override @Override
public String toString() { public String toString() {
return "AliasActions[" return "AliasActions["

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.indices.alias; package org.elasticsearch.action.admin.indices.alias;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.ActionFilters;
@ -28,9 +29,12 @@ import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.AliasAction; import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.MetaDataIndexAliasesService; import org.elasticsearch.cluster.metadata.MetaDataIndexAliasesService;
import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException; import org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException;
@ -75,9 +79,7 @@ public class TransportIndicesAliasesAction extends TransportMasterNodeAction<Ind
protected ClusterBlockException checkBlock(IndicesAliasesRequest request, ClusterState state) { protected ClusterBlockException checkBlock(IndicesAliasesRequest request, ClusterState state) {
Set<String> indices = new HashSet<>(); Set<String> indices = new HashSet<>();
for (AliasActions aliasAction : request.aliasActions()) { for (AliasActions aliasAction : request.aliasActions()) {
for (String index : aliasAction.indices()) { Collections.addAll(indices, aliasAction.indices());
indices.add(index);
}
} }
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, indices.toArray(new String[indices.size()])); return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, indices.toArray(new String[indices.size()]));
} }
@ -97,12 +99,12 @@ public class TransportIndicesAliasesAction extends TransportMasterNodeAction<Ind
for (String index : concreteIndices) { for (String index : concreteIndices) {
switch (action.actionType()) { switch (action.actionType()) {
case ADD: case ADD:
for (String alias : action.concreteAliases(state.metaData(), index)) { for (String alias : concreteAliases(action, state.metaData(), index)) {
finalActions.add(new AliasAction.Add(index, alias, action.filter(), action.indexRouting(), action.searchRouting())); finalActions.add(new AliasAction.Add(index, alias, action.filter(), action.indexRouting(), action.searchRouting()));
} }
break; break;
case REMOVE: case REMOVE:
for (String alias : action.concreteAliases(state.metaData(), index)) { for (String alias : concreteAliases(action, state.metaData(), index)) {
finalActions.add(new AliasAction.Remove(index, alias)); finalActions.add(new AliasAction.Remove(index, alias));
} }
break; break;
@ -134,4 +136,22 @@ public class TransportIndicesAliasesAction extends TransportMasterNodeAction<Ind
} }
}); });
} }
private static String[] concreteAliases(AliasActions action, MetaData metaData, String concreteIndex) {
if (action.expandAliasesWildcards()) {
//for DELETE we expand the aliases
String[] indexAsArray = {concreteIndex};
ImmutableOpenMap<String, List<AliasMetaData>> aliasMetaData = metaData.findAliases(action.aliases(), indexAsArray);
List<String> finalAliases = new ArrayList<>();
for (ObjectCursor<List<AliasMetaData>> curAliases : aliasMetaData.values()) {
for (AliasMetaData aliasMeta: curAliases.value) {
finalAliases.add(aliasMeta.alias());
}
}
return finalAliases.toArray(new String[finalAliases.size()]);
} else {
//for ADD and REMOVE_INDEX we just return the current aliases
return action.aliases();
}
}
} }

View File

@ -1,5 +1,5 @@
--- ---
"Remove and index and replace it with an alias": "Remove an index and replace it with an alias":
- do: - do:
indices.create: indices.create: