Skip to content

Commit

Permalink
Do not restore autoscaling policy from snapshots (elastic#66023)
Browse files Browse the repository at this point in the history
Autoscaling policies (custom metadata) are marked as non-restorable and hence
they are never restored from a snapshot.
  • Loading branch information
ywangd committed Dec 11, 2020
1 parent 2ba10f0 commit 6f03b1d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment, Clust
EnumSet<XContentContext> context();
}

public interface NonRestorableCustom extends Custom {
}

public static final Setting<Boolean> SETTING_READ_ONLY_SETTING =
Setting.boolSetting("cluster.blocks.read_only", false, Property.Dynamic, Property.NodeScope);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ restoreUUID, snapshot, overallState(RestoreInProgress.State.INIT, shards),
if (metadata.customs() != null) {
for (ObjectObjectCursor<String, Metadata.Custom> cursor : metadata.customs()) {
if (RepositoriesMetadata.TYPE.equals(cursor.key) == false
&& DataStreamMetadata.TYPE.equals(cursor.key) == false) {
&& DataStreamMetadata.TYPE.equals(cursor.key) == false
&& cursor.value instanceof Metadata.NonRestorableCustom == false) {
// Don't restore repositories while we are working with them
// TODO: Should we restore them at the end?
// Also, don't restore data streams here, we already added them to the metadata builder above
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.xpack.autoscaling;

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xpack.autoscaling.action.DeleteAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction;
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy;
import org.junit.Before;

import java.nio.file.Path;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicy;
import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicyOfName;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class AutoscalingSnapshotsIT extends AutoscalingIntegTestCase {

public static final String REPO = "repo";
public static final String SNAPSHOT = "snap";

@Before
public void setup() throws Exception {
Path location = randomRepoPath();
logger.info("--> creating repository [{}] [{}]", REPO, "fs");
assertAcked(clusterAdmin().preparePutRepository(REPO).setType("fs").setSettings(Settings.builder().put("location", location)));
}

public void testAutoscalingPolicyWillNotBeRestored() {
final Client client = client();

AutoscalingPolicy policy = randomAutoscalingPolicy();
putPolicy(policy);

CreateSnapshotResponse createSnapshotResponse = client.admin()
.cluster()
.prepareCreateSnapshot(REPO, SNAPSHOT)
.setWaitForCompletion(true)
.setIncludeGlobalState(true)
.get();
assertEquals(RestStatus.OK, createSnapshotResponse.status());

final boolean deletePolicy = randomBoolean();
if (deletePolicy) {
final DeleteAutoscalingPolicyAction.Request deleteRequest = new DeleteAutoscalingPolicyAction.Request(policy.name());
assertAcked(client.execute(DeleteAutoscalingPolicyAction.INSTANCE, deleteRequest).actionGet());
} else {
// Update the policy
policy = randomAutoscalingPolicyOfName(policy.name());
putPolicy(policy);
}
final AutoscalingPolicy anotherPolicy = randomAutoscalingPolicy();
putPolicy(anotherPolicy);

RestoreSnapshotResponse restoreSnapshotResponse = client.admin()
.cluster()
.prepareRestoreSnapshot(REPO, SNAPSHOT)
.setWaitForCompletion(true)
.setRestoreGlobalState(true)
.get();
assertEquals(RestStatus.OK, restoreSnapshotResponse.status());

if (deletePolicy) {
assertPolicyNotFound(policy);
} else {
assertPolicy(policy);
}
assertPolicy(anotherPolicy);
}

private void putPolicy(AutoscalingPolicy policy) {
final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request(
policy.name(),
policy.roles(),
policy.deciders()
);
assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet());
}

private void assertPolicy(AutoscalingPolicy policy) {
final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name());
final AutoscalingPolicy actualPolicy = client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy();
assertThat(actualPolicy, equalTo(policy));
}

private void assertPolicyNotFound(AutoscalingPolicy policy) {
final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name());
final ResourceNotFoundException e = expectThrows(
ResourceNotFoundException.class,
() -> client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy()
);
assertThat(e.getMessage(), containsString("autoscaling policy with name [" + policy.name() + "] does not exist"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class AutoscalingMetadata implements XPackPlugin.XPackMetadataCustom {
public class AutoscalingMetadata implements XPackPlugin.XPackMetadataCustom, Metadata.NonRestorableCustom {

public static final String NAME = "autoscaling";

Expand Down

0 comments on commit 6f03b1d

Please sign in to comment.