Skip to content

Commit 4f41853

Browse files
Add Consistency Assertion to SnapshotsInProgress (#47598)
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 #47552
1 parent 9945d5c commit 4f41853

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

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

+16-2
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) {
@@ -190,7 +206,6 @@ public boolean equals(Object o) {
190206
if (!shards.equals(entry.shards)) return false;
191207
if (!snapshot.equals(entry.snapshot)) return false;
192208
if (state != entry.state) return false;
193-
if (!waitingIndices.equals(entry.waitingIndices)) return false;
194209
if (repositoryStateId != entry.repositoryStateId) return false;
195210

196211
return true;
@@ -204,7 +219,6 @@ public int hashCode() {
204219
result = 31 * result + (partial ? 1 : 0);
205220
result = 31 * result + shards.hashCode();
206221
result = 31 * result + indices.hashCode();
207-
result = 31 * result + waitingIndices.hashCode();
208222
result = 31 * result + Long.hashCode(startTime);
209223
result = 31 * result + Long.hashCode(repositoryStateId);
210224
return result;

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

+12-7
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)