Skip to content

Commit

Permalink
Add an assert to detect double decrementing of ingest metrics (#81450)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Dec 7, 2021
1 parent 6626cc9 commit 9d8029e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}

0 comments on commit 9d8029e

Please sign in to comment.