Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment {
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 @@ -454,7 +454,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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should extend the test to also have the case where a policy does exist when restoring to check that it survives the restore. To protect us from "restore-global-state" clearing everything during restore. Could be just randomly doing that or the delete case.

Copy link
Member Author

@ywangd ywangd Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I have expanded the test to:

  • Randomly deleting or mutating the existing policy after snapshot is taken
  • Add another policy after snapshot is taken

Then assert things stay the same after restore.

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 @@ -31,7 +31,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class AutoscalingMetadata implements Metadata.Custom {
public class AutoscalingMetadata implements Metadata.NonRestorableCustom {

public static final String NAME = "autoscaling";

Expand Down