|
66 | 66 | import org.elasticsearch.index.Index; |
67 | 67 | import org.elasticsearch.index.shard.IndexShard; |
68 | 68 | import org.elasticsearch.index.shard.ShardId; |
| 69 | +import org.elasticsearch.repositories.IndexId; |
69 | 70 | import org.elasticsearch.repositories.RepositoriesService; |
70 | 71 | import org.elasticsearch.repositories.Repository; |
71 | 72 | import org.elasticsearch.repositories.RepositoryData; |
|
91 | 92 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED; |
92 | 93 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_UPGRADED; |
93 | 94 | import static org.elasticsearch.common.util.set.Sets.newHashSet; |
| 95 | +import static org.elasticsearch.snapshots.SnapshotUtils.filterIndices; |
94 | 96 |
|
95 | 97 | /** |
96 | 98 | * Service responsible for restoring snapshots |
@@ -182,17 +184,34 @@ public void restoreSnapshot(final RestoreRequest request, final ActionListener<R |
182 | 184 | if (matchingSnapshotId.isPresent() == false) { |
183 | 185 | throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, "snapshot does not exist"); |
184 | 186 | } |
| 187 | + |
185 | 188 | final SnapshotId snapshotId = matchingSnapshotId.get(); |
186 | 189 | final SnapshotInfo snapshotInfo = repository.getSnapshotInfo(snapshotId); |
187 | 190 | final Snapshot snapshot = new Snapshot(request.repositoryName, snapshotId); |
188 | | - List<String> filteredIndices = SnapshotUtils.filterIndices(snapshotInfo.indices(), request.indices(), request.indicesOptions()); |
189 | | - final MetaData metaData = repository.getSnapshotMetaData(snapshotInfo, repositoryData.resolveIndices(filteredIndices)); |
190 | 191 |
|
191 | 192 | // Make sure that we can restore from this snapshot |
192 | 193 | validateSnapshotRestorable(request.repositoryName, snapshotInfo); |
193 | 194 |
|
194 | | - // Find list of indices that we need to restore |
195 | | - final Map<String, String> renamedIndices = renamedIndices(request, filteredIndices); |
| 195 | + // Resolve the indices from the snapshot that need to be restored |
| 196 | + final List<String> indicesInSnapshot = filterIndices(snapshotInfo.indices(), request.indices(), request.indicesOptions()); |
| 197 | + |
| 198 | + final MetaData.Builder metaDataBuilder; |
| 199 | + if (request.includeGlobalState()) { |
| 200 | + metaDataBuilder = MetaData.builder(repository.getSnapshotGlobalMetaData(snapshotId)); |
| 201 | + } else { |
| 202 | + metaDataBuilder = MetaData.builder(); |
| 203 | + } |
| 204 | + |
| 205 | + final List<IndexId> indexIdsInSnapshot = repositoryData.resolveIndices(indicesInSnapshot); |
| 206 | + for (IndexId indexId : indexIdsInSnapshot) { |
| 207 | + metaDataBuilder.put(repository.getSnapshotIndexMetaData(snapshotId, indexId), false); |
| 208 | + } |
| 209 | + |
| 210 | + final MetaData metaData = metaDataBuilder.build(); |
| 211 | + |
| 212 | + // Apply renaming on index names, returning a map of names where |
| 213 | + // the key is the renamed index and the value is the original name |
| 214 | + final Map<String, String> indices = renamedIndices(request, indicesInSnapshot); |
196 | 215 |
|
197 | 216 | // Now we can start the actual restore process by adding shards to be recovered in the cluster state |
198 | 217 | // and updating cluster metadata (global and index) as needed |
@@ -222,12 +241,13 @@ public ClusterState execute(ClusterState currentState) { |
222 | 241 | RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable()); |
223 | 242 | ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards; |
224 | 243 | Set<String> aliases = new HashSet<>(); |
225 | | - if (!renamedIndices.isEmpty()) { |
| 244 | + |
| 245 | + if (indices.isEmpty() == false) { |
226 | 246 | // We have some indices to restore |
227 | 247 | ImmutableOpenMap.Builder<ShardId, RestoreInProgress.ShardRestoreStatus> shardsBuilder = ImmutableOpenMap.builder(); |
228 | 248 | final Version minIndexCompatibilityVersion = currentState.getNodes().getMaxNodeVersion() |
229 | 249 | .minimumIndexCompatibilityVersion(); |
230 | | - for (Map.Entry<String, String> indexEntry : renamedIndices.entrySet()) { |
| 250 | + for (Map.Entry<String, String> indexEntry : indices.entrySet()) { |
231 | 251 | String index = indexEntry.getValue(); |
232 | 252 | boolean partial = checkPartial(index); |
233 | 253 | SnapshotRecoverySource recoverySource = new SnapshotRecoverySource(snapshot, snapshotInfo.version(), index); |
@@ -304,21 +324,42 @@ public ClusterState execute(ClusterState currentState) { |
304 | 324 | } |
305 | 325 |
|
306 | 326 | shards = shardsBuilder.build(); |
307 | | - RestoreInProgress.Entry restoreEntry = new RestoreInProgress.Entry(snapshot, overallState(RestoreInProgress.State.INIT, shards), Collections.unmodifiableList(new ArrayList<>(renamedIndices.keySet())), shards); |
| 327 | + RestoreInProgress.Entry restoreEntry = new RestoreInProgress.Entry(snapshot, overallState(RestoreInProgress.State.INIT, shards), Collections.unmodifiableList(new ArrayList<>(indices.keySet())), shards); |
308 | 328 | builder.putCustom(RestoreInProgress.TYPE, new RestoreInProgress(restoreEntry)); |
309 | 329 | } else { |
310 | 330 | shards = ImmutableOpenMap.of(); |
311 | 331 | } |
312 | 332 |
|
313 | | - checkAliasNameConflicts(renamedIndices, aliases); |
| 333 | + checkAliasNameConflicts(indices, aliases); |
314 | 334 |
|
315 | 335 | // Restore global state if needed |
316 | | - restoreGlobalStateIfRequested(mdBuilder); |
| 336 | + if (request.includeGlobalState()) { |
| 337 | + if (metaData.persistentSettings() != null) { |
| 338 | + Settings settings = metaData.persistentSettings(); |
| 339 | + clusterSettings.validateUpdate(settings); |
| 340 | + mdBuilder.persistentSettings(settings); |
| 341 | + } |
| 342 | + if (metaData.templates() != null) { |
| 343 | + // TODO: Should all existing templates be deleted first? |
| 344 | + for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) { |
| 345 | + mdBuilder.put(cursor.value); |
| 346 | + } |
| 347 | + } |
| 348 | + if (metaData.customs() != null) { |
| 349 | + for (ObjectObjectCursor<String, MetaData.Custom> cursor : metaData.customs()) { |
| 350 | + if (!RepositoriesMetaData.TYPE.equals(cursor.key)) { |
| 351 | + // Don't restore repositories while we are working with them |
| 352 | + // TODO: Should we restore them at the end? |
| 353 | + mdBuilder.putCustom(cursor.key, cursor.value); |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + } |
317 | 358 |
|
318 | 359 | if (completed(shards)) { |
319 | 360 | // We don't have any indices to restore - we are done |
320 | 361 | restoreInfo = new RestoreInfo(snapshotId.getName(), |
321 | | - Collections.unmodifiableList(new ArrayList<>(renamedIndices.keySet())), |
| 362 | + Collections.unmodifiableList(new ArrayList<>(indices.keySet())), |
322 | 363 | shards.size(), |
323 | 364 | shards.size() - failedShards(shards)); |
324 | 365 | } |
@@ -426,32 +467,6 @@ private IndexMetaData updateIndexSettings(IndexMetaData indexMetaData, Settings |
426 | 467 | return builder.settings(settingsBuilder).build(); |
427 | 468 | } |
428 | 469 |
|
429 | | - private void restoreGlobalStateIfRequested(MetaData.Builder mdBuilder) { |
430 | | - if (request.includeGlobalState()) { |
431 | | - if (metaData.persistentSettings() != null) { |
432 | | - Settings settings = metaData.persistentSettings(); |
433 | | - clusterSettings.validateUpdate(settings); |
434 | | - mdBuilder.persistentSettings(settings); |
435 | | - } |
436 | | - if (metaData.templates() != null) { |
437 | | - // TODO: Should all existing templates be deleted first? |
438 | | - for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) { |
439 | | - mdBuilder.put(cursor.value); |
440 | | - } |
441 | | - } |
442 | | - if (metaData.customs() != null) { |
443 | | - for (ObjectObjectCursor<String, MetaData.Custom> cursor : metaData.customs()) { |
444 | | - if (!RepositoriesMetaData.TYPE.equals(cursor.key)) { |
445 | | - // Don't restore repositories while we are working with them |
446 | | - // TODO: Should we restore them at the end? |
447 | | - mdBuilder.putCustom(cursor.key, cursor.value); |
448 | | - } |
449 | | - } |
450 | | - } |
451 | | - } |
452 | | - } |
453 | | - |
454 | | - |
455 | 470 | @Override |
456 | 471 | public void onFailure(String source, Exception e) { |
457 | 472 | logger.warn(() -> new ParameterizedMessage("[{}] failed to restore snapshot", snapshotId), e); |
@@ -757,7 +772,7 @@ private Map<String, String> renamedIndices(RestoreRequest request, List<String> |
757 | 772 | "indices [" + index + "] and [" + previousIndex + "] are renamed into the same index [" + renamedIndex + "]"); |
758 | 773 | } |
759 | 774 | } |
760 | | - return renamedIndices; |
| 775 | + return Collections.unmodifiableMap(renamedIndices); |
761 | 776 | } |
762 | 777 |
|
763 | 778 | /** |
|
0 commit comments