diff options
Diffstat (limited to 'core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java')
-rw-r--r-- | core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java | 37 |
1 files changed, 14 insertions, 23 deletions
diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java index 388d454813..cf22462865 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.cluster.routing; import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ESAllocationTestCase; import org.elasticsearch.cluster.health.ClusterStateHealth; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; @@ -28,9 +29,7 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.node.DiscoveryNodes.Builder; import org.elasticsearch.cluster.routing.allocation.AllocationService; import org.elasticsearch.cluster.routing.allocation.FailedRerouteAllocation; -import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.cluster.ESAllocationTestCase; import java.util.ArrayList; import java.util.Collections; @@ -42,13 +41,12 @@ import java.util.Set; import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; public class PrimaryTermsTests extends ESAllocationTestCase { private static final String TEST_INDEX_1 = "test1"; private static final String TEST_INDEX_2 = "test2"; - private RoutingTable testRoutingTable; private int numberOfShards; private int numberOfReplicas; private static final Settings DEFAULT_SETTINGS = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); @@ -73,7 +71,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { .put(createIndexMetaData(TEST_INDEX_2)) .build(); - this.testRoutingTable = new RoutingTable.Builder() + RoutingTable routingTable = new RoutingTable.Builder() .add(new IndexRoutingTable.Builder(metaData.index(TEST_INDEX_1).getIndex()).initializeAsNew(metaData.index(TEST_INDEX_1)) .build()) .add(new IndexRoutingTable.Builder(metaData.index(TEST_INDEX_2).getIndex()).initializeAsNew(metaData.index(TEST_INDEX_2)) @@ -81,7 +79,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { .build(); this.clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) - .metaData(metaData).routingTable(testRoutingTable).build(); + .metaData(metaData).routingTable(routingTable).build(); } /** @@ -94,9 +92,8 @@ public class PrimaryTermsTests extends ESAllocationTestCase { discoBuilder = discoBuilder.add(newNode("node" + i)); } this.clusterState = ClusterState.builder(clusterState).nodes(discoBuilder).build(); - RoutingAllocation.Result rerouteResult = allocationService.reroute(clusterState, "reroute"); - this.testRoutingTable = rerouteResult.routingTable(); - assertThat(rerouteResult.changed(), is(true)); + ClusterState rerouteResult = allocationService.reroute(clusterState, "reroute"); + assertThat(rerouteResult, not(equalTo(this.clusterState))); applyRerouteResult(rerouteResult); primaryTermsPerIndex.keySet().forEach(this::incrementPrimaryTerm); } @@ -113,17 +110,16 @@ public class PrimaryTermsTests extends ESAllocationTestCase { } private boolean startInitializingShards(String index) { - this.clusterState = ClusterState.builder(clusterState).routingTable(this.testRoutingTable).build(); final List<ShardRouting> startedShards = this.clusterState.getRoutingNodes().shardsWithState(index, INITIALIZING); logger.info("start primary shards for index [{}]: {} ", index, startedShards); - RoutingAllocation.Result rerouteResult = allocationService.applyStartedShards(this.clusterState, startedShards); + ClusterState rerouteResult = allocationService.applyStartedShards(this.clusterState, startedShards); + boolean changed = rerouteResult.equals(this.clusterState) == false; applyRerouteResult(rerouteResult); - return rerouteResult.changed(); + return changed; } - private void applyRerouteResult(RoutingAllocation.Result rerouteResult) { + private void applyRerouteResult(ClusterState newClusterState) { ClusterState previousClusterState = this.clusterState; - ClusterState newClusterState = ClusterState.builder(previousClusterState).routingResult(rerouteResult).build(); ClusterState.Builder builder = ClusterState.builder(newClusterState).incrementVersion(); if (previousClusterState.routingTable() != newClusterState.routingTable()) { builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1) @@ -133,7 +129,6 @@ public class PrimaryTermsTests extends ESAllocationTestCase { builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1)); } this.clusterState = builder.build(); - this.testRoutingTable = rerouteResult.routingTable(); final ClusterStateHealth clusterHealth = new ClusterStateHealth(clusterState); logger.info("applied reroute. active shards: p [{}], t [{}], init shards: [{}], relocating: [{}]", clusterHealth.getActivePrimaryShards(), clusterHealth.getActiveShards(), @@ -141,8 +136,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { } private void failSomePrimaries(String index) { - this.clusterState = ClusterState.builder(clusterState).routingTable(this.testRoutingTable).build(); - final IndexRoutingTable indexShardRoutingTable = testRoutingTable.index(index); + final IndexRoutingTable indexShardRoutingTable = clusterState.routingTable().index(index); Set<Integer> shardIdsToFail = new HashSet<>(); for (int i = 1 + randomInt(numberOfShards - 1); i > 0; i--) { shardIdsToFail.add(randomInt(numberOfShards - 1)); @@ -153,9 +147,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { failedShards.add(new FailedRerouteAllocation.FailedShard(indexShardRoutingTable.shard(shard).primaryShard(), "test", null)); incrementPrimaryTerm(index, shard); // the primary failure should increment the primary term; } - RoutingAllocation.Result rerouteResult = allocationService.applyFailedShards(this.clusterState, failedShards, - Collections.emptyList()); - applyRerouteResult(rerouteResult); + applyRerouteResult(allocationService.applyFailedShards(this.clusterState, failedShards,Collections.emptyList())); } private void addNodes() { @@ -166,8 +158,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { nodesBuilder.add(newNode("extra_" + i)); } this.clusterState = ClusterState.builder(clusterState).nodes(nodesBuilder).build(); - RoutingAllocation.Result rerouteResult = allocationService.reroute(this.clusterState, "nodes added"); - applyRerouteResult(rerouteResult); + applyRerouteResult(allocationService.reroute(this.clusterState, "nodes added")); } @@ -191,7 +182,7 @@ public class PrimaryTermsTests extends ESAllocationTestCase { private void assertPrimaryTerm(String index) { final long[] terms = primaryTermsPerIndex.get(index); final IndexMetaData indexMetaData = clusterState.metaData().index(index); - for (IndexShardRoutingTable shardRoutingTable : this.testRoutingTable.index(index)) { + for (IndexShardRoutingTable shardRoutingTable : this.clusterState.routingTable().index(index)) { final int shard = shardRoutingTable.shardId().id(); assertThat("primary term mismatch between indexMetaData of [" + index + "] and shard [" + shard + "]'s routing", indexMetaData.primaryTerm(shard), equalTo(terms[shard])); |