YARN-3276. Code cleanup for timeline service API records. Contributed by Junping Du.

(cherry picked from commit d88f30ba5359f59fb71b93a55e1c1d9a1c0dff8e)
This commit is contained in:
Zhijie Shen 2015-06-03 15:13:29 -07:00 committed by Sangjin Lee
parent f668212529
commit 42d7864b84
8 changed files with 144 additions and 54 deletions

View File

@ -34,6 +34,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Evolving;
import org.apache.hadoop.yarn.util.TimelineServiceHelper;
/** /**
* <p> * <p>
@ -231,11 +232,8 @@ public class TimelineEntity implements Comparable<TimelineEntity> {
*/ */
public void setRelatedEntities( public void setRelatedEntities(
Map<String, Set<String>> relatedEntities) { Map<String, Set<String>> relatedEntities) {
if (relatedEntities != null && !(relatedEntities instanceof HashMap)) { this.relatedEntities = TimelineServiceHelper.mapCastToHashMap(
this.relatedEntities = new HashMap<String, Set<String>>(relatedEntities); relatedEntities);
} else {
this.relatedEntities = (HashMap<String, Set<String>>) relatedEntities;
}
} }
/** /**
@ -297,11 +295,8 @@ public class TimelineEntity implements Comparable<TimelineEntity> {
* a map of primary filters * a map of primary filters
*/ */
public void setPrimaryFilters(Map<String, Set<Object>> primaryFilters) { public void setPrimaryFilters(Map<String, Set<Object>> primaryFilters) {
if (primaryFilters != null && !(primaryFilters instanceof HashMap)) { this.primaryFilters =
this.primaryFilters = new HashMap<String, Set<Object>>(primaryFilters); TimelineServiceHelper.mapCastToHashMap(primaryFilters);
} else {
this.primaryFilters = (HashMap<String, Set<Object>>) primaryFilters;
}
} }
/** /**
@ -350,11 +345,7 @@ public class TimelineEntity implements Comparable<TimelineEntity> {
* a map of other information * a map of other information
*/ */
public void setOtherInfo(Map<String, Object> otherInfo) { public void setOtherInfo(Map<String, Object> otherInfo) {
if (otherInfo != null && !(otherInfo instanceof HashMap)) { this.otherInfo = TimelineServiceHelper.mapCastToHashMap(otherInfo);
this.otherInfo = new HashMap<String, Object>(otherInfo);
} else {
this.otherInfo = (HashMap<String, Object>) otherInfo;
}
} }
/** /**

View File

@ -29,6 +29,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Evolving;
import org.apache.hadoop.yarn.util.TimelineServiceHelper;
/** /**
* The class that contains the information of an event that is related to some * The class that contains the information of an event that is related to some
@ -135,11 +136,8 @@ public class TimelineEvent implements Comparable<TimelineEvent> {
* a map of of the information of the event * a map of of the information of the event
*/ */
public void setEventInfo(Map<String, Object> eventInfo) { public void setEventInfo(Map<String, Object> eventInfo) {
if (eventInfo != null && !(eventInfo instanceof HashMap)) { this.eventInfo = TimelineServiceHelper.mapCastToHashMap(
this.eventInfo = new HashMap<String, Object>(eventInfo); eventInfo);
} else {
this.eventInfo = (HashMap<String, Object>) eventInfo;
}
} }
@Override @Override

View File

@ -19,6 +19,7 @@ package org.apache.hadoop.yarn.api.records.timelineservice;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.yarn.util.TimelineServiceHelper;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
@ -184,11 +185,7 @@ public class TimelineEntity {
public void setInfo(Map<String, Object> info) { public void setInfo(Map<String, Object> info) {
if (real == null) { if (real == null) {
if (info != null && !(info instanceof HashMap)) { this.info = TimelineServiceHelper.mapCastToHashMap(info);
this.info = new HashMap<String, Object>(info);
} else {
this.info = (HashMap<String, Object>) info;
}
} else { } else {
real.setInfo(info); real.setInfo(info);
} }
@ -231,11 +228,7 @@ public class TimelineEntity {
public void setConfigs(Map<String, String> configs) { public void setConfigs(Map<String, String> configs) {
if (real == null) { if (real == null) {
if (configs != null && !(configs instanceof HashMap)) { this.configs = TimelineServiceHelper.mapCastToHashMap(configs);
this.configs = new HashMap<String, String>(configs);
} else {
this.configs = (HashMap<String, String>) configs;
}
} else { } else {
real.setConfigs(configs); real.setConfigs(configs);
} }
@ -345,14 +338,8 @@ public class TimelineEntity {
public void setIsRelatedToEntities( public void setIsRelatedToEntities(
Map<String, Set<String>> isRelatedToEntities) { Map<String, Set<String>> isRelatedToEntities) {
if (real == null) { if (real == null) {
if (isRelatedToEntities != null && this.isRelatedToEntities =
!(isRelatedToEntities instanceof HashMap)) { TimelineServiceHelper.mapCastToHashMap(isRelatedToEntities);
this.isRelatedToEntities =
new HashMap<String, Set<String>>(isRelatedToEntities);
} else {
this.isRelatedToEntities =
(HashMap<String, Set<String>>) isRelatedToEntities;
}
} else { } else {
real.setIsRelatedToEntities(isRelatedToEntities); real.setIsRelatedToEntities(isRelatedToEntities);
} }
@ -438,14 +425,8 @@ public class TimelineEntity {
public void setRelatesToEntities(Map<String, Set<String>> relatesToEntities) { public void setRelatesToEntities(Map<String, Set<String>> relatesToEntities) {
if (real == null) { if (real == null) {
if (relatesToEntities != null && this.relatesToEntities =
!(relatesToEntities instanceof HashMap)) { TimelineServiceHelper.mapCastToHashMap(relatesToEntities);
this.relatesToEntities =
new HashMap<String, Set<String>>(relatesToEntities);
} else {
this.relatesToEntities =
(HashMap<String, Set<String>>) relatesToEntities;
}
} else { } else {
real.setRelatesToEntities(relatesToEntities); real.setRelatesToEntities(relatesToEntities);
} }

View File

@ -19,6 +19,7 @@ package org.apache.hadoop.yarn.api.records.timelineservice;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.yarn.util.TimelineServiceHelper;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
@ -61,11 +62,7 @@ public class TimelineEvent {
} }
public void setInfo(Map<String, Object> info) { public void setInfo(Map<String, Object> info) {
if (info != null && !(info instanceof HashMap)) { this.info = TimelineServiceHelper.mapCastToHashMap(info);
this.info = new HashMap<String, Object>(info);
} else {
this.info = (HashMap<String, Object>) info;
}
} }
public void addInfo(Map<String, Object> info) { public void addInfo(Map<String, Object> info) {

View File

@ -44,7 +44,7 @@ public class TimelineMetric {
private Comparator<Long> reverseComparator = new Comparator<Long>() { private Comparator<Long> reverseComparator = new Comparator<Long>() {
@Override @Override
public int compare(Long l1, Long l2) { public int compare(Long l1, Long l2) {
return -l1.compareTo(l2); return l2.compareTo(l1);
} }
}; };
private TreeMap<Long, Number> values = new TreeMap<>(reverseComparator); private TreeMap<Long, Number> values = new TreeMap<>(reverseComparator);

View File

@ -0,0 +1,47 @@
/*
* 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.util.HashMap;
import java.util.Map;
import org.apache.hadoop.classification.InterfaceAudience.LimitedPrivate;
/**
* Helper class for Timeline service.
*/
@LimitedPrivate({ "MapReduce", "YARN" })
public final class TimelineServiceHelper {
private TimelineServiceHelper() {
// Utility classes should not have a public or default constructor.
}
/**
* Cast map to HashMap for generic type.
* @param originalMap the map need to be casted
* @return casted HashMap object
*/
public static <E, V> HashMap<E, V> mapCastToHashMap(
Map<E, V> originalMap) {
return originalMap == null ? null : originalMap instanceof HashMap ?
(HashMap<E, V>) originalMap : new HashMap<E, V>(originalMap);
}
}

View File

@ -387,13 +387,13 @@ public class AllocateResponsePBImpl extends AllocateResponse {
@Override @Override
public String getCollectorAddr() { public synchronized String getCollectorAddr() {
AllocateResponseProtoOrBuilder p = viaProto ? proto : builder; AllocateResponseProtoOrBuilder p = viaProto ? proto : builder;
return p.getCollectorAddr(); return p.getCollectorAddr();
} }
@Override @Override
public void setCollectorAddr(String collectorAddr) { public synchronized void setCollectorAddr(String collectorAddr) {
maybeInitBuilder(); maybeInitBuilder();
if (collectorAddr == null) { if (collectorAddr == null) {
builder.clearCollectorAddr(); builder.clearCollectorAddr();

View File

@ -0,0 +1,76 @@
/**
* 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.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.junit.Assert;
import org.junit.Test;
public class TestTimelineServiceHelper {
@Test
public void testMapCastToHashMap() {
// Test null map be casted to null
Map<String, String> nullMap = null;
Assert.assertNull(TimelineServiceHelper.mapCastToHashMap(nullMap));
// Test empty hashmap be casted to a empty hashmap
Map<String, String> emptyHashMap = new HashMap<String, String>();
Assert.assertEquals(TimelineServiceHelper.mapCastToHashMap(emptyHashMap).size(), 0);
// Test empty non-hashmap be casted to a empty hashmap
Map<String, String> emptyTreeMap = new TreeMap<String, String>();
Assert.assertEquals(TimelineServiceHelper.mapCastToHashMap(emptyTreeMap).size(), 0);
// Test non-empty hashmap be casted to hashmap correctly
Map<String, String> firstHashMap = new HashMap<String, String>();
String key = "KEY";
String value = "VALUE";
firstHashMap.put(key, value);
Assert.assertEquals(TimelineServiceHelper.mapCastToHashMap(firstHashMap), firstHashMap);
// Test non-empty non-hashmap is casted correctly.
Map<String, String> firstTreeMap = new TreeMap<String, String>();
firstTreeMap.put(key, value);
HashMap<String, String> alternateHashMap =
TimelineServiceHelper.mapCastToHashMap(firstTreeMap);
Assert.assertEquals(firstTreeMap.size(), alternateHashMap.size());
Assert.assertEquals(alternateHashMap.get(key), value);
// Test complicated hashmap be casted correctly
Map<String, Set<String>> complicatedHashMap = new HashMap<String, Set<String>>();
Set<String> hashSet = new HashSet<String>();
hashSet.add(value);
complicatedHashMap.put(key, hashSet);
Assert.assertEquals(TimelineServiceHelper.mapCastToHashMap(complicatedHashMap),
complicatedHashMap);
// Test complicated non-hashmap get casted correctly
Map<String, Set<String>> complicatedTreeMap = new TreeMap<String, Set<String>>();
complicatedTreeMap.put(key, hashSet);
Assert.assertEquals(TimelineServiceHelper.mapCastToHashMap(complicatedTreeMap).get(key),
hashSet);
}
}