Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Buffer {
}

boolean append(double value) {
int index = Math.abs((int) Thread.currentThread().getId()) % stripedObservationCounts.length;
int index = stripeIndex(Thread.currentThread().getId(), stripedObservationCounts.length);
AtomicLong observationCountForThread = stripedObservationCounts[index];
long count = observationCountForThread.incrementAndGet();
if ((count & bufferActiveBit) == 0) {
Expand All @@ -54,6 +54,10 @@ boolean append(double value) {
}
}

static int stripeIndex(long threadId, int stripeCount) {
return (int) Math.floorMod(threadId, stripeCount);
}

private void doAppend(double amount) {
appendLock.lock();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.prometheus.metrics.core.metrics;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class BufferTest {

@Test
void stripeIndexDoesNotOverflowWhenThreadIdNarrowsToIntegerMinValue() {
assertThat(Buffer.stripeIndex(2_147_483_648L, 3)).isEqualTo(2);
assertThat(Buffer.stripeIndex(2_147_483_648L, 6)).isEqualTo(2);
assertThat(Buffer.stripeIndex(2_147_483_648L, 12)).isEqualTo(8);
}
}