42
42
43
43
import java .util .Collections ;
44
44
45
- import static org .elasticsearch .common .unit .TimeValue .timeValueSeconds ;
46
45
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertExists ;
47
46
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertHitCount ;
48
47
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertThrows ;
49
48
import static org .hamcrest .Matchers .equalTo ;
50
49
import static org .hamcrest .Matchers .greaterThan ;
51
- import static org .hamcrest .Matchers .lessThan ;
52
50
53
51
@ ClusterScope (scope = Scope .TEST , numDataNodes = 0 , autoMinMasterNodes = false )
54
52
@ ESIntegTestCase .SuppressLocalMode
@@ -63,20 +61,17 @@ public void testNoMasterActions() throws Exception {
63
61
.put (DiscoverySettings .NO_MASTER_BLOCK_SETTING .getKey (), "all" )
64
62
.build ();
65
63
66
- TimeValue timeout = TimeValue .timeValueMillis (200 );
64
+ final TimeValue timeout = TimeValue .timeValueMillis (10 );
67
65
68
66
internalCluster ().startNode (settings );
69
67
// start a second node, create an index, and then shut it down so we have no master block
70
68
internalCluster ().startNode (settings );
71
69
createIndex ("test" );
72
70
client ().admin ().cluster ().prepareHealth ("test" ).setWaitForGreenStatus ().execute ().actionGet ();
73
71
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 ));
80
75
});
81
76
82
77
assertThrows (client ().prepareGet ("test" , "type1" , "1" ),
@@ -124,34 +119,30 @@ public void run() {
124
119
ScriptType .INLINE , Script .DEFAULT_SCRIPT_LANG , "test script" , Collections .emptyMap ())).setTimeout (timeout ));
125
120
126
121
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 ));
129
124
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 ));
132
127
133
128
BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
134
129
bulkRequestBuilder .add (client ().prepareIndex ("test" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
135
130
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 );
138
131
bulkRequestBuilder .setTimeout (timeout );
139
- checkWriteAction (false , timeout , bulkRequestBuilder );
132
+ checkWriteAction (bulkRequestBuilder );
140
133
141
134
bulkRequestBuilder = client ().prepareBulk ();
142
135
bulkRequestBuilder .add (client ().prepareIndex ("no_index" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
143
136
bulkRequestBuilder .add (client ().prepareIndex ("no_index" , "type1" , "2" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()));
144
- timeout = new TimeValue (200 );
145
137
bulkRequestBuilder .setTimeout (timeout );
146
- checkWriteAction (true , timeout , bulkRequestBuilder );
138
+ checkWriteAction (bulkRequestBuilder );
147
139
148
140
internalCluster ().startNode (settings );
149
141
client ().admin ().cluster ().prepareHealth ().setWaitForGreenStatus ().setWaitForNodes ("2" ).execute ().actionGet ();
150
142
}
151
143
152
144
void checkUpdateAction (boolean autoCreateIndex , TimeValue timeout , ActionRequestBuilder <?, ?, ?> builder ) {
153
145
// we clean the metadata when loosing a master, therefore all operations on indices will auto create it, if allowed
154
- long now = System .currentTimeMillis ();
155
146
try {
156
147
builder .get ();
157
148
fail ("expected ClusterBlockException or MasterNotDiscoveredException" );
@@ -161,26 +152,16 @@ void checkUpdateAction(boolean autoCreateIndex, TimeValue timeout, ActionRequest
161
152
} else {
162
153
assertFalse (autoCreateIndex );
163
154
}
164
- // verify we waited before giving up...
165
155
assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
166
- assertThat (System .currentTimeMillis () - now , greaterThan (timeout .millis () - 50 ));
167
156
}
168
157
}
169
158
170
- void checkWriteAction (boolean indexShouldBeAutoCreated , TimeValue timeout , ActionRequestBuilder <?, ?, ?> builder ) {
171
- long now = System .currentTimeMillis ();
159
+ void checkWriteAction (ActionRequestBuilder <?, ?, ?> builder ) {
172
160
try {
173
161
builder .get ();
174
162
fail ("Expected ClusterBlockException" );
175
163
} 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 ));
184
165
}
185
166
}
186
167
@@ -239,12 +220,10 @@ public void testNoMasterActionsWriteMasterBlock() throws Exception {
239
220
assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
240
221
}
241
222
242
- now = System .currentTimeMillis ();
243
223
try {
244
224
client ().prepareIndex ("test1" , "type1" , "1" ).setSource (XContentFactory .jsonBuilder ().startObject ().endObject ()).setTimeout (timeout ).get ();
245
225
fail ("Expected ClusterBlockException" );
246
226
} catch (ClusterBlockException e ) {
247
- assertThat (System .currentTimeMillis () - now , greaterThan (timeout .millis () - 50 ));
248
227
assertThat (e .status (), equalTo (RestStatus .SERVICE_UNAVAILABLE ));
249
228
}
250
229
0 commit comments