private void padPresentation(Bucket toPresent) { Map<String, List<Entry<Point, UntypedMetric>>> currentMetricNames = toPresent.getValuesByMetricName(); for (Map.Entry<String, List<Entry<Point, UntypedMetric>>> metric : currentMetricNames.entrySet()) { final int currentDataPoints = metric.getValue().size(); if (currentDataPoints < pointsToKeep) { padMetric(metric.getKey(), toPresent, currentDataPoints); } } Set<String> keysMissingFromPresentation = new HashSet<>(persistentData.keySet()); keysMissingFromPresentation.removeAll(currentMetricNames.keySet()); for (String cachedMetric : keysMissingFromPresentation) { padMetric(cachedMetric, toPresent, 0); } }
void updateDimensionPersistence(Bucket toDelete, Bucket toPresent) { updatePersistentData(toDelete); padPresentation(toPresent); }
private void updatePersistentData(Bucket toDelete) { if (toDelete == null) { return; } long millis = toDelete.gotTimeStamps ? toDelete.toMillis : System.currentTimeMillis(); for (Map.Entry<String, List<Entry<Point, UntypedMetric>>> metric : toDelete.getValuesByMetricName().entrySet()) { LinkedHashMap<Point, TimeStampedMetric> cachedPoints = getCachedMetric(metric.getKey()); for (Entry<Point, UntypedMetric> newestInterval : metric.getValue()) { // overwriting an existing entry does not update the order // in the map cachedPoints.remove(newestInterval.getKey()); TimeStampedMetric toInsert = new TimeStampedMetric(millis, newestInterval.getValue()); cachedPoints.put(newestInterval.getKey(), toInsert); } } }
private void createSnapshot(Bucket toDelete) { final Bucket toPresent = new Bucket(); for (Bucket b : buffer) { if (b == null) { continue; } toPresent.merge(b); } dimensions.updateDimensionPersistence(toDelete, toPresent); currentSnapshot.set(toPresent); }
MetricAggregator(ThreadLocalDirectory<Bucket, Sample> metricsCollection, AtomicReference<Bucket> currentSnapshot, ManagerConfig settings) { if (settings.reportPeriodSeconds() < 10) { throw new IllegalArgumentException( "Do not use this metrics implementation" + " if report periods of less than 10 seconds is desired."); } buffer = new Bucket[settings.reportPeriodSeconds()]; dimensions = new DimensionCache(settings.pointsToKeepPerMetric()); fromMillis = System.currentTimeMillis(); this.metricsCollection = metricsCollection; this.currentSnapshot = currentSnapshot; }
private void padMetric(String metric, Bucket toPresent, int currentDataPoints) { final LinkedHashMap<Point, TimeStampedMetric> cachedPoints = getCachedMetric(metric); int toAdd = pointsToKeep - currentDataPoints; @SuppressWarnings({"unchecked","rawtypes"}) Entry<Point, TimeStampedMetric>[] cachedEntries = cachedPoints.entrySet().toArray(new Entry[0]); long nowMillis = System.currentTimeMillis(); for (int i = cachedEntries.length - 1; i >= 0 && toAdd > 0; --i) { Entry<Point, TimeStampedMetric> leastOld = cachedEntries[i]; if (leastOld.getValue().millis + MAX_AGE_MILLIS < nowMillis) { continue; } final Identifier id = new Identifier(metric, leastOld.getKey()); if (!toPresent.hasIdentifier(id)) { toPresent.put(id, leastOld.getValue().metric.pruneData()); --toAdd; } } }