2424import org .elasticsearch .test .ESIntegTestCase ;
2525import org .hamcrest .Matcher ;
2626
27- import java .util .stream . IntStream ;
27+ import java .util .List ;
2828
2929import static org .hamcrest .Matchers .allOf ;
3030import static org .hamcrest .Matchers .containsString ;
@@ -38,7 +38,7 @@ public void testRepurpose() throws Exception {
3838 final String indexName = "test-repurpose" ;
3939
4040 logger .info ("--> starting two nodes" );
41- internalCluster ().startNodes (2 );
41+ List < String > nodes = internalCluster ().startNodes (2 );
4242
4343 logger .info ("--> creating index" );
4444 prepareCreate (indexName , Settings .builder ()
@@ -67,34 +67,28 @@ public void testRepurpose() throws Exception {
6767 () -> internalCluster ().startNode (noMasterNoDataSettings )
6868 );
6969
70+ int runningOrdinal = nodes .indexOf (internalCluster ().getNodeNames ()[0 ]);
71+ int stoppedOrdinal = 1 -runningOrdinal ;
72+
7073 logger .info ("--> Repurposing node" );
71- executeRepurposeCommand (noMasterNoDataSettings , indexUUID );
74+ executeRepurposeCommandForOrdinal (noMasterNoDataSettings , indexUUID , stoppedOrdinal );
75+
76+ ElasticsearchException lockedException = expectThrows (ElasticsearchException .class ,
77+ () -> executeRepurposeCommandForOrdinal (noMasterNoDataSettings , indexUUID , runningOrdinal )
78+ );
79+
80+ assertThat (lockedException .getMessage (), containsString (NodeRepurposeCommand .FAILED_TO_OBTAIN_NODE_LOCK_MSG ));
7281
7382 logger .info ("--> Starting node after repurpose" );
7483 internalCluster ().startNode (noMasterNoDataSettings );
7584 }
7685
77- private void executeRepurposeCommand (Settings settings , String indexUUID ) throws Exception {
78- assertEquals ("Exactly one node folder/ordinal must succeed" , 1 ,
79- IntStream .range (0 , 2 ).filter (i -> executeRepurposeCommandForOrdinal (settings , indexUUID , i )).count ());
80- }
81-
82- private boolean executeRepurposeCommandForOrdinal (Settings settings , String indexUUID , int ordinal ) {
83- try {
84- boolean verbose = randomBoolean ();
85- Matcher <String > matcher = allOf (
86- containsString (NodeRepurposeCommand .noMasterMessage (1 , 1 , TestEnvironment .newEnvironment (settings ).dataFiles ().length )),
87- not (contains (NodeRepurposeCommand .PRE_V7_MESSAGE )),
88- NodeRepurposeCommandTests .conditionalNot (containsString (indexUUID ), verbose == false ));
89- NodeRepurposeCommandTests .verifySuccess (settings , matcher , verbose , ordinal );
90- return true ;
91- } catch (ElasticsearchException e ) {
92- if (e .getMessage ().contains (NodeRepurposeCommand .FAILED_TO_OBTAIN_NODE_LOCK_MSG ))
93- return false ;
94- else
95- throw e ;
96- } catch (Exception e ) {
97- throw new RuntimeException (e );
98- }
86+ private void executeRepurposeCommandForOrdinal (Settings settings , String indexUUID , int ordinal ) throws Exception {
87+ boolean verbose = randomBoolean ();
88+ Matcher <String > matcher = allOf (
89+ containsString (NodeRepurposeCommand .noMasterMessage (1 , 1 , TestEnvironment .newEnvironment (settings ).dataFiles ().length )),
90+ not (contains (NodeRepurposeCommand .PRE_V7_MESSAGE )),
91+ NodeRepurposeCommandTests .conditionalNot (containsString (indexUUID ), verbose == false ));
92+ NodeRepurposeCommandTests .verifySuccess (settings , matcher , verbose , ordinal );
9993 }
10094}
0 commit comments