|
42 | 42 | import java.util.Arrays;
|
43 | 43 | import java.util.Collections;
|
44 | 44 | import java.util.HashMap;
|
| 45 | +import java.util.HashSet; |
45 | 46 | import java.util.List;
|
46 | 47 | import java.util.Map;
|
47 | 48 | import java.util.Objects;
|
| 49 | +import java.util.Set; |
| 50 | +import java.util.stream.Collectors; |
48 | 51 |
|
49 | 52 | import static org.elasticsearch.snapshots.SnapshotInfo.METADATA_FIELD_INTRODUCED;
|
50 | 53 |
|
@@ -106,12 +109,25 @@ public Entry(Snapshot snapshot, boolean includeGlobalState, boolean partial, Sta
|
106 | 109 | } else {
|
107 | 110 | this.shards = shards;
|
108 | 111 | this.waitingIndices = findWaitingIndices(shards);
|
| 112 | + assert assertShardsConsistent(state, indices, shards); |
109 | 113 | }
|
110 | 114 | this.repositoryStateId = repositoryStateId;
|
111 | 115 | this.failure = failure;
|
112 | 116 | this.userMetadata = userMetadata;
|
113 | 117 | }
|
114 | 118 |
|
| 119 | + private static boolean assertShardsConsistent(State state, List<IndexId> indices, |
| 120 | + ImmutableOpenMap<ShardId, ShardSnapshotStatus> shards) { |
| 121 | + if ((state == State.INIT || state == State.ABORTED) && shards.isEmpty()) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + final Set<String> indexNames = indices.stream().map(IndexId::getName).collect(Collectors.toSet()); |
| 125 | + final Set<String> indexNamesInShards = new HashSet<>(); |
| 126 | + shards.keysIt().forEachRemaining(s -> indexNamesInShards.add(s.getIndexName())); |
| 127 | + assert indexNames.equals(indexNamesInShards) |
| 128 | + : "Indices in shards " + indexNamesInShards + " differ from expected indices " + indexNames + " for state [" + state + "]"; |
| 129 | + return true; |
| 130 | + } |
115 | 131 | public Entry(Snapshot snapshot, boolean includeGlobalState, boolean partial, State state, List<IndexId> indices,
|
116 | 132 | long startTime, long repositoryStateId, ImmutableOpenMap<ShardId, ShardSnapshotStatus> shards,
|
117 | 133 | Map<String, Object> userMetadata) {
|
@@ -262,7 +278,10 @@ private ImmutableOpenMap<String, List<ShardId>> findWaitingIndices(ImmutableOpen
|
262 | 278 | Map<String, List<ShardId>> waitingIndicesMap = new HashMap<>();
|
263 | 279 | for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> entry : shards) {
|
264 | 280 | if (entry.value.state() == ShardState.WAITING) {
|
265 |
| - waitingIndicesMap.computeIfAbsent(entry.key.getIndexName(), k -> new ArrayList<>()).add(entry.key); |
| 281 | + List<ShardId> waitingShards = |
| 282 | + waitingIndicesMap.computeIfAbsent(entry.key.getIndexName(), k -> new ArrayList<>()); |
| 283 | + waitingShards.add(entry.key); |
| 284 | + waitingShards.sort(ShardId::compareTo); |
266 | 285 | }
|
267 | 286 | }
|
268 | 287 | if (waitingIndicesMap.isEmpty()) {
|
|
0 commit comments