feat(#4850): implement thread-safe caching with ConcurrentCache class#4855
Conversation
📝 WalkthroughWalkthroughThis PR addresses thread-safety concerns in the Cache class by introducing a new ConcurrentCache wrapper that coordinates access using per-tail locks, enabling concurrent operations across different tails while serializing operations for the same tail. Cache documentation is updated to recommend ConcurrentCache for concurrent scenarios, and a concurrent test validates the locking behavior. Changes
Sequence DiagramsequenceDiagram
participant Client
participant ConcurrentCache
participant LockMap as Per-Tail Lock Map
participant Cache
par Concurrent Threads
Client->>ConcurrentCache: apply(source, target, tail1)
Client->>ConcurrentCache: apply(source, target, tail1)
Client->>ConcurrentCache: apply(source, target, tail2)
end
ConcurrentCache->>LockMap: computeIfAbsent(tail1)
LockMap-->>ConcurrentCache: lock1
ConcurrentCache->>ConcurrentCache: synchronized(lock1)
ConcurrentCache->>Cache: apply(source, target, tail1.normalize())
Cache-->>ConcurrentCache: done
par Other Thread
ConcurrentCache->>LockMap: computeIfAbsent(tail2)
LockMap-->>ConcurrentCache: lock2
ConcurrentCache->>ConcurrentCache: synchronized(lock2)
ConcurrentCache->>Cache: apply(source, target, tail2.normalize())
Cache-->>ConcurrentCache: done
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🚀 Performance AnalysisAll benchmarks are within the acceptable range. No critical degradation detected (threshold is 100%). Please refer to the detailed report for more information. Click to see the detailed report
✅ Performance gain: |
There was a problem hiding this comment.
Pull request overview
This PR introduces a ConcurrentCache class to provide thread-safe caching functionality, addressing issues #4846 and #4850 where the existing Cache class lacked thread-safety mechanisms. The implementation uses a per-key locking strategy with ConcurrentHashMap to prevent race conditions when multiple threads access the same cache entries concurrently.
Changes:
- Added
ConcurrentCacheclass that wrapsCachewith thread-safe synchronization - Added comprehensive test coverage for concurrent cache behavior
- Added additional tests for
Cacheto verify behavior with source changes and cache misses - Removed puzzle comments from
Cacheclass documentation - Updated
Cachedocumentation to referenceConcurrentCachefor concurrent scenarios
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| eo-maven-plugin/src/main/java/org/eolang/maven/ConcurrentCache.java | New thread-safe wrapper class implementing per-key locking for cache operations |
| eo-maven-plugin/src/main/java/org/eolang/maven/Cache.java | Updated documentation to clarify thread-safety and removed resolved puzzle comments |
| eo-maven-plugin/src/test/java/org/eolang/maven/ConcurrentCacheTest.java | New test verifying concurrent cache access compiles source only once |
| eo-maven-plugin/src/test/java/org/eolang/maven/CacheTest.java | Added tests for cache invalidation on source changes, cache miss scenarios, and SHA-256 hash validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…issues with the Cachec
0a5dfc3 to
561e5b9
Compare
|
@yegor256 could you have a look, please? |
|
@volodya-lombrozo Thanks for the contribution! You've earned +12 points for this: +16 as a basis; +5.8 for hits-of-code (116 × 0.05, capped at 16); -4 for exceeding 100 hits-of-code; -5.8 for excessive review comments. Please, keep them coming. Your running score is +336; don't forget to check your Zerocracy account too. |
This PR introduces a
ConcurrentCacheclass to ensure thread-safety for theCachemechanism, resolving issue #4846.Fixes #4850
Summary by CodeRabbit
Documentation
New Features
Tests