[MNG-8736] Fix concurrency issue in model building with profile activation by gnodet · Pull Request #2378 · apache/maven (original) (raw)
This commit addresses multiple critical issues in the profile activation context recording and caching mechanism during model inheritance:
Issues Fixed:
Profile Activation Context Recording Bug: When there was a cache hit in readAsParentModel(), the keys accessed during cache matching re-evaluation were lost because the recording context was discarded without merging the recorded keys into the parent context.
Recording Context Isolation: Parent model processing was polluting the child's recording context with intermediate keys that weren't essential to the final result, leading to noisy cache keys and reduced cache effectiveness.
Profile Injection Precedence: Profile properties from distant ancestors were incorrectly overriding direct properties from closer ancestors due to improper profile injection timing during inheritance assembly.
Solutions Implemented:
Cache Hit Recording Fix:
- Create a separate recording context for cache matching re-evaluation
- Replay cached record keys into the parent recording context on cache hits
- Ensure all essential keys are captured regardless of cache hit/miss
Recording Context Isolation:
- Use separate recording contexts for parent model processing
- Only replay essential keys from the final result into parent context
- Maintain clean, precise cache keys for better performance
Profile Injection Timing:
- Inject profiles into child models using parent's profile list
- Ensure proper precedence where child elements override parent elements
- Maintain correct inheritance hierarchy during profile activation
Technical Details:
- Made DefaultProfileActivationContext.Record fields package-private for merging
- Added replayRecordIntoContext() method for proper key propagation
- Updated profile injection to respect inheritance precedence rules
- Enhanced MavenModelMerger to handle profile content merging correctly
Benefits:
- Eliminates race conditions in concurrent model building scenarios
- Improves cache effectiveness through cleaner, more precise cache keys
- Ensures correct profile activation context propagation through inheritance
- Maintains proper property precedence in complex inheritance hierarchies
- Reduces memory usage by avoiding over-recording of intermediate keys
This fix ensures that profile activation behaves consistently and correctly in both single-threaded and multi-threaded environments, while maintaining optimal performance through improved caching mechanisms.