Skip to content

Commit a68f781

Browse files
authored
[7.x] Fix human readable xcontent for snapshots in progress and deletion (#70256) (#70430)
Previously this returned start_time as a TimeValue, but start_time is a timestamp. This switches from humanReadableField to timeField during the XContent building.
1 parent a6fd2fa commit a68f781

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.common.io.stream.StreamInput;
1616
import org.elasticsearch.common.io.stream.StreamOutput;
1717
import org.elasticsearch.common.io.stream.Writeable;
18-
import org.elasticsearch.common.unit.TimeValue;
1918
import org.elasticsearch.common.util.CollectionUtils;
2019
import org.elasticsearch.common.xcontent.XContentBuilder;
2120
import org.elasticsearch.repositories.RepositoryOperation;
@@ -169,7 +168,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
169168
builder.value(snapshot.getName());
170169
}
171170
builder.endArray();
172-
builder.humanReadableField("start_time_millis", "start_time", new TimeValue(entry.startTime));
171+
builder.timeField("start_time_millis", "start_time", entry.startTime);
173172
builder.field("repository_state_id", entry.repositoryStateId);
174173
}
175174
builder.endObject();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.elasticsearch.common.io.stream.StreamInput;
2121
import org.elasticsearch.common.io.stream.StreamOutput;
2222
import org.elasticsearch.common.io.stream.Writeable;
23-
import org.elasticsearch.common.unit.TimeValue;
2423
import org.elasticsearch.common.xcontent.ToXContent;
2524
import org.elasticsearch.common.xcontent.XContentBuilder;
2625
import org.elasticsearch.index.shard.ShardId;
@@ -492,7 +491,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
492491
}
493492
}
494493
builder.endArray();
495-
builder.humanReadableField(START_TIME_MILLIS, START_TIME, new TimeValue(startTime));
494+
builder.timeField(START_TIME_MILLIS, START_TIME, startTime);
496495
builder.field(REPOSITORY_STATE_ID, repositoryStateId);
497496
builder.startArray(SHARDS);
498497
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.cluster;
10+
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.common.xcontent.ToXContent;
13+
import org.elasticsearch.common.xcontent.XContentBuilder;
14+
import org.elasticsearch.test.ESTestCase;
15+
16+
import java.io.IOException;
17+
import java.util.Collections;
18+
19+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
20+
import static org.hamcrest.Matchers.equalTo;
21+
22+
public class SnapshotDeletionsInProgressTests extends ESTestCase {
23+
public void testXContent() throws IOException {
24+
SnapshotDeletionsInProgress sdip =
25+
SnapshotDeletionsInProgress.newInstance(new SnapshotDeletionsInProgress.Entry(Collections.emptyList(),
26+
"repo", 736694267638L, 0, SnapshotDeletionsInProgress.State.STARTED));
27+
28+
try (XContentBuilder builder = jsonBuilder()) {
29+
builder.humanReadable(true);
30+
builder.startObject();
31+
sdip.toXContent(builder, ToXContent.EMPTY_PARAMS);
32+
builder.endObject();
33+
String json = Strings.toString(builder);
34+
assertThat(json,
35+
equalTo("{\"snapshot_deletions\":[{\"repository\":\"repo\",\"snapshots\":[]," +
36+
"\"start_time\":\"1993-05-06T13:17:47.638Z\",\"start_time_millis\":736694267638,\"repository_state_id\":0}]}"));
37+
}
38+
}
39+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,40 @@
88

99
package org.elasticsearch.snapshots;
1010

11+
import org.elasticsearch.Version;
1112
import org.elasticsearch.cluster.ClusterModule;
1213
import org.elasticsearch.cluster.ClusterState.Custom;
1314
import org.elasticsearch.cluster.Diff;
1415
import org.elasticsearch.cluster.SnapshotsInProgress;
1516
import org.elasticsearch.cluster.SnapshotsInProgress.Entry;
1617
import org.elasticsearch.cluster.SnapshotsInProgress.ShardState;
1718
import org.elasticsearch.cluster.SnapshotsInProgress.State;
19+
import org.elasticsearch.common.Strings;
1820
import org.elasticsearch.common.collect.ImmutableOpenMap;
1921
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2022
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.common.xcontent.ToXContent;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
2125
import org.elasticsearch.index.Index;
2226
import org.elasticsearch.index.shard.ShardId;
2327
import org.elasticsearch.repositories.IndexId;
2428
import org.elasticsearch.test.AbstractDiffableWireSerializationTestCase;
2529
import org.elasticsearch.test.ESTestCase;
2630
import org.elasticsearch.test.VersionUtils;
2731

32+
import java.io.IOException;
2833
import java.util.ArrayList;
2934
import java.util.Arrays;
35+
import java.util.Collections;
3036
import java.util.HashMap;
3137
import java.util.List;
3238
import java.util.Map;
3339
import java.util.stream.Collectors;
3440
import java.util.stream.Stream;
3541

42+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
43+
import static org.hamcrest.Matchers.equalTo;
44+
3645
public class SnapshotsInProgressSerializationTests extends AbstractDiffableWireSerializationTestCase<Custom> {
3746

3847
@Override
@@ -222,6 +231,32 @@ private Entry mutateEntry(Entry entry) {
222231
}
223232
}
224233

234+
public void testXContent() throws IOException {
235+
SnapshotsInProgress sip =
236+
SnapshotsInProgress.of(Collections.singletonList(new Entry(
237+
new Snapshot("repo", new SnapshotId("name", "uuid")), true, true, State.SUCCESS,
238+
Collections.singletonList(new IndexId("index", "uuid")), Collections.emptyList(), Collections.emptyList(), 1234567, 0,
239+
ImmutableOpenMap.<ShardId, SnapshotsInProgress.ShardSnapshotStatus>builder()
240+
.fPut(new ShardId("index", "uuid", 0),
241+
new SnapshotsInProgress.ShardSnapshotStatus("nodeId", ShardState.SUCCESS, "reason", "generation"))
242+
.build(), null, null, Version.CURRENT)));
243+
244+
try (XContentBuilder builder = jsonBuilder()) {
245+
builder.humanReadable(true);
246+
builder.startObject();
247+
sip.toXContent(builder, ToXContent.EMPTY_PARAMS);
248+
builder.endObject();
249+
String json = Strings.toString(builder);
250+
assertThat(json,
251+
equalTo("{\"snapshots\":[{\"repository\":\"repo\",\"snapshot\":\"name\",\"uuid\":\"uuid\"," +
252+
"\"include_global_state\":true,\"partial\":true,\"state\":\"SUCCESS\"," +
253+
"\"indices\":[{\"name\":\"index\",\"id\":\"uuid\"}],\"start_time\":\"1970-01-01T00:20:34.567Z\"," +
254+
"\"start_time_millis\":1234567,\"repository_state_id\":0," +
255+
"\"shards\":[{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"}," +
256+
"\"shard\":0,\"state\":\"SUCCESS\",\"node\":\"nodeId\"}],\"feature_states\":[],\"data_streams\":[]}]}"));
257+
}
258+
}
259+
225260
public static State randomState(ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards) {
226261
return SnapshotsInProgress.completed(shards.values())
227262
? randomFrom(State.SUCCESS, State.FAILED) : randomFrom(State.STARTED, State.INIT, State.ABORTED);

0 commit comments

Comments
 (0)