5454
5555import static org .elasticsearch .action .support .replication .ClusterStateCreationUtils .stateWithStartedPrimary ;
5656import static org .hamcrest .CoreMatchers .equalTo ;
57+ import static org .hamcrest .CoreMatchers .instanceOf ;
58+ import static org .hamcrest .Matchers .is ;
5759
5860public class ShardStateActionTests extends ESTestCase {
5961 private static ThreadPool THREAD_POOL ;
@@ -119,6 +121,41 @@ public static void stopThreadPool() {
119121 THREAD_POOL = null ;
120122 }
121123
124+ public void testSuccess () throws InterruptedException {
125+ final String index = "test" ;
126+
127+ clusterService .setState (stateWithStartedPrimary (index , true , randomInt (5 )));
128+
129+ String indexUUID = clusterService .state ().metaData ().index (index ).getIndexUUID ();
130+
131+ AtomicBoolean success = new AtomicBoolean ();
132+ CountDownLatch latch = new CountDownLatch (1 );
133+
134+ ShardRouting shardRouting = getRandomShardRouting (index );
135+ shardStateAction .shardFailed (shardRouting , indexUUID , "test" , getSimulatedFailure (), new ShardStateAction .Listener () {
136+ @ Override
137+ public void onSuccess () {
138+ success .set (true );
139+ latch .countDown ();
140+ }
141+ });
142+
143+ CapturingTransport .CapturedRequest [] capturedRequests = transport .getCapturedRequestsAndClear ();
144+ assertEquals (1 , capturedRequests .length );
145+ // the request is a shard failed request
146+ assertThat (capturedRequests [0 ].request , is (instanceOf (ShardStateAction .ShardRoutingEntry .class )));
147+ ShardStateAction .ShardRoutingEntry shardRoutingEntry = (ShardStateAction .ShardRoutingEntry )capturedRequests [0 ].request ;
148+ // for the right shard
149+ assertEquals (shardRouting , shardRoutingEntry .getShardRouting ());
150+ // sent to the master
151+ assertEquals (clusterService .state ().nodes ().masterNode ().getId (), capturedRequests [0 ].node .getId ());
152+
153+ transport .handleResponse (capturedRequests [0 ].requestId , TransportResponse .Empty .INSTANCE );
154+
155+ latch .await ();
156+ assertTrue (success .get ());
157+ }
158+
122159 public void testNoMaster () throws InterruptedException {
123160 final String index = "test" ;
124161
@@ -131,11 +168,10 @@ public void testNoMaster() throws InterruptedException {
131168 String indexUUID = clusterService .state ().metaData ().index (index ).getIndexUUID ();
132169
133170 CountDownLatch latch = new CountDownLatch (1 );
134- AtomicBoolean noMaster = new AtomicBoolean ();
135171 AtomicBoolean retried = new AtomicBoolean ();
136172 AtomicBoolean success = new AtomicBoolean ();
137173
138- setUpMasterRetryVerification (noMaster , retried , latch );
174+ setUpMasterRetryVerification (retried , latch );
139175
140176 shardStateAction .shardFailed (getRandomShardRouting (index ), indexUUID , "test" , getSimulatedFailure (), new ShardStateAction .Listener () {
141177 @ Override
@@ -147,7 +183,6 @@ public void onSuccess() {
147183
148184 latch .await ();
149185
150- assertTrue (noMaster .get ());
151186 assertTrue (retried .get ());
152187 assertTrue (success .get ());
153188 }
@@ -160,12 +195,11 @@ public void testMasterChannelException() throws InterruptedException {
160195 String indexUUID = clusterService .state ().metaData ().index (index ).getIndexUUID ();
161196
162197 CountDownLatch latch = new CountDownLatch (1 );
163- AtomicBoolean noMaster = new AtomicBoolean ();
164198 AtomicBoolean retried = new AtomicBoolean ();
165199 AtomicBoolean success = new AtomicBoolean ();
166200 AtomicReference <Exception > exception = new AtomicReference <>();
167201
168- setUpMasterRetryVerification (noMaster , retried , latch );
202+ setUpMasterRetryVerification (retried , latch );
169203
170204 shardStateAction .shardFailed (getRandomShardRouting (index ), indexUUID , "test" , getSimulatedFailure (), new ShardStateAction .Listener () {
171205 @ Override
@@ -185,6 +219,7 @@ public void onShardFailedFailure(Exception e) {
185219 final CapturingTransport .CapturedRequest [] capturedRequests = transport .getCapturedRequestsAndClear ();
186220 assertThat (capturedRequests .length , equalTo (1 ));
187221 assertFalse (success .get ());
222+ assertFalse (retried .get ());
188223 List <Exception > possibleExceptions = new ArrayList <>();
189224 possibleExceptions .add (new NotMasterException ("simulated" ));
190225 possibleExceptions .add (new NodeDisconnectedException (clusterService .state ().nodes ().masterNode (), ShardStateAction .SHARD_FAILED_ACTION_NAME ));
@@ -206,6 +241,11 @@ public void testUnhandledFailure() {
206241 AtomicBoolean failure = new AtomicBoolean ();
207242
208243 shardStateAction .shardFailed (getRandomShardRouting (index ), indexUUID , "test" , getSimulatedFailure (), new ShardStateAction .Listener () {
244+ @ Override
245+ public void onSuccess () {
246+ assert false ;
247+ }
248+
209249 @ Override
210250 public void onShardFailedFailure (Exception e ) {
211251 failure .set (true );
@@ -228,19 +268,17 @@ private ShardRouting getRandomShardRouting(String index) {
228268 return shardRouting ;
229269 }
230270
231- private void setUpMasterRetryVerification (AtomicBoolean noMaster , AtomicBoolean retried , CountDownLatch latch ) {
271+ private void setUpMasterRetryVerification (AtomicBoolean retried , CountDownLatch latch ) {
232272 shardStateAction .setOnBeforeWaitForNewMasterAndRetry (() -> {
233273 DiscoveryNodes .Builder masterBuilder = DiscoveryNodes .builder (clusterService .state ().nodes ());
234274 masterBuilder .masterNodeId (clusterService .state ().nodes ().masterNodes ().iterator ().next ().value .id ());
235275 clusterService .setState (ClusterState .builder (clusterService .state ()).nodes (masterBuilder ));
236276 });
237277
238- shardStateAction .setOnAfterWaitForNewMasterAndRetry (() -> verifyRetry (noMaster , retried , latch ));
278+ shardStateAction .setOnAfterWaitForNewMasterAndRetry (() -> verifyRetry (retried , latch ));
239279 }
240280
241- private void verifyRetry (AtomicBoolean invoked , AtomicBoolean retried , CountDownLatch latch ) {
242- invoked .set (true );
243-
281+ private void verifyRetry (AtomicBoolean retried , CountDownLatch latch ) {
244282 // assert a retry request was sent
245283 final CapturingTransport .CapturedRequest [] capturedRequests = transport .getCapturedRequestsAndClear ();
246284 retried .set (capturedRequests .length == 1 );
0 commit comments