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

improve NacosNamingServiceWrapper performance #11665

Merged
merged 2 commits into from
Mar 8, 2023
Merged

Conversation

sourcelliu
Copy link
Contributor

What is the purpose of the change

Because the map is ConcurrentHashMap, so mapLock is not necessary to use.

Brief changelog

improve NacosNamingServiceWrapper performance.

Verifying this change

In my location benchmark test, the code like this:

    @Test
    void testRegisterInstance() throws NacosException, InterruptedException{
        NamingService namingService = new MockNamingService() {
            @Override
            public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {

            }

            @Override
            public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
                return null;
            }
        };

        NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
        CountDownLatch startLatch = new CountDownLatch(100);
        long startTime = System.currentTimeMillis();
        for(int i=0;i<100;i++){
            new Thread(()->{
                try {
                    for(int j=0;j<100000;j++){
                        Instance instance = new Instance();
                        nacosNamingServiceWrapper.registerInstance("service_name"+j, "test", instance);
                    }
                    startLatch.countDown();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
        startLatch.await();
        System.out.printf("the cost time is:%d ms\n",System.currentTimeMillis()-startTime);
    }

before change: the cost time is: 14564 ms
after change: the cost time is: 5587 ms
the execution time reduced by 61.6%

Checklist

  • Make sure there is a GitHub_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GitHub issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Check if is necessary to patch to Dubbo 3 if you are work on Dubbo 2.7
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Add some description to dubbo-website project if you are requesting to add a feature.
  • GitHub Actions works fine on your own branch.
  • If this contribution is large, please follow the Software Donation Guide.

Signed-off-by: mantuliu <240951888@qq.com>
Comment on lines +247 to +249
if (instancesInfo.getInstances().isEmpty()) {
registerStatus.remove(new InstanceId(nacosServiceName, group));
instancesInfo.setValid(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operation here is not atomic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlbumenJ
I think there is no need for atomicity guarantee here.

  • because the code here has already got instancesInfo lock instancesInfo.lock();
  • and for the modification of registerStatus registerStatus.remove(new InstanceId(nacosServiceName, group));, due to the protection of the lock mechanism inside concurrenthashmap , which has the same effect as the previous mapLock.lock()

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

75.0% 75.0% Coverage
0.0% 0.0% Duplication

@AlbumenJ AlbumenJ merged commit d0b98b3 into apache:3.2 Mar 8, 2023
lcb11 pushed a commit to lcb11/dubbo that referenced this pull request Mar 31, 2023
Signed-off-by: mantuliu <240951888@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants