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
RuleService: preserve AttributeInfo ordering (#2678)
Eric Bariaux0631cb0e7b57
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex 0f302af4c..fba1c03d2 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -47,6 +47,7 @@ import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeInfo;+import org.openremote.model.attribute.AttributeRef;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.RulesetQuery;import org.openremote.model.query.filter.LocationAttributePredicate;@@ -130,11 +131,11 @@ public class RulesService extends RouteBuilder implements ContainerService {protected AssetLocationPredicateProcessor locationPredicateRulesConsumer;protected final Map<RulesEngine<?>, List<RulesEngine.AssetLocationPredicates>> engineAssetLocationPredicateMap = new ConcurrentHashMap<>();protected final Set<String> assetsWithModifiedLocationPredicates = new HashSet<>();- // Keep global list of asset states that have been pushed to any engines+ // Keep a keyed view of asset states that have been pushed to any engines.// The objects are already in memory inside the rule engines but keeping them- // here means we can quickly insert facts into newly started engines- protected final Set<AttributeEvent> attributeEvents = ConcurrentHashMap.newKeySet();- protected final Set<AttributeEvent> preInitAttributeEvents = new HashSet<>();+ // here means we can quickly insert facts into newly started engines.+ protected final Map<AttributeRef, AttributeEvent> attributeEventsByRef = new ConcurrentHashMap<>();+ protected final Map<AttributeRef, AttributeEvent> preInitAttributeEvents = new LinkedHashMap<>();protected long tempFactExpirationMillis;protected long quickFireMillis;protected boolean initDone;@@ -335,8 +336,12 @@ public class RulesService extends RouteBuilder implements ContainerService {assetEngines.values().forEach(RulesEngine::start);startDone = true;- preInitAttributeEvents.forEach(this::doProcessAttributeUpdate);- preInitAttributeEvents.clear();+ Map<AttributeRef, AttributeEvent> bufferedEvents;+ synchronized (preInitAttributeEvents) {+ bufferedEvents = new LinkedHashMap<>(preInitAttributeEvents);+ preInitAttributeEvents.clear();+ }+ bufferedEvents.values().forEach(this::doProcessAttributeUpdate);}}@@ -361,7 +366,10 @@ public class RulesService extends RouteBuilder implements ContainerService {globalEngine.set(null);}- attributeEvents.clear();+ attributeEventsByRef.clear();+ synchronized (preInitAttributeEvents) {+ preInitAttributeEvents.clear();+ }for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {geofenceAssetAdapter.stop(container);@@ -382,7 +390,7 @@ public class RulesService extends RouteBuilder implements ContainerService {*/public void onAttributeEvent(AttributeEvent event) throws AssetProcessingException {if (!startDone) {- preInitAttributeEvents.add(event);+ bufferPreInitAttributeEvent(event);} else {doProcessAttributeUpdate(event);}@@ -564,7 +572,7 @@ public class RulesService extends RouteBuilder implements ContainerService {if (isNewEngine) {// Push all existing facts into the engineRulesEngine<GlobalRuleset> finalEngine = engine;- attributeEvents.forEach(assetState -> finalEngine.insertOrUpdateAttributeInfo(assetState, true));+ getAttributeEvents().forEach(assetState -> finalEngine.insertOrUpdateAttributeInfo(assetState, true));}engine.addRuleset(ruleset);@@ -614,7 +622,7 @@ public class RulesService extends RouteBuilder implements ContainerService {// Push all existing facts into the engineRulesEngine<RealmRuleset> finalRealmRulesEngine = realmRulesEngine;- attributeEvents.forEach(assetState -> {+ getAttributeEvents().forEach(assetState -> {if (assetState.getRealm().equals(ruleset.getRealm())) {finalRealmRulesEngine.insertOrUpdateAttributeInfo(assetState, true);}@@ -724,11 +732,23 @@ public class RulesService extends RouteBuilder implements ContainerService {return;}- LOG.log(FINEST, () -> "Inserting attribute event: " + attributeEvent);+ final AttributeEvent[] previousEventHolder = new AttributeEvent[1];+ final boolean[] appliedHolder = new boolean[1];+ attributeEventsByRef.compute(attributeEvent.getRef(), (ref, currentEvent) -> {+ previousEventHolder[0] = currentEvent;+ if (isOlderThanCurrent(attributeEvent, currentEvent)) {+ LOG.log(FINEST, () -> "Ignoring attribute event older than current rule state: incoming=" + attributeEvent + ", current=" + currentEvent);+ return currentEvent;+ }- // Remove asset state with same attribute ref as new state, add new state- boolean inserted = !attributeEvents.remove(attributeEvent);- attributeEvents.add(attributeEvent);+ LOG.log(FINEST, () -> "Inserting attribute event: " + attributeEvent);+ appliedHolder[0] = true;+ return attributeEvent;+ });+ if (!appliedHolder[0]) {+ return;+ }+ boolean inserted = previousEventHolder[0] == null;// Get the chain of rule engines that we need to pass throughList<RulesEngine<?>> rulesEngines = getEnginesInScope(attributeEvent.getRealm(), attributeEvent.getPath());@@ -741,9 +761,7 @@ public class RulesService extends RouteBuilder implements ContainerService {protected void retractAttributeInfo(AttributeEvent attributeEvent) {LOG.log(FINEST, () -> "Retracting attribute event: " + attributeEvent);-- // Remove asset state with same attribute ref- attributeEvents.remove(attributeEvent);+ attributeEventsByRef.remove(attributeEvent.getRef());// Get the chain of rule engines that we need to pass throughList<RulesEngine<?>> rulesEngines = getEnginesInScope(attributeEvent.getRealm(), attributeEvent.getPath());@@ -755,12 +773,46 @@ public class RulesService extends RouteBuilder implements ContainerService {}protected List<AttributeInfo> getAssetStatesInScope(String assetId) {- return attributeEvents+ return getAttributeEvents().stream().filter(assetState -> Arrays.asList(assetState.getPath()).contains(assetId)).collect(Collectors.toList());}+ protected Collection<AttributeEvent> getAttributeEvents() {+ return attributeEventsByRef.values();+ }++ protected void bufferPreInitAttributeEvent(AttributeEvent attributeEvent) {+ synchronized (preInitAttributeEvents) {+ AttributeEvent currentEvent = preInitAttributeEvents.get(attributeEvent.getRef());+ if (!shouldReplaceBufferedPreInitEvent(currentEvent, attributeEvent)) {+ LOG.log(FINEST, () -> "Ignoring buffered pre-init attribute event older than current buffered state: incoming=" + attributeEvent + ", current=" + currentEvent);+ return;+ }+ preInitAttributeEvents.put(attributeEvent.getRef(), attributeEvent);+ }+ }++ protected boolean isOlderThanCurrent(AttributeEvent incomingEvent, AttributeEvent currentEvent) {+ return currentEvent != null && incomingEvent.getTimestamp() < currentEvent.getTimestamp();+ }++ protected boolean shouldReplaceBufferedPreInitEvent(AttributeEvent currentEvent, AttributeEvent incomingEvent) {+ if (currentEvent == null) {+ return true;+ }++ boolean currentInserts = isRuleState(currentEvent) && !currentEvent.isDeleted();+ boolean incomingInserts = isRuleState(incomingEvent) && !incomingEvent.isDeleted();++ if (currentInserts && incomingInserts) {+ return !isOlderThanCurrent(incomingEvent, currentEvent);+ }++ return true;+ }+protected List<RulesEngine<?>> getEnginesInScope(String realm, String[] assetPath) {List<RulesEngine<?>> rulesEngines = new ArrayList<>();diff --git a/test/src/test/groovy/org/openremote/test/rules/RulesServiceStateOrderingTest.groovy b/test/src/test/groovy/org/openremote/test/rules/RulesServiceStateOrderingTest.groovynew file mode 100644index 000000000..96e96ea53--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/rules/RulesServiceStateOrderingTest.groovy@@ -0,0 +1,277 @@+/*+ * Copyright 2026, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.test.rules++import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.rules.RulesEngine+import org.openremote.manager.rules.RulesService+import org.openremote.manager.rules.RulesetStorageService+import org.openremote.model.Constants+import org.openremote.model.asset.impl.ThingAsset+import org.openremote.model.attribute.Attribute+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.attribute.AttributeRef+import org.openremote.model.attribute.MetaItem+import org.openremote.model.attribute.MetaMap+import org.openremote.model.rules.RealmRuleset+import org.openremote.model.rules.Ruleset+import org.openremote.model.util.UniqueIdentifierGenerator+import org.openremote.model.value.MetaItemType+import org.openremote.model.value.ValueType+import org.openremote.test.ManagerContainerTrait+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++class RulesServiceStateOrderingTest extends Specification implements ManagerContainerTrait {++ def "pre-init buffer should keep the newest rule state event for the same attribute"() {+ given:+ def rulesService = new RulesService()+ def ref = new AttributeRef(UniqueIdentifierGenerator.generateId("Buffered state asset"), "count")+ def ruleStateMeta = new MetaMap([new MetaItem<>(MetaItemType.RULE_STATE)])++ and:+ def olderEvent = new AttributeEvent(ref, 1, 1000L)+ .setMeta(ruleStateMeta)+ .setRealm(Constants.MASTER_REALM)+ def newerEvent = new AttributeEvent(ref, 2, 2000L)+ .setMeta(ruleStateMeta)+ .setRealm(Constants.MASTER_REALM)++ when:+ rulesService.onAttributeEvent(olderEvent)+ rulesService.onAttributeEvent(newerEvent)++ then:+ rulesService.preInitAttributeEvents.size() == 1+ def bufferedEvent = rulesService.preInitAttributeEvents.get(ref)+ bufferedEvent != null+ bufferedEvent.timestamp == 2000L+ bufferedEvent.value.get() == 2+ }++ def "pre-init buffer should keep a later retract even when its value timestamp is older"() {+ given:+ def rulesService = new RulesService()+ def ref = new AttributeRef(UniqueIdentifierGenerator.generateId("Buffered retract asset"), "count")+ def ruleStateMeta = new MetaMap([new MetaItem<>(MetaItemType.RULE_STATE)])++ and:+ def newerInsert = new AttributeEvent(ref, 2, 2000L)+ .setMeta(ruleStateMeta)+ .setRealm(Constants.MASTER_REALM)+ def laterRetract = new AttributeEvent(ref, null, 1000L)+ .setMeta(ruleStateMeta)+ .setRealm(Constants.MASTER_REALM)+ .setDeleted(true)++ when:+ rulesService.onAttributeEvent(newerInsert)+ rulesService.onAttributeEvent(laterRetract)++ then:+ rulesService.preInitAttributeEvents.size() == 1+ def bufferedEvent = rulesService.preInitAttributeEvents.get(ref)+ bufferedEvent != null+ bufferedEvent.deleted+ bufferedEvent.timestamp == 1000L+ }++ @SuppressWarnings("GroovyAccessibility")+ def "rules service should keep the newest rule state when committed events arrive out of order"() {+ given: "the container is started"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)+ def container = startContainer(defaultConfig(), defaultServices())+ def rulesService = container.getService(RulesService.class)+ def assetStorageService = container.getService(AssetStorageService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)++ and: "a rule-state asset exists"+ def asset = new ThingAsset("Ordered state asset")+ .setId(UniqueIdentifierGenerator.generateId("Ordered state asset"))+ .setRealm(Constants.MASTER_REALM)+ .addAttributes(+ new Attribute<>("count", ValueType.INTEGER, 0)+ .addMeta(new MetaItem<>(MetaItemType.RULE_STATE))+ )+ asset = assetStorageService.merge(asset)++ expect: "the rule-state cache contains the asset attribute"+ conditions.eventually {+ def ruleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ assert ruleState != null+ assert ruleState.timestamp > 0+ }++ when: "a realm ruleset is deployed"+ def ruleset = new RealmRuleset(+ Constants.MASTER_REALM,+ "Rule state ordering",+ Ruleset.Lang.GROOVY,+ getClass().getResource("/org/openremote/test/rules/BasicMatchAllAssetStates.groovy").text+ )+ rulesetStorageService.merge(ruleset)+ RulesEngine engine = null++ then: "the rules engine starts with the cached attribute fact"+ conditions.eventually {+ engine = rulesService.realmEngines.get(Constants.MASTER_REALM)+ assert engine != null+ assert engine.isRunning()+ assert engine.facts.assetStates.find { it.id == asset.id && it.name == "count" } != null+ }++ when: "committed rule-state events for the same attribute arrive out of timestamp order"+ def storedAsset = assetStorageService.find(asset.id, true) as ThingAsset+ def storedAttribute = storedAsset.getAttribute("count").get()+ def initialRuleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ long initialTimestamp = initialRuleState.timestamp+ long newerTimestamp = initialTimestamp + 2000+ long olderTimestamp = initialTimestamp + 1000++ def newerEvent = new AttributeEvent(+ storedAsset,+ storedAttribute,+ getClass().getSimpleName(),+ 2,+ newerTimestamp,+ storedAttribute.getValue().orElse(null),+ initialTimestamp+ )+ def olderEvent = new AttributeEvent(+ storedAsset,+ storedAttribute,+ getClass().getSimpleName(),+ 1,+ olderTimestamp,+ storedAttribute.getValue().orElse(null),+ initialTimestamp+ )++ rulesService.onAttributeEvent(newerEvent)+ rulesService.onAttributeEvent(olderEvent)++ then: "the cached rule state should still keep the newer timestamp and latest value"+ def latestRuleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ assert latestRuleState != null+ assert latestRuleState.timestamp == newerTimestamp+ assert latestRuleState.value.get() == 2++ and: "the rule engine fact should also remain on the newer timestamp and latest value"+ conditions.eventually {+ def engineState = engine.facts.assetStates.find { it.id == asset.id && it.name == "count" }+ assert engineState != null+ assert engineState.timestamp == newerTimestamp+ assert engineState.value.get() == 2+ }++ cleanup:+ stopContainer()+ }++ @SuppressWarnings("GroovyAccessibility")+ def "rules service should still retract a cached rule state when a later retract arrives with an older value timestamp"() {+ given: "the container is started"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)+ def container = startContainer(defaultConfig(), defaultServices())+ def rulesService = container.getService(RulesService.class)+ def assetStorageService = container.getService(AssetStorageService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)++ and: "a rule-state asset exists"+ def asset = new ThingAsset("Retract state asset")+ .setId(UniqueIdentifierGenerator.generateId("Retract state asset"))+ .setRealm(Constants.MASTER_REALM)+ .addAttributes(+ new Attribute<>("count", ValueType.INTEGER, 0)+ .addMeta(new MetaItem<>(MetaItemType.RULE_STATE))+ )+ asset = assetStorageService.merge(asset)++ expect: "the initial rule state is cached"+ conditions.eventually {+ def ruleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ assert ruleState != null+ assert ruleState.timestamp > 0+ }++ when: "a realm ruleset is deployed"+ def ruleset = new RealmRuleset(+ Constants.MASTER_REALM,+ "Rule state retract ordering",+ Ruleset.Lang.GROOVY,+ getClass().getResource("/org/openremote/test/rules/BasicMatchAllAssetStates.groovy").text+ )+ rulesetStorageService.merge(ruleset)+ RulesEngine engine = null++ then: "the rules engine starts with the cached attribute fact"+ conditions.eventually {+ engine = rulesService.realmEngines.get(Constants.MASTER_REALM)+ assert engine != null+ assert engine.isRunning()+ assert engine.facts.assetStates.find { it.id == asset.id && it.name == "count" } != null+ }++ when: "a newer committed state is cached first"+ def storedAsset = assetStorageService.find(asset.id, true) as ThingAsset+ def storedAttribute = storedAsset.getAttribute("count").get()+ def initialRuleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ long initialTimestamp = initialRuleState.timestamp+ long newerTimestamp = initialTimestamp + 2000+ long olderTimestamp = initialTimestamp + 1000++ def newerEvent = new AttributeEvent(+ storedAsset,+ storedAttribute,+ getClass().getSimpleName(),+ 2,+ newerTimestamp,+ storedAttribute.getValue().orElse(null),+ initialTimestamp+ )+ rulesService.onAttributeEvent(newerEvent)++ and: "a delete for the same rule state arrives afterwards carrying an older value timestamp"+ def olderRetractEvent = new AttributeEvent(+ storedAsset,+ storedAttribute,+ getClass().getSimpleName(),+ null,+ olderTimestamp,+ storedAttribute.getValue().orElse(null),+ initialTimestamp+ ).setDeleted(true)+ rulesService.onAttributeEvent(olderRetractEvent)++ then: "the cached rule state should be removed"+ def latestRuleState = rulesService.attributeEvents.find { it.id == asset.id && it.name == "count" }+ assert latestRuleState == null++ and: "the rule engine fact should also be removed"+ conditions.eventually {+ def engineState = engine.facts.assetStates.find { it.id == asset.id && it.name == "count" }+ assert engineState == null+ }++ cleanup:+ stopContainer()+ }+}