Skip to content

Commit 40cef22

Browse files
authored
Fix getJobStatus to correctly serialize the state changes (#388)
* Correctly serialize and deserialize the statusChanges field * Use escaped JSON instead when returning from getJobStatus
1 parent 6b965aa commit 40cef22

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

management-api-agent-common/src/main/java/com/datastax/mgmtapi/NodeOpsProvider.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import com.datastax.oss.driver.api.querybuilder.schema.CreateTableWithOptions;
2121
import com.datastax.oss.driver.api.querybuilder.schema.OngoingPartitionKey;
2222
import com.datastax.oss.driver.internal.core.metadata.schema.parsing.DataTypeCqlNameParser;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
2325
import com.google.common.base.Preconditions;
2426
import com.google.common.base.Supplier;
2527
import com.google.common.base.Suppliers;
28+
import com.google.common.collect.Maps;
2629
import java.io.IOException;
2730
import java.net.InetAddress;
2831
import java.net.UnknownHostException;
@@ -94,6 +97,23 @@ public Map<String, String> getJobStatus(@RpcParam(name = "job_id") String jobId)
9497
resultMap.put("error", jobWithId.getError().getLocalizedMessage());
9598
}
9699

100+
List<Map<String, String>> statusChanges = new ArrayList<>();
101+
for (Job.StatusChange statusChange : jobWithId.getStatusChanges()) {
102+
Map<String, String> change = Maps.newHashMap();
103+
change.put("status", statusChange.getStatus().name());
104+
change.put("change_time", Long.valueOf(statusChange.getChangeTime()).toString());
105+
change.put("message", statusChange.getMessage());
106+
statusChanges.add(change);
107+
}
108+
109+
ObjectMapper objectMapper = new ObjectMapper();
110+
try {
111+
String s = objectMapper.writeValueAsString(statusChanges);
112+
resultMap.put("status_changes", s);
113+
} catch (JsonProcessingException e) {
114+
throw new RuntimeException(e);
115+
}
116+
97117
return resultMap;
98118
}
99119

management-api-server/src/main/java/com/datastax/mgmtapi/resources/models/Job.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
import com.fasterxml.jackson.annotation.JsonCreator;
99
import com.fasterxml.jackson.annotation.JsonProperty;
10+
import com.fasterxml.jackson.core.type.TypeReference;
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import java.io.IOException;
1014
import java.io.Serializable;
15+
import java.util.ArrayList;
1116
import java.util.List;
1217

1318
public class Job implements Serializable {
@@ -35,7 +40,7 @@ public enum JobStatus {
3540
@JsonProperty(value = "error")
3641
private String error;
3742

38-
public class StatusChange {
43+
public static class StatusChange {
3944
@JsonProperty(value = "status")
4045
String status;
4146

@@ -45,8 +50,12 @@ public class StatusChange {
4550
@JsonProperty(value = "message")
4651
String message;
4752

48-
public StatusChange(String type, String message) {
49-
changeTime = System.currentTimeMillis();
53+
@JsonCreator
54+
public StatusChange(
55+
@JsonProperty(value = "status") String type,
56+
@JsonProperty(value = "change_time") String time,
57+
@JsonProperty(value = "message") String message) {
58+
changeTime = Long.parseLong(time);
5059
status = type;
5160
this.message = message;
5261
}
@@ -75,14 +84,32 @@ public Job(
7584
@JsonProperty(value = "submit_time") long submitTime,
7685
@JsonProperty(value = "end_time") long finishedTime,
7786
@JsonProperty(value = "error") String error,
78-
@JsonProperty(value = "status_changes") List<StatusChange> changes) {
87+
@JsonProperty(value = "status_changes") String changes) {
7988
this.jobId = jobId;
8089
this.jobType = jobType;
8190
this.status = JobStatus.valueOf(status);
8291
this.submitTime = submitTime;
8392
this.finishedTime = finishedTime;
8493
this.error = error;
85-
this.statusChanges = changes;
94+
this.statusChanges = changes(changes);
95+
}
96+
97+
public List<StatusChange> changes(String s) {
98+
ObjectMapper objectMapper = new ObjectMapper();
99+
if (s.length() < 2) {
100+
return new ArrayList<>();
101+
}
102+
try {
103+
JsonNode parent = objectMapper.readTree(s);
104+
105+
List<StatusChange> changes =
106+
objectMapper.readValue(parent.traverse(), new TypeReference<List<StatusChange>>() {});
107+
108+
return changes;
109+
110+
} catch (IOException e) {
111+
throw new RuntimeException(e);
112+
}
86113
}
87114

88115
public String getJobId() {

management-api-server/src/test/java/com/datastax/mgmtapi/K8OperatorResourcesTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
import com.datastax.oss.driver.api.core.cql.ResultSet;
4141
import com.datastax.oss.driver.api.core.cql.Row;
4242
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
43+
import com.fasterxml.jackson.core.JsonProcessingException;
4344
import com.fasterxml.jackson.core.type.TypeReference;
4445
import com.fasterxml.jackson.databind.ObjectMapper;
4546
import com.fasterxml.jackson.databind.json.JsonMapper;
4647
import com.google.common.collect.ImmutableList;
4748
import com.google.common.collect.ImmutableMap;
4849
import com.google.common.collect.Lists;
50+
import com.google.common.collect.Maps;
4951
import java.io.IOException;
5052
import java.net.URISyntaxException;
5153
import java.util.ArrayList;
@@ -585,13 +587,28 @@ public void testJobStatus() throws Exception {
585587

586588
when(mockResultSet.one()).thenReturn(mockRow);
587589

588-
Map<String, String> jobDetailsRow = new HashMap<>();
590+
Map<String, Object> jobDetailsRow = new HashMap<>();
589591
jobDetailsRow.put("id", "0fe65b47-98c2-47d8-9c3c-5810c9988e10");
590592
jobDetailsRow.put("type", "CLEANUP");
591593
jobDetailsRow.put("status", "COMPLETED");
592594
jobDetailsRow.put("submit_time", String.valueOf(System.currentTimeMillis()));
593595
jobDetailsRow.put("end_time", String.valueOf(System.currentTimeMillis()));
594596

597+
List<Map<String, String>> statusChanges = new ArrayList<>();
598+
Map<String, String> change = Maps.newHashMap();
599+
change.put("status", "SUCCESS");
600+
change.put("change_time", "1695183696663");
601+
change.put("message", "No message");
602+
statusChanges.add(change);
603+
604+
ObjectMapper objectMapper = new ObjectMapper();
605+
try {
606+
String s = objectMapper.writeValueAsString(statusChanges);
607+
jobDetailsRow.put("status_changes", s);
608+
} catch (JsonProcessingException e) {
609+
throw new RuntimeException(e);
610+
}
611+
595612
when(mockRow.getObject(0)).thenReturn(jobDetailsRow);
596613

597614
MockHttpResponse response =
@@ -609,6 +626,8 @@ public void testJobStatus() throws Exception {
609626
assertEquals("0fe65b47-98c2-47d8-9c3c-5810c9988e10", jobDetails.getJobId());
610627
assertEquals("COMPLETED", jobDetails.getStatus().toString());
611628
assertEquals("CLEANUP", jobDetails.getJobType());
629+
assertEquals(1, jobDetails.getStatusChanges().size());
630+
assertEquals("SUCCESS", jobDetails.getStatusChanges().get(0).getStatus());
612631
}
613632

614633
@Test

0 commit comments

Comments
 (0)