-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add Filtering by SLM Policy to Get Snapshots API #77321
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
Changes from all commits
5ce81c2
5cd1e5a
406822c
57be270
93d488f
01646f4
3bf417b
d25fcc7
6c70726
da2f4d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; | ||
| import org.elasticsearch.client.Request; | ||
| import org.elasticsearch.client.Response; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.xcontent.DeprecationHandler; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
|
|
@@ -23,6 +24,7 @@ | |
| import org.elasticsearch.search.sort.SortOrder; | ||
| import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; | ||
| import org.elasticsearch.snapshots.SnapshotInfo; | ||
| import org.elasticsearch.snapshots.SnapshotsService; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
|
|
||
| import java.io.IOException; | ||
|
|
@@ -31,8 +33,10 @@ | |
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase.assertSnapshotListSorted; | ||
| import static org.hamcrest.Matchers.empty; | ||
| import static org.hamcrest.Matchers.in; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
|
|
@@ -183,6 +187,67 @@ public void testSortAndPaginateWithInProgress() throws Exception { | |
| assertStablePagination(repoName, allSnapshotNames, GetSnapshotsRequest.SortBy.INDICES); | ||
| } | ||
|
|
||
| public void testFilterBySLMPolicy() throws Exception { | ||
| final String repoName = "test-repo"; | ||
| AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs"); | ||
| AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5)); | ||
| final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*").setSnapshots("*") | ||
| .setSort(GetSnapshotsRequest.SortBy.NAME).get().getSnapshots(); | ||
| final String snapshotWithPolicy = "snapshot-with-policy"; | ||
| final String policyName = "some-policy"; | ||
| final SnapshotInfo withPolicy = AbstractSnapshotIntegTestCase.assertSuccessful( | ||
| logger, | ||
| clusterAdmin().prepareCreateSnapshot(repoName, snapshotWithPolicy) | ||
| .setUserMetadata(Map.of(SnapshotsService.POLICY_ID_METADATA_FIELD, policyName)) | ||
| .setWaitForCompletion(true) | ||
| .execute() | ||
| ); | ||
|
|
||
| assertThat(getAllSnapshotsForPolicies(policyName), is(List.of(withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies("some-*"), is(List.of(withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies("*", "-" + policyName), empty()); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); | ||
| assertThat( | ||
| getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-" + policyName), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-*"), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies("no-such-policy"), empty()); | ||
| assertThat(getAllSnapshotsForPolicies("no-such-policy*"), empty()); | ||
|
|
||
| final String snapshotWithOtherPolicy = "snapshot-with-other-policy"; | ||
| final String otherPolicyName = "other-policy"; | ||
| final SnapshotInfo withOtherPolicy = AbstractSnapshotIntegTestCase.assertSuccessful( | ||
| logger, | ||
| clusterAdmin().prepareCreateSnapshot(repoName, snapshotWithOtherPolicy) | ||
| .setUserMetadata(Map.of(SnapshotsService.POLICY_ID_METADATA_FIELD, otherPolicyName)) | ||
| .setWaitForCompletion(true) | ||
| .execute() | ||
| ); | ||
| assertThat(getAllSnapshotsForPolicies("*"), is(List.of(withOtherPolicy, withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName), is(List.of(withOtherPolicy, withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think just |
||
| final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots("*") | ||
| .setSnapshots("*") | ||
| .setSort(GetSnapshotsRequest.SortBy.NAME) | ||
| .get() | ||
| .getSnapshots(); | ||
| assertThat( | ||
| getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), | ||
| is(allSnapshots) | ||
| ); | ||
| assertThat( | ||
| getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "*"), | ||
| is(allSnapshots) | ||
| ); | ||
| } | ||
|
|
||
| private static List<SnapshotInfo> getAllSnapshotsForPolicies(String... policies) throws IOException { | ||
| final Request requestWithPolicy = new Request(HttpGet.METHOD_NAME, "/_snapshot/*/*"); | ||
| requestWithPolicy.addParameter("slm_policy_filter", Strings.arrayToCommaDelimitedString(policies)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we need an explicit sort here or to compare the results by converrting to sets? Since we spawn the creation of snapshots in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, we could actually theoretically fail if everything executes in the same millisecond (... looking at some/windows systems that seems possible). Forced sort-by-name everywhere now in both tests. Thanks! |
||
| requestWithPolicy.addParameter("sort", GetSnapshotsRequest.SortBy.NAME.toString()); | ||
| return readSnapshotInfos(getRestClient().performRequest(requestWithPolicy)).getSnapshots(); | ||
| } | ||
|
|
||
| private void createIndexWithContent(String indexName) { | ||
| logger.info("--> creating index [{}]", indexName); | ||
| createIndex(indexName, AbstractSnapshotIntegTestCase.SINGLE_SHARD_NO_REPLICA); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,9 @@ | |
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.hamcrest.Matchers.empty; | ||
| import static org.hamcrest.Matchers.in; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
|
|
@@ -208,6 +210,64 @@ public void testPaginationRequiresVerboseListing() throws Exception { | |
| ); | ||
| } | ||
|
|
||
| public void testFilterBySLMPolicy() throws Exception { | ||
| final String repoName = "test-repo"; | ||
| createRepository(repoName, "fs"); | ||
| createNSnapshots(repoName, randomIntBetween(1, 5)); | ||
| final List<SnapshotInfo> snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*") | ||
| .setSnapshots("*") | ||
| .setSort(GetSnapshotsRequest.SortBy.NAME) | ||
| .get() | ||
| .getSnapshots(); | ||
| final String snapshotWithPolicy = "snapshot-with-policy"; | ||
| final String policyName = "some-policy"; | ||
| final SnapshotInfo withPolicy = assertSuccessful( | ||
| clusterAdmin().prepareCreateSnapshot(repoName, snapshotWithPolicy) | ||
| .setUserMetadata(Map.of(SnapshotsService.POLICY_ID_METADATA_FIELD, policyName)) | ||
| .setWaitForCompletion(true) | ||
| .execute() | ||
| ); | ||
|
|
||
| assertThat(getAllSnapshotsForPolicies(policyName), is(List.of(withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies("some-*"), is(List.of(withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies("*", "-" + policyName), empty()); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-" + policyName), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-*"), is(snapshotsWithoutPolicy)); | ||
| assertThat(getAllSnapshotsForPolicies("no-such-policy"), empty()); | ||
| assertThat(getAllSnapshotsForPolicies("no-such-policy*"), empty()); | ||
|
|
||
| final String snapshotWithOtherPolicy = "snapshot-with-other-policy"; | ||
| final String otherPolicyName = "other-policy"; | ||
| final SnapshotInfo withOtherPolicy = assertSuccessful( | ||
| clusterAdmin().prepareCreateSnapshot(repoName, snapshotWithOtherPolicy) | ||
| .setUserMetadata(Map.of(SnapshotsService.POLICY_ID_METADATA_FIELD, otherPolicyName)) | ||
| .setWaitForCompletion(true) | ||
| .execute() | ||
| ); | ||
| assertThat(getAllSnapshotsForPolicies("*"), is(List.of(withOtherPolicy, withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName), is(List.of(withOtherPolicy, withPolicy))); | ||
| assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add just |
||
|
|
||
| final List<SnapshotInfo> allSnapshots = clusterAdmin().prepareGetSnapshots("*") | ||
| .setSnapshots("*") | ||
| .setSort(GetSnapshotsRequest.SortBy.NAME) | ||
| .get() | ||
| .getSnapshots(); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots)); | ||
| assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "*"), is(allSnapshots)); | ||
| } | ||
|
|
||
| private static List<SnapshotInfo> getAllSnapshotsForPolicies(String... policies) { | ||
| return clusterAdmin().prepareGetSnapshots("*") | ||
| .setSnapshots("*") | ||
| .setPolicies(policies) | ||
| .setSort(GetSnapshotsRequest.SortBy.NAME) | ||
| .get() | ||
| .getSnapshots(); | ||
| } | ||
|
|
||
| private static void assertStablePagination(String repoName, Collection<String> allSnapshotNames, GetSnapshotsRequest.SortBy sort) { | ||
| final SortOrder order = randomFrom(SortOrder.values()); | ||
| final List<SnapshotInfo> allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,8 +38,11 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest> | |
|
|
||
| public static final String ALL_SNAPSHOTS = "_all"; | ||
| public static final String CURRENT_SNAPSHOT = "_current"; | ||
| public static final String NO_POLICY_PATTERN = "_none"; | ||
| public static final boolean DEFAULT_VERBOSE_MODE = true; | ||
|
|
||
| public static final Version SLM_POLICY_FILTERING_VERSION = Version.V_8_0_0; | ||
|
|
||
| public static final Version MULTIPLE_REPOSITORIES_SUPPORT_ADDED = Version.V_7_14_0; | ||
|
|
||
| public static final Version PAGINATED_GET_SNAPSHOTS_VERSION = Version.V_7_14_0; | ||
|
|
@@ -71,6 +74,8 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest> | |
|
|
||
| private String[] snapshots = Strings.EMPTY_ARRAY; | ||
|
|
||
| private String[] policies = Strings.EMPTY_ARRAY; | ||
|
|
||
| private boolean ignoreUnavailable; | ||
|
|
||
| private boolean verbose = DEFAULT_VERBOSE_MODE; | ||
|
|
@@ -115,6 +120,9 @@ public GetSnapshotsRequest(StreamInput in) throws IOException { | |
| if (in.getVersion().onOrAfter(NUMERIC_PAGINATION_VERSION)) { | ||
| offset = in.readVInt(); | ||
| } | ||
| if (in.getVersion().onOrAfter(SLM_POLICY_FILTERING_VERSION)) { | ||
| policies = in.readStringArray(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -157,6 +165,13 @@ public void writeTo(StreamOutput out) throws IOException { | |
| } else if (sort != SortBy.START_TIME || size != NO_LIMIT || after != null || order != SortOrder.ASC) { | ||
| throw new IllegalArgumentException("can't use paginated get snapshots request with node version [" + out.getVersion() + "]"); | ||
| } | ||
| if (out.getVersion().onOrAfter(SLM_POLICY_FILTERING_VERSION)) { | ||
| out.writeStringArray(policies); | ||
| } else if (policies.length > 0) { | ||
| throw new IllegalArgumentException( | ||
| "can't use slm policy filter in snapshots request with node version [" + out.getVersion() + "]" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -184,6 +199,9 @@ public ActionRequestValidationException validate() { | |
| if (order != SortOrder.ASC) { | ||
| validationException = addValidationError("can't use non-default sort order with verbose=false", validationException); | ||
| } | ||
| if (policies.length != 0) { | ||
| validationException = addValidationError("can't use slm policy filter with verbose=false", validationException); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a generic validation to not use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else if (after != null && offset > 0) { | ||
| validationException = addValidationError("can't use after and offset simultaneously", validationException); | ||
| } | ||
|
|
@@ -210,6 +228,26 @@ public String[] repositories() { | |
| return this.repositories; | ||
| } | ||
|
|
||
| /** | ||
| * Sets slm policy patterns | ||
| * | ||
| * @param policies policy patterns | ||
| * @return this request | ||
| */ | ||
| public GetSnapshotsRequest policies(String... policies) { | ||
| this.policies = policies; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns policy patterns | ||
| * | ||
| * @return policy patterns | ||
| */ | ||
| public String[] policies() { | ||
| return policies; | ||
| } | ||
|
|
||
| public boolean isSingleRepositoryRequest() { | ||
| return repositories.length == 1 | ||
| && repositories[0] != null | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.