diff --git a/src/test/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardStateTests.java b/src/test/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardStateTests.java index a862cc3e7c4..955404bc2bb 100644 --- a/src/test/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardStateTests.java +++ b/src/test/java/org/elasticsearch/gateway/local/state/shards/LocalGatewayShardStateTests.java @@ -18,22 +18,15 @@ */ package org.elasticsearch.gateway.local.state.shards; -import com.carrotsearch.hppc.cursors.ObjectCursor; 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.cluster.node.DiscoveryNode; -import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.routing.*; import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ElasticsearchTestCase; -import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; @@ -41,59 +34,47 @@ import java.util.Map; public class LocalGatewayShardStateTests extends ElasticsearchTestCase { public void testWriteShardState() throws Exception { - LocalGatewayShardsState state = new LocalGatewayShardsState(ImmutableSettings.EMPTY, newNodeEnvironment(), null); - ShardId id = new ShardId("foo", 1); - long version = between(1, Integer.MAX_VALUE / 2); - boolean primary = randomBoolean(); - ShardStateInfo state1 = new ShardStateInfo(version, primary); - state.maybeWriteShardState(id, state1, null); - ShardStateInfo shardStateInfo = state.loadShardInfo(id); - assertEquals(shardStateInfo, state1); + try (NodeEnvironment env = newNodeEnvironment()) { + LocalGatewayShardsState state = new LocalGatewayShardsState(ImmutableSettings.EMPTY, env, null); + ShardId id = new ShardId("foo", 1); + long version = between(1, Integer.MAX_VALUE / 2); + boolean primary = randomBoolean(); + ShardStateInfo state1 = new ShardStateInfo(version, primary); + state.maybeWriteShardState(id, state1, null); + ShardStateInfo shardStateInfo = state.loadShardInfo(id); + assertEquals(shardStateInfo, state1); - ShardStateInfo state2 = new ShardStateInfo(version, primary); - state.maybeWriteShardState(id, state2, state1); - shardStateInfo = state.loadShardInfo(id); - assertEquals(shardStateInfo, state1); - - ShardStateInfo state3 = new ShardStateInfo(version+1, primary); - state.maybeWriteShardState(id, state3, state1); - shardStateInfo = state.loadShardInfo(id); - assertEquals(shardStateInfo, state3); - assertTrue(state.getCurrentState().isEmpty()); + ShardStateInfo state2 = new ShardStateInfo(version, primary); + state.maybeWriteShardState(id, state2, state1); + shardStateInfo = state.loadShardInfo(id); + assertEquals(shardStateInfo, state1); + ShardStateInfo state3 = new ShardStateInfo(version + 1, primary); + state.maybeWriteShardState(id, state3, state1); + shardStateInfo = state.loadShardInfo(id); + assertEquals(shardStateInfo, state3); + assertTrue(state.getCurrentState().isEmpty()); + } } public void testPersistRoutingNode() throws Exception { - LocalGatewayShardsState state = new LocalGatewayShardsState(ImmutableSettings.EMPTY, newNodeEnvironment(), null); - int numShards = between(0, 100); - List shards = new ArrayList<>(); - List active = new ArrayList<>(); - for (int i = 0; i < numShards; i++) { - long version = between(1, Integer.MAX_VALUE / 2); - ShardRoutingState shardRoutingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.RELOCATING, ShardRoutingState.STARTED); - MutableShardRouting mutableShardRouting = new MutableShardRouting("idx", i, "foo", randomBoolean(), shardRoutingState, version); - if (mutableShardRouting.active()) { - active.add(mutableShardRouting); + try (NodeEnvironment env = newNodeEnvironment()) { + LocalGatewayShardsState state = new LocalGatewayShardsState(ImmutableSettings.EMPTY, env, null); + int numShards = between(0, 100); + List shards = new ArrayList<>(); + List active = new ArrayList<>(); + for (int i = 0; i < numShards; i++) { + long version = between(1, Integer.MAX_VALUE / 2); + ShardRoutingState shardRoutingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.RELOCATING, ShardRoutingState.STARTED); + MutableShardRouting mutableShardRouting = new MutableShardRouting("idx", i, "foo", randomBoolean(), shardRoutingState, version); + if (mutableShardRouting.active()) { + active.add(mutableShardRouting); + } + shards.add(mutableShardRouting); } - shards.add(mutableShardRouting); - } - RoutingNode node = new RoutingNode("foo", new DiscoveryNode("foo", null, Version.CURRENT), shards); + RoutingNode node = new RoutingNode("foo", new DiscoveryNode("foo", null, Version.CURRENT), shards); - Map shardIdShardStateInfoMap = state.persistRoutingNodeState(node); - assertEquals(shardIdShardStateInfoMap.size(), active.size()); - for (Map.Entry written : shardIdShardStateInfoMap.entrySet()) { - ShardStateInfo shardStateInfo = state.loadShardInfo(written.getKey()); - assertEquals(shardStateInfo, written.getValue()); - if (randomBoolean()) { - assertNull(state.loadShardInfo(new ShardId("no_such_index", written.getKey().id()))); - } - } - assertTrue(state.getCurrentState().isEmpty()); - - state.getCurrentState().putAll(shardIdShardStateInfoMap); - - if (randomBoolean()) { // sometimes write the same thing twice - shardIdShardStateInfoMap = state.persistRoutingNodeState(node); + Map shardIdShardStateInfoMap = state.persistRoutingNodeState(node); assertEquals(shardIdShardStateInfoMap.size(), active.size()); for (Map.Entry written : shardIdShardStateInfoMap.entrySet()) { ShardStateInfo shardStateInfo = state.loadShardInfo(written.getKey()); @@ -102,23 +83,38 @@ public class LocalGatewayShardStateTests extends ElasticsearchTestCase { assertNull(state.loadShardInfo(new ShardId("no_such_index", written.getKey().id()))); } } - } + assertTrue(state.getCurrentState().isEmpty()); - List nextRoundOfShards = new ArrayList<>(); + state.getCurrentState().putAll(shardIdShardStateInfoMap); - for (MutableShardRouting routing : shards) { - nextRoundOfShards.add(new MutableShardRouting(routing, routing.version() + 1)); - } - node = new RoutingNode("foo", new DiscoveryNode("foo", null, Version.CURRENT), nextRoundOfShards); - Map shardIdShardStateInfoMapNew = state.persistRoutingNodeState(node); - assertEquals(shardIdShardStateInfoMapNew.size(), active.size()); - for (Map.Entry written : shardIdShardStateInfoMapNew.entrySet()) { - ShardStateInfo shardStateInfo = state.loadShardInfo(written.getKey()); - assertEquals(shardStateInfo, written.getValue()); - ShardStateInfo oldStateInfo = shardIdShardStateInfoMap.get(written.getKey()); - assertEquals(oldStateInfo.version, written.getValue().version - 1); - if (randomBoolean()) { - assertNull(state.loadShardInfo(new ShardId("no_such_index", written.getKey().id()))); + if (randomBoolean()) { // sometimes write the same thing twice + shardIdShardStateInfoMap = state.persistRoutingNodeState(node); + assertEquals(shardIdShardStateInfoMap.size(), active.size()); + for (Map.Entry written : shardIdShardStateInfoMap.entrySet()) { + ShardStateInfo shardStateInfo = state.loadShardInfo(written.getKey()); + assertEquals(shardStateInfo, written.getValue()); + if (randomBoolean()) { + assertNull(state.loadShardInfo(new ShardId("no_such_index", written.getKey().id()))); + } + } + } + + List nextRoundOfShards = new ArrayList<>(); + + for (MutableShardRouting routing : shards) { + nextRoundOfShards.add(new MutableShardRouting(routing, routing.version() + 1)); + } + node = new RoutingNode("foo", new DiscoveryNode("foo", null, Version.CURRENT), nextRoundOfShards); + Map shardIdShardStateInfoMapNew = state.persistRoutingNodeState(node); + assertEquals(shardIdShardStateInfoMapNew.size(), active.size()); + for (Map.Entry written : shardIdShardStateInfoMapNew.entrySet()) { + ShardStateInfo shardStateInfo = state.loadShardInfo(written.getKey()); + assertEquals(shardStateInfo, written.getValue()); + ShardStateInfo oldStateInfo = shardIdShardStateInfoMap.get(written.getKey()); + assertEquals(oldStateInfo.version, written.getValue().version - 1); + if (randomBoolean()) { + assertNull(state.loadShardInfo(new ShardId("no_such_index", written.getKey().id()))); + } } } }