Fix JSON serde for taskStatusPlus (#5469)

* Fix JSON serde for taskStatusPlus

* add newline

* fix to statusCode

* fix fobidden api check

* remove debugging code
This commit is contained in:
Jihoon Son 2018-03-05 18:49:59 -08:00 committed by Jonathan Wei
parent 8a51800693
commit c9b12e7813
2 changed files with 130 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import com.google.common.base.Preconditions;
import org.joda.time.DateTime;
import javax.annotation.Nullable;
import java.util.Objects;
public class TaskStatusPlus
{
@ -42,7 +43,7 @@ public class TaskStatusPlus
@JsonProperty("type") @Nullable String type, // nullable for backward compatibility
@JsonProperty("createdTime") DateTime createdTime,
@JsonProperty("queueInsertionTime") DateTime queueInsertionTime,
@JsonProperty("state") @Nullable TaskState state,
@JsonProperty("statusCode") @Nullable TaskState state,
@JsonProperty("duration") @Nullable Long duration,
@JsonProperty("location") TaskLocation location
)
@ -85,7 +86,7 @@ public class TaskStatusPlus
}
@Nullable
@JsonProperty
@JsonProperty("statusCode")
public TaskState getState()
{
return state;
@ -103,4 +104,43 @@ public class TaskStatusPlus
{
return location;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final TaskStatusPlus that = (TaskStatusPlus) o;
if (!id.equals(that.id)) {
return false;
}
if (!type.equals(that.type)) {
return false;
}
if (!createdTime.equals(that.createdTime)) {
return false;
}
if (!queueInsertionTime.equals(that.queueInsertionTime)) {
return false;
}
if (!Objects.equals(state, that.state)) {
return false;
}
if (!Objects.equals(duration, that.duration)) {
return false;
}
return location.equals(that.location);
}
@Override
public int hashCode()
{
return Objects.hash(id, type, createdTime, queueInsertionTime, state, duration, location);
}
}

View File

@ -0,0 +1,88 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.indexer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.druid.java.util.common.DateTimes;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
public class TaskStatusPlusTest
{
@Test
public void testSerde() throws IOException
{
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(
new SimpleModule()
.addDeserializer(DateTime.class, new DateTimeDeserializer())
.addSerializer(DateTime.class, ToStringSerializer.instance)
);
final TaskStatusPlus status = new TaskStatusPlus(
"testId",
"testType",
DateTimes.nowUtc(),
DateTimes.nowUtc(),
TaskState.RUNNING,
1000L,
TaskLocation.create("testHost", 1010, -1)
);
final String json = mapper.writeValueAsString(status);
Assert.assertEquals(status, mapper.readValue(json, TaskStatusPlus.class));
}
// Copied from io.druid.jackson.JodaStuff
private static class DateTimeDeserializer extends StdDeserializer<DateTime>
{
public DateTimeDeserializer()
{
super(DateTime.class);
}
@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT) {
return DateTimes.utc(jp.getLongValue());
}
if (t == JsonToken.VALUE_STRING) {
String str = jp.getText().trim();
if (str.length() == 0) { // [JACKSON-360]
return null;
}
// make sure to preserve time zone information when parsing timestamps
return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str);
}
throw ctxt.mappingException(getValueClass());
}
}
}