Skip to content

Commit b1fc83c

Browse files
Fix fromXContent() bug, add @nullable, update description string
1 parent b2f4bf3 commit b1fc83c

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
16+
import org.elasticsearch.core.Nullable;
1617
import org.elasticsearch.index.shard.ShardId;
1718
import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;
1819
import org.elasticsearch.xcontent.ToXContentFragment;
@@ -90,7 +91,7 @@ public SnapshotIndexShardStatus(StreamInput in) throws IOException {
9091
SnapshotStats stats,
9192
String nodeId,
9293
String failure,
93-
String description
94+
@Nullable String description
9495
) {
9596
super(shardId);
9697
this.stage = stage;
@@ -146,6 +147,7 @@ public String getFailure() {
146147
/**
147148
* Returns the optional description of the data values contained in the {@code stats} field.
148149
*/
150+
@Nullable
149151
public String getDescription() {
150152
return description;
151153
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ public static SnapshotStats fromXContent(XContentParser parser) throws IOExcepti
250250
long time = 0;
251251
int incrementalFileCount = 0;
252252
int totalFileCount = 0;
253-
int processedFileCount = 0;
253+
int processedFileCount = Integer.MIN_VALUE;
254254
long incrementalSize = 0;
255255
long totalSize = 0;
256-
long processedSize = 0;
256+
long processedSize = Long.MIN_VALUE;
257257
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
258258
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
259259
String currentName = parser.currentName();
@@ -328,10 +328,11 @@ public static SnapshotStats fromXContent(XContentParser parser) throws IOExcepti
328328
}
329329
}
330330
}
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();
331+
// Handle the case where the "processed" sub-object is omitted in toXContent() when processedFileCount == incrementalFileCount.
332+
if (processedFileCount == Integer.MIN_VALUE) {
333+
assert processedSize == Long.MIN_VALUE;
334+
processedFileCount = incrementalFileCount;
335+
processedSize = incrementalSize;
335336
}
336337
return new SnapshotStats(
337338
startTime,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ void buildResponse(
246246
if (minTransportVersion.onOrAfter(TransportVersions.SNAPSHOT_INDEX_SHARD_STATUS_MISSING_STATS)) {
247247
shardStatus = SnapshotIndexShardStatus.forDoneButMissingStats(shardId, """
248248
Snapshot shard stats missing from a currently running snapshot due to a node leaving the cluster after \
249-
completing the shard snapshot; use /_snapshot/<repository>/<snapshot>/_status to load from the repository \
250-
once the snapshot has completed.""");
249+
completing the shard snapshot; retry once the snapshot has completed to load all shard stats from the \
250+
repository.""");
251251
} else {
252252
// BWC behavior, load the stats directly from the repository.
253253
shardStatus = new SnapshotIndexShardStatus(

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
@@ -55,6 +55,26 @@ protected boolean supportsUnknownFields() {
5555
return true;
5656
}
5757

58+
public void testXContentSerializationWhenProcessedFileCountEqualsIncrementalFileCount() throws IOException {
59+
final var instance = createTestInstance();
60+
final var incrementalSameAsProcessed = new SnapshotStats(
61+
instance.getStartTime(),
62+
instance.getTime(),
63+
instance.getIncrementalFileCount(),
64+
instance.getTotalFileCount(),
65+
instance.getIncrementalFileCount(), // processedFileCount
66+
instance.getIncrementalSize(),
67+
instance.getTotalSize(),
68+
instance.getIncrementalSize() // processedSize
69+
);
70+
// toXContent() omits the "processed" sub-object in this case, make sure the processed values are set as expected in fromXContent().
71+
testFromXContent(() -> incrementalSameAsProcessed);
72+
}
73+
74+
public void testXContentSerializationForEmptyStats() throws IOException {
75+
testFromXContent(SnapshotStats::new);
76+
}
77+
5878
public void testMissingStats() throws IOException {
5979
final var populatedStats = createTestInstance();
6080
final var missingStats = SnapshotStats.forMissingStats();

0 commit comments

Comments
 (0)