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

[ISSUE #3922] method createServiceIfAbsent in ServiceManager require sync #4713

Merged
merged 2 commits into from
Jan 18, 2021
Merged

Conversation

a7217107
Copy link
Contributor

method createServiceIfAbsent in ServiceManager require sync #3922

What is the purpose of the change

change method createServiceIfAbsent sync

Brief changelog

Double-Check

method createServiceIfAbsent in ServiceManager require sync #3922
@CLAassistant
Copy link

CLAassistant commented Jan 15, 2021

CLA assistant check
All committers have signed the CLA.

putServiceAndInit(service);
if (!local) {
addOrReplaceService(service);
synchronized (this) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

lock in this may affect the performance.
I think fix like following will be better

    public void putService(Service service) {
        if (!serviceMap.containsKey(service.getNamespaceId())) {
            synchronized (putServiceLock) {
                if (!serviceMap.containsKey(service.getNamespaceId())) {
                    serviceMap.put(service.getNamespaceId(), new ConcurrentHashMap<>(16));
                }
            }
        }
        *serviceMap.get(service.getNamespaceId()).putIfAbsent(service.getName(), service);
    }

    private void putServiceAndInit(Service service) throws NacosException {
        putService(service);
        *service = getService(service.getNamespaceId(), service.getName());
        service.init();
        consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service);
        consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service);
        Loggers.SRV_LOG.info("[NEW-SERVICE] {}", service.toJSON());
    }

What's your think about

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right, "synchronized (this)" in this method will affect the performance, your plan is better

@a7217107 a7217107 closed this Jan 16, 2021
@a7217107 a7217107 reopened this Jan 16, 2021
@KomachiSion KomachiSion merged commit d462eaa into alibaba:develop Jan 18, 2021
@KomachiSion KomachiSion added the kind/enhancement Category issues or prs related to enhancement. label Jan 18, 2021
@KomachiSion KomachiSion added this to the 1.4.2 milestone Jan 18, 2021
wjm0729 added a commit to wjm0729/nacos that referenced this pull request Jan 21, 2021
* 'develop' of https://github.com/alibaba/nacos:
  Use SafeConstructor to parse yaml configuration for AbstractConfigChangeListener (alibaba#4753)
  [ISSUE alibaba#3922] method createServiceIfAbsent in ServiceManager require sync (alibaba#4713)
  fix cloning configuration last desc (alibaba#4697)
  [ISSUE alibaba#4661]configServletInner#doGetConfig code optimization (alibaba#4666)
  Use revision to set version and upgrade to 1.4.2-SNAPSHOT (alibaba#4711)
  Revert interceptor all ui problem
  Fix alibaba#4701 (alibaba#4703)
  delete comment.
  fix metadata batch operation may delete instance problem.
  Upgrade to 1.4.1 (alibaba#4695)
  fix alibaba#4686 (alibaba#4692)
  Build nacos console front for 1.4.1 (alibaba#4690)
wjm0729 added a commit to wjm0729/nacos that referenced this pull request Jan 21, 2021
…op-import-v1

* 'develop' of https://github.com/alibaba/nacos:
  Use SafeConstructor to parse yaml configuration for AbstractConfigChangeListener (alibaba#4753)
  [ISSUE alibaba#3922] method createServiceIfAbsent in ServiceManager require sync (alibaba#4713)
  fix cloning configuration last desc (alibaba#4697)
  [ISSUE alibaba#4661]configServletInner#doGetConfig code optimization (alibaba#4666)
  Use revision to set version and upgrade to 1.4.2-SNAPSHOT (alibaba#4711)
  Revert interceptor all ui problem
  Fix alibaba#4701 (alibaba#4703)
  delete comment.
  fix metadata batch operation may delete instance problem.
  Upgrade to 1.4.1 (alibaba#4695)
  fix alibaba#4686 (alibaba#4692)
  Build nacos console front for 1.4.1 (alibaba#4690)
  For checkstyle
  fix: fix Jraft WriteRequest type problem.
  Add server identity to replace user-agent white list. (alibaba#4683)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants