diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineEntity.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineEntity.java index 6cab753..3be7f52 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineEntity.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineEntity.java @@ -80,7 +80,7 @@ public String toString() { private TimelineEntity real; private Identifier identifier; private HashMap info = new HashMap<>(); - private HashMap configs = new HashMap<>(); + private HashMap configs = new HashMap<>(); private Set metrics = new HashSet<>(); private Set events = new HashSet<>(); private HashMap> isRelatedToEntities = new HashMap<>(); @@ -213,7 +213,7 @@ public void addInfo(String key, Object value) { // required by JAXB @InterfaceAudience.Private @XmlElement(name = "configs") - public HashMap getConfigsJAXB() { + public HashMap getConfigsJAXB() { if (real == null) { return configs; } else { @@ -221,7 +221,7 @@ public void addInfo(String key, Object value) { } } - public Map getConfigs() { + public Map getConfigs() { if (real == null) { return configs; } else { @@ -229,19 +229,19 @@ public void addInfo(String key, Object value) { } } - public void setConfigs(Map configs) { + public void setConfigs(Map configs) { if (real == null) { if (configs != null && !(configs instanceof HashMap)) { - this.configs = new HashMap(configs); + this.configs = new HashMap(configs); } else { - this.configs = (HashMap) configs; + this.configs = (HashMap) configs; } } else { real.setConfigs(configs); } } - public void addConfigs(Map configs) { + public void addConfigs(Map configs) { if (real == null) { this.configs.putAll(configs); } else { @@ -249,7 +249,7 @@ public void addConfigs(Map configs) { } } - public void addConfig(String key, Object value) { + public void addConfig(String key, String value) { if (real == null) { configs.put(key, value); } else { diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineMetric.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineMetric.java index 57babbf..08a8e35 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineMetric.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/timelineservice/TimelineMetric.java @@ -19,6 +19,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -34,11 +35,8 @@ @InterfaceStability.Unstable public class TimelineMetric { private String id; - private HashMap info = new HashMap<>(); - private Object singleData; - private HashMap timeSeries = new LinkedHashMap<>(); - private long startTime; - private long endTime; + private Number singleData; + private HashMap timeSeries = new LinkedHashMap<>(); public TimelineMetric() { @@ -53,84 +51,60 @@ public void setId(String id) { this.id = id; } - // required by JAXB - @InterfaceAudience.Private - @XmlElement(name = "info") - public HashMap getInfoJAXB() { - return info; - } - - public Map getInfo() { - return info; - } - - public void setInfo(Map info) { - if (info != null && !(info instanceof HashMap)) { - this.info = new HashMap(info); - } else { - this.info = (HashMap) info; - } - } - - public void addInfo(Map info) { - this.info.putAll(info); - } - - public void addInfo(String key, Object value) { - info.put(key, value); - } - @XmlElement(name = "data") - public Object getSingleData() { + public Number getSingleData() { return singleData; } - public void setSingleData(Object singleData) { + public void setSingleData(Number singleData) { + checkIfTheOtherExists(null, timeSeries); this.singleData = singleData; } // required by JAXB @InterfaceAudience.Private @XmlElement(name = "timeseries") - public HashMap getTimeSeriesJAXB() { + public HashMap getTimeSeriesJAXB() { return timeSeries; } - public Map getTimeSeries() { + public Map getTimeSeries() { return timeSeries; } - public void setTimeSeries(Map timeSeries) { + public void setTimeSeries(Map timeSeries) { + checkIfTheOtherExists(singleData, null); if (timeSeries != null && !(timeSeries instanceof LinkedHashMap)) { - this.timeSeries = new LinkedHashMap(timeSeries); + this.timeSeries = new LinkedHashMap(timeSeries); } else { - this.timeSeries = (LinkedHashMap) timeSeries; + this.timeSeries = (LinkedHashMap) timeSeries; } } - public void addTimeSeries(Map timeSeries) { + public void addTimeSeries(Map timeSeries) { + checkIfTheOtherExists(singleData, null); this.timeSeries.putAll(timeSeries); } - public void addTimeSeriesData(long timestamp, Object value) { + public void addTimeSeriesData(long timestamp, Number value) { + checkIfTheOtherExists(singleData, null); timeSeries.put(timestamp, value); } - @XmlElement(name = "starttime") - public long getStartTime() { - return startTime; - } - - public void setStartTime(long startTime) { - this.startTime = startTime; - } - - @XmlElement(name = "endtime") - public long getEndTime() { - return endTime; + private static void checkIfTheOtherExists( + Number singleData, HashMap timeSeries) { + if (singleData != null || timeSeries != null && timeSeries.size() > 0) { + throw new YarnRuntimeException( + "Single data point and time series can't be both set"); + } } - public void setEndTime(long endTime) { - this.endTime = endTime; + /** + * + * @return true if the time series exist or false if the other cases, + * including the single data metric or the empty metric + */ + public boolean isTimeSeries() { + return timeSeries != null && timeSeries.size() > 0; } } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/timelineservice/TestTimelineServiceRecords.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/timelineservice/TestTimelineServiceRecords.java index caa3f3f..6cb0e5a 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/timelineservice/TestTimelineServiceRecords.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/timelineservice/TestTimelineServiceRecords.java @@ -28,6 +28,7 @@ import org.junit.Test; import org.junit.Assert; +import java.util.Arrays; import java.util.Collections; @@ -41,38 +42,66 @@ public void testTimelineEntities() throws Exception { entity.setType("test type 1"); entity.setId("test id 1"); entity.addInfo("test info key 1", "test info value 1"); - entity.addInfo("test info key 2", "test info value 2"); + entity.addInfo("test info key 2", + Arrays.asList("test info value 2", "test info value 3")); + entity.addInfo("test info key 3", true); + Assert.assertTrue( + entity.getInfo().get("test info key 3") instanceof Boolean); entity.addConfig("test config key 1", "test config value 1"); entity.addConfig("test config key 2", "test config value 2"); + TimelineMetric metric1 = new TimelineMetric(); metric1.setId("test metric id 1"); - metric1.addInfo("test info key 1", "test info value 1"); - metric1.addInfo("test info key 2", "test info value 2"); - metric1.addTimeSeriesData(1L, "test time series 1"); - metric1.addTimeSeriesData(2L, "test time series 2"); - metric1.setStartTime(0L); - metric1.setEndTime(1L); + metric1.addTimeSeriesData(1L, 1.0F); + metric1.addTimeSeriesData(2L, 2.0D); + Assert.assertTrue(metric1.getTimeSeries().get(1L) instanceof Float); + Assert.assertTrue(metric1.getTimeSeries().get(2L) instanceof Double); + Assert.assertTrue(metric1.isTimeSeries()); + try { + metric1.setSingleData(3L); + Assert.fail(); + } catch (YarnRuntimeException e) { + Assert.assertTrue(e.getMessage().contains( + "Single data point and time series can't be both set")); + } entity.addMetric(metric1); + TimelineMetric metric2 = new TimelineMetric(); metric2.setId("test metric id 1"); - metric2.addInfo("test info key 1", "test info value 1"); - metric2.addInfo("test info key 2", "test info value 2"); - metric2.setSingleData("test info value 3"); - metric1.setStartTime(0L); - metric1.setEndTime(1L); + metric2.setSingleData((short) 4); + Assert.assertTrue(metric2.getSingleData() instanceof Short); + Assert.assertFalse(metric2.isTimeSeries()); + try { + metric2.addTimeSeriesData(3L, 5); + Assert.fail(); + } catch (YarnRuntimeException e) { + Assert.assertTrue(e.getMessage().contains( + "Single data point and time series can't be both set")); + } entity.addMetric(metric2); + TimelineEvent event1 = new TimelineEvent(); event1.setId("test event id 1"); event1.addInfo("test info key 1", "test info value 1"); - event1.addInfo("test info key 2", "test info value 2"); + event1.addInfo("test info key 2", + Arrays.asList("test info value 2", "test info value 3")); + event1.addInfo("test info key 3", true); + Assert.assertTrue( + event1.getInfo().get("test info key 3") instanceof Boolean); event1.setTimestamp(0L); entity.addEvent(event1); + TimelineEvent event2 = new TimelineEvent(); event2.setId("test event id 2"); event2.addInfo("test info key 1", "test info value 1"); - event2.addInfo("test info key 2", "test info value 2"); + event2.addInfo("test info key 2", + Arrays.asList("test info value 2", "test info value 3")); + event2.addInfo("test info key 3", true); + Assert.assertTrue( + event2.getInfo().get("test info key 3") instanceof Boolean); event2.setTimestamp(1L); entity.addEvent(event2); + entity.setCreatedTime(0L); entity.setModifiedTime(1L); entity.addRelatesToEntity("test type 2", "test id 2");