Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DubboMergingDigest concurrent issue #12913

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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 @@ -18,11 +18,12 @@
package org.apache.dubbo.metrics.aggregate;


import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;

import com.tdunning.math.stats.Centroid;
import com.tdunning.math.stats.ScaleFunction;
import com.tdunning.math.stats.Sort;
import com.tdunning.math.stats.TDigest;
import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;

import java.nio.ByteBuffer;
import java.util.AbstractCollection;
Expand Down Expand Up @@ -285,16 +286,17 @@ private void add(double x, int w, List<Double> history) {
throw new IllegalArgumentException("Cannot add NaN to t-digest");
}

int where;
synchronized (this) {
// There is a small probability of entering here
if (tempUsed.get() >= tempWeight.length - lastUsedCell.get() - 1) {
mergeNewValues();
}
where = tempUsed.getAndIncrement();
tempWeight[where] = w;
tempMean[where] = x;
unmergedWeight.addAndGet(w);
}
int where = tempUsed.getAndIncrement();
tempWeight[where] = w;
tempMean[where] = x;
unmergedWeight.addAndGet(w);
if (x < min) {
min = x;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.aggregate;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
Expand All @@ -37,6 +38,7 @@ void test() {
}

@Test
@RepeatedTest(100)
void testMulti() {

ExecutorService executorService = Executors.newFixedThreadPool(200);
Expand All @@ -51,12 +53,14 @@ void testMulti() {
quantile.add(finalI));
}
index++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}

executorService.shutdown();
}

}