[TEST] don't fail if meta state cannot be loaded

Loading of meta state fails rarely if the state is deleted while we try to load it.
We should try again in this case instead of failing the test.
This commit is contained in:
Britta Weber 2015-05-05 15:13:00 +02:00
parent 00d663f594
commit 14e5880fea
1 changed files with 17 additions and 13 deletions

View File

@ -35,7 +35,6 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map;
import static org.elasticsearch.client.Requests.clusterHealthRequest; import static org.elasticsearch.client.Requests.clusterHealthRequest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -236,6 +235,7 @@ public class MetaDataWriteDataNodesTests extends ElasticsearchIntegrationTest {
assertThat(clusterStateResponse.getState().getMetaData().index("red_index").getState().name(), equalTo(IndexMetaData.State.OPEN.name())); assertThat(clusterStateResponse.getState().getMetaData().index("red_index").getState().name(), equalTo(IndexMetaData.State.OPEN.name()));
assertTrue(client().prepareGet("red_index", "doc", "1").get().isExists()); assertTrue(client().prepareGet("red_index", "doc", "1").get().isExists());
} }
@Test @Test
public void testMetaWrittenWhenIndexIsClosedAndMetaUpdated() throws Exception { public void testMetaWrittenWhenIndexIsClosedAndMetaUpdated() throws Exception {
String masterNode = startMasterNode(); String masterNode = startMasterNode();
@ -272,7 +272,7 @@ public class MetaDataWriteDataNodesTests extends ElasticsearchIntegrationTest {
.endObject()).get(); .endObject()).get();
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("red_index").addTypes("doc").get(); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("red_index").addTypes("doc").get();
assertNotNull(((LinkedHashMap)(getMappingsResponse.getMappings().get("red_index").get("doc").getSourceAsMap().get("properties"))).get("integer_field")); assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get("red_index").get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
// restart master with empty data folder and maybe red node // restart master with empty data folder and maybe red node
((InternalTestCluster) cluster()).stopCurrentMasterNode(); ((InternalTestCluster) cluster()).stopCurrentMasterNode();
masterNode = startMasterNode(); masterNode = startMasterNode();
@ -283,7 +283,7 @@ public class MetaDataWriteDataNodesTests extends ElasticsearchIntegrationTest {
clusterStateResponse = client().admin().cluster().prepareState().get(); clusterStateResponse = client().admin().cluster().prepareState().get();
assertThat(clusterStateResponse.getState().getMetaData().index("red_index").getState().name(), equalTo(IndexMetaData.State.CLOSE.name())); assertThat(clusterStateResponse.getState().getMetaData().index("red_index").getState().name(), equalTo(IndexMetaData.State.CLOSE.name()));
getMappingsResponse = client().admin().indices().prepareGetMappings("red_index").addTypes("doc").get(); getMappingsResponse = client().admin().indices().prepareGetMappings("red_index").addTypes("doc").get();
assertNotNull(((LinkedHashMap)(getMappingsResponse.getMappings().get("red_index").get("doc").getSourceAsMap().get("properties"))).get("integer_field")); assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get("red_index").get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
} }
@ -320,12 +320,20 @@ public class MetaDataWriteDataNodesTests extends ElasticsearchIntegrationTest {
assertMetaState(nodeName, indexName, true); assertMetaState(nodeName, indexName, true);
} }
private void assertMetaState(final String nodeName, final String indexName, final boolean shouldBe) throws Exception { private void assertMetaState(final String nodeName, final String indexName, final boolean shouldBe) throws Exception {
awaitBusy(new Predicate<Object>() { awaitBusy(new Predicate<Object>() {
@Override @Override
public boolean apply(Object o) { public boolean apply(Object o) {
logger.info("checking if meta state exists..."); logger.info("checking if meta state exists...");
return shouldBe == metaStateExists(nodeName, indexName); try {
return shouldBe == metaStateExists(nodeName, indexName);
} catch (Throwable t) {
logger.info("failed to load meta state", t);
// TODO: loading of meta state fails rarely if the state is deleted while we try to load it
// this here is a hack, would be much better to use for example a WatchService
return false;
}
} }
}); });
boolean inMetaSate = metaStateExists(nodeName, indexName); boolean inMetaSate = metaStateExists(nodeName, indexName);
@ -336,15 +344,11 @@ public class MetaDataWriteDataNodesTests extends ElasticsearchIntegrationTest {
} }
} }
private boolean metaStateExists(String nodeName, String indexName) { private boolean metaStateExists(String nodeName, String indexName) throws Exception {
GatewayMetaState redNodeMetaState = ((InternalTestCluster) cluster()).getInstance(GatewayMetaState.class, nodeName); GatewayMetaState nodeMetaState = ((InternalTestCluster) cluster()).getInstance(GatewayMetaState.class, nodeName);
MetaData redNodeMetaData = null; MetaData nodeMetaData = null;
try { nodeMetaData = nodeMetaState.loadMetaState();
redNodeMetaData = redNodeMetaState.loadMetaState(); ImmutableOpenMap<String, IndexMetaData> indices = nodeMetaData.getIndices();
} catch (Exception e) {
fail("failed to load meta state");
}
ImmutableOpenMap<String, IndexMetaData> indices = redNodeMetaData.getIndices();
boolean inMetaSate = false; boolean inMetaSate = false;
for (ObjectObjectCursor<String, IndexMetaData> index : indices) { for (ObjectObjectCursor<String, IndexMetaData> index : indices) {
inMetaSate = inMetaSate || index.key.equals(indexName); inMetaSate = inMetaSate || index.key.equals(indexName);