wxbty opened a new pull request, #12223:
URL: https://github.com/apache/dubbo/pull/12223

   ## What is the purpose of the change
   Issue Number: close https://github.com/apache/dubbo/issues/12153
   
   ## Brief changelog
   The MergingDigest data structure of the t-Digest package uses a cache with a 
default size (1050) to improve efficiency, and the cache increments by +1 each 
time it is added.
   If the cache is full, the space will be merged and tempUsed=0 will be reset. 
In this process, there are common concurrency problems in the judgment and 
merge methods, which need to be fixed.
   1. Add synchronized to the mergeNewValues ​​method. This method will be 
executed about 1050 times (the reason for the 0.1% error rate), so there is no 
need to worry about performance issues.
   2. The cache offset tempUsed is changed from int type to AtomicInteger, and 
volatile modification is added
   3. Because the source code of t-Digest  cannot be modified directly, copy 
the corresponding code to the dubbo source code separately
   
    private void add(double x, int w, List<Double> history) {
           if (Double.isNaN(x)) {
               throw new IllegalArgumentException("Cannot add NaN to t-digest");
           }
           if (tempUsed >= tempWeight.length - lastUsedCell - 1) {  
//判断是否达到缓存上限,tempWeight.length =1050
               mergeNewValues();
           }
           int where = tempUsed++;    // 每次add自增
           // ...
   }
   
   
![image](https://user-images.githubusercontent.com/38374721/235846216-5d9e1359-896e-4b75-90db-1aa0ef52fd82.png)
   
   
   ## Verifying this change
   
   1、git pull  3.2
   2、modify the ConsumerApplication code of dubbo-demo-spring-boot-consumer as 
follows. About 0.1% error rate before the change, no error rate after the change
   
    public static void main(String[] args) {
   
           ConfigurableApplicationContext context = 
SpringApplication.run(ConsumerApplication.class, args);
           ConsumerApplication application = 
context.getBean(ConsumerApplication.class);
           String result = application.doSayHello();
           System.out.println("result: " + result);
       }
   
       public String doSayHello() {
           int index = 0;
           while (index < 200) {
               System.out.println("-------------:" + index);
               for (int i = 0; i < 100; i++) {
                   new Thread(() -> {
   
                       try {
                           String result = demoService.sayHello("world");
                       } catch (Exception e) {
                           e.printStackTrace();
                       }
                   }).start();
   
               }
               index++;
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           return "";
       }
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to