You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/11/01 15:30:28 UTC

[GitHub] xiangwangcheng opened a new pull request #516: solve multi-thread problem of StatsItemSet#getAndCreateStatsItem

xiangwangcheng opened a new pull request #516: solve multi-thread problem of StatsItemSet#getAndCreateStatsItem
URL: https://github.com/apache/rocketmq/pull/516
 
 
   Closes#515 
   
   1. use putIfAbsent instead of put method of ConcurrentHashMap and 
   2. if the prev is not null then use get method
   to avoid multi-thread problem.
   
   test case:
   ```
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
   
   package org.apache.rocketmq.broker;
   
   import org.apache.rocketmq.common.ThreadFactoryImpl;
   import org.apache.rocketmq.store.stats.BrokerStatsManager;
   import org.junit.Before;
   import org.junit.Test;
   
   import java.util.concurrent.ArrayBlockingQueue;
   import java.util.concurrent.ThreadPoolExecutor;
   import java.util.concurrent.TimeUnit;
   
   import static org.apache.rocketmq.store.stats.BrokerStatsManager.TOPIC_PUT_NUMS;
   
   public class BrokerStatsManagerTest {
   
       private BrokerStatsManager brokerStatsManager;
       private ThreadPoolExecutor executor;
       @Before
       public void init() {
           brokerStatsManager = new BrokerStatsManager("DefaultCluster");
           executor = new ThreadPoolExecutor(100, 200, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1000), new ThreadFactoryImpl("testMultiThread"));
       }
   
       @Test
       public void testBrokerStatsManager() throws InterruptedException {
   
           for(int i =0; i < 10000; i++) {
               executor.submit(new Runnable() {
                   @Override
                   public void run() {
                       brokerStatsManager.incTopicPutNums("topicTest", 2, 1);
                   }
               });
           }
           Thread.sleep(5000);
           System.out.println(brokerStatsManager.getStatsItem(TOPIC_PUT_NUMS,"topicTest").getValue());
       }
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services