pg.ddx.io  aws/aws-advanced-jdbc-wrapper GitHub issues and pull requests  
help / color / mirror / Atom feed
From: jaeyeonme (@jaeyeonme) <noreply+jaeyeonme@github.com>
To: aws/aws-advanced-jdbc-wrapper <noreply+aws-aws-advanced-jdbc-wrapper@github.com>
Subject: [aws/aws-advanced-jdbc-wrapper] PR #2241: fix: read CacheMap entries without taking the ConcurrentHashMap bin lock
Date: Mon, 16 Nov 2026 03:09:27 +0000
Message-ID: <gh-aws-aws-advanced-jdbc-wrapper-2241@github.com> (raw)

### 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.


view thread (2+ messages)  latest in thread

Message-ID: <gh-aws-aws-advanced-jdbc-wrapper-2241@github.com>
Permalink:  ../gh-aws-aws-advanced-jdbc-wrapper-2241@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