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 #11986 ConcurrentHashMapUtils lock bug #11987

Merged
merged 20 commits into from May 10, 2023

Conversation

looly
Copy link
Contributor

@looly looly commented Apr 2, 2023

What is the purpose of the change

fix ConcurrentHashMapUtils lock bug

Brief changelog

Verifying this change

Closed #11986

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.

@looly looly changed the title fix ConcurrentHashMapUtils lock bug fix #11986 ConcurrentHashMapUtils lock bug Apr 2, 2023
@looly
Copy link
Contributor Author

looly commented Apr 2, 2023

issue : #11986

@AlbumenJ
Copy link
Member

AlbumenJ commented Apr 3, 2023

image

@looly
Copy link
Contributor Author

looly commented Apr 3, 2023

@AlbumenJ 限定到了jdk8,看行不行。

// https://github.com/apache/dubbo/issues/11986
final ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
// // map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
if (JRE.JAVA_8.isCurrentVersion()) {
Copy link
Member

Choose a reason for hiding this comment

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

这里应该任何版本的 JDK 都应该可以使用吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

并不是。这个bug在JDK8下会造成循环调用,但是jdk9+修复了这个问题,解决方式是有循环调用则抛出异常。这个test在jdk8下通过,jdk9+抛出异常。

Copy link
Member

Choose a reason for hiding this comment

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

那基于 junit5 的注解做两个测试吧
现在 PR的 UT 只跑 JDK17,JDK 8 11 17 的全量测试只有 scheduled 的时候跑

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 是这个意思么?我拆分成两个test。

Copy link
Member

Choose a reason for hiding this comment

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

    @EnabledForJreRange(max = JRE.JAVA_8)
    @EnabledForJreRange(min = JRE.JAVA_9)

Copy link
Member

Choose a reason for hiding this comment

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

写两个 test,分别通过 EnabledForJreRange 配置运行的 jdk 版本

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 辛苦看下。

Copy link
Contributor

@KamToHung KamToHung left a comment

Choose a reason for hiding this comment

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

#6032
ExtensionLoader以前也遇到过这种问题,这样修改后代码健壮性挺好的

// https://github.com/apache/dubbo/issues/11986
final ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
// // map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
if (JRE.JAVA_8.isCurrentVersion()) {
Copy link
Member

Choose a reason for hiding this comment

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

那基于 junit5 的注解做两个测试吧
现在 PR的 UT 只跑 JDK17,JDK 8 11 17 的全量测试只有 scheduled 的时候跑

@codecov-commenter
Copy link

codecov-commenter commented Apr 5, 2023

Codecov Report

Merging #11987 (b012096) into 3.2 (8b65828) will decrease coverage by 0.06%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##                3.2   #11987      +/-   ##
============================================
- Coverage     69.40%   69.35%   -0.06%     
  Complexity        2        2              
============================================
  Files          1605     1605              
  Lines         66165    66171       +6     
  Branches       9698     9701       +3     
============================================
- Hits          45924    45893      -31     
- Misses        15811    15841      +30     
- Partials       4430     4437       +7     

see 24 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

// https://github.com/apache/dubbo/issues/11986
final ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
// // map.computeIfAbsent("AaAa", key->map.computeIfAbsent("BBBB",key2->42));
if (JRE.JAVA_8.isCurrentVersion()) {
Copy link
Member

Choose a reason for hiding this comment

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

写两个 test,分别通过 EnabledForJreRange 配置运行的 jdk 版本

Comment on lines 50 to 51
// JDK8下,由于循环调用bug,会造成执行computeIfAbsent死循环问题
// ConcurrentHashMapUtils.computeIfAbsent用于解决此问题,保证正常执行
Copy link
Member

Choose a reason for hiding this comment

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

Please comment in English

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 check it, please.

@looly looly requested a review from AlbumenJ April 13, 2023 14:28
// v = map.computeIfAbsent(key, func);

// this bug fix methods maybe cause `func.apply` multiple calls.
v = func.apply(key);
Copy link
Member

Choose a reason for hiding this comment

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

Will this allocate more memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Copy link
Member

@AlbumenJ AlbumenJ left a comment

Choose a reason for hiding this comment

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

LGTM.
@chickenlj @CrazyHZM PTAL

@sonarcloud
Copy link

sonarcloud bot commented May 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 3 Code Smells

9.1% 9.1% Coverage
0.0% 0.0% Duplication

@AlbumenJ AlbumenJ requested a review from CrazyHZM May 7, 2023 12:47
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.

ConcurrentHashMapUtils无法解决jdk8的循环bug
5 participants