From 0e8d9e385902f89dca31bd3abc2ed8f9cd487171 Mon Sep 17 00:00:00 2001 From: shikhman Date: Tue, 6 Sep 2016 13:59:43 -0700 Subject: [PATCH] Force endTimestamp to be strictly greater than or equal to startTimestamp This fixes an error that we've been seeing from Stackdriver. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132358790 --- java/google/registry/monitoring/metrics/Counter.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/java/google/registry/monitoring/metrics/Counter.java b/java/google/registry/monitoring/metrics/Counter.java index 911c35b0f..f04bec8ad 100644 --- a/java/google/registry/monitoring/metrics/Counter.java +++ b/java/google/registry/monitoring/metrics/Counter.java @@ -21,6 +21,7 @@ import static google.registry.monitoring.metrics.MetricsUtils.newConcurrentHashM import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Ordering; import com.google.common.util.concurrent.AtomicLongMap; import com.google.common.util.concurrent.Striped; import google.registry.monitoring.metrics.MetricSchema.Kind; @@ -131,6 +132,12 @@ public final class Counter extends AbstractMetric valueLocks.get(labelValues).unlock(); } + // There is an opportunity for endTimestamp to be less than startTimestamp if + // one of the modification methods is called on a value before the lock for that value is + // acquired but after getTimestampedValues has been invoked. Just set endTimestamp equal to + // startTimestamp if that happens. + endTimestamp = Ordering.natural().max(startTimestamp, endTimestamp); + timestampedValues.add( MetricPoint.create(this, labelValues, startTimestamp, endTimestamp, entry.getValue()));