You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "daguimu (via GitHub)" <gi...@apache.org> on 2026/03/25 11:29:06 UTC
[PR] fix: make LRU2Cache.computeIfAbsent thread-safe [dubbo]
daguimu opened a new pull request, #16170:
URL: https://github.com/apache/dubbo/pull/16170
## Problem
`LRU2Cache.computeIfAbsent()` releases the lock between `get()` and `put()`, breaking the atomicity that **all other methods** in this class maintain. Every other method (`get`, `put`, `remove`, `size`, `clear`, `containsKey`, `setMaxCapacity`) correctly holds the lock for the entire operation, but `computeIfAbsent` does not:
```java
// Current broken code - lock released between get() and put()
public V computeIfAbsent(K key, Function<? super K, ? extends V> fn) {
V value = get(key); // acquires lock, releases lock
if (value == null) {
value = fn.apply(key); // no lock held
put(key, value); // acquires lock, releases lock
}
return value;
}
```
This is called from the **Triple protocol hot path** in `StreamUtils.lruHeaderMap.computeIfAbsent(key, k -> k.toLowerCase())` for every RPC request.
### Impact
Under concurrency, multiple threads can:
1. Both see `get(key) == null`
2. Both compute `fn.apply(key)` (duplicate work)
3. Both call `put(key, value)` — the double-put defeats the LRU-2 promotion logic (first put goes to pre-cache, second put unintentionally promotes to main cache)
## Root Cause
The method was composed from the public `get()` and `put()` methods (which each independently acquire/release the lock) instead of operating on the underlying `LinkedHashMap` directly under a single lock span.
## Fix
Hold the lock across the entire `computeIfAbsent` operation, using `super.get()` / `super.put()` directly and inlining the LRU-2 promotion logic to avoid nested lock acquisition:
```java
public V computeIfAbsent(K key, Function<? super K, ? extends V> fn) {
lock.lock();
try {
V value = super.get(key);
if (value == null) {
value = fn.apply(key);
if (preCache.containsKey(key)) {
preCache.remove(key);
super.put(key, value);
} else {
preCache.put(key, true);
}
}
return value;
} finally {
lock.unlock();
}
}
```
## Tests Added
- `testComputeIfAbsentBasic` — Verifies correct LRU-2 semantics: first call goes to pre-cache, second promotes, third returns from main cache without calling the function
- `testComputeIfAbsentConcurrency` — 10 threads × 1000 iterations calling `computeIfAbsent` concurrently, verifying no errors or corruption
All 6 tests in `LRU2CacheTest` pass.
## Impact
- Minimal change: 1 method rewritten in 1 file
- Preserves existing LRU-2 semantics
- Fixes thread safety in Triple protocol header processing hot path
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] fix: make LRU2Cache.computeIfAbsent thread-safe [dubbo]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #16170:
URL: https://github.com/apache/dubbo/pull/16170#issuecomment-4126255040
## [Codecov](https://app.codecov.io/gh/apache/dubbo/pull/16170?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 36.95%. Comparing base ([`63fe8c3`](https://app.codecov.io/gh/apache/dubbo/commit/63fe8c3a982a554ba40cd5e62f07d74cebb45e39?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`7a5fa6b`](https://app.codecov.io/gh/apache/dubbo/commit/7a5fa6bf8d3e58910e7c196cb2a281c9b58faac3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 1 commits behind head on 3.3.
> :exclamation: There is a different number of reports uploaded between BASE (63fe8c3) and HEAD (7a5fa6b). Click for more details.
>
> <details><summary>HEAD has 5 uploads less than BASE</summary>
>
>| Flag | BASE (63fe8c3) | HEAD (7a5fa6b) |
>|------|------|------|
>|unit-tests-java11|1|0|
>|unit-tests-java25|1|0|
>|unit-tests-java8|1|0|
>|unit-tests-java17|1|0|
>|unit-tests-java21|1|0|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## 3.3 #16170 +/- ##
=============================================
- Coverage 60.81% 36.95% -23.87%
+ Complexity 11765 11735 -30
=============================================
Files 1953 1952 -1
Lines 89118 89101 -17
Branches 13444 13361 -83
=============================================
- Hits 54197 32926 -21271
- Misses 29364 51584 +22220
+ Partials 5557 4591 -966
```
| [Flag](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [integration-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `32.14% <100.00%> (-0.03%)` | :arrow_down: |
| [integration-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `32.21% <100.00%> (-0.11%)` | :arrow_down: |
| [samples-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `32.11% <100.00%> (-0.08%)` | :arrow_down: |
| [samples-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `29.85% <100.00%> (+0.09%)` | :arrow_up: |
| [unit-tests-java11](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [unit-tests-java17](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [unit-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [unit-tests-java25](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [unit-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16170/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/dubbo/pull/16170?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org