[TEST] Close the node env after test is done

This commit is contained in:
Simon Willnauer 2014-12-11 21:21:09 +01:00
parent 3877dc618d
commit dac520170f
1 changed files with 65 additions and 69 deletions

View File

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