@@ -194,27 +194,42 @@ private <T> T assertListenerThrows(String msg, PlainActionFuture<?> listener, Cl
194194 return cause ;
195195 }
196196
197+ private void setStateWithBlock (final ClusterService clusterService , final ClusterBlock block , final boolean globalBlock ) {
198+ final ClusterBlocks .Builder blocks = ClusterBlocks .builder ();
199+ if (globalBlock ) {
200+ blocks .addGlobalBlock (block );
201+ } else {
202+ blocks .addIndexBlock ("index" , block );
203+ }
204+ setState (clusterService , ClusterState .builder (clusterService .state ()).blocks (blocks ).build ());
205+ }
206+
197207 public void testBlocksInReroutePhase () throws Exception {
198208 final ClusterBlock nonRetryableBlock =
199209 new ClusterBlock (1 , "non retryable" , false , true , false , RestStatus .SERVICE_UNAVAILABLE , ClusterBlockLevel .ALL );
200210 final ClusterBlock retryableBlock =
201211 new ClusterBlock (1 , "retryable" , true , true , false , RestStatus .SERVICE_UNAVAILABLE , ClusterBlockLevel .ALL );
202212
213+ final boolean globalBlock = randomBoolean ();
203214 final TestAction action = new TestAction (Settings .EMPTY , "internal:testActionWithBlocks" ,
204215 transportService , clusterService , shardStateAction , threadPool ) {
205216 @ Override
206217 protected ClusterBlockLevel globalBlockLevel () {
207- return ClusterBlockLevel .WRITE ;
218+ return globalBlock ? ClusterBlockLevel .WRITE : null ;
219+ }
220+
221+ @ Override
222+ protected ClusterBlockLevel indexBlockLevel () {
223+ return globalBlock == false ? ClusterBlockLevel .WRITE : null ;
208224 }
209225 };
210226
211227 setState (clusterService , ClusterStateCreationUtils .stateWithActivePrimary ("index" , true , 0 ));
212228
213229 {
214- setState (clusterService ,
215- ClusterState .builder (clusterService .state ()).blocks (ClusterBlocks .builder ().addGlobalBlock (nonRetryableBlock )));
230+ setStateWithBlock (clusterService , nonRetryableBlock , globalBlock );
216231
217- Request request = new Request ();
232+ Request request = globalBlock ? new Request () : new Request (). index ( "index" );
218233 PlainActionFuture <TestResponse > listener = new PlainActionFuture <>();
219234 ReplicationTask task = maybeTask ();
220235
@@ -227,10 +242,9 @@ protected ClusterBlockLevel globalBlockLevel() {
227242 assertPhase (task , "failed" );
228243 }
229244 {
230- setState (clusterService ,
231- ClusterState .builder (clusterService .state ()).blocks (ClusterBlocks .builder ().addGlobalBlock (retryableBlock )));
245+ setStateWithBlock (clusterService , retryableBlock , globalBlock );
232246
233- Request requestWithTimeout = new Request ().timeout ("5ms" );
247+ Request requestWithTimeout = ( globalBlock ? new Request () : new Request (). index ( "index" ) ).timeout ("5ms" );
234248 PlainActionFuture <TestResponse > listener = new PlainActionFuture <>();
235249 ReplicationTask task = maybeTask ();
236250
@@ -244,10 +258,9 @@ protected ClusterBlockLevel globalBlockLevel() {
244258 assertTrue (requestWithTimeout .isRetrySet .get ());
245259 }
246260 {
247- setState (clusterService ,
248- ClusterState .builder (clusterService .state ()).blocks (ClusterBlocks .builder ().addGlobalBlock (retryableBlock )));
261+ setStateWithBlock (clusterService , retryableBlock , globalBlock );
249262
250- Request request = new Request ();
263+ Request request = globalBlock ? new Request () : new Request (). index ( "index" );
251264 PlainActionFuture <TestResponse > listener = new PlainActionFuture <>();
252265 ReplicationTask task = maybeTask ();
253266
@@ -258,8 +271,7 @@ protected ClusterBlockLevel globalBlockLevel() {
258271 assertPhase (task , "waiting_for_retry" );
259272 assertTrue (request .isRetrySet .get ());
260273
261- setState (clusterService ,
262- ClusterState .builder (clusterService .state ()).blocks (ClusterBlocks .builder ().addGlobalBlock (nonRetryableBlock )));
274+ setStateWithBlock (clusterService , nonRetryableBlock , globalBlock );
263275
264276 ClusterBlockException exception = assertListenerThrows ("primary phase should fail operation when moving from a retryable " +
265277 "block to a non-retryable one" , listener , ClusterBlockException .class );
0 commit comments