pg.ddx.io  aws/aws-advanced-jdbc-wrapper GitHub issues and pull requests  
help / color / mirror / Atom feed
[aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock
2+ messages / 1 participants
[nested] [flat]

* [aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock
@ 2026-11-16 03:09  "jaeyeonme (@jaeyeonme)" <noreply+jaeyeonme@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: jaeyeonme (@jaeyeonme) @ 2026-11-16 03:09 UTC (permalink / raw)
  To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>

### Summary

Threads reading the same live `CacheMap` entry no longer wait on each other: `CacheMap.get(K)` now reads with a plain `get()` and takes the `ConcurrentHashMap` bin lock only when the entry has expired.

Before this change every read went through `computeIfPresent`, which locks the bin even when nothing is written. The benchmark findings in #2074 measured the effect: "`CacheMap.get` degrades about 21x under four threads on a single hot key".

### Description

**Change**

- For an expired entry, `computeIfPresent` re-checks the current value under the lock, so a fresh entry stored by another thread in the meantime is kept and returned.
- I did not use `cache.remove(key, cacheItem)` for the expired case. `CacheItem.equals` compares only `item`, so it would also remove a fresh entry holding an equal value, which is common in `hostAvailabilityExpiringCache`.
- The unlocked check is safe because `CacheMap` never extends an item's expiration, so an item seen as expired stays expired until it is replaced. If `CacheMap` items ever become renewable, `expirationTimeNanos` would need to be volatile.

**Tests**

New `CacheMapTest`, in the style of `ExpirationCacheTest`. Besides the basic get, absent-key and expiry cases, one test reproduces the refresh race without threads: a `CacheItem` subclass stores a fresh, equal-valued item on its first `isExpired()` call. It fails with the `remove(key, cacheItem)` variant above and passes with this change.

**Measurements**

`StorageBenchmarks` from #2074, run alternately on the unmodified and patched builds (`-f 3`, two runs each, Apple M1 Pro, JDK 21). Score ± 99.9% error, ns/op:

| Benchmark | before | after |
|---|---|---|
| `contendedCacheMapGetHit` (4 threads) | 481.5 ± 95.2, 537.2 ± 23.2 | 208.0 ± 4.4, 209.8 ± 4.7 |
| `contendedExpirationCacheGetHit` (unchanged, reference) | 206.7 ± 7.2, 212.3 ± 5.3 | 208.1 ± 4.7, 209.1 ± 7.4 |

After the change `CacheMap` matches `ExpirationCache`, whose `get` already reads without the lock. On this machine that lock-free read costs about 208 ns with four threads, so the gain here is about 2.4x rather than the 21x reported in #2074.

In five `jstack` dumps taken while four threads read one key, two or three readers on the unmodified build were `BLOCKED` in `ConcurrentHashMap.computeIfPresent` called from `CacheMap.get`. With this change none were.

`util.storage` tests and the CI lint tasks (`checkstyleMain checkstyleTest spotbugsMain spotbugsTest`) pass locally.

### Additional Reviewers

None.

### By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


^ permalink  raw  [nested|flat] 2+ messages in thread

* Re: [aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock
@ 2026-11-20 05:03  "jaeyeonme (@jaeyeonme)" <noreply+jaeyeonme@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: jaeyeonme (@jaeyeonme) @ 2026-11-20 05:03 UTC (permalink / raw)
  To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>

@sergiyvamz this addresses the `CacheMap.get` contention your #2074 benchmarks measured (about 21x slower with four threads on one hot key). The CI workflows are still waiting for approval, so nothing has run yet. Could you approve them when you have a moment? If you'd rather discuss the approach in an issue first, I can open one.


^ permalink  raw  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-11-20 05:03 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 03:09 [aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock "jaeyeonme (@jaeyeonme)" <noreply+jaeyeonme@github.com>
2026-11-20 05:03 Re: [aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock "jaeyeonme (@jaeyeonme)" <noreply+jaeyeonme@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox