Moved SimpleIndexStateTests to AbstractIntegrationTest

This commit is contained in:
Luca Cavanna 2013-09-21 02:10:31 +02:00
parent c6eebf0515
commit 2ca1d79530
1 changed files with 32 additions and 48 deletions

View File

@ -19,24 +19,23 @@
package org.elasticsearch.indices.state; package org.elasticsearch.indices.state;
import junit.framework.Assert;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.status.IndicesStatusResponse; import org.elasticsearch.action.admin.indices.status.IndicesStatusResponse;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException; import org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException;
import org.elasticsearch.test.AbstractNodesTests; import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
@ -46,131 +45,116 @@ import static org.hamcrest.Matchers.nullValue;
/** /**
* *
*/ */
public class SimpleIndexStateTests extends AbstractNodesTests { public class SimpleIndexStateTests extends AbstractIntegrationTest {
private final ESLogger logger = Loggers.getLogger(SimpleIndexStateTests.class); private final ESLogger logger = Loggers.getLogger(SimpleIndexStateTests.class);
@After
public void closeNodes() {
closeAllNodes();
}
@Test @Test
public void testSimpleOpenClose() { public void testSimpleOpenClose() {
logger.info("--> starting two nodes....");
startNode("node1");
startNode("node2");
logger.info("--> creating test index"); logger.info("--> creating test index");
client("node1").admin().indices().prepareCreate("test").execute().actionGet(); createIndex("test");
logger.info("--> waiting for green status"); logger.info("--> waiting for green status");
ClusterHealthResponse health = client("node1").admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet(); ensureGreen();
assertThat(health.isTimedOut(), equalTo(false));
ClusterStateResponse stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet(); ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN)); assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN));
assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5)); assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5));
assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10));
logger.info("--> indexing a simple document"); logger.info("--> indexing a simple document");
client("node1").prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
logger.info("--> closing test index..."); logger.info("--> closing test index...");
client("node1").admin().indices().prepareClose("test").execute().actionGet(); CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose("test").get();
assertThat(closeIndexResponse.isAcknowledged(), equalTo(true));
stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet(); stateResponse = client().admin().cluster().prepareState().get();
assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.CLOSE)); assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.CLOSE));
assertThat(stateResponse.getState().routingTable().index("test"), nullValue()); assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
logger.info("--> testing indices status api..."); logger.info("--> testing indices status api...");
IndicesStatusResponse indicesStatusResponse = client("node1").admin().indices().prepareStatus().execute().actionGet(); IndicesStatusResponse indicesStatusResponse = client().admin().indices().prepareStatus().get();
assertThat(indicesStatusResponse.getIndices().size(), equalTo(0));
logger.info("--> trying to index into a closed index ..."); logger.info("--> trying to index into a closed index ...");
try { try {
client("node1").prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
assert false; assert false;
} catch (ClusterBlockException e) { } catch (ClusterBlockException e) {
// all is well // all is well
} }
logger.info("--> opening index..."); logger.info("--> opening index...");
client("node1").admin().indices().prepareOpen("test").execute().actionGet(); OpenIndexResponse openIndexResponse = client().admin().indices().prepareOpen("test").get();
assertThat(openIndexResponse.isAcknowledged(), equalTo(true));
logger.info("--> waiting for green status"); logger.info("--> waiting for green status");
health = client("node1").admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet(); ensureGreen();
assertThat(health.isTimedOut(), equalTo(false));
stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet(); stateResponse = client().admin().cluster().prepareState().get();
assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN)); assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN));
assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5)); assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5));
assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10));
logger.info("--> indexing a simple document"); logger.info("--> indexing a simple document");
client("node1").prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
} }
@Test @Test
public void testFastCloseAfterCreateDoesNotClose() { public void testFastCloseAfterCreateDoesNotClose() {
logger.info("--> starting two nodes....");
startNode("node1");
startNode("node2");
logger.info("--> creating test index that cannot be allocated"); logger.info("--> creating test index that cannot be allocated");
client("node1").admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder() client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder()
.put("index.routing.allocation.include.tag", "no_such_node") .put("index.routing.allocation.include.tag", "no_such_node")
.put("index.number_of_replicas", 1).build()).execute().actionGet(); .put("index.number_of_replicas", 1).build()).get();
ClusterHealthResponse health = client("node1").admin().cluster().prepareHealth("test").setWaitForNodes("2").execute().actionGet(); ClusterHealthResponse health = client().admin().cluster().prepareHealth("test").setWaitForNodes(">=2").get();
assertThat(health.isTimedOut(), equalTo(false)); assertThat(health.isTimedOut(), equalTo(false));
assertThat(health.getStatus(), equalTo(ClusterHealthStatus.RED)); assertThat(health.getStatus(), equalTo(ClusterHealthStatus.RED));
try { try {
client().admin().indices().prepareClose("test").execute().actionGet(); client().admin().indices().prepareClose("test").get();
Assert.fail("Exception should have been thrown"); fail("Exception should have been thrown");
} catch(IndexPrimaryShardNotAllocatedException e) { } catch(IndexPrimaryShardNotAllocatedException e) {
// expected // expected
} }
logger.info("--> updating test index settings to allow allocation"); logger.info("--> updating test index settings to allow allocation");
client("node1").admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.settingsBuilder() client().admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.settingsBuilder()
.put("index.routing.allocation.include.tag", "").build()).execute().actionGet(); .put("index.routing.allocation.include.tag", "").build()).get();
logger.info("--> waiting for green status"); logger.info("--> waiting for green status");
health = client("node1").admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet(); ensureGreen();
assertThat(health.isTimedOut(), equalTo(false));
ClusterStateResponse stateResponse = client("node1").admin().cluster().prepareState().execute().actionGet(); ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN)); assertThat(stateResponse.getState().metaData().index("test").state(), equalTo(IndexMetaData.State.OPEN));
assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5)); assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(5));
assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10));
logger.info("--> indexing a simple document"); logger.info("--> indexing a simple document");
client("node1").prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
} }
@Test @Test
public void testConsistencyAfterIndexCreationFailure() { public void testConsistencyAfterIndexCreationFailure() {
logger.info("--> starting one node....");
startNode("node1");
logger.info("--> deleting test index...."); logger.info("--> deleting test index....");
try { try {
client("node1").admin().indices().prepareDelete("test").execute().actionGet(); client().admin().indices().prepareDelete("test").get();
} catch (IndexMissingException ex) { } catch (IndexMissingException ex) {
// Ignore // Ignore
} }
logger.info("--> creating test index with invalid settings "); logger.info("--> creating test index with invalid settings ");
try { try {
client("node1").admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", "bad")).execute().actionGet(); client().admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", "bad")).get();
assert false; assert false;
} catch (SettingsException ex) { } catch (SettingsException ex) {
// Expected // Expected
} }
logger.info("--> creating test index with valid settings "); logger.info("--> creating test index with valid settings ");
CreateIndexResponse response = client("node1").admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", 1)).execute().actionGet(); CreateIndexResponse response = client().admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", 1)).get();
assertThat(response.isAcknowledged(), equalTo(true)); assertThat(response.isAcknowledged(), equalTo(true));
} }