Adds Steps for ReplicasAction

Also adds tests for those steps and ReplicasAction.toSteps
This commit is contained in:
Colin Goodheart-Smithe 2018-04-09 14:43:57 +01:00
parent 2eb8fd9336
commit 54c6d280a5
5 changed files with 177 additions and 350 deletions

View File

@ -5,31 +5,20 @@
*/
package org.elasticsearch.xpack.core.indexlifecycle;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.Index;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.LongSupplier;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
@ -84,13 +73,10 @@ public class ReplicasAction implements LifecycleAction {
@Override
public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey) {
// ClusterStateUpdateStep updateAllocationSettings = new ClusterStateUpdateStep(
// "update_replica_count", NAME, phase, index.getName(), (currentState) ->
// ClusterState.builder(currentState).metaData(MetaData.builder(currentState.metaData())
// .updateNumberOfReplicas(numberOfReplicas, index.getName())).build());
// ConditionalWaitStep isReplicatedCheck = new ConditionalWaitStep("wait_replicas_allocated", NAME,
// phase, index.getName(), (currentState) -> ActiveShardCount.ALL.enoughShardsActive(currentState, index.getName()) );
return Arrays.asList();
StepKey updateReplicasKey = new StepKey(phase, NAME, UpdateReplicaSettingsStep.NAME);
StepKey enoughKey = new StepKey(phase, NAME, EnoughShardsWaitStep.NAME);
return Arrays.asList(new UpdateReplicaSettingsStep(updateReplicasKey, enoughKey, numberOfReplicas),
new EnoughShardsWaitStep(enoughKey, nextStepKey));
}
public int getNumberOfReplicas() {

View File

@ -0,0 +1,60 @@
/*
* 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.core.indexlifecycle;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import java.util.Objects;
public class UpdateReplicaSettingsStep extends ClusterStateActionStep {
public static final String NAME = "update-replicas";
private int numberOfReplicas;
public UpdateReplicaSettingsStep(StepKey key, StepKey nextStepKey, int numberOfReplicas) {
super(key, nextStepKey);
this.numberOfReplicas = numberOfReplicas;
}
@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetaData idxMeta = clusterState.metaData().index(index);
if (idxMeta == null) {
return clusterState;
}
Settings.Builder newSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas);
return ClusterState.builder(clusterState)
.metaData(MetaData.builder(clusterState.metaData())
.updateSettings(newSettings.build(), index.getName())).build();
}
public int getNumberOfReplicas() {
return numberOfReplicas;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), numberOfReplicas);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
UpdateReplicaSettingsStep other = (UpdateReplicaSettingsStep) obj;
return super.equals(obj) &&
Objects.equals(numberOfReplicas, other.numberOfReplicas);
}
}

View File

@ -5,36 +5,13 @@
*/
package org.elasticsearch.xpack.core.indexlifecycle;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsTestHelper;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.AbstractSerializingTestCase;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
import java.io.IOException;
import java.util.List;
public class ReplicasActionTests extends AbstractSerializingTestCase<ReplicasAction> {
@ -64,310 +41,22 @@ public class ReplicasActionTests extends AbstractSerializingTestCase<ReplicasAct
assertEquals("[" + ReplicasAction.NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0", exception.getMessage());
}
// public void testExecuteDifferentReplicaCount() {
// int existingNumReplicas = randomIntBetween(0, 10);
// int newNumReplicas = randomValueOtherThan(existingNumReplicas, () -> randomIntBetween(0, 10));
//
// ReplicasAction action = new ReplicasAction(newNumReplicas);
//
// IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLengthBetween(1, 20))
// .settings(Settings.builder().put("index.version.created", Version.CURRENT.id)).numberOfShards(randomIntBetween(1, 5))
// .numberOfReplicas(existingNumReplicas).build();
// Index index = indexMetadata.getIndex();
// ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
// indexMetadata);
// ClusterState clusterstate = ClusterState.builder(ClusterState.EMPTY_STATE).metaData(MetaData.builder().indices(indices.build()))
// .build();
//
// Client client = Mockito.mock(Client.class);
// AdminClient adminClient = Mockito.mock(AdminClient.class);
// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
// ClusterService clusterService = Mockito.mock(ClusterService.class);
//
// Mockito.when(client.admin()).thenReturn(adminClient);
// Mockito.when(adminClient.indices()).thenReturn(indicesClient);
// Mockito.doAnswer(new Answer<Void>() {
//
// @Override
// public Void answer(InvocationOnMock invocation) throws Throwable {
// UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
// @SuppressWarnings("unchecked")
// ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
// Settings expectedSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, newNumReplicas).build();
// UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, index.getName());
// listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
// return null;
// }
//
// }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
// Mockito.when(clusterService.state()).thenReturn(clusterstate);
//
// SetOnce<Boolean> actionCompleted = new SetOnce<>();
// action.execute(index, client, clusterService, new Listener() {
//
// @Override
// public void onSuccess(boolean completed) {
// actionCompleted.set(completed);
// }
//
// @Override
// public void onFailure(Exception e) {
// throw new AssertionError("Unexpected method call", e);
// }
// });
//
// assertEquals(false, actionCompleted.get());
//
// Mockito.verify(client, Mockito.only()).admin();
// Mockito.verify(adminClient, Mockito.only()).indices();
// Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any());
// Mockito.verify(clusterService, Mockito.only()).state();
// }
//
// public void testExecuteUpdateReplicaCountFailure() {
// int existingNumReplicas = randomIntBetween(0, 10);
// int newNumReplicas = randomValueOtherThan(existingNumReplicas, () -> randomIntBetween(0, 10));
//
// ReplicasAction action = new ReplicasAction(newNumReplicas);
// Exception exception = new RuntimeException();
//
// IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLengthBetween(1, 20))
// .settings(Settings.builder().put("index.version.created", Version.CURRENT.id)).numberOfShards(randomIntBetween(1, 5))
// .numberOfReplicas(existingNumReplicas).build();
// Index index = indexMetadata.getIndex();
// ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
// indexMetadata);
// ClusterState clusterstate = ClusterState.builder(ClusterState.EMPTY_STATE).metaData(MetaData.builder().indices(indices.build()))
// .build();
//
// Client client = Mockito.mock(Client.class);
// AdminClient adminClient = Mockito.mock(AdminClient.class);
// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
// ClusterService clusterService = Mockito.mock(ClusterService.class);
//
// Mockito.when(client.admin()).thenReturn(adminClient);
// Mockito.when(adminClient.indices()).thenReturn(indicesClient);
// Mockito.doAnswer(new Answer<Void>() {
//
// @Override
// public Void answer(InvocationOnMock invocation) throws Throwable {
// UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
// @SuppressWarnings("unchecked")
// ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
// Settings expectedSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, newNumReplicas).build();
// UpdateSettingsTestHelper.assertSettingsRequest(request, expectedSettings, index.getName());
// listener.onFailure(exception);
// return null;
// }
//
// }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any());
// Mockito.when(clusterService.state()).thenReturn(clusterstate);
//
// SetOnce<Boolean> exceptionThrown = new SetOnce<>();
// action.execute(index, client, clusterService, new Listener() {
//
// @Override
// public void onSuccess(boolean completed) {
// throw new AssertionError("Unexpected method call");
// }
//
// @Override
// public void onFailure(Exception e) {
// exceptionThrown.set(true);
// }
// });
//
// assertEquals(true, exceptionThrown.get());
//
// Mockito.verify(client, Mockito.only()).admin();
// Mockito.verify(adminClient, Mockito.only()).indices();
// Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any());
// Mockito.verify(clusterService, Mockito.only()).state();
// }
//
// public void testExecuteAllocationNotComplete() {
//
// ReplicasAction action = new ReplicasAction(randomIntBetween(1, 10));
//
// int numberOfShards = randomIntBetween(1, 5);
// int numberOfReplicas = action.getNumberOfReplicas();
// IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLengthBetween(1, 20))
// .settings(Settings.builder().put("index.version.created", Version.CURRENT.id)).numberOfShards(numberOfShards)
// .numberOfReplicas(numberOfReplicas).build();
// Index index = indexMetadata.getIndex();
// ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
// indexMetadata);
// IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index);
// for (int shard = 0; shard < numberOfShards; shard++) {
// for (int replica = 0; replica < numberOfReplicas + 1; replica++) {
// ShardRoutingState state;
// if (replica == 0) {
// state = ShardRoutingState.STARTED;
// } else if ((replica == numberOfReplicas) || randomBoolean()) {
// state = randomFrom(ShardRoutingState.UNASSIGNED, ShardRoutingState.INITIALIZING);
// } else {
// state = ShardRoutingState.STARTED;
// }
// String nodeId = "node" + replica;
// if (ShardRoutingState.UNASSIGNED.equals(state)) {
// nodeId = null;
// }
// indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(index, shard), nodeId, replica == 0, state));
// }
// }
// ClusterState clusterstate = ClusterState.builder(ClusterState.EMPTY_STATE).metaData(MetaData.builder().indices(indices.build()))
// .routingTable(RoutingTable.builder().add(indexRoutingTable).build())
// .build();
//
// Client client = Mockito.mock(Client.class);
// AdminClient adminClient = Mockito.mock(AdminClient.class);
// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
// ClusterService clusterService = Mockito.mock(ClusterService.class);
//
// Mockito.when(client.admin()).thenReturn(adminClient);
// Mockito.when(adminClient.indices()).thenReturn(indicesClient);
// Mockito.when(clusterService.state()).thenReturn(clusterstate);
//
// SetOnce<Boolean> actionCompleted = new SetOnce<>();
// action.execute(index, client, clusterService, new Listener() {
//
// @Override
// public void onSuccess(boolean completed) {
// actionCompleted.set(completed);
// }
//
// @Override
// public void onFailure(Exception e) {
// throw new AssertionError("Unexpected method call", e);
// }
// });
//
// assertEquals(false, actionCompleted.get());
//
// Mockito.verify(clusterService, Mockito.times(2)).state();
// Mockito.verify(client, Mockito.never()).admin();
// Mockito.verify(adminClient, Mockito.never()).indices();
// Mockito.verify(indicesClient, Mockito.never()).updateSettings(Mockito.any(), Mockito.any());
// }
//
// public void testExecuteAllocationUnassignedPrimaries() {
//
// ReplicasAction action = createTestInstance();
//
// int numberOfShards = randomIntBetween(1, 5);
// int numberOfReplicas = action.getNumberOfReplicas();
// IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLengthBetween(1, 20))
// .settings(Settings.builder().put("index.version.created", Version.CURRENT.id)).numberOfShards(numberOfShards)
// .numberOfReplicas(numberOfReplicas).build();
// Index index = indexMetadata.getIndex();
// ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
// indexMetadata);
// IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index);
// for (int shard = 0; shard < numberOfShards; shard++) {
// boolean unassignedPrimary = shard == 0 || randomBoolean();
// for (int replica = 0; replica < numberOfReplicas + 1; replica++) {
// ShardRoutingState state;
// if (unassignedPrimary) {
// state = ShardRoutingState.UNASSIGNED;
// } else if (replica == 0) {
// state = ShardRoutingState.STARTED;
// } else if ((replica == numberOfReplicas) || randomBoolean()) {
// state = randomFrom(ShardRoutingState.UNASSIGNED, ShardRoutingState.INITIALIZING);
// } else {
// state = ShardRoutingState.STARTED;
// }
// String nodeId = "node" + replica;
// if (ShardRoutingState.UNASSIGNED.equals(state)) {
// nodeId = null;
// }
// indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(index, shard), nodeId, replica == 0, state));
// }
// }
// ClusterState clusterstate = ClusterState.builder(ClusterState.EMPTY_STATE).metaData(MetaData.builder().indices(indices.build()))
// .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build();
//
// Client client = Mockito.mock(Client.class);
// AdminClient adminClient = Mockito.mock(AdminClient.class);
// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
// ClusterService clusterService = Mockito.mock(ClusterService.class);
//
// Mockito.when(client.admin()).thenReturn(adminClient);
// Mockito.when(adminClient.indices()).thenReturn(indicesClient);
// Mockito.when(clusterService.state()).thenReturn(clusterstate);
//
// SetOnce<Boolean> actionCompleted = new SetOnce<>();
// action.execute(index, client, clusterService, new Listener() {
//
// @Override
// public void onSuccess(boolean completed) {
// actionCompleted.set(completed);
// }
//
// @Override
// public void onFailure(Exception e) {
// throw new AssertionError("Unexpected method call", e);
// }
// });
//
// assertEquals(false, actionCompleted.get());
//
// Mockito.verify(clusterService, Mockito.times(2)).state();
// Mockito.verify(client, Mockito.never()).admin();
// Mockito.verify(adminClient, Mockito.never()).indices();
// Mockito.verify(indicesClient, Mockito.never()).updateSettings(Mockito.any(), Mockito.any());
// }
//
// public void testExecuteAllocationComplete() {
//
// ReplicasAction action = createTestInstance();
//
// int numberOfShards = randomIntBetween(1, 5);
// int numberOfReplicas = action.getNumberOfReplicas();
// IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLengthBetween(1, 20))
// .settings(Settings.builder().put("index.version.created", Version.CURRENT.id)).numberOfShards(numberOfShards)
// .numberOfReplicas(numberOfReplicas).build();
// Index index = indexMetadata.getIndex();
// ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
// indexMetadata);
// IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index);
// for (int shard = 0; shard < numberOfShards; shard++) {
// for (int replica = 0; replica < numberOfReplicas + 1; replica++) {
// ShardRoutingState state = ShardRoutingState.STARTED;
// String nodeId = "node" + replica;
// indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(index, shard), nodeId, replica == 0, state));
// }
// }
// ClusterState clusterstate = ClusterState.builder(ClusterState.EMPTY_STATE).metaData(MetaData.builder().indices(indices.build()))
// .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build();
//
// Client client = Mockito.mock(Client.class);
// AdminClient adminClient = Mockito.mock(AdminClient.class);
// IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
// ClusterService clusterService = Mockito.mock(ClusterService.class);
//
// Mockito.when(client.admin()).thenReturn(adminClient);
// Mockito.when(adminClient.indices()).thenReturn(indicesClient);
// Mockito.when(clusterService.state()).thenReturn(clusterstate);
//
// SetOnce<Boolean> actionCompleted = new SetOnce<>();
// action.execute(index, client, clusterService, new Listener() {
//
// @Override
// public void onSuccess(boolean completed) {
// actionCompleted.set(completed);
// }
//
// @Override
// public void onFailure(Exception e) {
// throw new AssertionError("Unexpected method call", e);
// }
// });
//
// assertEquals(true, actionCompleted.get());
//
// Mockito.verify(clusterService, Mockito.times(2)).state();
// Mockito.verify(client, Mockito.never()).admin();
// Mockito.verify(adminClient, Mockito.never()).indices();
// Mockito.verify(indicesClient, Mockito.never()).updateSettings(Mockito.any(), Mockito.any());
// }
public void testToSteps() {
ReplicasAction action = createTestInstance();
String phase = randomAlphaOfLengthBetween(1, 10);
StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10),
randomAlphaOfLengthBetween(1, 10));
List<Step> steps = action.toSteps(null, phase, nextStepKey);
assertNotNull(steps);
assertEquals(2, steps.size());
StepKey expectedFirstStepKey = new StepKey(phase, ReplicasAction.NAME, UpdateReplicaSettingsStep.NAME);
StepKey expectedSecondStepKey = new StepKey(phase, ReplicasAction.NAME, EnoughShardsWaitStep.NAME);
UpdateReplicaSettingsStep firstStep = (UpdateReplicaSettingsStep) steps.get(0);
assertEquals(expectedFirstStepKey, firstStep.getKey());
assertEquals(expectedSecondStepKey, firstStep.getNextStepKey());
assertEquals(action.getNumberOfReplicas(), firstStep.getNumberOfReplicas());
EnoughShardsWaitStep secondStep = (EnoughShardsWaitStep) steps.get(1);
assertEquals(expectedSecondStepKey, secondStep.getKey());
assertEquals(nextStepKey, secondStep.getNextStepKey());
}
}

View File

@ -91,6 +91,7 @@ public class UpdateAllocationSettingsStepTests extends ESTestCase {
UpdateAllocationSettingsStep step = createRandomInstance();
ClusterState newState = step.performAction(index, clusterState);
assertNotSame(clusterState, newState);
assertThat(getRouting(index, newState, IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey()), equalTo(step.getInclude()));
assertThat(getRouting(index, newState, IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey()), equalTo(step.getExclude()));
assertThat(getRouting(index, newState, IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey()), equalTo(step.getRequire()));

View File

@ -0,0 +1,91 @@
/*
* 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.core.indexlifecycle;
import com.carrotsearch.randomizedtesting.annotations.Repeat;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.index.Index;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
@Repeat(iterations = 1000, useConstantSeed = false)
public class UpdateReplicaSettingsStepTests extends ESTestCase {
public UpdateReplicaSettingsStep createRandomInstance() {
StepKey stepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
StepKey nextStepKey = new StepKey(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10));
return new UpdateReplicaSettingsStep(stepKey, nextStepKey, randomIntBetween(0, 100));
}
public UpdateReplicaSettingsStep mutateInstance(UpdateReplicaSettingsStep instance) {
StepKey key = instance.getKey();
StepKey nextKey = instance.getNextStepKey();
int replicas = instance.getNumberOfReplicas();
switch (between(0, 2)) {
case 0:
key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
break;
case 1:
nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
break;
case 2:
replicas += 1;
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new UpdateReplicaSettingsStep(key, nextKey, replicas);
}
public void testHashcodeAndEquals() {
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(),
instance -> new UpdateReplicaSettingsStep(instance.getKey(), instance.getNextStepKey(), instance.getNumberOfReplicas()),
this::mutateInstance);
}
public void testPerformAction() {
IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5))
.settings(settings(Version.CURRENT))
.numberOfShards(1)
.numberOfReplicas(0).build();
MetaData metaData = MetaData.builder()
.persistentSettings(settings(Version.CURRENT).build())
.put(IndexMetaData.builder(indexMetadata))
.build();
Index index = indexMetadata.getIndex();
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build();
UpdateReplicaSettingsStep step = createRandomInstance();
ClusterState newState = step.performAction(index, clusterState);
assertNotSame(clusterState, newState);
IndexMetaData newIndexMetadata = newState.metaData().index(index);
assertNotNull(newIndexMetadata);
assertNotSame(indexMetadata, newIndexMetadata);
assertTrue(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(newIndexMetadata.getSettings()));
assertEquals(step.getNumberOfReplicas(),
(int) IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.get(newIndexMetadata.getSettings()));
}
public void testPerformActionNoIndex() {
MetaData metaData = MetaData.builder().persistentSettings(settings(Version.CURRENT).build()).build();
Index index = new Index("invalid_index", "invalid_index_id");
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build();
UpdateReplicaSettingsStep step = createRandomInstance();
ClusterState newState = step.performAction(index, clusterState);
assertSame(clusterState, newState);
}
}