YARN-1706. Created an utility method to dump timeline records to JSON strings. Contributed by Zhijie Shen.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1566982 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5c7b27bae0
commit
3587b6774c
@ -173,6 +173,9 @@ Release 2.4.0 - UNRELEASED
|
|||||||
on the configuration-provider mechanism during startup too. (Xuan Gong via
|
on the configuration-provider mechanism during startup too. (Xuan Gong via
|
||||||
vinodkv)
|
vinodkv)
|
||||||
|
|
||||||
|
YARN-1706. Created an utility method to dump timeline records to JSON
|
||||||
|
strings. (zjshen)
|
||||||
|
|
||||||
OPTIMIZATIONS
|
OPTIMIZATIONS
|
||||||
|
|
||||||
BUG FIXES
|
BUG FIXES
|
||||||
|
@ -0,0 +1,86 @@
|
|||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF 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 org.apache.hadoop.yarn.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.hadoop.classification.InterfaceAudience.Public;
|
||||||
|
import org.apache.hadoop.classification.InterfaceStability.Evolving;
|
||||||
|
import org.codehaus.jackson.JsonGenerationException;
|
||||||
|
import org.codehaus.jackson.map.AnnotationIntrospector;
|
||||||
|
import org.codehaus.jackson.map.JsonMappingException;
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
|
||||||
|
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The helper class for the timeline module.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Public
|
||||||
|
@Evolving
|
||||||
|
public class TimelineUtils {
|
||||||
|
|
||||||
|
private static ObjectMapper mapper;
|
||||||
|
|
||||||
|
static {
|
||||||
|
mapper = new ObjectMapper();
|
||||||
|
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
|
||||||
|
mapper.setAnnotationIntrospector(introspector);
|
||||||
|
mapper.getSerializationConfig()
|
||||||
|
.setSerializationInclusion(Inclusion.NON_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize a POJO object into a JSON string not in a pretty format
|
||||||
|
*
|
||||||
|
* @param o
|
||||||
|
* an object to serialize
|
||||||
|
* @return a JSON string
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JsonMappingException
|
||||||
|
* @throws JsonGenerationException
|
||||||
|
*/
|
||||||
|
public static String dumpTimelineRecordtoJSON(Object o)
|
||||||
|
throws JsonGenerationException, JsonMappingException, IOException {
|
||||||
|
return dumpTimelineRecordtoJSON(o, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize a POJO object into a JSON string
|
||||||
|
*
|
||||||
|
* @param o
|
||||||
|
* an object to serialize
|
||||||
|
* @param pretty
|
||||||
|
* whether in a pretty format or not
|
||||||
|
* @return a JSON string
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JsonMappingException
|
||||||
|
* @throws JsonGenerationException
|
||||||
|
*/
|
||||||
|
public static String dumpTimelineRecordtoJSON(Object o, boolean pretty)
|
||||||
|
throws JsonGenerationException, JsonMappingException, IOException {
|
||||||
|
if (pretty) {
|
||||||
|
return mapper.defaultPrettyPrintingWriter().writeValueAsString(o);
|
||||||
|
} else {
|
||||||
|
return mapper.writeValueAsString(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,18 +19,23 @@
|
|||||||
package org.apache.hadoop.yarn.api.records.apptimeline;
|
package org.apache.hadoop.yarn.api.records.apptimeline;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.hadoop.yarn.api.records.apptimeline.ATSPutErrors.ATSPutError;
|
import org.apache.hadoop.yarn.api.records.apptimeline.ATSPutErrors.ATSPutError;
|
||||||
|
import org.apache.hadoop.yarn.util.TimelineUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestApplicationTimelineRecords {
|
public class TestApplicationTimelineRecords {
|
||||||
|
|
||||||
|
private static final Log LOG =
|
||||||
|
LogFactory.getLog(TestApplicationTimelineRecords.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testATSEntities() {
|
public void testATSEntities() throws Exception {
|
||||||
ATSEntities entities = new ATSEntities();
|
ATSEntities entities = new ATSEntities();
|
||||||
for (int j = 0; j < 2; ++j) {
|
for (int j = 0; j < 2; ++j) {
|
||||||
ATSEntity entity = new ATSEntity();
|
ATSEntity entity = new ATSEntity();
|
||||||
@ -53,6 +58,9 @@ public class TestApplicationTimelineRecords {
|
|||||||
entity.addOtherInfo("okey2", "oval2");
|
entity.addOtherInfo("okey2", "oval2");
|
||||||
entities.addEntity(entity);
|
entities.addEntity(entity);
|
||||||
}
|
}
|
||||||
|
LOG.info("Entities in JSON:");
|
||||||
|
LOG.info(TimelineUtils.dumpTimelineRecordtoJSON(entities, true));
|
||||||
|
|
||||||
Assert.assertEquals(2, entities.getEntities().size());
|
Assert.assertEquals(2, entities.getEntities().size());
|
||||||
ATSEntity entity1 = entities.getEntities().get(0);
|
ATSEntity entity1 = entities.getEntities().get(0);
|
||||||
Assert.assertEquals("entity id 0", entity1.getEntityId());
|
Assert.assertEquals("entity id 0", entity1.getEntityId());
|
||||||
@ -71,7 +79,7 @@ public class TestApplicationTimelineRecords {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testATSEvents() {
|
public void testATSEvents() throws Exception {
|
||||||
ATSEvents events = new ATSEvents();
|
ATSEvents events = new ATSEvents();
|
||||||
for (int j = 0; j < 2; ++j) {
|
for (int j = 0; j < 2; ++j) {
|
||||||
ATSEvents.ATSEventsOfOneEntity partEvents =
|
ATSEvents.ATSEventsOfOneEntity partEvents =
|
||||||
@ -88,6 +96,9 @@ public class TestApplicationTimelineRecords {
|
|||||||
}
|
}
|
||||||
events.addEvent(partEvents);
|
events.addEvent(partEvents);
|
||||||
}
|
}
|
||||||
|
LOG.info("Events in JSON:");
|
||||||
|
LOG.info(TimelineUtils.dumpTimelineRecordtoJSON(events, true));
|
||||||
|
|
||||||
Assert.assertEquals(2, events.getAllEvents().size());
|
Assert.assertEquals(2, events.getAllEvents().size());
|
||||||
ATSEvents.ATSEventsOfOneEntity partEvents1 = events.getAllEvents().get(0);
|
ATSEvents.ATSEventsOfOneEntity partEvents1 = events.getAllEvents().get(0);
|
||||||
Assert.assertEquals("entity id 0", partEvents1.getEntityId());
|
Assert.assertEquals("entity id 0", partEvents1.getEntityId());
|
||||||
@ -112,7 +123,7 @@ public class TestApplicationTimelineRecords {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testATSPutErrors() {
|
public void testATSPutErrors() throws Exception {
|
||||||
ATSPutErrors atsPutErrors = new ATSPutErrors();
|
ATSPutErrors atsPutErrors = new ATSPutErrors();
|
||||||
ATSPutError error1 = new ATSPutError();
|
ATSPutError error1 = new ATSPutError();
|
||||||
error1.setEntityId("entity id 1");
|
error1.setEntityId("entity id 1");
|
||||||
@ -127,6 +138,8 @@ public class TestApplicationTimelineRecords {
|
|||||||
error2.setErrorCode(ATSPutError.IO_EXCEPTION);
|
error2.setErrorCode(ATSPutError.IO_EXCEPTION);
|
||||||
errors.add(error2);
|
errors.add(error2);
|
||||||
atsPutErrors.addErrors(errors);
|
atsPutErrors.addErrors(errors);
|
||||||
|
LOG.info("Errors in JSON:");
|
||||||
|
LOG.info(TimelineUtils.dumpTimelineRecordtoJSON(atsPutErrors, true));
|
||||||
|
|
||||||
Assert.assertEquals(3, atsPutErrors.getErrors().size());
|
Assert.assertEquals(3, atsPutErrors.getErrors().size());
|
||||||
ATSPutError e = atsPutErrors.getErrors().get(0);
|
ATSPutError e = atsPutErrors.getErrors().get(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user