Skip to content

Commit 3c1d2b3

Browse files
authored
Switch to AtomicLong for ingestCurrent metric to prevent negative values (#52581) (#52761)
1 parent 8e2aaea commit 3c1d2b3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

server/src/main/java/org/elasticsearch/ingest/IngestMetric.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.common.metrics.CounterMetric;
2323
import org.elasticsearch.common.metrics.MeanMetric;
2424

25+
import java.util.concurrent.atomic.AtomicLong;
26+
2527
/**
2628
* <p>Metrics to measure ingest actions.
2729
* <p>This counts measure documents and timings for a given scope.
@@ -39,7 +41,7 @@ class IngestMetric {
3941
* The current count of things being measure. Should most likely ever be 0 or 1.
4042
* Useful when aggregating multiple metrics to see how many things are in flight.
4143
*/
42-
private final CounterMetric ingestCurrent = new CounterMetric();
44+
private final AtomicLong ingestCurrent = new AtomicLong();
4345
/**
4446
* The ever increasing count of things being measured
4547
*/
@@ -53,15 +55,15 @@ class IngestMetric {
5355
* Call this prior to the ingest action.
5456
*/
5557
void preIngest() {
56-
ingestCurrent.inc();
58+
ingestCurrent.incrementAndGet();
5759
}
5860

5961
/**
6062
* Call this after the performing the ingest action, even if the action failed.
6163
* @param ingestTimeInMillis The time it took to perform the action.
6264
*/
6365
void postIngest(long ingestTimeInMillis) {
64-
ingestCurrent.dec();
66+
ingestCurrent.decrementAndGet();
6567
ingestTime.inc(ingestTimeInMillis);
6668
ingestCount.inc();
6769
}
@@ -90,6 +92,6 @@ void add(IngestMetric metrics) {
9092
* Creates a serializable representation for these metrics.
9193
*/
9294
IngestStats.Stats createStats() {
93-
return new IngestStats.Stats(ingestCount.count(), ingestTime.sum(), ingestCurrent.count(), ingestFailed.count());
95+
return new IngestStats.Stats(ingestCount.count(), ingestTime.sum(), ingestCurrent.get(), ingestFailed.count());
9496
}
9597
}

0 commit comments

Comments
 (0)