Repository navigation
Cache entity codecs in a ConcurrentHashMap (plan item 2.8) - #4397
Merged
Merged
Conversation
MorphiaCodecProvider cached codecs in a plain HashMap that concurrent lookups mutated, and each thread that missed built and stored its own codec. It now uses a ConcurrentHashMap with putIfAbsent, so every caller shares the first codec stored. Not computeIfAbsent, because building a codec looks up other codecs that can land back in this provider.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused change safely handles concurrent cache insertion without restricting recursive construction, and no blocking issues remain.
0 open findings
What changed in this PR
Makes Morphia’s entity codec cache thread-safe while preserving recursive codec construction.
Changes:
- Uses
ConcurrentHashMapandputIfAbsentto return one cached codec per type. - Adds concurrent lookup coverage for both mappers.
- Marks plan item 2.8 complete.
| File | Description |
|---|---|
| design/mapping-game-plan.md | Marks the cache concurrency fix complete. |
| core/src/test/java/dev/morphia/test/mapping/codec/harness/CodecHarnessTest.java | Tests shared codec identity across concurrent lookups. |
| core/src/main/java/dev/morphia/mapping/codec/MorphiaCodecProvider.java | Atomically caches codecs and returns the winning instance. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Plan item 2.8 (review D8). This completes the Phase 2 fixes. 2.5 ships with 3.8, as the plan says.
The bug
MorphiaCodecProvider.codecswas a plainHashMap, andgetmutated it on every miss. With concurrent first use of an entity type, aHashMapcan be corrupted. Even when it isn't, every thread that missed built its ownMorphiaCodecand stored it, overwriting the others, so callers ended up holding different codec instances for the same type.Change
codecsis aConcurrentHashMap. On a miss,getbuilds the codec and stores it withputIfAbsent. If another thread stored one first, it returns that one, so every caller shares a single codec per type.It doesn't use
computeIfAbsent, as the plan says: building a codec looks up other codecs, and those lookups can land back in this provider, which isn't allowed inside aConcurrentHashMapcompute.Tests
CodecHarnessTest.concurrentCodecLookupsShareOneCodecruns under both mappers. For 20 fresh providers, 8 threads released together callprovider.get(HarnessScalars.class, registry), and the test asserts that all of them get the same codec. It failed before the fix: "REFLECTION: every lookup should get the one cached codec".The core and Kotlin suites pass with
-Dmorphia.mapper=critterand with reflection: 1483 + 5 tests, 0 failures.Decisions to review