You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ds...@apache.org on 2015/01/23 21:43:47 UTC

[1/2] ambari git commit: AMBARI-9304 AMS: Optimize LikeCondition (dsen)

Repository: ambari
Updated Branches:
  refs/heads/trunk 8d785ce3d -> 9bb3bc007


AMBARI-9304 AMS: Optimize LikeCondition (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/9bb3bc00
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/9bb3bc00
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/9bb3bc00

Branch: refs/heads/trunk
Commit: 9bb3bc007c927bca9d85af645eebd027f2c633c8
Parents: f7efd2b
Author: Dmytro Sen <ds...@apache.org>
Authored: Fri Jan 23 22:12:55 2015 +0200
Committer: Dmytro Sen <ds...@apache.org>
Committed: Fri Jan 23 22:43:10 2015 +0200

----------------------------------------------------------------------
 .../timeline/HBaseTimelineMetricStore.java      |   6 +-
 .../metrics/timeline/PhoenixTransactSQL.java    | 101 +++++++------------
 .../timeline/TestPhoenixTransactSQL.java        |  35 ++++---
 3 files changed, 58 insertions(+), 84 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/9bb3bc00/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/HBaseTimelineMetricStore.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/HBaseTimelineMetricStore.java b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/HBaseTimelineMetricStore.java
index 83fc0bd..0fa0834 100644
--- a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/HBaseTimelineMetricStore.java
+++ b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/HBaseTimelineMetricStore.java
@@ -32,7 +32,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.Condition;
-import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.LikeCondition;
+import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.DefaultCondition;
 
 public class HBaseTimelineMetricStore extends AbstractService
     implements TimelineMetricStore {
@@ -107,7 +107,7 @@ public class HBaseTimelineMetricStore extends AbstractService
       Long startTime, Long endTime, Precision precision, Integer limit,
       boolean groupedByHosts) throws SQLException, IOException {
 
-    Condition condition = new LikeCondition(metricNames, hostname, applicationId,
+    Condition condition = new DefaultCondition(metricNames, hostname, applicationId,
       instanceId, startTime, endTime, precision, limit, groupedByHosts);
 
     if (hostname == null) {
@@ -124,7 +124,7 @@ public class HBaseTimelineMetricStore extends AbstractService
       throws SQLException, IOException {
 
     TimelineMetrics metrics = hBaseAccessor.getMetricRecords(
-      new LikeCondition(Collections.singletonList(metricName), hostname,
+      new DefaultCondition(Collections.singletonList(metricName), hostname,
         applicationId, instanceId, startTime, endTime, precision, limit, true)
     );
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/9bb3bc00/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixTransactSQL.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixTransactSQL.java b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixTransactSQL.java
index a6ad040..e30f710 100644
--- a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixTransactSQL.java
+++ b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/PhoenixTransactSQL.java
@@ -561,7 +561,7 @@ public class PhoenixTransactSQL {
     void setPrecision(Precision precision);
     String getAppId();
     String getInstanceId();
-    String getConditionClause();
+    StringBuilder getConditionClause();
     String getOrderByClause();
     String getStatement();
     Long getStartTime();
@@ -614,34 +614,47 @@ public class PhoenixTransactSQL {
       return metricNames == null || metricNames.isEmpty() ? null : metricNames;
     }
 
-    String getMetricsClause() {
-      StringBuilder sb = new StringBuilder("(");
-      if (metricNames != null) {
+    public StringBuilder getConditionClause() {
+      StringBuilder sb = new StringBuilder();
+      boolean appendConjunction = false;
+      StringBuilder metricsLike = new StringBuilder();
+      StringBuilder metricsIn = new StringBuilder();
+
+      if (getMetricNames() != null) {
         for (String name : getMetricNames()) {
-          if (sb.length() != 1) {
-            sb.append(", ");
+          if (name.contains("%")) {
+            if (metricsLike.length() > 1) {
+              metricsLike.append(" OR ");
+            }
+            metricsLike.append("METRIC_NAME LIKE ?");
+          } else {
+            if (metricsIn.length() > 0) {
+              metricsIn.append(", ");
+            }
+            metricsIn.append("?");
           }
-          sb.append("?");
         }
-        sb.append(")");
-        return sb.toString();
-      } else {
-        return null;
-      }
-    }
 
-    public String getConditionClause() {
-      StringBuilder sb = new StringBuilder();
-      boolean appendConjunction = false;
+        if (metricsIn.length()>0) {
+          sb.append("(METRIC_NAME IN (");
+          sb.append(metricsIn);
+          sb.append(")");
+          appendConjunction = true;
+        }
 
-      if (getMetricNames() != null) {
-        if (appendConjunction) {
-          sb.append(" AND");
+        if (metricsLike.length() > 0) {
+          if (appendConjunction) {
+            sb.append(" OR ");
+          } else {
+            sb.append("(");
+          }
+          sb.append(metricsLike);
+          appendConjunction = true;
         }
 
-        sb.append("METRIC_NAME IN ");
-        sb.append(getMetricsClause());
-        appendConjunction = true;
+        if (appendConjunction) {
+          sb.append(")");
+        }
       }
 
       appendConjunction = append(sb, appendConjunction, getHostname(), " HOSTNAME = ?");
@@ -650,7 +663,7 @@ public class PhoenixTransactSQL {
       appendConjunction = append(sb, appendConjunction, getStartTime(), " SERVER_TIME >= ?");
       append(sb, appendConjunction, getEndTime(), " SERVER_TIME < ?");
 
-      return sb.toString();
+      return sb;
     }
 
     protected static boolean append(StringBuilder sb,
@@ -791,44 +804,6 @@ public class PhoenixTransactSQL {
     }
   }
 
-  static class LikeCondition extends DefaultCondition {
-
-    LikeCondition(List<String> metricNames, String hostname,
-                  String appId, String instanceId, Long startTime,
-                  Long endTime, Precision precision, Integer limit,
-                  boolean grouped) {
-      super(metricNames, hostname, appId, instanceId, startTime, endTime,
-        precision, limit, grouped);
-    }
-
-    @Override
-    public String getConditionClause() {
-      StringBuilder sb = new StringBuilder();
-      boolean appendConjunction = false;
-
-      if (getMetricNames() != null) {
-        sb.append("(");
-        for (String name : getMetricNames()) {
-          if (sb.length() > 1) {
-            sb.append(" OR ");
-          }
-          sb.append("METRIC_NAME LIKE ?");
-        }
-
-        sb.append(")");
-        appendConjunction = true;
-      }
-
-      appendConjunction = append(sb, appendConjunction, getHostname(), " HOSTNAME = ?");
-      appendConjunction = append(sb, appendConjunction, getAppId(), " APP_ID = ?");
-      appendConjunction = append(sb, appendConjunction, getInstanceId(), " INSTANCE_ID = ?");
-      appendConjunction = append(sb, appendConjunction, getStartTime(), " SERVER_TIME >= ?");
-      append(sb, appendConjunction, getEndTime(), " SERVER_TIME < ?");
-
-      return sb.toString();
-    }
-  }
-
   static class SplitByMetricNamesCondition implements Condition {
     private final Condition adaptee;
     private String currentMetric;
@@ -888,7 +863,7 @@ public class PhoenixTransactSQL {
     }
 
     @Override
-    public String getConditionClause() {
+    public StringBuilder getConditionClause() {
       StringBuilder sb = new StringBuilder();
       boolean appendConjunction = false;
 
@@ -914,7 +889,7 @@ public class PhoenixTransactSQL {
       DefaultCondition.append(sb, appendConjunction, getEndTime(),
         " SERVER_TIME < ?");
 
-      return sb.toString();
+      return sb;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/9bb3bc00/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java b/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java
index af472c7..038f3de 100644
--- a/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java
+++ b/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java
@@ -29,7 +29,6 @@ import java.util.Collections;
 
 import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.Condition;
 import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.DefaultCondition;
-import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.LikeCondition;
 import static org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixTransactSQL.SplitByMetricNamesCondition;
 
 import static org.easymock.EasyMock.createNiceMock;
@@ -46,8 +45,8 @@ public class TestPhoenixTransactSQL {
       Arrays.asList("cpu_user", "mem_free"), "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    String preparedClause = condition.getConditionClause();
-    String expectedClause = "METRIC_NAME IN (?, ?) AND HOSTNAME = ? AND " +
+    String preparedClause = condition.getConditionClause().toString();
+    String expectedClause = "(METRIC_NAME IN (?, ?)) AND HOSTNAME = ? AND " +
       "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
     Assert.assertNotNull(preparedClause);
@@ -63,7 +62,7 @@ public class TestPhoenixTransactSQL {
     SplitByMetricNamesCondition condition = new SplitByMetricNamesCondition(c);
     condition.setCurrentMetric(c.getMetricNames().get(0));
 
-    String preparedClause = condition.getConditionClause();
+    String preparedClause = condition.getConditionClause().toString();
     String expectedClause = "METRIC_NAME = ? AND HOSTNAME = ? AND " +
       "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
@@ -73,23 +72,23 @@ public class TestPhoenixTransactSQL {
 
   @Test
   public void testLikeConditionClause() throws Exception {
-    Condition condition = new LikeCondition(
-        Arrays.asList("cpu_user", "mem_free"), "h1", "a1", "i1",
+    Condition condition = new DefaultCondition(
+        Arrays.asList("cpu_user", "some=%.metric"), "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    String preparedClause = condition.getConditionClause();
-    String expectedClause = "(METRIC_NAME LIKE ? OR METRIC_NAME LIKE ?) AND HOSTNAME = ? AND " +
+    String preparedClause = condition.getConditionClause().toString();
+    String expectedClause = "(METRIC_NAME IN (?) OR METRIC_NAME LIKE ?) AND HOSTNAME = ? AND " +
         "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
     Assert.assertNotNull(preparedClause);
     Assert.assertEquals(expectedClause, preparedClause);
 
 
-    condition = new LikeCondition(
+    condition = new DefaultCondition(
         Collections.<String>emptyList(), "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    preparedClause = condition.getConditionClause();
+    preparedClause = condition.getConditionClause().toString();
     expectedClause = " HOSTNAME = ? AND " +
         "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
@@ -97,11 +96,11 @@ public class TestPhoenixTransactSQL {
     Assert.assertEquals(expectedClause, preparedClause);
 
 
-    condition = new LikeCondition(
+    condition = new DefaultCondition(
         null, "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    preparedClause = condition.getConditionClause();
+    preparedClause = condition.getConditionClause().toString();
     expectedClause = " HOSTNAME = ? AND " +
         "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
@@ -109,11 +108,11 @@ public class TestPhoenixTransactSQL {
     Assert.assertEquals(expectedClause, preparedClause);
 
 
-    condition = new LikeCondition(
-        Arrays.asList("cpu_user"), "h1", "a1", "i1",
+    condition = new DefaultCondition(
+        Arrays.asList("some=%.metric"), "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    preparedClause = condition.getConditionClause();
+    preparedClause = condition.getConditionClause().toString();
     expectedClause = "(METRIC_NAME LIKE ?) AND HOSTNAME = ? AND " +
         "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 
@@ -121,11 +120,11 @@ public class TestPhoenixTransactSQL {
     Assert.assertEquals(expectedClause, preparedClause);
 
 
-    condition = new LikeCondition(
-        Arrays.asList("cpu_user", "mem_free", "cpu_aidle"), "h1", "a1", "i1",
+    condition = new DefaultCondition(
+        Arrays.asList("some=%.metric1", "some=%.metric2", "some=%.metric3"), "h1", "a1", "i1",
         1407959718L, 1407959918L, null, null, false);
 
-    preparedClause = condition.getConditionClause();
+    preparedClause = condition.getConditionClause().toString();
     expectedClause = "(METRIC_NAME LIKE ? OR METRIC_NAME LIKE ? OR METRIC_NAME LIKE ?) AND HOSTNAME = ? AND " +
         "APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?";
 


[2/2] ambari git commit: AMBARI-9305 AMS: certain metric charts look incorrect (dsen)

Posted by ds...@apache.org.
AMBARI-9305 AMS: certain metric charts look incorrect (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f7efd2b3
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f7efd2b3
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f7efd2b3

Branch: refs/heads/trunk
Commit: f7efd2b3af03a10ae04c96d8af91da2926e72d54
Parents: 8d785ce
Author: Dmytro Sen <ds...@apache.org>
Authored: Fri Jan 23 22:12:15 2015 +0200
Committer: Dmytro Sen <ds...@apache.org>
Committed: Fri Jan 23 22:43:10 2015 +0200

----------------------------------------------------------------------
 ambari-metrics/ambari-metrics-common/pom.xml    |  6 ++
 .../timeline/cache/TimelineMetricsCache.java    | 26 ++++++
 .../cache/TimelineMetricsCacheTest.java         | 88 ++++++++++++++++++++
 .../timeline/HadoopTimelineMetricsSink.java     |  2 +-
 4 files changed, 121 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/f7efd2b3/ambari-metrics/ambari-metrics-common/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-common/pom.xml b/ambari-metrics/ambari-metrics-common/pom.xml
index 7bfd9f1..ea8ecb3 100644
--- a/ambari-metrics/ambari-metrics-common/pom.xml
+++ b/ambari-metrics/ambari-metrics-common/pom.xml
@@ -50,5 +50,11 @@
       <artifactId>jackson-mapper-asl</artifactId>
       <version>1.8.0</version>
     </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+      <version>4.10</version>
+    </dependency>
   </dependencies>
 </project>

http://git-wip-us.apache.org/repos/asf/ambari/blob/f7efd2b3/ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCache.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCache.java b/ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCache.java
index 5e89e14..0f2c9a3 100644
--- a/ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCache.java
+++ b/ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCache.java
@@ -17,15 +17,18 @@
  */
 package org.apache.hadoop.metrics2.sink.timeline.cache;
 
+import com.google.common.base.Optional;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.metrics2.MetricType;
 import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric;
 
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.TreeMap;
 
 @InterfaceAudience.Public
 @InterfaceStability.Evolving
@@ -37,6 +40,7 @@ public class TimelineMetricsCache {
   public static final int MAX_EVICTION_TIME_MILLIS = 59000; // ~ 1 min
   private final int maxRecsPerName;
   private final int maxEvictionTimeInMillis;
+  private final Map<String, Double> counterMetricLastValue = new HashMap<String, Double>();
 
   public TimelineMetricsCache(int maxRecsPerName, int maxEvictionTimeInMillis) {
     this.maxRecsPerName = maxRecsPerName;
@@ -151,4 +155,26 @@ public class TimelineMetricsCache {
   public void putTimelineMetric(TimelineMetric timelineMetric) {
     timelineMetricCache.put(timelineMetric.getMetricName(), timelineMetric);
   }
+
+  private void transformMetricValuesToDerivative(TimelineMetric timelineMetric) {
+    String metricName = timelineMetric.getMetricName();
+    double firstValue = timelineMetric.getMetricValues().size() > 0
+        ? timelineMetric.getMetricValues().entrySet().iterator().next().getValue() : 0;
+    double previousValue = Optional.fromNullable(counterMetricLastValue.get(metricName)).or(firstValue);
+    Map<Long, Double> metricValues = timelineMetric.getMetricValues();
+    Map<Long, Double>   newMetricValues = new TreeMap<Long, Double>();
+    for (Map.Entry<Long, Double> entry : metricValues.entrySet()) {
+      newMetricValues.put(entry.getKey(), entry.getValue() - previousValue);
+      previousValue = entry.getValue();
+    }
+    timelineMetric.setMetricValues(newMetricValues);
+    counterMetricLastValue.put(metricName, previousValue);
+  }
+
+  public void putTimelineMetric(TimelineMetric timelineMetric, MetricType type) {
+    if (type == MetricType.COUNTER) {
+      transformMetricValuesToDerivative(timelineMetric);
+    }
+    putTimelineMetric(timelineMetric);
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f7efd2b3/ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCacheTest.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCacheTest.java b/ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCacheTest.java
new file mode 100644
index 0000000..5c48b90
--- /dev/null
+++ b/ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/cache/TimelineMetricsCacheTest.java
@@ -0,0 +1,88 @@
+/**
+ * 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.cache;
+
+import static com.sun.tools.javac.util.Assert.assertEquals;
+
+import org.apache.hadoop.metrics2.MetricType;
+import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.TreeMap;
+
+public class TimelineMetricsCacheTest {
+
+  private static final long DEFAULT_START_TIME = 1411023766;
+  private static final String METRIC_NAME = "Test name";
+  private static final double delta = 0.00001;
+
+  private final TimelineMetricsCache timelineMetricsCache = new TimelineMetricsCache(
+      TimelineMetricsCache.MAX_RECS_PER_NAME_DEFAULT,
+      TimelineMetricsCache.MAX_EVICTION_TIME_MILLIS);
+
+  @Test
+  public void testPutGetCounterTimelineMetric() throws Exception {
+    TimelineMetric metric = createTimelineMetric(new TreeMap<Long, Double>() {{
+      put(1L, 10.0);
+    }}, DEFAULT_START_TIME);
+    timelineMetricsCache.putTimelineMetric(metric, MetricType.COUNTER);
+    metric = createTimelineMetric(new TreeMap<Long, Double>() {{
+      put(2L, 10.0);
+      put(3L, 20.0);
+      put(4L, 30.0);
+    }}, DEFAULT_START_TIME + 2 * TimelineMetricsCache.MAX_EVICTION_TIME_MILLIS);
+    timelineMetricsCache.putTimelineMetric(metric, MetricType.COUNTER);
+    TimelineMetric cachedMetric
+        = timelineMetricsCache.getTimelineMetric(METRIC_NAME);
+    assertEquals(0, cachedMetric.getMetricValues().get(1L), delta);
+    assertEquals(0, cachedMetric.getMetricValues().get(2L), delta);
+    assertEquals(10, cachedMetric.getMetricValues().get(3L), delta);
+    assertEquals(10, cachedMetric.getMetricValues().get(4L), delta);
+
+    metric = createTimelineMetric(new TreeMap<Long, Double>() {{
+      put(5L, 100.0);
+      put(6L, 120.0);
+      put(7L, 230.0);
+    }}, DEFAULT_START_TIME + 3 * TimelineMetricsCache.MAX_EVICTION_TIME_MILLIS);
+    timelineMetricsCache.putTimelineMetric(metric, MetricType.COUNTER);
+    metric = createTimelineMetric(new TreeMap<Long, Double>() {{
+      put(8L, 300.0);
+    }}, DEFAULT_START_TIME + 5 * TimelineMetricsCache.MAX_EVICTION_TIME_MILLIS);
+
+    timelineMetricsCache.putTimelineMetric(metric, MetricType.COUNTER);
+    cachedMetric = timelineMetricsCache.getTimelineMetric(METRIC_NAME);
+    assertEquals(70, cachedMetric.getMetricValues().get(5L), delta);
+    assertEquals(20, cachedMetric.getMetricValues().get(6L), delta);
+    assertEquals(110, cachedMetric.getMetricValues().get(7L), delta);
+    assertEquals(70, cachedMetric.getMetricValues().get(8L), delta);
+  }
+
+  private TimelineMetric createTimelineMetric(Map<Long, Double> metricValues,
+                                              long startTime) {
+    TimelineMetric timelineMetric = new TimelineMetric();
+    timelineMetric.setMetricName(METRIC_NAME);
+    timelineMetric.setHostName("Test hostName");
+    timelineMetric.setAppId("test serviceName");
+    timelineMetric.setStartTime(startTime);
+    timelineMetric.setType("Number");
+    timelineMetric.setMetricValues(metricValues);
+    return timelineMetric;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/f7efd2b3/ambari-metrics/ambari-metrics-hadoop-sink/src/main/java/org/apache/hadoop/metrics2/sink/timeline/HadoopTimelineMetricsSink.java
----------------------------------------------------------------------
diff --git a/ambari-metrics/ambari-metrics-hadoop-sink/src/main/java/org/apache/hadoop/metrics2/sink/timeline/HadoopTimelineMetricsSink.java b/ambari-metrics/ambari-metrics-hadoop-sink/src/main/java/org/apache/hadoop/metrics2/sink/timeline/HadoopTimelineMetricsSink.java
index a8d3a37..c0fa373 100644
--- a/ambari-metrics/ambari-metrics-hadoop-sink/src/main/java/org/apache/hadoop/metrics2/sink/timeline/HadoopTimelineMetricsSink.java
+++ b/ambari-metrics/ambari-metrics-hadoop-sink/src/main/java/org/apache/hadoop/metrics2/sink/timeline/HadoopTimelineMetricsSink.java
@@ -164,7 +164,7 @@ public class HadoopTimelineMetricsSink extends AbstractTimelineMetricsSink imple
         timelineMetric.setType(ClassUtils.getShortCanonicalName(value, "Number"));
         timelineMetric.getMetricValues().put(startTime, value.doubleValue());
         // Put intermediate values into the cache until it is time to send
-        metricsCache.putTimelineMetric(timelineMetric);
+        metricsCache.putTimelineMetric(timelineMetric, metric.type());
 
         // Retrieve all values from cache if it is time to send
         TimelineMetric cachedMetric = metricsCache.getTimelineMetric(name);