Skip to content

Commit 37468f7

Browse files
Fix SnapshotStats toXContent()/fromXContent() round trip serialization
toXContent() has a check to omit the "processed" sub-object if the processedFileCount equals the incrementalFileCount. In the scenario where they are equal and greater than zero, if you were to read the JSON back in with fromXContent(), the SnapshotStats object would not be the same as the original since processedFileCount is not parsed and set. This change adds a fix and a unit test for this scenario.
1 parent 2730de1 commit 37468f7

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ public static SnapshotStats fromXContent(XContentParser parser) throws IOExcepti
204204
long time = 0;
205205
int incrementalFileCount = 0;
206206
int totalFileCount = 0;
207-
int processedFileCount = 0;
207+
int processedFileCount = Integer.MIN_VALUE;
208208
long incrementalSize = 0;
209209
long totalSize = 0;
210-
long processedSize = 0;
210+
long processedSize = Long.MIN_VALUE;
211211
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
212212
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
213213
String currentName = parser.currentName();
@@ -282,6 +282,12 @@ public static SnapshotStats fromXContent(XContentParser parser) throws IOExcepti
282282
}
283283
}
284284
}
285+
// Handle the case where the "processed" sub-object is omitted in toXContent() when processedFileCount == incrementalFileCount.
286+
if (processedFileCount == Integer.MIN_VALUE) {
287+
assert processedSize == Long.MIN_VALUE;
288+
processedFileCount = incrementalFileCount;
289+
processedSize = incrementalSize;
290+
}
285291
return new SnapshotStats(
286292
startTime,
287293
time,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ protected SnapshotStats createTestInstance() {
3939
);
4040
}
4141

42+
public void testXContentSerializationWhenProcessedFileCountEqualsIncrementalFileCount() throws IOException {
43+
final var instance = createTestInstance();
44+
final var incrementalSameAsProcessed = new SnapshotStats(
45+
instance.getStartTime(),
46+
instance.getTime(),
47+
instance.getIncrementalFileCount(),
48+
instance.getTotalFileCount(),
49+
instance.getIncrementalFileCount(), // processedFileCount
50+
instance.getIncrementalSize(),
51+
instance.getTotalSize(),
52+
instance.getIncrementalSize() // processedSize
53+
);
54+
// toXContent() omits the "processed" sub-object in this case, make sure the processed values are set as expected in fromXContent().
55+
testFromXContent(() -> incrementalSameAsProcessed);
56+
}
57+
58+
public void testXContentSerializationForEmptyStats() throws IOException {
59+
testFromXContent(SnapshotStats::new);
60+
}
61+
4262
@Override
4363
protected SnapshotStats doParseInstance(XContentParser parser) throws IOException {
4464
return SnapshotStats.fromXContent(parser);

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)