Skip to content

Commit f44b890

Browse files
committed
added sync block
1 parent 1eee9ff commit f44b890

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

runtime/service/src/main/java/org/apache/polaris/service/tracing/RequestIdGenerator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.annotations.VisibleForTesting;
2323
import jakarta.enterprise.context.ApplicationScoped;
2424
import java.util.UUID;
25-
import java.util.concurrent.atomic.AtomicBoolean;
2625
import java.util.concurrent.atomic.AtomicLong;
2726

2827
@ApplicationScoped
@@ -31,17 +30,18 @@ public class RequestIdGenerator {
3130
static final AtomicLong COUNTER = new AtomicLong();
3231
static final Long COUNTER_SOFT_MAX = Long.MAX_VALUE / 2;
3332

34-
private final AtomicBoolean resetInProgress = new AtomicBoolean(false);
35-
3633
public String generateRequestId() {
3734
long currentCounter = COUNTER.incrementAndGet();
3835
String requestId = baseDefaultUuid + "_" + currentCounter;
39-
if (currentCounter >= COUNTER_SOFT_MAX && resetInProgress.compareAndSet(false, true)) {
40-
// If we get anywhere close to danger of overflowing Long (which, theoretically,
41-
// would be extremely unlikely) rotate the UUID and start again.
42-
baseDefaultUuid = UUID.randomUUID().toString();
43-
COUNTER.set(0);
44-
resetInProgress.set(false);
36+
if (currentCounter >= COUNTER_SOFT_MAX) {
37+
synchronized (this) {
38+
if (COUNTER.get() >= COUNTER_SOFT_MAX) {
39+
// If we get anywhere close to danger of overflowing Long (which, theoretically,
40+
// would be extremely unlikely) rotate the UUID and start again.
41+
baseDefaultUuid = UUID.randomUUID().toString();
42+
COUNTER.set(0);
43+
}
44+
}
4545
}
4646
return requestId;
4747
}

0 commit comments

Comments
 (0)