Skip to content

Commit

Permalink
Switch to AtomicLong for ingestCurrent metric to prevent negative val…
Browse files Browse the repository at this point in the history
…ues (#52581)
  • Loading branch information
danhermann authored Feb 24, 2020
1 parent 7f1d05c commit 57fa346
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/src/main/java/org/elasticsearch/ingest/IngestMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.elasticsearch.common.metrics.CounterMetric;
import org.elasticsearch.common.metrics.MeanMetric;

import java.util.concurrent.atomic.AtomicLong;

/**
* <p>Metrics to measure ingest actions.
* <p>This counts measure documents and timings for a given scope.
Expand All @@ -39,7 +41,7 @@ class IngestMetric {
* The current count of things being measure. Should most likely ever be 0 or 1.
* Useful when aggregating multiple metrics to see how many things are in flight.
*/
private final CounterMetric ingestCurrent = new CounterMetric();
private final AtomicLong ingestCurrent = new AtomicLong();
/**
* The ever increasing count of things being measured
*/
Expand All @@ -53,15 +55,15 @@ class IngestMetric {
* Call this prior to the ingest action.
*/
void preIngest() {
ingestCurrent.inc();
ingestCurrent.incrementAndGet();
}

/**
* Call this after the performing the ingest action, even if the action failed.
* @param ingestTimeInMillis The time it took to perform the action.
*/
void postIngest(long ingestTimeInMillis) {
ingestCurrent.dec();
ingestCurrent.decrementAndGet();
ingestTime.inc(ingestTimeInMillis);
ingestCount.inc();
}
Expand Down Expand Up @@ -90,6 +92,6 @@ void add(IngestMetric metrics) {
* Creates a serializable representation for these metrics.
*/
IngestStats.Stats createStats() {
return new IngestStats.Stats(ingestCount.count(), ingestTime.sum(), ingestCurrent.count(), ingestFailed.count());
return new IngestStats.Stats(ingestCount.count(), ingestTime.sum(), ingestCurrent.get(), ingestFailed.count());
}
}

0 comments on commit 57fa346

Please sign in to comment.