42
42
import java .util .HashMap ;
43
43
import java .util .List ;
44
44
import java .util .Map ;
45
+ import java .util .Objects ;
45
46
46
47
/**
47
48
* Meta data about snapshots that are currently executing
@@ -53,12 +54,7 @@ public class SnapshotsInProgress extends AbstractNamedDiffable<Custom> implement
53
54
public boolean equals (Object o ) {
54
55
if (this == o ) return true ;
55
56
if (o == null || getClass () != o .getClass ()) return false ;
56
-
57
- SnapshotsInProgress that = (SnapshotsInProgress ) o ;
58
-
59
- if (!entries .equals (that .entries )) return false ;
60
-
61
- return true ;
57
+ return entries .equals (((SnapshotsInProgress ) o ).entries );
62
58
}
63
59
64
60
@ Override
@@ -208,18 +204,11 @@ public String toString() {
208
204
return snapshot .toString ();
209
205
}
210
206
211
- // package private for testing
212
- ImmutableOpenMap <String , List <ShardId >> findWaitingIndices (ImmutableOpenMap <ShardId , ShardSnapshotStatus > shards ) {
207
+ private ImmutableOpenMap <String , List <ShardId >> findWaitingIndices (ImmutableOpenMap <ShardId , ShardSnapshotStatus > shards ) {
213
208
Map <String , List <ShardId >> waitingIndicesMap = new HashMap <>();
214
209
for (ObjectObjectCursor <ShardId , ShardSnapshotStatus > entry : shards ) {
215
- if (entry .value .state () == State .WAITING ) {
216
- final String indexName = entry .key .getIndexName ();
217
- List <ShardId > waitingShards = waitingIndicesMap .get (indexName );
218
- if (waitingShards == null ) {
219
- waitingShards = new ArrayList <>();
220
- waitingIndicesMap .put (indexName , waitingShards );
221
- }
222
- waitingShards .add (entry .key );
210
+ if (entry .value .state () == ShardState .WAITING ) {
211
+ waitingIndicesMap .computeIfAbsent (entry .key .getIndexName (), k -> new ArrayList <>()).add (entry .key );
223
212
}
224
213
}
225
214
if (waitingIndicesMap .isEmpty ()) {
@@ -241,28 +230,27 @@ ImmutableOpenMap<String, List<ShardId>> findWaitingIndices(ImmutableOpenMap<Shar
241
230
*/
242
231
public static boolean completed (ObjectContainer <ShardSnapshotStatus > shards ) {
243
232
for (ObjectCursor <ShardSnapshotStatus > status : shards ) {
244
- if (status .value .state ().completed () == false ) {
233
+ if (status .value .state ().completed == false ) {
245
234
return false ;
246
235
}
247
236
}
248
237
return true ;
249
238
}
250
239
251
-
252
240
public static class ShardSnapshotStatus {
253
- private final State state ;
241
+ private final ShardState state ;
254
242
private final String nodeId ;
255
243
private final String reason ;
256
244
257
245
public ShardSnapshotStatus (String nodeId ) {
258
- this (nodeId , State .INIT );
246
+ this (nodeId , ShardState .INIT );
259
247
}
260
248
261
- public ShardSnapshotStatus (String nodeId , State state ) {
249
+ public ShardSnapshotStatus (String nodeId , ShardState state ) {
262
250
this (nodeId , state , null );
263
251
}
264
252
265
- public ShardSnapshotStatus (String nodeId , State state , String reason ) {
253
+ public ShardSnapshotStatus (String nodeId , ShardState state , String reason ) {
266
254
this .nodeId = nodeId ;
267
255
this .state = state ;
268
256
this .reason = reason ;
@@ -272,11 +260,11 @@ public ShardSnapshotStatus(String nodeId, State state, String reason) {
272
260
273
261
public ShardSnapshotStatus (StreamInput in ) throws IOException {
274
262
nodeId = in .readOptionalString ();
275
- state = State .fromValue (in .readByte ());
263
+ state = ShardState .fromValue (in .readByte ());
276
264
reason = in .readOptionalString ();
277
265
}
278
266
279
- public State state () {
267
+ public ShardState state () {
280
268
return state ;
281
269
}
282
270
@@ -298,14 +286,9 @@ public void writeTo(StreamOutput out) throws IOException {
298
286
public boolean equals (Object o ) {
299
287
if (this == o ) return true ;
300
288
if (o == null || getClass () != o .getClass ()) return false ;
301
-
302
289
ShardSnapshotStatus status = (ShardSnapshotStatus ) o ;
290
+ return Objects .equals (nodeId , status .nodeId ) && Objects .equals (reason , status .reason ) && state == status .state ;
303
291
304
- if (nodeId != null ? !nodeId .equals (status .nodeId ) : status .nodeId != null ) return false ;
305
- if (reason != null ? !reason .equals (status .reason ) : status .reason != null ) return false ;
306
- if (state != status .state ) return false ;
307
-
308
- return true ;
309
292
}
310
293
311
294
@ Override
@@ -331,11 +314,11 @@ public enum State {
331
314
MISSING ((byte ) 5 , true , true ),
332
315
WAITING ((byte ) 6 , false , false );
333
316
334
- private byte value ;
317
+ private final byte value ;
335
318
336
- private boolean completed ;
319
+ private final boolean completed ;
337
320
338
- private boolean failed ;
321
+ private final boolean failed ;
339
322
340
323
State (byte value , boolean completed , boolean failed ) {
341
324
this .value = value ;
@@ -379,7 +362,6 @@ public static State fromValue(byte value) {
379
362
380
363
private final List <Entry > entries ;
381
364
382
-
383
365
public SnapshotsInProgress (List <Entry > entries ) {
384
366
this .entries = entries ;
385
367
}
@@ -534,4 +516,52 @@ public void toXContent(Entry entry, XContentBuilder builder, ToXContent.Params p
534
516
builder .endArray ();
535
517
builder .endObject ();
536
518
}
519
+
520
+ public enum ShardState {
521
+ INIT ((byte ) 0 , false , false ),
522
+ SUCCESS ((byte ) 2 , true , false ),
523
+ FAILED ((byte ) 3 , true , true ),
524
+ ABORTED ((byte ) 4 , false , true ),
525
+ MISSING ((byte ) 5 , true , true ),
526
+ WAITING ((byte ) 6 , false , false );
527
+
528
+ private final byte value ;
529
+
530
+ private final boolean completed ;
531
+
532
+ private final boolean failed ;
533
+
534
+ ShardState (byte value , boolean completed , boolean failed ) {
535
+ this .value = value ;
536
+ this .completed = completed ;
537
+ this .failed = failed ;
538
+ }
539
+
540
+ public boolean completed () {
541
+ return completed ;
542
+ }
543
+
544
+ public boolean failed () {
545
+ return failed ;
546
+ }
547
+
548
+ public static ShardState fromValue (byte value ) {
549
+ switch (value ) {
550
+ case 0 :
551
+ return INIT ;
552
+ case 2 :
553
+ return SUCCESS ;
554
+ case 3 :
555
+ return FAILED ;
556
+ case 4 :
557
+ return ABORTED ;
558
+ case 5 :
559
+ return MISSING ;
560
+ case 6 :
561
+ return WAITING ;
562
+ default :
563
+ throw new IllegalArgumentException ("No shard snapshot state for value [" + value + "]" );
564
+ }
565
+ }
566
+ }
537
567
}
0 commit comments