Kaynağa Gözat

AMBARI-15638 : [AMS] Sum Calculation Incorrect - Patch 2 (avijayan)

Aravindan Vijayan 9 yıl önce
ebeveyn
işleme
dc366dbe7f

+ 1 - 3
ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/AggregatorUtils.java

@@ -43,11 +43,9 @@ public class AggregatorUtils {
             min = value;
           }
           sum += value;
-          if (value > 0.0) {
-            metricCount++;
-          }
         }
       }
+      metricCount = metricValues.values().size();
     }
     // BR: WHY ZERO is a good idea?
     values[0] = sum;

+ 23 - 11
ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/aggregators/TimelineMetricClusterAggregatorSecond.java

@@ -209,6 +209,11 @@ public class TimelineMetricClusterAggregatorSecond extends AbstractTimelineAggre
       timeShift = 0l;
     }
 
+    Long prevTimestamp = -1l;
+    TimelineClusterMetric prevMetric = null;
+    int count = 0;
+    double sum = 0.0;
+
     Map<Long,Double> timeSliceValueMap = new HashMap<>();
     for (Map.Entry<Long, Double> metric : timelineMetric.getMetricValues().entrySet()) {
       // TODO: investigate null values - pre filter
@@ -226,24 +231,31 @@ public class TimelineMetricClusterAggregatorSecond extends AbstractTimelineAggre
           timestamp,
           timelineMetric.getType());
 
-        // do a sum / count here to get average for all points in a slice
-        int count = 1;
-        Double sum = 0.0;
-        if (!timelineClusterMetricMap.containsKey(clusterMetric)) {
-          sum = metric.getValue();
-        } else {
+        if (prevTimestamp < 0 || timestamp.equals(prevTimestamp)) {
           Double newValue = metric.getValue();
           if (newValue > 0.0) {
+            sum += newValue;
             count++;
-            Double oldValue = timelineClusterMetricMap.get(clusterMetric);
-            sum = oldValue + newValue;
           }
+        } else {
+          double metricValue = (count > 0) ? (sum / count) : 0.0;
+            timelineClusterMetricMap.put(prevMetric, metricValue);
+          timeSliceValueMap.put(prevMetric.getTimestamp(), metricValue);
+          sum = metric.getValue();
+          count = sum > 0.0 ? 1 : 0;
         }
-        double metricValue = sum / count;
-        timelineClusterMetricMap.put(clusterMetric, metricValue);
-        timeSliceValueMap.put(timestamp, metricValue);
+
+        prevTimestamp = timestamp;
+        prevMetric = clusterMetric;
       }
     }
+
+    if (prevTimestamp > 0) {
+      double metricValue = (count > 0) ? (sum / count) : 0.0;
+      timelineClusterMetricMap.put(prevMetric, metricValue);
+      timeSliceValueMap.put(prevTimestamp, metricValue);
+    }
+
     if (interpolationEnabled) {
       interpolateMissingPeriods(timelineClusterMetricMap, timelineMetric, timeSlices, timeSliceValueMap);
     }

+ 1 - 1
ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/PhoenixTransactSQL.java

@@ -271,7 +271,7 @@ public class PhoenixTransactSQL {
   public static final String GET_AGGREGATED_APP_METRIC_GROUPBY_SQL = "UPSERT %s " +
     "INTO %s (METRIC_NAME, APP_ID, INSTANCE_ID, SERVER_TIME, UNITS, " +
     "METRIC_SUM, METRIC_COUNT, METRIC_MAX, METRIC_MIN) SELECT METRIC_NAME, APP_ID, " +
-    "INSTANCE_ID, %s AS SERVER_TIME, UNITS, AVG(METRIC_SUM), AVG(%s), " +
+    "INSTANCE_ID, %s AS SERVER_TIME, UNITS, ROUND(AVG(METRIC_SUM),2), ROUND(AVG(%s)), " +
     "MAX(METRIC_MAX), MIN(METRIC_MIN) FROM %s WHERE SERVER_TIME > %s AND " +
     "SERVER_TIME <= %s GROUP BY METRIC_NAME, APP_ID, INSTANCE_ID, UNITS";