mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-25 14:26:27 +00:00
Internal: add AliasesRequest interface to mark requests that manage aliases
We currently have the IndicesRequest interface to mark indices related requests and be able to retrieve the indices they relate to in a generic way. This commit introduces a similar abstraction for requests that manage aliases, to be able to retrieve/replace the aliases they relate to. Also, IndicesAliasesRequest becomes a CompositeIndicesRequest, as it allows to perform multiple operations (e.g. add/remote multiple aliases). Each single operation (AliasActions) implements now the newly introduced AliasesRequest. AliasesRequest is also implemented by GetAliasesRequest, which allows to retrieve aliases information. Closes #9460
This commit is contained in:
parent
896e8657ea
commit
74c7b5a197
44
src/main/java/org/elasticsearch/action/AliasesRequest.java
Normal file
44
src/main/java/org/elasticsearch/action/AliasesRequest.java
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Needs to be implemented by all {@link org.elasticsearch.action.ActionRequest} subclasses that relate to
|
||||
* one or more indices and one or more aliases. Meant to be used for aliases management requests (e.g. add/remove alias,
|
||||
* get aliases) that hold aliases and indices in separate fields.
|
||||
* Allows to retrieve which indices and aliases the action relates to.
|
||||
*/
|
||||
public interface AliasesRequest extends IndicesRequest.Replaceable {
|
||||
|
||||
/**
|
||||
* Returns the array of aliases that the action relates to
|
||||
*/
|
||||
String[] aliases();
|
||||
|
||||
/**
|
||||
* Sets the array of aliases that the action relates to
|
||||
*/
|
||||
AliasesRequest aliases(String[] aliases);
|
||||
|
||||
/**
|
||||
* Returns true if wildcards expressions among aliases should be resolved, false otherwise
|
||||
*/
|
||||
boolean expandAliasesWildcards();
|
||||
}
|
@ -22,8 +22,9 @@ package org.elasticsearch.action.admin.indices.alias;
|
||||
import com.carrotsearch.hppc.cursors.ObjectCursor;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.AliasesRequest;
|
||||
import org.elasticsearch.action.CompositeIndicesRequest;
|
||||
import org.elasticsearch.action.IndicesRequest;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.action.support.master.AcknowledgedRequest;
|
||||
@ -39,7 +40,10 @@ import org.elasticsearch.common.util.CollectionUtils;
|
||||
import org.elasticsearch.index.query.FilterBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.action.ValidateActions.addValidationError;
|
||||
import static org.elasticsearch.cluster.metadata.AliasAction.readAliasAction;
|
||||
@ -47,11 +51,13 @@ import static org.elasticsearch.cluster.metadata.AliasAction.readAliasAction;
|
||||
/**
|
||||
* A request to add/remove aliases for one or more indices.
|
||||
*/
|
||||
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest {
|
||||
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements CompositeIndicesRequest {
|
||||
|
||||
private List<AliasActions> allAliasActions = Lists.newArrayList();
|
||||
|
||||
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, false);
|
||||
|
||||
//indices options that require every specified index to exist, expand wildcards only to open indices and
|
||||
//don't allow that no indices are resolved from wildcard expressions
|
||||
private static final IndicesOptions INDICES_OPTIONS = IndicesOptions.fromOptions(false, false, true, false);
|
||||
|
||||
public IndicesAliasesRequest() {
|
||||
|
||||
@ -64,11 +70,11 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
|
||||
* holds the AliasAction and in addition the arrays or alias names and
|
||||
* indices that is later used to create the final AliasAction instances.
|
||||
*/
|
||||
public static class AliasActions {
|
||||
public static class AliasActions implements AliasesRequest {
|
||||
private String[] indices = Strings.EMPTY_ARRAY;
|
||||
private String[] aliases = Strings.EMPTY_ARRAY;
|
||||
private AliasAction aliasAction;
|
||||
|
||||
|
||||
public AliasActions(AliasAction.Type type, String[] indices, String[] aliases) {
|
||||
aliasAction = new AliasAction(type);
|
||||
indices(indices);
|
||||
@ -132,38 +138,46 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
|
||||
aliasAction.filter(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void indices(String... indices) {
|
||||
if (indices == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("indices must not be null");
|
||||
}
|
||||
List<String> finalIndices = new ArrayList<>();
|
||||
for (String index : indices) {
|
||||
if (index != null) {
|
||||
finalIndices.add(index);
|
||||
}
|
||||
}
|
||||
this.indices = finalIndices.toArray(new String[finalIndices.size()]);
|
||||
|
||||
@Override
|
||||
public AliasActions indices(String... indices) {
|
||||
this.indices = indices;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void aliases(String... aliases) {
|
||||
|
||||
@Override
|
||||
public AliasActions aliases(String... aliases) {
|
||||
this.aliases = aliases;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String[] aliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean expandAliasesWildcards() {
|
||||
//remove operations support wildcards among aliases, add operations don't
|
||||
return aliasAction.actionType() == Type.REMOVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] indices() {
|
||||
return indices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndicesOptions indicesOptions() {
|
||||
return INDICES_OPTIONS;
|
||||
}
|
||||
|
||||
public AliasAction aliasAction() {
|
||||
return aliasAction;
|
||||
}
|
||||
|
||||
public String[] concreteAliases(MetaData metaData, String concreteIndex) {
|
||||
if (aliasAction.actionType() == Type.REMOVE) {
|
||||
if (expandAliasesWildcards()) {
|
||||
//for DELETE we expand the aliases
|
||||
String[] indexAsArray = {concreteIndex};
|
||||
ImmutableOpenMap<String, ImmutableList<AliasMetaData>> aliasMetaData = metaData.findAliases(aliases, indexAsArray);
|
||||
@ -295,32 +309,21 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(aliasAction.indices)) {
|
||||
if (CollectionUtils.isEmpty(aliasAction.indices)) {
|
||||
validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
|
||||
+ "]: Property [index] was either missing or null", validationException);
|
||||
} else {
|
||||
for (String index : aliasAction.indices) {
|
||||
if (!Strings.hasText(index)) {
|
||||
validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
|
||||
+ "]: [index] may not be empty string", validationException);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
|
||||
+ "]: Property [index] was either missing or null", validationException);
|
||||
}
|
||||
}
|
||||
return validationException;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] indices() {
|
||||
List<String> indices = Lists.newArrayList();
|
||||
for (AliasActions aliasActions : aliasActions()) {
|
||||
if (!CollectionUtils.isEmpty(aliasActions.indices())) {
|
||||
Collections.addAll(indices, aliasActions.indices);
|
||||
}
|
||||
}
|
||||
return indices.toArray(new String[indices.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(StreamInput in) throws IOException {
|
||||
super.readFrom(in);
|
||||
@ -341,14 +344,17 @@ public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesReq
|
||||
writeTimeout(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndicesOptions indicesOptions() {
|
||||
return indicesOptions;
|
||||
return INDICES_OPTIONS;
|
||||
}
|
||||
|
||||
private AliasActions readAliasActions(StreamInput in) throws IOException {
|
||||
private static AliasActions readAliasActions(StreamInput in) throws IOException {
|
||||
AliasActions actions = new AliasActions();
|
||||
return actions.readFrom(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends IndicesRequest> subRequests() {
|
||||
return allAliasActions;
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class IndicesAliasesRequestBuilder extends AcknowledgedRequestBuilder<Ind
|
||||
/**
|
||||
* Adds an alias to the index.
|
||||
*
|
||||
* @param index The indices
|
||||
* @param indices The indices
|
||||
* @param alias The alias
|
||||
*/
|
||||
public IndicesAliasesRequestBuilder addAlias(String[] indices, String alias) {
|
||||
@ -184,13 +184,10 @@ public class IndicesAliasesRequestBuilder extends AcknowledgedRequestBuilder<Ind
|
||||
/**
|
||||
* Adds an alias action to the request.
|
||||
*
|
||||
* @param aliasAction The alias action
|
||||
* @param action The alias action
|
||||
*/
|
||||
public IndicesAliasesRequestBuilder addAliasAction(
|
||||
AliasActions action) {
|
||||
public IndicesAliasesRequestBuilder addAliasAction(AliasActions action) {
|
||||
request.addAliasAction(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -38,10 +38,7 @@ import org.elasticsearch.rest.action.admin.indices.alias.delete.AliasesMissingEx
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.transport.TransportService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Add/remove aliases action
|
||||
@ -96,9 +93,7 @@ public class TransportIndicesAliasesAction extends TransportMasterNodeOperationA
|
||||
//expand indices
|
||||
String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), action.indices());
|
||||
//collect the aliases
|
||||
for (String alias : action.aliases()) {
|
||||
aliases.add(alias);
|
||||
}
|
||||
Collections.addAll(aliases, action.aliases());
|
||||
for (String index : concreteIndices) {
|
||||
for (String alias : action.concreteAliases(state.metaData(), index)) {
|
||||
AliasAction finalAction = new AliasAction(action.aliasAction());
|
||||
|
@ -20,7 +20,7 @@ package org.elasticsearch.action.admin.indices.alias.get;
|
||||
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.action.ActionRequestValidationException;
|
||||
import org.elasticsearch.action.IndicesRequest;
|
||||
import org.elasticsearch.action.AliasesRequest;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
|
||||
import org.elasticsearch.common.Strings;
|
||||
@ -31,7 +31,7 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> implements IndicesRequest.Replaceable {
|
||||
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> implements AliasesRequest {
|
||||
|
||||
private String[] indices = Strings.EMPTY_ARRAY;
|
||||
private String[] aliases = Strings.EMPTY_ARRAY;
|
||||
@ -55,6 +55,7 @@ public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliases
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GetAliasesRequest aliases(String... aliases) {
|
||||
this.aliases = aliases;
|
||||
return this;
|
||||
@ -70,10 +71,16 @@ public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliases
|
||||
return indices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] aliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean expandAliasesWildcards() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndicesOptions indicesOptions() {
|
||||
return indicesOptions;
|
||||
|
@ -320,9 +320,9 @@ public class MetaData implements Iterable<IndexMetaData> {
|
||||
return mapBuilder.build();
|
||||
}
|
||||
|
||||
private boolean matchAllAliases(final String[] aliases) {
|
||||
private static boolean matchAllAliases(final String[] aliases) {
|
||||
for (String alias : aliases) {
|
||||
if (alias.equals("_all")) {
|
||||
if (alias.equals(ALL)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,10 @@ import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.rest.*;
|
||||
import org.elasticsearch.rest.BaseRestHandler;
|
||||
import org.elasticsearch.rest.RestChannel;
|
||||
import org.elasticsearch.rest.RestController;
|
||||
import org.elasticsearch.rest.RestRequest;
|
||||
import org.elasticsearch.rest.action.support.AcknowledgedRestListener;
|
||||
|
||||
import java.util.Map;
|
||||
|
@ -762,11 +762,9 @@ public class IndexAliasesTests extends ElasticsearchIntegrationTest {
|
||||
assertAcked(admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, "alias1")));
|
||||
fail("create alias should have failed due to null index");
|
||||
} catch (ElasticsearchIllegalArgumentException e) {
|
||||
assertThat("Exception text does not contain \"Property [index] was either missing or null\"",
|
||||
e.getMessage().contains("Property [index] was either missing or null"),
|
||||
equalTo(true));
|
||||
assertThat("Exception text does not contain \"Alias action [add]: [index] may not be empty string\"",
|
||||
e.getMessage(), containsString("Alias action [add]: [index] may not be empty string"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -781,9 +779,8 @@ public class IndexAliasesTests extends ElasticsearchIntegrationTest {
|
||||
assertAcked(admin().indices().prepareAliases().addAlias((String) null, "empty-alias"));
|
||||
fail("create alias should have failed due to null index");
|
||||
} catch (ElasticsearchIllegalArgumentException e) {
|
||||
assertThat("Exception text does not contain \"Property [index] was either missing or null\"",
|
||||
e.getMessage().contains("Property [index] was either missing or null"),
|
||||
equalTo(true));
|
||||
assertThat("Exception text does not contain \"Alias action [add]: [index] may not be empty string\"",
|
||||
e.getMessage(), containsString("Alias action [add]: [index] may not be empty string"));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user