1919
2020package org .elasticsearch .cluster ;
2121
22+ import com .carrotsearch .hppc .cursors .ObjectCursor ;
2223import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
2324import org .elasticsearch .Version ;
2425import org .elasticsearch .cluster .ClusterState .Custom ;
3233
3334import java .io .IOException ;
3435import java .util .ArrayList ;
35- import java .util .Arrays ;
3636import java .util .Collections ;
37+ import java .util .Iterator ;
3738import java .util .List ;
3839import java .util .Objects ;
40+ import java .util .UUID ;
3941
4042/**
4143 * Meta data about restore processes that are currently executing
4244 */
43- public class RestoreInProgress extends AbstractNamedDiffable <Custom > implements Custom {
45+ public class RestoreInProgress extends AbstractNamedDiffable <Custom > implements Custom , Iterable <RestoreInProgress .Entry > {
46+
47+ /**
48+ * Fallback UUID used for restore operations that were started before v7.0 and don't have a uuid in the cluster state.
49+ */
50+ public static final String BWC_UUID = new UUID (0 , 0 ).toString ();
4451
4552 public static final String TYPE = "restore" ;
4653
47- private final List < Entry > entries ;
54+ private final ImmutableOpenMap < String , Entry > entries ;
4855
4956 /**
5057 * Constructs new restore metadata
5158 *
52- * @param entries list of currently running restore processes
59+ * @param entries map of currently running restore processes keyed by their restore uuid
5360 */
54- public RestoreInProgress (Entry ... entries ) {
55- this .entries = Arrays .asList (entries );
56- }
57-
58- /**
59- * Returns list of currently running restore processes
60- *
61- * @return list of currently running restore processes
62- */
63- public List <Entry > entries () {
64- return this .entries ;
61+ private RestoreInProgress (ImmutableOpenMap <String , Entry > entries ) {
62+ this .entries = entries ;
6563 }
6664
6765 @ Override
@@ -83,20 +81,48 @@ public int hashCode() {
8381
8482 @ Override
8583 public String toString () {
86- StringBuilder builder = new StringBuilder ("RestoreInProgress[" );
87- for (int i = 0 ; i < entries .size (); i ++) {
88- builder .append (entries .get (i ).snapshot ().getSnapshotId ().getName ());
89- if (i + 1 < entries .size ()) {
90- builder .append ("," );
91- }
84+ return new StringBuilder ("RestoreInProgress[" ).append (entries ).append ("]" ).toString ();
85+ }
86+
87+ public Entry get (String restoreUUID ) {
88+ return entries .get (restoreUUID );
89+ }
90+
91+ public boolean isEmpty () {
92+ return entries .isEmpty ();
93+ }
94+
95+ @ Override
96+ public Iterator <Entry > iterator () {
97+ return entries .valuesIt ();
98+ }
99+
100+ public static final class Builder {
101+
102+ private final ImmutableOpenMap .Builder <String , Entry > entries = ImmutableOpenMap .builder ();
103+
104+ public Builder () {
105+ }
106+
107+ public Builder (RestoreInProgress restoreInProgress ) {
108+ entries .putAll (restoreInProgress .entries );
109+ }
110+
111+ public Builder add (Entry entry ) {
112+ entries .put (entry .uuid , entry );
113+ return this ;
114+ }
115+
116+ public RestoreInProgress build () {
117+ return new RestoreInProgress (entries .build ());
92118 }
93- return builder .append ("]" ).toString ();
94119 }
95120
96121 /**
97122 * Restore metadata
98123 */
99124 public static class Entry {
125+ private final String uuid ;
100126 private final State state ;
101127 private final Snapshot snapshot ;
102128 private final ImmutableOpenMap <ShardId , ShardRestoreStatus > shards ;
@@ -105,12 +131,14 @@ public static class Entry {
105131 /**
106132 * Creates new restore metadata
107133 *
134+ * @param uuid uuid of the restore
108135 * @param snapshot snapshot
109136 * @param state current state of the restore process
110137 * @param indices list of indices being restored
111138 * @param shards map of shards being restored to their current restore status
112139 */
113- public Entry (Snapshot snapshot , State state , List <String > indices , ImmutableOpenMap <ShardId , ShardRestoreStatus > shards ) {
140+ public Entry (String uuid , Snapshot snapshot , State state , List <String > indices ,
141+ ImmutableOpenMap <ShardId , ShardRestoreStatus > shards ) {
114142 this .snapshot = Objects .requireNonNull (snapshot );
115143 this .state = Objects .requireNonNull (state );
116144 this .indices = Objects .requireNonNull (indices );
@@ -119,6 +147,15 @@ public Entry(Snapshot snapshot, State state, List<String> indices, ImmutableOpen
119147 } else {
120148 this .shards = shards ;
121149 }
150+ this .uuid = Objects .requireNonNull (uuid );
151+ }
152+
153+ /**
154+ * Returns restore uuid
155+ * @return restore uuid
156+ */
157+ public String uuid () {
158+ return uuid ;
122159 }
123160
124161 /**
@@ -166,15 +203,16 @@ public boolean equals(Object o) {
166203 return false ;
167204 }
168205 @ SuppressWarnings ("unchecked" ) Entry entry = (Entry ) o ;
169- return snapshot .equals (entry .snapshot ) &&
206+ return uuid .equals (entry .uuid ) &&
207+ snapshot .equals (entry .snapshot ) &&
170208 state == entry .state &&
171209 indices .equals (entry .indices ) &&
172210 shards .equals (entry .shards );
173211 }
174212
175213 @ Override
176214 public int hashCode () {
177- return Objects .hash (snapshot , state , indices , shards );
215+ return Objects .hash (uuid , snapshot , state , indices , shards );
178216 }
179217 }
180218
@@ -393,8 +431,15 @@ public static NamedDiff<Custom> readDiffFrom(StreamInput in) throws IOException
393431 }
394432
395433 public RestoreInProgress (StreamInput in ) throws IOException {
396- Entry [] entries = new Entry [in .readVInt ()];
397- for (int i = 0 ; i < entries .length ; i ++) {
434+ int count = in .readVInt ();
435+ final ImmutableOpenMap .Builder <String , Entry > entriesBuilder = ImmutableOpenMap .builder (count );
436+ for (int i = 0 ; i < count ; i ++) {
437+ final String uuid ;
438+ if (in .getVersion ().onOrAfter (Version .V_6_6_0 )) {
439+ uuid = in .readString ();
440+ } else {
441+ uuid = BWC_UUID ;
442+ }
398443 Snapshot snapshot = new Snapshot (in );
399444 State state = State .fromValue (in .readByte ());
400445 int indices = in .readVInt ();
@@ -409,9 +454,9 @@ public RestoreInProgress(StreamInput in) throws IOException {
409454 ShardRestoreStatus shardState = ShardRestoreStatus .readShardRestoreStatus (in );
410455 builder .put (shardId , shardState );
411456 }
412- entries [ i ] = new Entry (snapshot , state , Collections .unmodifiableList (indexBuilder ), builder .build ());
457+ entriesBuilder . put ( uuid , new Entry (uuid , snapshot , state , Collections .unmodifiableList (indexBuilder ), builder .build () ));
413458 }
414- this .entries = Arrays . asList ( entries );
459+ this .entries = entriesBuilder . build ( );
415460 }
416461
417462 /**
@@ -420,7 +465,11 @@ public RestoreInProgress(StreamInput in) throws IOException {
420465 @ Override
421466 public void writeTo (StreamOutput out ) throws IOException {
422467 out .writeVInt (entries .size ());
423- for (Entry entry : entries ) {
468+ for (ObjectCursor <Entry > v : entries .values ()) {
469+ Entry entry = v .value ;
470+ if (out .getVersion ().onOrAfter (Version .V_6_6_0 )) {
471+ out .writeString (entry .uuid );
472+ }
424473 entry .snapshot ().writeTo (out );
425474 out .writeByte (entry .state ().value ());
426475 out .writeVInt (entry .indices ().size ());
@@ -441,8 +490,8 @@ public void writeTo(StreamOutput out) throws IOException {
441490 @ Override
442491 public XContentBuilder toXContent (XContentBuilder builder , ToXContent .Params params ) throws IOException {
443492 builder .startArray ("snapshots" );
444- for (Entry entry : entries ) {
445- toXContent (entry , builder , params );
493+ for (ObjectCursor < Entry > entry : entries . values () ) {
494+ toXContent (entry . value , builder , params );
446495 }
447496 builder .endArray ();
448497 return builder ;
0 commit comments