Skip to content

Commit

Permalink
Standardize snapshot indices parsing so that combinations of included…
Browse files Browse the repository at this point in the history
… and excluded indices are treated the same regardless of the order they are listed in

Signed-off-by: Stephen Crawford <steecraw@amazon.com>
  • Loading branch information
stephen-crawford committed Dec 30, 2022
1 parent 5862e07 commit 6beb70e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 13 additions & 13 deletions server/src/main/java/org/opensearch/snapshots/SnapshotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,18 +72,14 @@ public static List<String> filterIndices(List<String> 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<String> 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<String> result = null;
for (int i = 0; i < selectedIndices.length; i++) {
Expand All @@ -103,7 +101,9 @@ public static List<String> filterIndices(List<String> 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);
}
Expand Down

0 comments on commit 6beb70e

Please sign in to comment.