@@ -69,7 +69,7 @@ public final class RepositoryData {
69
69
* An instance initialized for an empty repository.
70
70
*/
71
71
public static final RepositoryData EMPTY = new RepositoryData (EMPTY_REPO_GEN ,
72
- Collections .emptyMap (), Collections .emptyMap (), Collections .emptyMap (), ShardGenerations .EMPTY );
72
+ Collections .emptyMap (), Collections .emptyMap (), Collections .emptyMap (), Collections . emptyMap (), ShardGenerations .EMPTY );
73
73
74
74
/**
75
75
* The generational id of the index file from which the repository data was read.
@@ -92,26 +92,44 @@ public final class RepositoryData {
92
92
*/
93
93
private final Map <IndexId , Set <SnapshotId >> indexSnapshots ;
94
94
95
+ private final Map <String , Version > snapshotVersions ;
96
+
95
97
/**
96
98
* Shard generations.
97
99
*/
98
100
private final ShardGenerations shardGenerations ;
99
101
100
102
public RepositoryData (long genId , Map <String , SnapshotId > snapshotIds , Map <String , SnapshotState > snapshotStates ,
101
- Map <IndexId , Set <SnapshotId >> indexSnapshots , ShardGenerations shardGenerations ) {
103
+ Map <String , Version > snapshotVersions , Map <IndexId , Set <SnapshotId >> indexSnapshots ,
104
+ ShardGenerations shardGenerations ) {
102
105
this .genId = genId ;
103
106
this .snapshotIds = Collections .unmodifiableMap (snapshotIds );
104
107
this .snapshotStates = Collections .unmodifiableMap (snapshotStates );
105
108
this .indices = Collections .unmodifiableMap (indexSnapshots .keySet ().stream ()
106
109
.collect (Collectors .toMap (IndexId ::getName , Function .identity ())));
107
110
this .indexSnapshots = Collections .unmodifiableMap (indexSnapshots );
108
111
this .shardGenerations = Objects .requireNonNull (shardGenerations );
112
+ this .snapshotVersions = snapshotVersions ;
109
113
assert indices .values ().containsAll (shardGenerations .indices ()) : "ShardGenerations contained indices "
110
114
+ shardGenerations .indices () + " but snapshots only reference indices " + indices .values ();
111
115
}
112
116
113
117
protected RepositoryData copy () {
114
- return new RepositoryData (genId , snapshotIds , snapshotStates , indexSnapshots , shardGenerations );
118
+ return new RepositoryData (genId , snapshotIds , snapshotStates , snapshotVersions , indexSnapshots , shardGenerations );
119
+ }
120
+
121
+ /**
122
+ * Creates a copy of this instance that contains updated version data.
123
+ * @param versions map of snapshot versions
124
+ * @return copy with updated version data
125
+ */
126
+ public RepositoryData withVersions (Map <SnapshotId , Version > versions ) {
127
+ if (versions .isEmpty ()) {
128
+ return this ;
129
+ }
130
+ final Map <String , Version > newVersions = new HashMap <>(snapshotVersions );
131
+ versions .forEach ((id , version ) -> newVersions .put (id .getUUID (), version ));
132
+ return new RepositoryData (genId , snapshotIds , snapshotStates , newVersions , indexSnapshots , shardGenerations );
115
133
}
116
134
117
135
public ShardGenerations shardGenerations () {
@@ -141,6 +159,14 @@ public SnapshotState getSnapshotState(final SnapshotId snapshotId) {
141
159
return snapshotStates .get (snapshotId .getUUID ());
142
160
}
143
161
162
+ /**
163
+ * Returns the {@link Version} for the given snapshot or {@code null} if unknown.
164
+ */
165
+ @ Nullable
166
+ public Version getVersion (SnapshotId snapshotId ) {
167
+ return snapshotVersions .get (snapshotId .getUUID ());
168
+ }
169
+
144
170
/**
145
171
* Returns an unmodifiable map of the index names to {@link IndexId} in the repository.
146
172
*/
@@ -173,6 +199,7 @@ public List<IndexId> indicesToUpdateAfterRemovingSnapshot(SnapshotId snapshotId)
173
199
*/
174
200
public RepositoryData addSnapshot (final SnapshotId snapshotId ,
175
201
final SnapshotState snapshotState ,
202
+ final Version version ,
176
203
final ShardGenerations shardGenerations ) {
177
204
if (snapshotIds .containsKey (snapshotId .getUUID ())) {
178
205
// if the snapshot id already exists in the repository data, it means an old master
@@ -184,11 +211,13 @@ public RepositoryData addSnapshot(final SnapshotId snapshotId,
184
211
snapshots .put (snapshotId .getUUID (), snapshotId );
185
212
Map <String , SnapshotState > newSnapshotStates = new HashMap <>(snapshotStates );
186
213
newSnapshotStates .put (snapshotId .getUUID (), snapshotState );
214
+ Map <String , Version > newSnapshotVersions = new HashMap <>(snapshotVersions );
215
+ newSnapshotVersions .put (snapshotId .getUUID (), version );
187
216
Map <IndexId , Set <SnapshotId >> allIndexSnapshots = new HashMap <>(indexSnapshots );
188
217
for (final IndexId indexId : shardGenerations .indices ()) {
189
218
allIndexSnapshots .computeIfAbsent (indexId , k -> new LinkedHashSet <>()).add (snapshotId );
190
219
}
191
- return new RepositoryData (genId , snapshots , newSnapshotStates , allIndexSnapshots ,
220
+ return new RepositoryData (genId , snapshots , newSnapshotStates , newSnapshotVersions , allIndexSnapshots ,
192
221
ShardGenerations .builder ().putAll (this .shardGenerations ).putAll (shardGenerations ).build ());
193
222
}
194
223
@@ -202,7 +231,7 @@ public RepositoryData withGenId(long newGeneration) {
202
231
if (newGeneration == genId ) {
203
232
return this ;
204
233
}
205
- return new RepositoryData (newGeneration , this . snapshotIds , this . snapshotStates , this . indexSnapshots , this . shardGenerations );
234
+ return new RepositoryData (newGeneration , snapshotIds , snapshotStates , snapshotVersions , indexSnapshots , shardGenerations );
206
235
}
207
236
208
237
/**
@@ -222,6 +251,8 @@ public RepositoryData removeSnapshot(final SnapshotId snapshotId, final ShardGen
222
251
}
223
252
Map <String , SnapshotState > newSnapshotStates = new HashMap <>(snapshotStates );
224
253
newSnapshotStates .remove (snapshotId .getUUID ());
254
+ final Map <String , Version > newSnapshotVersions = new HashMap <>(snapshotVersions );
255
+ newSnapshotVersions .remove (snapshotId .getUUID ());
225
256
Map <IndexId , Set <SnapshotId >> indexSnapshots = new HashMap <>();
226
257
for (final IndexId indexId : indices .values ()) {
227
258
Set <SnapshotId > set ;
@@ -241,7 +272,7 @@ public RepositoryData removeSnapshot(final SnapshotId snapshotId, final ShardGen
241
272
indexSnapshots .put (indexId , set );
242
273
}
243
274
244
- return new RepositoryData (genId , newSnapshotIds , newSnapshotStates , indexSnapshots ,
275
+ return new RepositoryData (genId , newSnapshotIds , newSnapshotStates , newSnapshotVersions , indexSnapshots ,
245
276
ShardGenerations .builder ().putAll (shardGenerations ).putAll (updatedShardGenerations )
246
277
.retainIndicesAndPruneDeletes (indexSnapshots .keySet ()).build ()
247
278
);
@@ -269,14 +300,15 @@ public boolean equals(Object obj) {
269
300
RepositoryData that = (RepositoryData ) obj ;
270
301
return snapshotIds .equals (that .snapshotIds )
271
302
&& snapshotStates .equals (that .snapshotStates )
303
+ && snapshotVersions .equals (that .snapshotVersions )
272
304
&& indices .equals (that .indices )
273
305
&& indexSnapshots .equals (that .indexSnapshots )
274
306
&& shardGenerations .equals (that .shardGenerations );
275
307
}
276
308
277
309
@ Override
278
310
public int hashCode () {
279
- return Objects .hash (snapshotIds , snapshotStates , indices , indexSnapshots , shardGenerations );
311
+ return Objects .hash (snapshotIds , snapshotStates , snapshotVersions , indices , indexSnapshots , shardGenerations );
280
312
}
281
313
282
314
/**
@@ -323,6 +355,7 @@ public List<IndexId> resolveNewIndices(final List<String> indicesToResolve) {
323
355
private static final String NAME = "name" ;
324
356
private static final String UUID = "uuid" ;
325
357
private static final String STATE = "state" ;
358
+ private static final String VERSION = "version" ;
326
359
private static final String MIN_VERSION = "min_version" ;
327
360
328
361
/**
@@ -339,6 +372,9 @@ public XContentBuilder snapshotsToXContent(final XContentBuilder builder, final
339
372
if (snapshotStates .containsKey (snapshot .getUUID ())) {
340
373
builder .field (STATE , snapshotStates .get (snapshot .getUUID ()).value ());
341
374
}
375
+ if (snapshotVersions .containsKey (snapshot .getUUID ())) {
376
+ builder .field (VERSION , snapshotVersions .get (snapshot .getUUID ()).toString ());
377
+ }
342
378
builder .endObject ();
343
379
}
344
380
builder .endArray ();
@@ -378,6 +414,7 @@ public XContentBuilder snapshotsToXContent(final XContentBuilder builder, final
378
414
public static RepositoryData snapshotsFromXContent (final XContentParser parser , long genId ) throws IOException {
379
415
final Map <String , SnapshotId > snapshots = new HashMap <>();
380
416
final Map <String , SnapshotState > snapshotStates = new HashMap <>();
417
+ final Map <String , Version > snapshotVersions = new HashMap <>();
381
418
final Map <IndexId , Set <SnapshotId >> indexSnapshots = new HashMap <>();
382
419
final ShardGenerations .Builder shardGenerations = ShardGenerations .builder ();
383
420
@@ -390,6 +427,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
390
427
String name = null ;
391
428
String uuid = null ;
392
429
SnapshotState state = null ;
430
+ Version version = null ;
393
431
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
394
432
String currentFieldName = parser .currentName ();
395
433
parser .nextToken ();
@@ -399,12 +437,17 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
399
437
uuid = parser .text ();
400
438
} else if (STATE .equals (currentFieldName )) {
401
439
state = SnapshotState .fromValue (parser .numberValue ().byteValue ());
440
+ } else if (VERSION .equals (currentFieldName )) {
441
+ version = Version .fromString (parser .text ());
402
442
}
403
443
}
404
444
final SnapshotId snapshotId = new SnapshotId (name , uuid );
405
445
if (state != null ) {
406
446
snapshotStates .put (uuid , state );
407
447
}
448
+ if (version != null ) {
449
+ snapshotVersions .put (uuid , version );
450
+ }
408
451
snapshots .put (snapshotId .getUUID (), snapshotId );
409
452
}
410
453
} else {
@@ -488,7 +531,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
488
531
} else {
489
532
throw new ElasticsearchParseException ("start object expected" );
490
533
}
491
- return new RepositoryData (genId , snapshots , snapshotStates , indexSnapshots , shardGenerations .build ());
534
+ return new RepositoryData (genId , snapshots , snapshotStates , snapshotVersions , indexSnapshots , shardGenerations .build ());
492
535
}
493
536
494
537
}
0 commit comments