Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove alias with must_exist (#65141) #65209

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
"Remove alias with must_exist":

- skip:
version: " - 7.10.99"
reason: "must_exist does not work until 7.11"

- do:
indices.create:
index: test_index

- do:
indices.update_aliases:
body:
actions:
- add:
index: test_index
aliases: test_alias1
- do:
indices.exists_alias:
name: test_alias1
- is_true: ''

- do:
indices.update_aliases:
body:
actions:
- remove:
index: test_index
alias: test_alias1
must_exist: true
- add:
index: test_index
alias: test_alias2
- do:
indices.exists_alias:
name: test_alias1
- is_false: ''
- do:
indices.exists_alias:
name: test_alias2
- is_true: ''

- do:
catch: missing
indices.update_aliases:
body:
actions:
- remove:
index: test_index
alias: test_alias1
must_exist: true
- add:
index: test_index
alias: test_alias3
- do:
indices.exists_alias:
name: test_alias3
- is_false: ''

---
"Alias must_exist only on remove":
- do:
indices.create:
index: test_index

- do:
catch: bad_request
indices.update_aliases:
body:
actions:
- add:
index: test_index
aliases: test_alias1
must_exist: true

- do:
catch: bad_request
indices.update_aliases:
body:
actions:
- remove_index:
index: test_index
must_exist: true
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ private static ObjectParser<AliasActions, Void> parser(String name, Supplier<Ali
}

private static final ObjectParser<AliasActions, Void> ADD_PARSER = parser(ADD.getPreferredName(), AliasActions::add);
private static final ObjectParser<AliasActions, Void> REMOVE_PARSER = parser(REMOVE.getPreferredName(), AliasActions::remove);
private static final ObjectParser<AliasActions, Void> REMOVE_INDEX_PARSER = parser(REMOVE_INDEX.getPreferredName(),
AliasActions::removeIndex);
static {
ADD_PARSER.declareObject(AliasActions::filter, (parser, m) -> {
try {
Expand All @@ -196,11 +199,8 @@ private static ObjectParser<AliasActions, Void> parser(String name, Supplier<Ali
ADD_PARSER.declareField(AliasActions::searchRouting, XContentParser::text, SEARCH_ROUTING, ValueType.INT);
ADD_PARSER.declareField(AliasActions::writeIndex, XContentParser::booleanValue, IS_WRITE_INDEX, ValueType.BOOLEAN);
ADD_PARSER.declareField(AliasActions::isHidden, XContentParser::booleanValue, IS_HIDDEN, ValueType.BOOLEAN);
ADD_PARSER.declareField(AliasActions::mustExist, XContentParser::booleanValue, MUST_EXIST, ValueType.BOOLEAN);
REMOVE_PARSER.declareField(AliasActions::mustExist, XContentParser::booleanValue, MUST_EXIST, ValueType.BOOLEAN);
}
private static final ObjectParser<AliasActions, Void> REMOVE_PARSER = parser(REMOVE.getPreferredName(), AliasActions::remove);
private static final ObjectParser<AliasActions, Void> REMOVE_INDEX_PARSER = parser(REMOVE_INDEX.getPreferredName(),
AliasActions::removeIndex);

/**
* Parser for any one {@link AliasAction}.
Expand Down Expand Up @@ -547,6 +547,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (null != isHidden) {
builder.field(IS_HIDDEN.getPreferredName(), isHidden);
}
if (null != mustExist) {
builder.field(MUST_EXIST.getPreferredName(), mustExist);
}
builder.endObject();
builder.endObject();
return builder;
Expand All @@ -567,6 +570,7 @@ public String toString() {
+ ",indexRouting=" + indexRouting
+ ",searchRouting=" + searchRouting
+ ",writeIndex=" + writeIndex
+ ",mustExist=" + mustExist
+ "]";
}

Expand All @@ -585,12 +589,13 @@ public boolean equals(Object obj) {
&& Objects.equals(indexRouting, other.indexRouting)
&& Objects.equals(searchRouting, other.searchRouting)
&& Objects.equals(writeIndex, other.writeIndex)
&& Objects.equals(isHidden, other.isHidden);
&& Objects.equals(isHidden, other.isHidden)
&& Objects.equals(mustExist, other.mustExist);
}

@Override
public int hashCode() {
return Objects.hash(type, indices, aliases, filter, routing, indexRouting, searchRouting, writeIndex, isHidden);
return Objects.hash(type, indices, aliases, filter, routing, indexRouting, searchRouting, writeIndex, isHidden, mustExist);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ private static String[] concreteAliases(AliasActions action, Metadata metadata,
finalAliases.add(aliasMeta.alias());
}
}
if (finalAliases.isEmpty() && action.mustExist() != null && action.mustExist()) {
return action.aliases();
}
return finalAliases.toArray(new String[finalAliases.size()]);
} else {
//for ADD and REMOVE_INDEX we just return the current aliases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -179,8 +180,8 @@ boolean removeIndex() {
@Override
boolean apply(NewAliasValidator aliasValidator, Metadata.Builder metadata, IndexMetadata index) {
if (false == index.getAliases().containsKey(alias)) {
if (mustExist != null && mustExist.booleanValue()) {
throw new IllegalArgumentException("required alias [" + alias + "] does not exist");
if (mustExist != null && mustExist) {
throw new ResourceNotFoundException("required alias [" + alias + "] does not exist");
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public void testParseAddDefaultRouting() throws IOException {
public void testParseRemove() throws IOException {
String[] indices = generateRandomStringArray(10, 5, false, false);
String[] aliases = generateRandomStringArray(10, 5, false, false);
Boolean mustExist = null;
XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
b.startObject();
{
Expand All @@ -231,6 +232,10 @@ public void testParseRemove() throws IOException {
} else {
b.field("alias", aliases[0]);
}
if (randomBoolean()) {
mustExist = randomBoolean();
b.field("must_exist", mustExist);
}
}
b.endObject();
}
Expand All @@ -241,6 +246,7 @@ public void testParseRemove() throws IOException {
assertEquals(AliasActions.Type.REMOVE, action.actionType());
assertThat(action.indices(), equalTo(indices));
assertThat(action.aliases(), equalTo(aliases));
assertThat(action.mustExist(), equalTo(mustExist));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
Expand Down Expand Up @@ -139,7 +140,7 @@ public void testMustExist() {

// Show that removing non-existing alias with mustExist == true fails
final ClusterState finalCS = after;
final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class,
final ResourceNotFoundException iae = expectThrows(ResourceNotFoundException.class,
() -> service.applyAliasActions(finalCS, singletonList(new AliasAction.Remove(index, "test_2", true))));
assertThat(iae.getMessage(), containsString("required alias [test_2] does not exist"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static AliasActions randomAliasAction(boolean useStringAsFilter) {
}
action.indices(indices);
}
if (action.actionType() == AliasActions.Type.REMOVE) {
if (randomBoolean()) {
action.mustExist(randomBoolean());
}
}
if (action.actionType() != AliasActions.Type.REMOVE_INDEX) {
if (randomBoolean()) {
action.alias(randomAlphaOfLength(5));
Expand Down