4242
4343import java .util .Collections ;
4444
45- import static org .elasticsearch .common .unit .TimeValue .timeValueSeconds ;
4645import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertExists ;
4746import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertHitCount ;
4847import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertThrows ;
4948import static org .hamcrest .Matchers .equalTo ;
5049import static org .hamcrest .Matchers .greaterThan ;
51- import static org .hamcrest .Matchers .lessThan ;
5250
5351@ ClusterScope (scope = Scope .TEST , numDataNodes = 0 , autoMinMasterNodes = false )
5452@ ESIntegTestCase .SuppressLocalMode
@@ -63,20 +61,17 @@ public void testNoMasterActions() throws Exception {
6361 .put (DiscoverySettings .NO_MASTER_BLOCK_SETTING .getKey (), "all" )
6462 .build ();
6563
66- TimeValue timeout = TimeValue .timeValueMillis (200 );
64+ final TimeValue timeout = TimeValue .timeValueMillis (10 );
6765
6866 internalCluster ().startNode (settings );
6967 // start a second node, create an index, and then shut it down so we have no master block
7068 internalCluster ().startNode (settings );
7169 createIndex ("test" );
7270 client ().admin ().cluster ().prepareHealth ("test" ).setWaitForGreenStatus ().execute ().actionGet ();
7371 internalCluster ().stopRandomDataNode ();
74- assertBusy (new Runnable () {
75- @ Override
76- public void run () {
77- ClusterState state = client ().admin ().cluster ().prepareState ().setLocal (true ).execute ().actionGet ().getState ();
78- assertTrue (state .blocks ().hasGlobalBlock (DiscoverySettings .NO_MASTER_BLOCK_ID ));
79- }
72+ assertBusy (() -> {
73+ ClusterState state = client ().admin ().cluster ().prepareState ().setLocal (true ).execute ().actionGet ().getState ();
74+ assertTrue (state .blocks ().hasGlobalBlock (DiscoverySettings .NO_MASTER_BLOCK_ID ));
8075 });
8176
8277 assertThrows (client ().prepareGet ("test" , "type1" , "1" ),
@@ -124,34 +119,30 @@ public void run() {
124119 ScriptType .INLINE , Script .DEFAULT_SCRIPT_LANG , "test script" , Collections .emptyMap ())).setTimeout (timeout ));
125120
126121
127- checkWriteAction (false , timeout ,
128- client ().prepareIndex ("test" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ));
122+ checkWriteAction (
123+ client ().prepareIndex ("test" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ));
129124
130- checkWriteAction (true , timeout ,
131- client ().prepareIndex ("no_index" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ));
125+ checkWriteAction (
126+ client ().prepareIndex ("no_index" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ));
132127
133128 BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
134129 bulkRequestBuilder .add (client ().prepareIndex ("test" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
135130 bulkRequestBuilder .add (client ().prepareIndex ("test" , "type1" , "2" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
136- // the request should fail very quickly - use a large timeout and make sure it didn't pass...
137- timeout = new TimeValue (5000 );
138131 bulkRequestBuilder .setTimeout (timeout );
139- checkWriteAction (false , timeout , bulkRequestBuilder );
132+ checkWriteAction (bulkRequestBuilder );
140133
141134 bulkRequestBuilder = client ().prepareBulk ();
142135 bulkRequestBuilder .add (client ().prepareIndex ("no_index" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
143136 bulkRequestBuilder .add (client ().prepareIndex ("no_index" , "type1" , "2" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
144- timeout = new TimeValue (200 );
145137 bulkRequestBuilder .setTimeout (timeout );
146- checkWriteAction (true , timeout , bulkRequestBuilder );
138+ checkWriteAction (bulkRequestBuilder );
147139
148140 internalCluster ().startNode (settings );
149141 client ().admin ().cluster ().prepareHealth ().setWaitForGreenStatus ().setWaitForNodes ("2" ).execute ().actionGet ();
150142 }
151143
152144 void checkUpdateAction (boolean autoCreateIndex , TimeValue timeout , ActionRequestBuilder <?, ?, ?> builder ) {
153145 // we clean the metadata when loosing a master, therefore all operations on indices will auto create it, if allowed
154- long now = System .currentTimeMillis ();
155146 try {
156147 builder .get ();
157148 fail ("expected ClusterBlockException or MasterNotDiscoveredException" );
@@ -161,26 +152,16 @@ void checkUpdateAction(boolean autoCreateIndex, TimeValue timeout, ActionRequest
161152 } else {
162153 assertFalse (autoCreateIndex );
163154 }
164- // verify we waited before giving up...
165155 assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
166- assertThat (System .currentTimeMillis () - now , greaterThan (timeout .millis () - 50 ));
167156 }
168157 }
169158
170- void checkWriteAction (boolean indexShouldBeAutoCreated , TimeValue timeout , ActionRequestBuilder <?, ?, ?> builder ) {
171- long now = System .currentTimeMillis ();
159+ void checkWriteAction (ActionRequestBuilder <?, ?, ?> builder ) {
172160 try {
173161 builder .get ();
174162 fail ("Expected ClusterBlockException" );
175163 } catch (ClusterBlockException e ) {
176- if (indexShouldBeAutoCreated ) {
177- // timeout is 200
178- assertThat (System .currentTimeMillis () - now , greaterThan (timeout .millis () - 50 ));
179- assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
180- } else {
181- // timeout is 5000
182- assertThat (System .currentTimeMillis () - now , lessThan (timeout .millis () + 50 ));
183- }
164+ assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
184165 }
185166 }
186167
@@ -239,12 +220,10 @@ public void testNoMasterActionsWriteMasterBlock() throws Exception {
239220 assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
240221 }
241222
242- now = System .currentTimeMillis ();
243223 try {
244224 client ().prepareIndex ("test1" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ).get ();
245225 fail ("Expected ClusterBlockException" );
246226 } catch (ClusterBlockException e ) {
247- assertThat (System .currentTimeMillis () - now , greaterThan (timeout .millis () - 50 ));
248227 assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
249228 }
250229
0 commit comments