fix digest thread_safe bug (#12712)

* fix thread_safe bug

* fix thread_safe bug

* remove unused

* add comment
This commit is contained in:
wxbty 2023-07-11 20:44:46 +08:00 committed by GitHub
parent d2eb2ba3d7
commit d4856e2b0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 18 deletions

View File

@ -119,8 +119,6 @@ public class DubboMergingDigest extends DubboAbstractTDigest {
// weight limits.
public static boolean useWeightLimit = true;
private volatile boolean merging = false;
/**
* Allocates a buffer merging t-digest. This is the normally used constructor that
* allocates default sized internal arrays. Other versions are available, but should
@ -286,16 +284,13 @@ public class DubboMergingDigest extends DubboAbstractTDigest {
if (Double.isNaN(x)) {
throw new IllegalArgumentException("Cannot add NaN to t-digest");
}
while (merging) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
synchronized (this) {
// There is a small probability of entering here
if (tempUsed.get() >= tempWeight.length - lastUsedCell.get() - 1) {
mergeNewValues();
}
}
if (tempUsed.get() >= tempWeight.length - lastUsedCell.get() - 1) {
mergeNewValues();
}
int where = tempUsed.getAndIncrement();
tempWeight[where] = w;
tempMean[where] = x;
@ -326,13 +321,8 @@ public class DubboMergingDigest extends DubboAbstractTDigest {
throw new MetricsNeverHappenException("Method not used");
}
private synchronized void mergeNewValues() {
merging = true;
try {
mergeNewValues(false, compression);
} finally {
merging = false;
}
private void mergeNewValues() {
mergeNewValues(false, compression);
}
private void mergeNewValues(boolean force, double compression) {

View File

@ -46,8 +46,9 @@ class TimeWindowQuantileTest {
while (index < 100) {
for (int i = 0; i < 100; i++) {
int finalI = i;
Assertions.assertDoesNotThrow(() -> quantile.add(finalI));
executorService.execute(() ->
Assertions.assertDoesNotThrow(() -> quantile.add(finalI)));
quantile.add(finalI));
}
index++;
try {