Skip to content

Commit 8265f44

Browse files
authored
[Backport] Fix JSON serde for taskStatusPlus (#5469) (#5472)
* Fix JSON serde for taskStatusPlus (#5469) * Fix JSON serde for taskStatusPlus * add newline * fix to statusCode * fix fobidden api check * remove debugging code * Compile fix
1 parent c9e5f1b commit 8265f44

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

api/src/main/java/io/druid/indexer/TaskStatusPlus.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.joda.time.DateTime;
2626

2727
import javax.annotation.Nullable;
28+
import java.util.Objects;
2829

2930
public class TaskStatusPlus
3031
{
@@ -40,7 +41,7 @@ public TaskStatusPlus(
4041
@JsonProperty("id") String id,
4142
@JsonProperty("createdTime") DateTime createdTime,
4243
@JsonProperty("queueInsertionTime") DateTime queueInsertionTime,
43-
@JsonProperty("state") @Nullable TaskState state,
44+
@JsonProperty("statusCode") @Nullable TaskState state,
4445
@JsonProperty("duration") @Nullable Long duration,
4546
@JsonProperty("location") TaskLocation location
4647
)
@@ -74,7 +75,8 @@ public DateTime getQueueInsertionTime()
7475
return queueInsertionTime;
7576
}
7677

77-
@JsonProperty
78+
@Nullable
79+
@JsonProperty("statusCode")
7880
public TaskState getState()
7981
{
8082
return state;
@@ -91,4 +93,40 @@ public TaskLocation getLocation()
9193
{
9294
return location;
9395
}
96+
97+
@Override
98+
public boolean equals(Object o)
99+
{
100+
if (this == o) {
101+
return true;
102+
}
103+
104+
if (o == null || getClass() != o.getClass()) {
105+
return false;
106+
}
107+
108+
final TaskStatusPlus that = (TaskStatusPlus) o;
109+
if (!id.equals(that.id)) {
110+
return false;
111+
}
112+
if (!createdTime.equals(that.createdTime)) {
113+
return false;
114+
}
115+
if (!queueInsertionTime.equals(that.queueInsertionTime)) {
116+
return false;
117+
}
118+
if (!Objects.equals(state, that.state)) {
119+
return false;
120+
}
121+
if (!Objects.equals(duration, that.duration)) {
122+
return false;
123+
}
124+
return location.equals(that.location);
125+
}
126+
127+
@Override
128+
public int hashCode()
129+
{
130+
return Objects.hash(id, createdTime, queueInsertionTime, state, duration, location);
131+
}
94132
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Metamarkets licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package io.druid.indexer;
21+
22+
import com.fasterxml.jackson.core.JsonParser;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.core.JsonToken;
25+
import com.fasterxml.jackson.databind.DeserializationContext;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
28+
import com.fasterxml.jackson.databind.module.SimpleModule;
29+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
30+
import io.druid.java.util.common.DateTimes;
31+
import org.joda.time.DateTime;
32+
import org.junit.Assert;
33+
import org.junit.Test;
34+
35+
import java.io.IOException;
36+
37+
public class TaskStatusPlusTest
38+
{
39+
@Test
40+
public void testSerde() throws IOException
41+
{
42+
final ObjectMapper mapper = new ObjectMapper();
43+
mapper.registerModule(
44+
new SimpleModule()
45+
.addDeserializer(DateTime.class, new DateTimeDeserializer())
46+
.addSerializer(DateTime.class, ToStringSerializer.instance)
47+
);
48+
final TaskStatusPlus status = new TaskStatusPlus(
49+
"testId",
50+
DateTimes.nowUtc(),
51+
DateTimes.nowUtc(),
52+
TaskState.RUNNING,
53+
1000L,
54+
TaskLocation.create("testHost", 1010, -1)
55+
);
56+
final String json = mapper.writeValueAsString(status);
57+
Assert.assertEquals(status, mapper.readValue(json, TaskStatusPlus.class));
58+
}
59+
60+
// Copied from io.druid.jackson.JodaStuff
61+
private static class DateTimeDeserializer extends StdDeserializer<DateTime>
62+
{
63+
public DateTimeDeserializer()
64+
{
65+
super(DateTime.class);
66+
}
67+
68+
@Override
69+
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt)
70+
throws IOException, JsonProcessingException
71+
{
72+
JsonToken t = jp.getCurrentToken();
73+
if (t == JsonToken.VALUE_NUMBER_INT) {
74+
return DateTimes.utc(jp.getLongValue());
75+
}
76+
if (t == JsonToken.VALUE_STRING) {
77+
String str = jp.getText().trim();
78+
if (str.length() == 0) { // [JACKSON-360]
79+
return null;
80+
}
81+
// make sure to preserve time zone information when parsing timestamps
82+
return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str);
83+
}
84+
throw ctxt.mappingException(getValueClass());
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)