Skip to content

Commit 78ad68f

Browse files
authored
Do not restore autoscaling policy from snapshots (#66023)
Autoscaling policies (custom metadata) are marked as non-restorable and hence they are never restored from a snapshot.
1 parent 8919c3a commit 78ad68f

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment {
131131
EnumSet<XContentContext> context();
132132
}
133133

134+
public interface NonRestorableCustom extends Custom {
135+
}
136+
134137
public static final Setting<Boolean> SETTING_READ_ONLY_SETTING =
135138
Setting.boolSetting("cluster.blocks.read_only", false, Property.Dynamic, Property.NodeScope);
136139

server/src/main/java/org/elasticsearch/snapshots/RestoreService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ restoreUUID, snapshot, overallState(RestoreInProgress.State.INIT, shards),
454454
if (metadata.customs() != null) {
455455
for (ObjectObjectCursor<String, Metadata.Custom> cursor : metadata.customs()) {
456456
if (RepositoriesMetadata.TYPE.equals(cursor.key) == false
457-
&& DataStreamMetadata.TYPE.equals(cursor.key) == false) {
457+
&& DataStreamMetadata.TYPE.equals(cursor.key) == false
458+
&& cursor.value instanceof Metadata.NonRestorableCustom == false) {
458459
// Don't restore repositories while we are working with them
459460
// TODO: Should we restore them at the end?
460461
// Also, don't restore data streams here, we already added them to the metadata builder above
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.autoscaling;
8+
9+
import org.elasticsearch.ResourceNotFoundException;
10+
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
11+
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
12+
import org.elasticsearch.client.Client;
13+
import org.elasticsearch.common.settings.Settings;
14+
import org.elasticsearch.rest.RestStatus;
15+
import org.elasticsearch.xpack.autoscaling.action.DeleteAutoscalingPolicyAction;
16+
import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingPolicyAction;
17+
import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction;
18+
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy;
19+
import org.junit.Before;
20+
21+
import java.nio.file.Path;
22+
23+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
24+
import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicy;
25+
import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicyOfName;
26+
import static org.hamcrest.Matchers.containsString;
27+
import static org.hamcrest.Matchers.equalTo;
28+
29+
public class AutoscalingSnapshotsIT extends AutoscalingIntegTestCase {
30+
31+
public static final String REPO = "repo";
32+
public static final String SNAPSHOT = "snap";
33+
34+
@Before
35+
public void setup() throws Exception {
36+
Path location = randomRepoPath();
37+
logger.info("--> creating repository [{}] [{}]", REPO, "fs");
38+
assertAcked(clusterAdmin().preparePutRepository(REPO).setType("fs").setSettings(Settings.builder().put("location", location)));
39+
}
40+
41+
public void testAutoscalingPolicyWillNotBeRestored() {
42+
final Client client = client();
43+
44+
AutoscalingPolicy policy = randomAutoscalingPolicy();
45+
putPolicy(policy);
46+
47+
CreateSnapshotResponse createSnapshotResponse = client.admin()
48+
.cluster()
49+
.prepareCreateSnapshot(REPO, SNAPSHOT)
50+
.setWaitForCompletion(true)
51+
.setIncludeGlobalState(true)
52+
.get();
53+
assertEquals(RestStatus.OK, createSnapshotResponse.status());
54+
55+
final boolean deletePolicy = randomBoolean();
56+
if (deletePolicy) {
57+
final DeleteAutoscalingPolicyAction.Request deleteRequest = new DeleteAutoscalingPolicyAction.Request(policy.name());
58+
assertAcked(client.execute(DeleteAutoscalingPolicyAction.INSTANCE, deleteRequest).actionGet());
59+
} else {
60+
// Update the policy
61+
policy = randomAutoscalingPolicyOfName(policy.name());
62+
putPolicy(policy);
63+
}
64+
final AutoscalingPolicy anotherPolicy = randomAutoscalingPolicy();
65+
putPolicy(anotherPolicy);
66+
67+
RestoreSnapshotResponse restoreSnapshotResponse = client.admin()
68+
.cluster()
69+
.prepareRestoreSnapshot(REPO, SNAPSHOT)
70+
.setWaitForCompletion(true)
71+
.setRestoreGlobalState(true)
72+
.get();
73+
assertEquals(RestStatus.OK, restoreSnapshotResponse.status());
74+
75+
if (deletePolicy) {
76+
assertPolicyNotFound(policy);
77+
} else {
78+
assertPolicy(policy);
79+
}
80+
assertPolicy(anotherPolicy);
81+
}
82+
83+
private void putPolicy(AutoscalingPolicy policy) {
84+
final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request(
85+
policy.name(),
86+
policy.roles(),
87+
policy.deciders()
88+
);
89+
assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet());
90+
}
91+
92+
private void assertPolicy(AutoscalingPolicy policy) {
93+
final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name());
94+
final AutoscalingPolicy actualPolicy = client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy();
95+
assertThat(actualPolicy, equalTo(policy));
96+
}
97+
98+
private void assertPolicyNotFound(AutoscalingPolicy policy) {
99+
final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name());
100+
final ResourceNotFoundException e = expectThrows(
101+
ResourceNotFoundException.class,
102+
() -> client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy()
103+
);
104+
assertThat(e.getMessage(), containsString("autoscaling policy with name [" + policy.name() + "] does not exist"));
105+
}
106+
}

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/AutoscalingMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.function.Function;
3232
import java.util.stream.Collectors;
3333

34-
public class AutoscalingMetadata implements Metadata.Custom {
34+
public class AutoscalingMetadata implements Metadata.NonRestorableCustom {
3535

3636
public static final String NAME = "autoscaling";
3737

0 commit comments

Comments
 (0)