12
12
import org .elasticsearch .action .admin .indices .segments .IndexShardSegments ;
13
13
import org .elasticsearch .action .admin .indices .segments .IndicesSegmentResponse ;
14
14
import org .elasticsearch .action .admin .indices .segments .ShardSegments ;
15
+ import org .elasticsearch .action .support .DefaultShardOperationFailedException ;
15
16
import org .elasticsearch .client .AdminClient ;
16
17
import org .elasticsearch .client .Client ;
17
18
import org .elasticsearch .client .IndicesAdminClient ;
@@ -130,6 +131,7 @@ public void onResponse(boolean conditionMet, ToXContentObject info) {
130
131
131
132
@ Override
132
133
public void onFailure (Exception e ) {
134
+ logger .warn ("unexpected onFailure call" , e );
133
135
throw new AssertionError ("unexpected method call" );
134
136
}
135
137
});
@@ -187,6 +189,7 @@ public void onResponse(boolean conditionMet, ToXContentObject info) {
187
189
188
190
@ Override
189
191
public void onFailure (Exception e ) {
192
+ logger .warn ("unexpected onFailure call" , e );
190
193
throw new AssertionError ("unexpected method call" );
191
194
}
192
195
});
@@ -195,6 +198,67 @@ public void onFailure(Exception e) {
195
198
assertEquals (new SegmentCountStep .Info (0L ), conditionInfo .get ());
196
199
}
197
200
201
+ public void testFailedToRetrieveSomeSegments () {
202
+ int maxNumSegments = randomIntBetween (3 , 10 );
203
+ Index index = new Index (randomAlphaOfLengthBetween (1 , 20 ), randomAlphaOfLengthBetween (1 , 20 ));
204
+ Client client = Mockito .mock (Client .class );
205
+ AdminClient adminClient = Mockito .mock (AdminClient .class );
206
+ IndicesAdminClient indicesClient = Mockito .mock (IndicesAdminClient .class );
207
+ IndicesSegmentResponse indicesSegmentResponse = Mockito .mock (IndicesSegmentResponse .class );
208
+ IndexSegments indexSegments = Mockito .mock (IndexSegments .class );
209
+ IndexShardSegments indexShardSegments = Mockito .mock (IndexShardSegments .class );
210
+ Map <Integer , IndexShardSegments > indexShards = Collections .singletonMap (0 , indexShardSegments );
211
+ ShardSegments shardSegmentsOne = Mockito .mock (ShardSegments .class );
212
+ ShardSegments [] shardSegmentsArray = new ShardSegments [] { shardSegmentsOne };
213
+ Spliterator <IndexShardSegments > iss = indexShards .values ().spliterator ();
214
+ List <Segment > segments = new ArrayList <>();
215
+ for (int i = 0 ; i < maxNumSegments + randomIntBetween (1 , 3 ); i ++) {
216
+ segments .add (null );
217
+ }
218
+ Mockito .when (indicesSegmentResponse .getStatus ()).thenReturn (RestStatus .OK );
219
+ Mockito .when (indicesSegmentResponse .getIndices ()).thenReturn (Collections .singletonMap (index .getName (), null ));
220
+ Mockito .when (indicesSegmentResponse .getShardFailures ())
221
+ .thenReturn (new DefaultShardOperationFailedException []{new DefaultShardOperationFailedException (index .getName (),
222
+ 0 , new IllegalArgumentException ("fake" ))});
223
+ Mockito .when (indexSegments .spliterator ()).thenReturn (iss );
224
+ Mockito .when (indexShardSegments .getShards ()).thenReturn (shardSegmentsArray );
225
+ Mockito .when (shardSegmentsOne .getSegments ()).thenReturn (segments );
226
+
227
+ Mockito .when (client .admin ()).thenReturn (adminClient );
228
+ Mockito .when (adminClient .indices ()).thenReturn (indicesClient );
229
+
230
+ Step .StepKey stepKey = randomStepKey ();
231
+ StepKey nextStepKey = randomStepKey ();
232
+
233
+ Mockito .doAnswer (invocationOnMock -> {
234
+ @ SuppressWarnings ("unchecked" )
235
+ ActionListener <IndicesSegmentResponse > listener = (ActionListener <IndicesSegmentResponse >) invocationOnMock .getArguments ()[1 ];
236
+ listener .onResponse (indicesSegmentResponse );
237
+ return null ;
238
+ }).when (indicesClient ).segments (any (), any ());
239
+
240
+ SetOnce <Boolean > conditionMetResult = new SetOnce <>();
241
+ SetOnce <ToXContentObject > conditionInfo = new SetOnce <>();
242
+
243
+ SegmentCountStep step = new SegmentCountStep (stepKey , nextStepKey , client , maxNumSegments );
244
+ step .evaluateCondition (makeMeta (index ), new AsyncWaitStep .Listener () {
245
+ @ Override
246
+ public void onResponse (boolean conditionMet , ToXContentObject info ) {
247
+ conditionMetResult .set (conditionMet );
248
+ conditionInfo .set (info );
249
+ }
250
+
251
+ @ Override
252
+ public void onFailure (Exception e ) {
253
+ logger .warn ("unexpected onFailure call" , e );
254
+ throw new AssertionError ("unexpected method call: " + e );
255
+ }
256
+ });
257
+
258
+ assertTrue (conditionMetResult .get ());
259
+ assertEquals (new SegmentCountStep .Info (-1L ), conditionInfo .get ());
260
+ }
261
+
198
262
public void testThrowsException () {
199
263
Exception exception = new RuntimeException ("error" );
200
264
Index index = new Index (randomAlphaOfLengthBetween (1 , 20 ), randomAlphaOfLengthBetween (1 , 20 ));
0 commit comments