Skip to content

Commit 64217d4

Browse files
Improve serialization tests, address code review comments
1 parent a534a12 commit 64217d4

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

server/src/internalClusterTest/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ public void testCorrectCountsForDoneShards() throws Exception {
254254
internalCluster().restartNode(dataNodeTwo);
255255

256256
final var snapshotStatusAfterRestart = getSnapshotStatus(repoName, snapshotOne);
257-
final var snapshotShardStateAfterNodeRestart = stateFirstShard(snapshotStatusAfterRestart, indexTwo);
258-
assertThat(snapshotShardStateAfterNodeRestart.getStage(), is(SnapshotIndexShardStage.DONE));
259-
assertNotNull("expected a non-null description string for missing stats", snapshotShardStateAfterNodeRestart.getDescription());
260-
final var missingStats = snapshotShardStateAfterNodeRestart.getStats();
257+
258+
final var snapshotShardStateIndexTwo = stateFirstShard(snapshotStatusAfterRestart, indexTwo);
259+
assertThat(snapshotShardStateIndexTwo.getStage(), is(SnapshotIndexShardStage.DONE));
260+
assertNotNull("expected a non-null description string for missing stats", snapshotShardStateIndexTwo.getDescription());
261+
final var missingStats = snapshotShardStateIndexTwo.getStats();
261262
assertThat(missingStats.getTotalFileCount(), equalTo(-1));
262263
assertThat(missingStats.getTotalSize(), equalTo(-1L));
263264

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatus.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ public SnapshotIndexShardStatus(StreamInput in) throws IOException {
101101
}
102102

103103
/**
104-
* Creates an instance for scenarios where the snapshot stats are unavailable, with a non-null description of why the stats are missing.
104+
* Creates an instance for scenarios where the snapshot is {@link SnapshotIndexShardStage#DONE} but the stats are unavailable, with a
105+
* non-null description of why the stats are missing.
105106
*/
106-
public static SnapshotIndexShardStatus forMissingStats(ShardId shardId, SnapshotIndexShardStage stage, String description) {
107+
public static SnapshotIndexShardStatus forDoneButMissingStats(ShardId shardId, String description) {
107108
return new SnapshotIndexShardStatus(
108109
shardId,
109-
stage,
110+
SnapshotIndexShardStage.DONE,
110111
SnapshotStats.forMissingStats(),
111112
null,
112113
null,

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStats.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SnapshotStats implements Writeable, ToXContentObject {
3939
SnapshotStats() {}
4040

4141
SnapshotStats(StreamInput in) throws IOException {
42+
// We use a boolean to indicate if the stats are present (true) or missing (false), to skip writing all the values if missing.
4243
if (in.getTransportVersion().onOrAfter(SNAPSHOT_INDEX_SHARD_STATUS_MISSING_STATS) && in.readBoolean() == false) {
4344
startTime = 0L;
4445
time = 0L;
@@ -158,6 +159,7 @@ public long getProcessedSize() {
158159
@Override
159160
public void writeTo(StreamOutput out) throws IOException {
160161
if (out.getTransportVersion().onOrAfter(SNAPSHOT_INDEX_SHARD_STATUS_MISSING_STATS)) {
162+
// We use a boolean to indicate if the stats are present (true) or missing (false), to skip writing all the values if missing.
161163
if (isMissingStats()) {
162164
out.writeBoolean(false);
163165
return;
@@ -326,6 +328,11 @@ public static SnapshotStats fromXContent(XContentParser parser) throws IOExcepti
326328
}
327329
}
328330
}
331+
// For missing stats incrementalFileCount will be -1, and we expect processedFileCount and processedSize to be omitted (still zero).
332+
if (incrementalFileCount == -1) {
333+
assert processedFileCount == 0 && processedSize == 0L && incrementalSize == -1L && totalFileCount == -1 && totalSize == -1L;
334+
return SnapshotStats.forMissingStats();
335+
}
329336
return new SnapshotStats(
330337
startTime,
331338
time,

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void buildResponse(
244244
// expensive, we choose instead to provide a message to the caller explaining why the stats are missing and the API
245245
// that can be used to load them once the snapshot has completed.
246246
if (minTransportVersion.onOrAfter(TransportVersions.SNAPSHOT_INDEX_SHARD_STATUS_MISSING_STATS)) {
247-
shardStatus = SnapshotIndexShardStatus.forMissingStats(shardId, stage, """
247+
shardStatus = SnapshotIndexShardStatus.forDoneButMissingStats(shardId, """
248248
Snapshot shard stats missing from a currently running snapshot due to a node leaving the cluster after \
249249
completing the shard snapshot; use /_snapshot/<repository>/<snapshot>/_status to load from the repository \
250250
once the snapshot has completed.""");

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,8 @@ public static SnapshotIndexShardStatus fromXContent(XContentParser parser, Strin
123123
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser);
124124
return PARSER.parse(parser, indexId, parser.currentName());
125125
}
126+
127+
public void testForDoneButMissingStatsXContentSerialization() throws IOException {
128+
testFromXContent(() -> SnapshotIndexShardStatus.forDoneButMissingStats(createTestInstance().getShardId(), randomAlphaOfLength(16)));
129+
}
126130
}

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatsTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testMissingStats() throws IOException {
6767
assertEquals(-1L, missingStats.getIncrementalSize());
6868
assertEquals(-1L, missingStats.getProcessedSize());
6969

70-
// Verify round trip serialization.
70+
// Verify round trip Transport serialization.
7171
for (var transportVersion : List.of(
7272
TransportVersions.MINIMUM_COMPATIBLE,
7373
TransportVersions.SNAPSHOT_INDEX_SHARD_STATUS_MISSING_STATS,
@@ -95,5 +95,8 @@ public void testMissingStats() throws IOException {
9595
}
9696
}
9797
}
98+
99+
// Verify round trip XContent serialization.
100+
testFromXContent(SnapshotStats::forMissingStats);
98101
}
99102
}

test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,16 @@ public static <T extends ToXContent> void testFromXContent(
284284
* both for equality and asserts equality on the two queries.
285285
*/
286286
public final void testFromXContent() throws IOException {
287+
testFromXContent(this::createTestInstance);
288+
}
289+
290+
/**
291+
* Generic test that creates a new instance using the given supplier and verifies XContent round trip serialization.
292+
*/
293+
public final void testFromXContent(Supplier<T> testInstanceSupplier) throws IOException {
287294
testFromXContent(
288295
NUMBER_OF_TEST_RUNS,
289-
this::createTestInstance,
296+
testInstanceSupplier,
290297
supportsUnknownFields(),
291298
getShuffleFieldsExceptions(),
292299
getRandomFieldsExcludeFilter(),

0 commit comments

Comments
 (0)