Repository navigation
Create each endpoint's JedisPool once under concurrent first use - #407
Merged
Merged
Conversation
ConnectionPool.connect called getOrElseUpdate on its ConcurrentHashMap through JavaConversions, which wraps it in a JConcurrentMapWrapper. On Scala 2.12 that method is concurrent.Map.getOrElseUpdate: get, then run the factory, then putIfAbsent. Every caller agrees on one pool, but each task that reaches a new endpoint at the same moment builds its own JedisPool first. The pools that lose the putIfAbsent are dropped without close(), so they stay registered in JMX (which keeps them reachable) with their evictor running for the life of the executor. With 16 threads racing on one endpoint, 16 pools were built and 15 leaked. Use ConcurrentHashMap.computeIfAbsent, which runs the factory at most once per key, behind a plain get so the common case stays lock-free on Java 8 (where computeIfAbsent locks even when the key is present). Pool creation moves into its own method, poolFor is private[redis] so it can be tested, and the JavaConversions import is gone. ConnectionPoolThreadSafetyTest needs no Redis server: it races 16 threads on fresh endpoints and checks they share one pool and that only one pool was registered in JMX. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #90.
Problem
ConnectionPool.connectusedpools.getOrElseUpdate(re, { new JedisPool(...) })throughJavaConversions. On Scala 2.12 the implicit conversion gives aconcurrent.Mapwrapper, and itsgetOrElseUpdateisget, then build the value, thenputIfAbsent. The map stays consistent and every caller gets the same pool. But each thread that misses thegetruns the factory, so when several tasks in one executor reach a new endpoint at once, severalJedisPools are built and all but one are dropped withoutclose().The dropped pools never served a connection, so no connections leak. But each one stays registered in JMX (commons-pool2 enables JMX by default) and keeps its evictor task running every 30s for the life of the JVM. On Scala 2.11, which the reporter used,
getOrElseUpdatewasget+update(last writer wins), so callers could also get different pools.Fix
poolFor(re)does a lock-freepools.get(re)first. On a miss it callspools.computeIfAbsent(re, createPool), which runscreatePoolat most once per endpoint.getkeeps the common path lock-free; on Java 8,computeIfAbsentlocks the bin even when the key exists.createPool.Tests
ConnectionPoolThreadSafetyTest(no Redis needed): 16 threads racepoolForon 10 fresh endpoints. The test asserts they all get the same instance and that exactly one pool per endpoint is registered in JMX.getOrElseUpdatelogic it fails:16 was not equal to 1.connect()concurrently, then SET and PING. All succeed through one pool.make startsetup. CI covers them.Making the pool size configurable (also mentioned in #90) is left as a separate feature request.
🤖 Generated with Claude Code