Skip to content

Commit

Permalink
Close API might cause index data to be wiped, closes elastic#560.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Dec 10, 2010
1 parent bc2dc94 commit a914865
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ public LocalGatewayStartedShards currentStartedShards() {
builder.state(currentStartedShards);
}
builder.version(event.state().version());
// remove from the current state all the shards that are primary and started, we won't need them anymore
// remove from the current state all the shards that are primary and started somewhere, we won't need them anymore
// and if they are still here, we will add them in the next phase

// Also note, this works well when closing an index, since a closed index will have no routing shards entries
// so they won't get removed (we want to keep the fact that those shards are allocated on this node if needed)
for (IndexRoutingTable indexRoutingTable : event.state().routingTable()) {
for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
if (indexShardRoutingTable.primaryShard().active()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ private void applyDeletedShards(final ClusterChangedEvent event) {
return;
}
for (final String index : indicesService.indices()) {
if (event.state().metaData().hasIndex(index)) {
IndexMetaData indexMetaData = event.state().metaData().index(index);
if (indexMetaData != null) {
// now, go over and delete shards that needs to get deleted
Set<Integer> newShardIds = newHashSet();
for (final ShardRouting shardRouting : routingNodes) {
Expand All @@ -183,10 +184,17 @@ private void applyDeletedShards(final ClusterChangedEvent event) {
}
for (Integer existingShardId : indexService.shardIds()) {
if (!newShardIds.contains(existingShardId)) {
if (logger.isDebugEnabled()) {
logger.debug("[{}][{}] deleting shard", index, existingShardId);
if (indexMetaData.state() == IndexMetaData.State.CLOSE) {
if (logger.isDebugEnabled()) {
logger.debug("[{}][{}] removing shard (index is closed)", index, existingShardId);
}
indexService.removeShard(existingShardId);
} else {
if (logger.isDebugEnabled()) {
logger.debug("[{}][{}] cleaning shard locally (not allocated)", index, existingShardId);
}
indexService.cleanShard(existingShardId);
}
indexService.cleanShard(existingShardId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ public class LocalGatewayIndexStateTests extends AbstractNodesTests {
// all is well
}

logger.info("--> creating another index (test2) by indexing into it");
client("node1").prepareIndex("test2", "type1", "1").setSource("field1", "value1").execute().actionGet();
logger.info("--> verifying that the state is green");
health = client("node1").admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet();
assertThat(health.timedOut(), equalTo(false));
assertThat(health.status(), equalTo(ClusterHealthStatus.GREEN));

logger.info("--> opening the first index again...");
client("node1").admin().indices().prepareOpen("test").execute().actionGet();

logger.info("--> verifying that the state is green");
health = client("node1").admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet();
assertThat(health.timedOut(), equalTo(false));
assertThat(health.status(), equalTo(ClusterHealthStatus.GREEN));

stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet();
assertThat(stateResponse.state().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN));
assertThat(stateResponse.state().routingTable().index("test").shards().size(), equalTo(2));
assertThat(stateResponse.state().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(4));

logger.info("--> trying to get the indexed document on the first index");
GetResponse getResponse = client("node1").prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));

logger.info("--> closing test index...");
client("node1").admin().indices().prepareClose("test").execute().actionGet();
stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet();
assertThat(stateResponse.state().metaData().index("test").state(), equalTo(IndexMetaData.State.CLOSE));
assertThat(stateResponse.state().routingTable().index("test"), nullValue());

logger.info("--> closing nodes...");
closeNode("node2");
closeNode("node1");
Expand All @@ -156,8 +186,8 @@ public class LocalGatewayIndexStateTests extends AbstractNodesTests {
startNode("node1", settingsBuilder().put("gateway.type", "local").build());
startNode("node2", settingsBuilder().put("gateway.type", "local").build());

logger.info("--> waiting for two nodes");
health = client("node1").admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet();
logger.info("--> waiting for two nodes and green status");
health = client("node1").admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet();
assertThat(health.timedOut(), equalTo(false));

stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet();
Expand Down Expand Up @@ -185,7 +215,7 @@ public class LocalGatewayIndexStateTests extends AbstractNodesTests {
assertThat(stateResponse.state().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(4));

logger.info("--> trying to get the indexed document on the first round (before close and shutdown)");
GetResponse getResponse = client("node1").prepareGet("test", "type1", "1").execute().actionGet();
getResponse = client("node1").prepareGet("test", "type1", "1").execute().actionGet();
assertThat(getResponse.exists(), equalTo(true));

logger.info("--> indexing a simple document");
Expand Down

0 comments on commit a914865

Please sign in to comment.