Skip to content

Commit 5f96032

Browse files
Add Consistency Assertion to SnapshtosInProgress
Assert given input shards and indices are consistent. Also, fixed the equality check for SnapshotsInProgress. Before this change the tests never had more than a single waiting shard per index so they never failed as a result of the waiting shards list not being ordered. Follow up to elastic#47552
1 parent beb5096 commit 5f96032

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

server/src/main/java/org/elasticsearch/cluster/SnapshotsInProgress.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@
4242
import java.util.Arrays;
4343
import java.util.Collections;
4444
import java.util.HashMap;
45+
import java.util.HashSet;
4546
import java.util.List;
4647
import java.util.Map;
4748
import java.util.Objects;
49+
import java.util.Set;
50+
import java.util.stream.Collectors;
4851

4952
import static org.elasticsearch.snapshots.SnapshotInfo.METADATA_FIELD_INTRODUCED;
5053

@@ -106,12 +109,25 @@ public Entry(Snapshot snapshot, boolean includeGlobalState, boolean partial, Sta
106109
} else {
107110
this.shards = shards;
108111
this.waitingIndices = findWaitingIndices(shards);
112+
assert assertShardsConsistent(state, indices, shards);
109113
}
110114
this.repositoryStateId = repositoryStateId;
111115
this.failure = failure;
112116
this.userMetadata = userMetadata;
113117
}
114118

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+
}
115131
public Entry(Snapshot snapshot, boolean includeGlobalState, boolean partial, State state, List<IndexId> indices,
116132
long startTime, long repositoryStateId, ImmutableOpenMap<ShardId, ShardSnapshotStatus> shards,
117133
Map<String, Object> userMetadata) {
@@ -262,7 +278,10 @@ private ImmutableOpenMap<String, List<ShardId>> findWaitingIndices(ImmutableOpen
262278
Map<String, List<ShardId>> waitingIndicesMap = new HashMap<>();
263279
for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> entry : shards) {
264280
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);
266285
}
267286
}
268287
if (waitingIndicesMap.isEmpty()) {

server/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import java.util.ArrayList;
3838
import java.util.List;
39+
import java.util.stream.Collectors;
3940

4041
public class SnapshotsInProgressSerializationTests extends AbstractDiffableWireSerializationTestCase<Custom> {
4142

@@ -62,13 +63,17 @@ private Entry randomSnapshot() {
6263
long startTime = randomLong();
6364
long repositoryStateId = randomLong();
6465
ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
65-
int shardsCount = randomIntBetween(0, 10);
66-
for (int j = 0; j < shardsCount; j++) {
67-
ShardId shardId = new ShardId(new Index(randomAlphaOfLength(10), randomAlphaOfLength(10)), randomIntBetween(0, 10));
68-
String nodeId = randomAlphaOfLength(10);
69-
ShardState shardState = randomFrom(ShardState.values());
70-
builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(nodeId, shardState,
71-
shardState.failed() ? randomAlphaOfLength(10) : null, "1"));
66+
final List<Index> esIndices =
67+
indices.stream().map(i -> new Index(i.getName(), randomAlphaOfLength(10))).collect(Collectors.toList());
68+
for (Index idx : esIndices) {
69+
int shardsCount = randomIntBetween(1, 10);
70+
for (int j = 0; j < shardsCount; j++) {
71+
ShardId shardId = new ShardId(idx, j);
72+
String nodeId = randomAlphaOfLength(10);
73+
ShardState shardState = randomFrom(ShardState.values());
74+
builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(nodeId, shardState,
75+
shardState.failed() ? randomAlphaOfLength(10) : null, "1"));
76+
}
7277
}
7378
ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = builder.build();
7479
return new Entry(snapshot, includeGlobalState, partial, state, indices, startTime, repositoryStateId, shards,

0 commit comments

Comments
 (0)