Improve RulesEngine locking#2745
Draft
wborn wants to merge 1 commit into
Draft
Conversation
This prevents the issue that on systems with many attribute events hundreds of threads may get blocked by this synchronous block whenever a deployment runs. When the thread pool gets exhausted UIs will stop working as there won't be any threads available for handling WebSockets.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces lock contention in RulesEngine by limiting the time spent inside the synchronization block used to coordinate queued attribute state changes, aiming to prevent thread pool exhaustion during deployments on high event-rate systems.
Changes:
- Snapshot
insert/update/retractattribute-info queues under a short synchronized section. - Move
factsupdates and deployment notifications outside the synchronized block to avoid blocking producers.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+491
to
499
| synchronized (this.insertInfos) { | ||
| insertInfos = new HashSet<>(this.insertInfos); | ||
| updateInfos = new HashSet<>(this.updateInfos); | ||
| retractInfos = new HashSet<>(this.retractInfos); | ||
|
|
||
| retractInfos.forEach(attributeInfo -> { | ||
| facts.removeAssetState(attributeInfo); | ||
| // Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates | ||
| trackLocationPredicates(trackLocationPredicates || attributeInfo.getName().equals(Asset.LOCATION.getName())); | ||
| notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.DELETE, attributeInfo)); | ||
| }); | ||
|
|
||
| insertInfos.forEach(attributeInfo -> { | ||
| facts.putAssetState(attributeInfo); | ||
| // Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates | ||
| trackLocationPredicates(trackLocationPredicates || attributeInfo.getName().equals(Asset.LOCATION.getName())); | ||
| notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.CREATE, attributeInfo)); | ||
| }); | ||
|
|
||
| updateInfos.forEach(attributeInfo -> { | ||
| facts.putAssetState(attributeInfo); | ||
| notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.UPDATE, attributeInfo)); | ||
| }); | ||
|
|
||
| insertInfos.clear(); | ||
| updateInfos.clear(); | ||
| retractInfos.clear(); | ||
| this.insertInfos.clear(); | ||
| this.updateInfos.clear(); | ||
| this.retractInfos.clear(); | ||
| } |
Comment on lines
+486
to
+488
| Set<AttributeInfo> insertInfos; | ||
| Set<AttributeInfo> updateInfos; | ||
| Set<AttributeInfo> retractInfos; |
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.



This prevents the issue that on systems with many attribute events hundreds of threads may get blocked by this synchronous block whenever a deployment runs. When the thread pool gets exhausted UIs will stop working as there won't be any threads available for handling WebSockets.