100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Upgraded Drools and used atomic operation to insert/update AssetState in sessions
Christian Bauer73bd61626e7e
diff --git a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java b/agent/src/main/java/org/openremote/agent/protocol/Protocol.javaindex e78b2555f..d8573dadd 100644--- a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java@@ -34,7 +34,6 @@ import org.openremote.model.attribute.*;import org.openremote.model.value.ArrayValue;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.ValueType;-import sun.plugin2.message.Message;import java.io.IOException;import java.util.ArrayList;diff --git a/gradle.properties b/gradle.propertiesindex 35aef2f15..b5c6ad12a 100644--- a/gradle.properties+++ b/gradle.properties@@ -27,7 +27,7 @@ objenesisVersion = 2.2camelVersion = 2.19.0tyrusVersion = 1.10sunriseSunsetCalculatorVersion = 1.2-droolsVersion = 6.5.0.Final+droolsVersion = 7.5.0.FinalquartzVersion= 2.2.3calimeroVersion = 2.4-SNAPSHOTclingVersion = 2.1.1diff --git a/manager/client/src/main/webapp/css/style.html b/manager/client/src/main/webapp/css/style.htmlindex 7948a70aa..4f9523542 100644--- a/manager/client/src/main/webapp/css/style.html+++ b/manager/client/src/main/webapp/css/style.html@@ -634,8 +634,8 @@}.or-JsonEditor {- width: 48em;- height: 30em;+ width: 55em;+ height: 40em;}.or-AdminNotificationEditor {diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.javaindex e70ae46f4..c77339132 100644--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java@@ -437,12 +437,7 @@ public class RulesEngine<T extends Ruleset> {// Insert initial asset statestry {- Set<AssetState> initialState = new HashSet<>(assetStates.keySet());- LOG.info("On " + this + ", inserting initial asset states: " + initialState.size());- for (AssetState assetState : initialState) {- insertAssetState(assetState);- }- LOG.info("On " + this + ", inserted initial asset states: " + initialState.size());+ insertInitialAssetStates();} catch (Exception ex) {// This is called in a background timer thread, we must log here or the exception is swallowedLOG.log(Level.SEVERE, "On " + this + ", inserting initial asset states failed", ex);@@ -495,28 +490,72 @@ public class RulesEngine<T extends Ruleset> {}}- protected synchronized void insertAssetState(AssetState newAssetState) {- FactHandle factHandle = insertIntoSession(newAssetState);- assetStates.put(newAssetState, factHandle);+ protected synchronized void insertInitialAssetStates() {+ synchronized (assetStates) {+ LOG.info("On " + this + ", inserting initial asset states: " + assetStates.size());+ for (AssetState assetState : new HashSet<>(assetStates.keySet())) {+ updateAssetState(assetState, true);+ }+ LOG.info("On " + this + ", inserted initial asset states: " + assetStates.size());+ }}-protected synchronized void updateAssetState(AssetState assetState) {- // Check if fact already exists using equals()- if (!assetStates.containsKey(assetState)) {- // Delete any existing fact for this attribute ref- // Put the fact into working memory and store the handle- retractAssetState(assetState);+ updateAssetState(assetState, false);+ }- if (isRunning()) {- insertAssetState(assetState);- } else {+ protected synchronized void updateAssetState(AssetState assetState, boolean skipDirtyCheck) {+ synchronized (assetStates) {++ // Check if fact already exists using equals(), this will deduplicate asset state writes+ if (!skipDirtyCheck && assetStates.containsKey(assetState)) {+ LOG.finest("On " + this + ", update is the same as existing, ignoring: " + assetState);+ return;+ }++ LOG.fine("On " + this + ", updating: " + assetState);++ // Get the old asset state+ AssetState oldAssetState = assetStates.keySet()+ .stream()+ .filter(au -> au.attributeRefsEqual(assetState))+ .findFirst()+ .orElse(null);++ // Always remove the old asset state and get its fact handle+ FactHandle oldFactHandle = oldAssetState != null ? assetStates.remove(oldAssetState) : null;++ if (!isRunning()) {+ LOG.fine("On " + this + ", engine is in error state or not running, storing for later update: " + assetState);assetStates.put(assetState, null);+ return;}++ deleteAndInsertInKnowledgeSession(oldFactHandle, assetState, factHandle -> {+ // Store the new asset state and the fact handle+ synchronized (assetStates) {+ assetStates.put(assetState, factHandle);+ }+ });}}- protected synchronized void retractAssetState(AssetState assetState) {+ protected synchronized void deleteAndInsertInKnowledgeSession(FactHandle oldFactHandle,+ AssetState assetState,+ Consumer<FactHandle> factHandleConsumer) {+ // Atomically delete the old fact and insert a new fact+ knowledgeSession.submit(session -> {+ if (oldFactHandle != null) {+ session.delete(oldFactHandle);+ }+ FactHandle factHandle = session.insert(assetState);+ if (factHandleConsumer != null) {+ factHandleConsumer.accept(factHandle);+ }+ });+ }+ protected synchronized void retractAssetState(AssetState assetState) {+ LOG.fine("On " + this + ", retracting: " + assetState);// If there already is a fact in working memory for this attribute then delete itAssetState update = assetStates.keySet().stream()@@ -540,7 +579,11 @@ public class RulesEngine<T extends Ruleset> {}protected synchronized void insertAssetEvent(long expirationOffset, AssetEvent assetEvent) {- FactHandle factHandle = insertIntoSession(assetEvent);+ if (!isRunning()) {+ LOG.fine("On " + this + ", engine is in error state or not running, ignoring: " + assetEvent);+ return;+ }+ FactHandle factHandle = knowledgeSession.insert(assetEvent);if (factHandle != null) {scheduleExpiration(assetEvent, factHandle, expirationOffset);}@@ -549,23 +592,6 @@ public class RulesEngine<T extends Ruleset> {}}- protected synchronized FactHandle insertIntoSession(AbstractAssetUpdate update) {- if (!isRunning()) {- LOG.fine("On " + this + ", engine is in error state or not running, ignoring: " + update);- return null;- }- FactHandle fh = knowledgeSession.insert(update);- LOG.fine("On " + this + ", fact count after insert: " + knowledgeSession.getFactCount());- /* TODO Not synchronous with actual session state due to active mode, and a bit too much output?- if (LOG.isLoggable(Level.FINEST)) {- for (FactHandle factHandle : knowledgeSession.getFactHandles()) {- LOG.fine("Fact: " + knowledgeSession.getObject(factHandle));- }- }- */- return fh;- }-protected Assets createAssetsFacade() {return new Assets() {@Overridediff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.javaindex d958b3dbb..cf8904956 100644--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java@@ -28,6 +28,8 @@ import java.util.Optional;*/@org.kie.api.definition.type.Role(Role.Type.EVENT)@org.kie.api.definition.type.Timestamp("valueTimestamp")+// TODO Bug in drools, it fails if there is no @Expires annotation https://issues.jboss.org/browse/DROOLS-2182[email protected](value = "10000d")public class AssetEvent extends AbstractAssetUpdate {public AssetEvent(AbstractAssetUpdate that) {