瀏覽代碼

AMBARI-9652 AMS : Hosts metrics - "mem_shared" metrics array contains "null" (dsen)

Dmytro Sen 10 年之前
父節點
當前提交
58597876ef

+ 7 - 1
ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/TimelineMetric.java

@@ -19,6 +19,10 @@ package org.apache.hadoop.metrics2.sink.timeline;
 
 
 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.metrics2.sink.timeline.deserialize
+  .IgnoringNullsLinkedHashMap;
+import org.codehaus.jackson.map.annotate.JsonDeserialize;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElement;
@@ -39,7 +43,9 @@ public class TimelineMetric implements Comparable<TimelineMetric> {
   private long timestamp;
   private long timestamp;
   private long startTime;
   private long startTime;
   private String type;
   private String type;
-  private Map<Long, Double> metricValues = new TreeMap<Long, Double>();
+
+  @JsonDeserialize(as = IgnoringNullsLinkedHashMap.class)
+  private Map<Long, Double> metricValues = new IgnoringNullsLinkedHashMap<Long, Double>();
 
 
   @XmlElement(name = "metricname")
   @XmlElement(name = "metricname")
   public String getMetricName() {
   public String getMetricName() {

+ 34 - 0
ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/deserialize/IgnoringNullsLinkedHashMap.java

@@ -0,0 +1,34 @@
+/**
+ * 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.metrics2.sink.timeline.deserialize;
+
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+
+/**
+ * This Map does not store neither null values. When null value is
+ * inserted it is ignored.
+ */
+public class IgnoringNullsLinkedHashMap<K, V> extends TreeMap<K, V> {
+  @Override
+  public V put(K key, V value) {
+    if (value == null) return null;
+
+    return super.put(key, value);
+  }
+}

+ 39 - 0
ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/deserialize/IgnoringNullsLinkedHashMapTest.java

@@ -0,0 +1,39 @@
+/**
+ * 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.metrics2.sink.timeline.deserialize;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class IgnoringNullsLinkedHashMapTest {
+
+  @Test
+  public void testPut() throws Exception {
+    IgnoringNullsLinkedHashMap<Integer, String> map
+      = new IgnoringNullsLinkedHashMap<Integer, String>();
+
+    map.put(1, null);
+    map.put(2, "two");
+    map.put(3, null);
+    map.put(4, "four");
+    map.put(5, "five");
+
+    assertEquals(3,map.size());
+  }
+}

+ 2 - 3
ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/AHSWebApp.java

@@ -25,7 +25,6 @@ import org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.
 import org.apache.hadoop.yarn.server.applicationhistoryservice.timeline.TimelineStore;
 import org.apache.hadoop.yarn.server.applicationhistoryservice.timeline.TimelineStore;
 import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
 import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
 import org.apache.hadoop.yarn.webapp.WebApp;
 import org.apache.hadoop.yarn.webapp.WebApp;
-import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider;
 import org.apache.hadoop.yarn.webapp.YarnWebParams;
 import org.apache.hadoop.yarn.webapp.YarnWebParams;
 
 
 public class AHSWebApp extends WebApp implements YarnWebParams {
 public class AHSWebApp extends WebApp implements YarnWebParams {
@@ -43,7 +42,7 @@ public class AHSWebApp extends WebApp implements YarnWebParams {
 
 
   @Override
   @Override
   public void setup() {
   public void setup() {
-    bind(YarnJacksonJaxbJsonProvider.class);
+    bind(YarnAMSJacksonJaxbJsonProvider.class);
     bind(AHSWebServices.class);
     bind(AHSWebServices.class);
     bind(TimelineWebServices.class);
     bind(TimelineWebServices.class);
     bind(GenericExceptionHandler.class);
     bind(GenericExceptionHandler.class);
@@ -60,4 +59,4 @@ public class AHSWebApp extends WebApp implements YarnWebParams {
       pajoin("/logs", NM_NODENAME, CONTAINER_ID, ENTITY_STRING, APP_OWNER,
       pajoin("/logs", NM_NODENAME, CONTAINER_ID, ENTITY_STRING, APP_OWNER,
         CONTAINER_LOG_TYPE), AHSController.class, "logs");
         CONTAINER_LOG_TYPE), AHSController.class, "logs");
   }
   }
-}
+}

+ 57 - 0
ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/YarnAMSJacksonJaxbJsonProvider.java

@@ -0,0 +1,57 @@
+/**
+ * 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.server.applicationhistoryservice.webapp;
+
+import com.google.inject.Singleton;
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
+import org.codehaus.jackson.map.AnnotationIntrospector;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
+import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.Provider;
+
+/**
+ *
+ */
+@Singleton
+@Provider
+@InterfaceStability.Unstable
+@InterfaceAudience.Private
+public class YarnAMSJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {
+
+  public YarnAMSJacksonJaxbJsonProvider() {
+    super();
+  }
+
+  @Override
+  public ObjectMapper locateMapper(Class<?> type, MediaType mediaType) {
+    ObjectMapper mapper = super.locateMapper(type, mediaType);
+    configObjectMapper(mapper);
+    return mapper;
+  }
+
+  public static void configObjectMapper(ObjectMapper mapper) {
+    mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
+  }
+
+}

+ 6 - 4
ambari-server/src/main/java/org/apache/hadoop/metrics2/sink/timeline/TimelineMetric.java

@@ -17,15 +17,15 @@
  */
  */
 package org.apache.hadoop.metrics2.sink.timeline;
 package org.apache.hadoop.metrics2.sink.timeline;
 
 
+import org.apache.hadoop.metrics2.sink.timeline.deserialize
+  .IgnoringNullsLinkedHashMap;
+import org.codehaus.jackson.map.annotate.JsonDeserialize;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlRootElement;
-import java.text.DecimalFormat;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Map;
-import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeMap;
 
 
 @XmlRootElement(name = "metric")
 @XmlRootElement(name = "metric")
@@ -39,6 +39,8 @@ public class TimelineMetric implements Comparable<TimelineMetric> {
   private long timestamp;
   private long timestamp;
   private long startTime;
   private long startTime;
   private String type;
   private String type;
+
+  @JsonDeserialize(as = IgnoringNullsLinkedHashMap.class)
   private Map<Long, Double> metricValues = new TreeMap<Long, Double>();
   private Map<Long, Double> metricValues = new TreeMap<Long, Double>();
 
 
   @XmlElement(name = "metricname")
   @XmlElement(name = "metricname")

+ 35 - 0
ambari-server/src/main/java/org/apache/hadoop/metrics2/sink/timeline/deserialize/IgnoringNullsLinkedHashMap.java

@@ -0,0 +1,35 @@
+ /**
+ * 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.metrics2.sink.timeline.deserialize;
+
+
+ import java.util.LinkedHashMap;
+
+ /**
+ * This Map does not store neither null values. When null value is
+ * inserted it is ignored.
+ */
+public class IgnoringNullsLinkedHashMap<K, V> extends LinkedHashMap<K, V> {
+  @Override
+  public V put(K key, V value) {
+    if (value == null) return null;
+
+    return super.put(key, value);
+  }
+}