protected void updateTimelineMetricsInCache(TimelineMetrics newMetrics,
      TimelineMetricsCacheValue timelineMetricsCacheValue,
      Long requestedStartTime, Long requestedEndTime, boolean removeAll) {
    TimelineMetrics existingTimelineMetrics = timelineMetricsCacheValue.getTimelineMetrics();

    // Remove values that do not fit before adding new data
    updateExistingMetricValues(existingTimelineMetrics, requestedStartTime,
      requestedEndTime, removeAll);

    if (newMetrics != null && !newMetrics.getMetrics().isEmpty()) {
      for (TimelineMetric timelineMetric : newMetrics.getMetrics()) {
        if (LOG.isTraceEnabled()) {
          TreeMap<Long, Double> sortedMetrics = new TreeMap<>(timelineMetric.getMetricValues());

          LOG.trace("New metric: {} # {}, startTime = {}, endTime = {}",
            timelineMetric.getMetricName(), timelineMetric.getMetricValues().size(), sortedMetrics.firstKey(), sortedMetrics.lastKey());
        }

        TimelineMetric existingMetric = null;

        for (TimelineMetric metric : existingTimelineMetrics.getMetrics()) {
          if (metric.equalsExceptTime(timelineMetric)) {
            existingMetric = metric;
          }
        }

        if (existingMetric != null) {
          // Add new ones
          existingMetric.getMetricValues().putAll(timelineMetric.getMetricValues());

          if (LOG.isTraceEnabled()) {
            TreeMap<Long, Double> sortedMetrics = new TreeMap<>(existingMetric.getMetricValues());
            LOG.trace("Merged metric: {}, Final size: {}, startTime = {}, endTime = {}",
              timelineMetric.getMetricName(), existingMetric.getMetricValues().size(), sortedMetrics.firstKey(), sortedMetrics.lastKey());
          }
        } else {
          existingTimelineMetrics.getMetrics().add(timelineMetric);
        }
      }
    }
  }
Update cache with new timeseries data
ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/timeline/cache/TimelineMetricCacheEntryFactory.java