diff --git a/CHANGELOG.md b/CHANGELOG.md index f83c2dfa1dfca..229b4d7fb29d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,7 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Changed http code on create index API with bad input raising NotXContentException from 500 to 400 ([#4773](https://github.com/opensearch-project/OpenSearch/pull/4773)) - Change http code for DecommissioningFailedException from 500 to 400 ([#5283](https://github.com/opensearch-project/OpenSearch/pull/5283)) - Pre conditions check before updating weighted routing metadata ([#4955](https://github.com/opensearch-project/OpenSearch/pull/4955)) -- Correct index parsing logic for SnapshotUtils ([5626]https://github.com/opensearch-project/OpenSearch/pull/5626) +- Standardize snapshot indices parsing so that combinations of included and excluded indices are treated the same regardless of the order they are listed in ([#5626](https://github.com/opensearch-project/OpenSearch/pull/5626)) ### Deprecated diff --git a/server/src/main/java/org/opensearch/snapshots/SnapshotUtils.java b/server/src/main/java/org/opensearch/snapshots/SnapshotUtils.java index 330103849b3a4..32e95891ff697 100644 --- a/server/src/main/java/org/opensearch/snapshots/SnapshotUtils.java +++ b/server/src/main/java/org/opensearch/snapshots/SnapshotUtils.java @@ -49,6 +49,8 @@ import java.util.Set; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Snapshot utilities @@ -70,18 +72,14 @@ public static List filterIndices(List availableIndices, String[] return availableIndices; } - selectedIndices = Arrays.stream(selectedIndices).filter(s -> !s.isEmpty()).toArray(a -> new String[a]); // Remove all empty strings - Arrays.sort(selectedIndices, (o1, o2) -> { // Make '-' lower priority then everything - - char o1FirstChar = o1.charAt(0); - char o2FirstChar = o2.charAt(0); - if (o1FirstChar == '-' && o2FirstChar != '-') { - return 1; - } else if (o1FirstChar != '-' && o2FirstChar == '-') { - return -1; - } - return o1.compareTo(o2); - }); + // Move the exclusions to end of list to ensure they are processed + // after explicitly selected indices are chosen. + final List excludesAtEndSelectedIndices = Stream.concat( + Arrays.stream(selectedIndices) + .filter(s -> s.isEmpty() || s.charAt(0) != '-'), + Arrays.stream(selectedIndices) + .filter(s -> !s.isEmpty() && s.charAt(0) == '-')) + .collect(Collectors.toUnmodifiableList()); Set result = null; for (int i = 0; i < selectedIndices.length; i++) { @@ -103,7 +101,9 @@ public static List filterIndices(List availableIndices, String[] result = new HashSet<>(); } } else if (indexOrPattern.charAt(0) == '-') { - // if its the first index pattern, fill it with all the indices... + // If the first index pattern is an exclusion, then all patterns are exclusions due to the + // reordering logic above. In this case, the request is interpreted as "include all indexes except + // those matching the exclusions" so we add all indices here and then remove the ones that match the exclusion patterns. if (i == 0) { result = new HashSet<>(availableIndices); }