Skip to content

Improve RulesEngine locking#2745

Draft
wborn wants to merge 1 commit into
masterfrom
improve-rulesengine-locking
Draft

Improve RulesEngine locking#2745
wborn wants to merge 1 commit into
masterfrom
improve-rulesengine-locking

Conversation

@wborn
Copy link
Copy Markdown
Member

@wborn wborn commented May 12, 2026

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.

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.
Copilot AI review requested due to automatic review settings May 12, 2026 10:20
@wborn wborn added the Enhancement Improvement of an existing feature label May 12, 2026
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/retract attribute-info queues under a short synchronized section.
  • Move facts updates 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;
@wborn wborn marked this pull request as draft May 12, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants