Skip to content

Commit

Permalink
NoMasterNodeIT shouldn't try to validate the length of a timeout
Browse files Browse the repository at this point in the history
The current log tries make sure we waited some (but not too long). This is unpredictable and fails all the time. This commit removes all of it and just make sure that we throw the right exceptions after timing out.

Fixes #24369
  • Loading branch information
bleskes committed May 24, 2017
1 parent aa0b9f9 commit b58f198
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions core/src/test/java/org/elasticsearch/cluster/NoMasterNodeIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@

import java.util.Collections;

import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertExists;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

@ClusterScope(scope = Scope.TEST, numDataNodes = 0, autoMinMasterNodes = false)
public class NoMasterNodeIT extends ESIntegTestCase {
Expand All @@ -70,20 +68,17 @@ public void testNoMasterActions() throws Exception {
.put(DiscoverySettings.NO_MASTER_BLOCK_SETTING.getKey(), "all")
.build();

TimeValue timeout = TimeValue.timeValueMillis(200);
final TimeValue timeout = TimeValue.timeValueMillis(10);

internalCluster().startNode(settings);
// start a second node, create an index, and then shut it down so we have no master block
internalCluster().startNode(settings);
createIndex("test");
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
internalCluster().stopRandomDataNode();
assertBusy(new Runnable() {
@Override
public void run() {
ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
assertTrue(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID));
}
assertBusy(() -> {
ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
assertTrue(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID));
});

assertThrows(client().prepareGet("test", "type1", "1"),
Expand Down Expand Up @@ -130,33 +125,30 @@ public void run() {
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "test script", Collections.emptyMap())).setTimeout(timeout));


checkWriteAction(false, timeout,
client().prepareIndex("test", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout));
checkWriteAction(
client().prepareIndex("test", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout));

checkWriteAction(true, timeout,
client().prepareIndex("no_index", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout));
checkWriteAction(
client().prepareIndex("no_index", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout));

BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()));
bulkRequestBuilder.add(client().prepareIndex("test", "type1", "2").setSource(XContentFactory.jsonBuilder().startObject().endObject()));
// the request should fail very quickly - use a large timeout and make sure it didn't pass...
timeout = new TimeValue(5000);
bulkRequestBuilder.setTimeout(timeout);
checkWriteAction(false, timeout, bulkRequestBuilder);
checkWriteAction(bulkRequestBuilder);

bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("no_index", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()));
bulkRequestBuilder.add(client().prepareIndex("no_index", "type1", "2").setSource(XContentFactory.jsonBuilder().startObject().endObject()));
bulkRequestBuilder.setTimeout(timeout);
checkWriteAction(true, timeValueSeconds(5), bulkRequestBuilder);
checkWriteAction(bulkRequestBuilder);

internalCluster().startNode(settings);
client().admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet();
}

void checkUpdateAction(boolean autoCreateIndex, TimeValue timeout, ActionRequestBuilder<?, ?, ?> builder) {
// we clean the metadata when loosing a master, therefore all operations on indices will auto create it, if allowed
long now = System.currentTimeMillis();
try {
builder.get();
fail("expected ClusterBlockException or MasterNotDiscoveredException");
Expand All @@ -166,26 +158,16 @@ void checkUpdateAction(boolean autoCreateIndex, TimeValue timeout, ActionRequest
} else {
assertFalse(autoCreateIndex);
}
// verify we waited before giving up...
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
}
}

void checkWriteAction(boolean indexShouldBeAutoCreated, TimeValue timeout, ActionRequestBuilder<?, ?, ?> builder) {
long now = System.currentTimeMillis();
void checkWriteAction(ActionRequestBuilder<?, ?, ?> builder) {
try {
builder.get();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
if (indexShouldBeAutoCreated) {
// timeout is 200
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
} else {
// timeout is 5000
assertThat(System.currentTimeMillis() - now, lessThan(timeout.millis() + 300));
}
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
}

Expand Down Expand Up @@ -244,12 +226,10 @@ public void testNoMasterActionsWriteMasterBlock() throws Exception {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}

now = System.currentTimeMillis();
try {
client().prepareIndex("test1", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}

Expand Down

0 comments on commit b58f198

Please sign in to comment.