diff --git a/server/src/main/java/org/elasticsearch/ingest/IngestMetric.java b/server/src/main/java/org/elasticsearch/ingest/IngestMetric.java index 264435576762f..de26acff1a024 100644 --- a/server/src/main/java/org/elasticsearch/ingest/IngestMetric.java +++ b/server/src/main/java/org/elasticsearch/ingest/IngestMetric.java @@ -52,7 +52,8 @@ void preIngest() { * @param ingestTimeInNanos The time it took to perform the action. */ void postIngest(long ingestTimeInNanos) { - ingestCurrent.decrementAndGet(); + long current = ingestCurrent.decrementAndGet(); + assert current >= 0 : "ingest metric current count double-decremented"; this.ingestTimeInNanos.inc(ingestTimeInNanos); ingestCount.inc(); } diff --git a/server/src/test/java/org/elasticsearch/ingest/IngestMetricTests.java b/server/src/test/java/org/elasticsearch/ingest/IngestMetricTests.java index 32a9ae5088356..d4fd0d6c0ef52 100644 --- a/server/src/test/java/org/elasticsearch/ingest/IngestMetricTests.java +++ b/server/src/test/java/org/elasticsearch/ingest/IngestMetricTests.java @@ -31,4 +31,18 @@ public void testIngestTimeInNanos() { assertThat(1L, equalTo(metric.createStats().getIngestTimeInMillis())); } + public void testPostIngestDoubleDecrement() { + IngestMetric metric = new IngestMetric(); + + metric.preIngest(); + assertThat(1L, equalTo(metric.createStats().getIngestCurrent())); + + metric.postIngest(500000L); + assertThat(0L, equalTo(metric.createStats().getIngestCurrent())); + + // the second postIngest triggers an assertion error + expectThrows(AssertionError.class, () -> metric.postIngest(500000L)); + assertThat(-1L, equalTo(metric.createStats().getIngestCurrent())); + } + }