From 576dd2d36a79e5a239201a69e9379058527b8d4d Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 7 Sep 2018 18:44:15 +0200 Subject: [PATCH 1/4] CORE: Make Pattern Exclusion Work with Aliases * Adds the pattern exclusion logic to finding aliases * Closes #33395 --- .../cluster/metadata/MetaData.java | 22 +++++++++++++++---- .../cluster/metadata/MetaDataTests.java | 15 +++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) 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 c3da63886140a..3ee8cf4d0ca28 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -296,14 +296,28 @@ private ImmutableOpenMap> findAliases(String[] origi return ImmutableOpenMap.of(); } - boolean matchAllAliases = matchAllAliases(aliases); + List includeAliases = new ArrayList<>(); + List excludeAliases = new ArrayList<>(); + boolean wildcardSeen = false; + for (final String alias : aliases) { + if (Regex.isSimpleMatchPattern(alias)) { + wildcardSeen = true; + } + if (wildcardSeen && alias.charAt(0) == '-') { + excludeAliases.add(alias.substring(1)); + } else { + includeAliases.add(alias); + } + } + boolean matchAllAliases = matchAllAliases(includeAliases); ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(); for (String index : concreteIndices) { IndexMetaData indexMetaData = indices.get(index); List filteredValues = new ArrayList<>(); for (ObjectCursor cursor : indexMetaData.getAliases().values()) { AliasMetaData value = cursor.value; - if (matchAllAliases || Regex.simpleMatch(aliases, value.alias())) { + if ((matchAllAliases || Regex.simpleMatch(includeAliases, value.alias())) + && Regex.simpleMatch(excludeAliases, value.alias()) == false) { filteredValues.add(value); } } @@ -317,13 +331,13 @@ private ImmutableOpenMap> findAliases(String[] origi return mapBuilder.build(); } - private static boolean matchAllAliases(final String[] aliases) { + private static boolean matchAllAliases(final List aliases) { for (String alias : aliases) { if (alias.equals(ALL)) { return true; } } - return aliases.length == 0; + return aliases.isEmpty(); } /** diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java index 9d82e9e1cdca5..0cbd9a55893b5 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java @@ -109,6 +109,21 @@ public void testFindAliases() { } } + public void testFindAliasWithMultiIndexAndExclusion() { + MetaData metaData = MetaData.builder().put( + IndexMetaData.builder("index") + .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(AliasMetaData.builder("alias1").build()) + .putAlias(AliasMetaData.builder("alias2").build()) + ).build(); + ImmutableOpenMap> aliases = + metaData.findAliases(new GetAliasesRequest().aliases("*", "-alias1"), new String[] {"index"}); + assertThat(aliases.size(), equalTo(1)); + assertThat(aliases.get("index").get(0).alias(), equalTo("alias2")); + } + public void testIndexAndAliasWithSameName() { IndexMetaData.Builder builder = IndexMetaData.builder("index") .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)) From 71a0d883666f98e285af296542410848acc251aa Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 7 Sep 2018 18:55:51 +0200 Subject: [PATCH 2/4] faster --- .../org/elasticsearch/cluster/metadata/MetaData.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 3ee8cf4d0ca28..cef6f9912bde7 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -309,15 +309,17 @@ private ImmutableOpenMap> findAliases(String[] origi includeAliases.add(alias); } } - boolean matchAllAliases = matchAllAliases(includeAliases); + String[] included = includeAliases.toArray(Strings.EMPTY_ARRAY); + String[] excluded = excludeAliases.toArray(Strings.EMPTY_ARRAY); + boolean matchAllAliases = matchAllAliases(included); ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(); for (String index : concreteIndices) { IndexMetaData indexMetaData = indices.get(index); List filteredValues = new ArrayList<>(); for (ObjectCursor cursor : indexMetaData.getAliases().values()) { AliasMetaData value = cursor.value; - if ((matchAllAliases || Regex.simpleMatch(includeAliases, value.alias())) - && Regex.simpleMatch(excludeAliases, value.alias()) == false) { + if ((matchAllAliases || Regex.simpleMatch(included, value.alias())) + && Regex.simpleMatch(excluded, value.alias()) == false) { filteredValues.add(value); } } @@ -331,13 +333,13 @@ private ImmutableOpenMap> findAliases(String[] origi return mapBuilder.build(); } - private static boolean matchAllAliases(final List aliases) { + private static boolean matchAllAliases(final String[] aliases) { for (String alias : aliases) { if (alias.equals(ALL)) { return true; } } - return aliases.isEmpty(); + return aliases.length == 0; } /** From 8b9e76981a9aa805a6be68b238d4df0aa6c40c48 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Sun, 9 Sep 2018 07:36:16 +0200 Subject: [PATCH 3/4] CR: Support overrides to exclusions --- .../cluster/metadata/MetaData.java | 46 +++++++++---------- .../cluster/metadata/MetaDataTests.java | 25 ++++++++-- 2 files changed, 43 insertions(+), 28 deletions(-) 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 cef6f9912bde7..67db5657355b1 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -296,30 +296,37 @@ private ImmutableOpenMap> findAliases(String[] origi return ImmutableOpenMap.of(); } - List includeAliases = new ArrayList<>(); - List excludeAliases = new ArrayList<>(); - boolean wildcardSeen = false; - for (final String alias : aliases) { - if (Regex.isSimpleMatchPattern(alias)) { - wildcardSeen = true; - } - if (wildcardSeen && alias.charAt(0) == '-') { - excludeAliases.add(alias.substring(1)); + String[] patterns = new String[aliases.length]; + boolean[] include = new boolean[aliases.length]; + for (int i = 0; i < aliases.length; i++) { + String alias = aliases[i]; + if (alias.charAt(0) == '-') { + patterns[i] = alias.substring(1); + include[i] = false; } else { - includeAliases.add(alias); + patterns[i] = alias; + include[i] = true; } } - String[] included = includeAliases.toArray(Strings.EMPTY_ARRAY); - String[] excluded = excludeAliases.toArray(Strings.EMPTY_ARRAY); - boolean matchAllAliases = matchAllAliases(included); + boolean matchAllAliases = patterns.length == 0; ImmutableOpenMap.Builder> mapBuilder = ImmutableOpenMap.builder(); for (String index : concreteIndices) { IndexMetaData indexMetaData = indices.get(index); List filteredValues = new ArrayList<>(); for (ObjectCursor cursor : indexMetaData.getAliases().values()) { AliasMetaData value = cursor.value; - if ((matchAllAliases || Regex.simpleMatch(included, value.alias())) - && Regex.simpleMatch(excluded, value.alias()) == false) { + boolean matched = matchAllAliases; + String alias = value.alias(); + for (int i = 0; i < patterns.length; i++) { + if (include[i]) { + if (matched == false) { + matched = Regex.simpleMatch(patterns[i], alias); + } + } else if (matched) { + matched = Regex.simpleMatch(patterns[i], alias) == false; + } + } + if (matched) { filteredValues.add(value); } } @@ -333,15 +340,6 @@ private ImmutableOpenMap> findAliases(String[] origi 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/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java index 0cbd9a55893b5..da50e99705dfb 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataTests.java @@ -109,7 +109,7 @@ public void testFindAliases() { } } - public void testFindAliasWithMultiIndexAndExclusion() { + public void testFindAliasWithExclusion() { MetaData metaData = MetaData.builder().put( IndexMetaData.builder("index") .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)) @@ -118,10 +118,27 @@ public void testFindAliasWithMultiIndexAndExclusion() { .putAlias(AliasMetaData.builder("alias1").build()) .putAlias(AliasMetaData.builder("alias2").build()) ).build(); - ImmutableOpenMap> aliases = - metaData.findAliases(new GetAliasesRequest().aliases("*", "-alias1"), new String[] {"index"}); + List aliases = + metaData.findAliases(new GetAliasesRequest().aliases("*", "-alias1"), new String[] {"index"}).get("index"); assertThat(aliases.size(), equalTo(1)); - assertThat(aliases.get("index").get(0).alias(), equalTo("alias2")); + assertThat(aliases.get(0).alias(), equalTo("alias2")); + } + + public void testFindAliasWithExclusionAndOverride() { + MetaData metaData = MetaData.builder().put( + IndexMetaData.builder("index") + .settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(AliasMetaData.builder("aa").build()) + .putAlias(AliasMetaData.builder("ab").build()) + .putAlias(AliasMetaData.builder("bb").build()) + ).build(); + List aliases = + metaData.findAliases(new GetAliasesRequest().aliases("a*", "-*b", "b*"), new String[] {"index"}).get("index"); + assertThat(aliases.size(), equalTo(2)); + assertThat(aliases.get(0).alias(), equalTo("aa")); + assertThat(aliases.get(1).alias(), equalTo("bb")); } public void testIndexAndAliasWithSameName() { From cc713f686933a7f44569b670a4fba52375be4692 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Sun, 9 Sep 2018 08:56:06 +0200 Subject: [PATCH 4/4] Fix issue with '_all' --- .../main/java/org/elasticsearch/cluster/metadata/MetaData.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 67db5657355b1..75869b54850d4 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -320,7 +320,8 @@ private ImmutableOpenMap> findAliases(String[] origi for (int i = 0; i < patterns.length; i++) { if (include[i]) { if (matched == false) { - matched = Regex.simpleMatch(patterns[i], alias); + String pattern = patterns[i]; + matched = ALL.equals(pattern) || Regex.simpleMatch(pattern, alias); } } else if (matched) { matched = Regex.simpleMatch(patterns[i], alias) == false;