Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Improve DefaultMQTTHandler multithreading and test reliability (#2265)

Rich Turnerbf5896c9347f
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
index 24e26ec95..28c95f0d0 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
@@ -42,6 +42,7 @@ import org.openremote.model.util.ValueUtil;
import java.nio.charset.StandardCharsets;
import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -68,7 +69,7 @@ public class DefaultMQTTHandler extends MQTTHandler {
public static final String ATTRIBUTE_VALUE_WRITE_TOPIC = "writeattributevalue";
public static final String ATTRIBUTE_WRITE_TOPIC = "writeattribute";
private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);
- final protected Map<String, Map<String, Consumer<? extends Event>>> sessionSubscriptionConsumers = new HashMap<>();
+ final protected Map<String, Map<String, Consumer<? extends Event>>> sessionSubscriptionConsumers = new ConcurrentHashMap<>();
// An authorisation cache for publishing
// TODO: Switch to caffeine library once ActiveMQ has migrated
protected final Cache<String, ConcurrentHashSet<String>> authorizationCache = CacheBuilder.newBuilder()
@@ -98,12 +99,10 @@ public class DefaultMQTTHandler extends MQTTHandler {
super.onDisconnect(connection);
String sessionKey = getSessionKey(connection);
LOG.log(Level.FINER, "Removing subscriptions for connection: " + connectionToString(connection));
- synchronized (sessionSubscriptionConsumers) {
- sessionSubscriptionConsumers.computeIfPresent(sessionKey, (s, subscriptionConsumers) -> {
- subscriptionConsumers.forEach((subscriptionKey, consumer) -> clientEventService.removeSubscription(consumer));
- return null;
- });
- }
+ sessionSubscriptionConsumers.computeIfPresent(sessionKey, (s, subscriptionConsumers) -> {
+ subscriptionConsumers.forEach((subscriptionKey, consumer) -> clientEventService.removeSubscription(consumer));
+ return null;
+ });
authorizationCache.invalidate(getConnectionIDString(connection));
}
@@ -112,12 +111,10 @@ public class DefaultMQTTHandler extends MQTTHandler {
super.onConnectionLost(connection);
String sessionKey = getSessionKey(connection);
LOG.log(Level.FINER, "Removing subscriptions for connection: " + connectionToString(connection));
- synchronized (sessionSubscriptionConsumers) {
- sessionSubscriptionConsumers.computeIfPresent(sessionKey, (s, subscriptionConsumers) -> {
- subscriptionConsumers.forEach((subscriptionKey, consumer) -> clientEventService.removeSubscription(consumer));
- return null;
- });
- }
+ sessionSubscriptionConsumers.computeIfPresent(sessionKey, (s, subscriptionConsumers) -> {
+ subscriptionConsumers.forEach((subscriptionKey, consumer) -> clientEventService.removeSubscription(consumer));
+ return null;
+ });
authorizationCache.invalidate(getConnectionIDString(connection));
}
@@ -309,13 +306,11 @@ public class DefaultMQTTHandler extends MQTTHandler {
Consumer<Event> consumer = getSubscriptionEventConsumer(connection, topic);
- synchronized (sessionSubscriptionConsumers) {
- // Create subscription consumer and track it for future removal requests
- Map<String, Consumer<? extends Event>> subscriptionConsumers = sessionSubscriptionConsumers.computeIfAbsent(sessionKey, (s) -> new HashMap<>());
- subscriptionConsumers.put(topic.toString(), consumer);
- clientEventService.addSubscription(subscription, consumer);
- LOG.finest(() -> "Client event subscription created for topic '" + topic + "': " + connectionToString(connection));
- }
+ // Create subscription consumer and track it for future removal requests
+ Map<String, Consumer<? extends Event>> subscriptionConsumers = sessionSubscriptionConsumers.computeIfAbsent(sessionKey, (s) -> new ConcurrentHashMap<>());
+ subscriptionConsumers.put(topic.toString(), consumer);
+ clientEventService.addSubscription(subscription, consumer);
+ LOG.finest(() -> "Client event subscription created for topic '" + topic + "': " + connectionToString(connection));
}
@Override
@@ -323,18 +318,16 @@ public class DefaultMQTTHandler extends MQTTHandler {
String subscriptionId = topic.toString();
String sessionKey = getSessionKey(connection);
- synchronized (sessionSubscriptionConsumers) {
- sessionSubscriptionConsumers.computeIfPresent(sessionKey, (connectionID, subscriptionConsumers) -> {
- Consumer<? extends Event> consumer = subscriptionConsumers.remove(subscriptionId);
- if (consumer != null) {
- clientEventService.removeSubscription(consumer);
- }
- if (subscriptionConsumers.isEmpty()) {
- return null;
- }
- return subscriptionConsumers;
- });
- }
+ sessionSubscriptionConsumers.computeIfPresent(sessionKey, (connectionID, subscriptionConsumers) -> {
+ Consumer<? extends Event> consumer = subscriptionConsumers.remove(subscriptionId);
+ if (consumer != null) {
+ clientEventService.removeSubscription(consumer);
+ }
+ if (subscriptionConsumers.isEmpty()) {
+ return null;
+ }
+ return subscriptionConsumers;
+ });
}
@Override
diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
index 0b83e471d..c04141af0 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
@@ -291,7 +291,6 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
and: "the container starts"
-
def assetStorageService = container.getService(AssetStorageService.class)
def assetProcessingService = container.getService(AssetProcessingService.class)
def agentService = container.getService(AgentService.class)
@@ -396,6 +395,17 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert connection != null
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
+
+ cleanup: "the agent and assets are deleted"
+ if (asset != null) {
+ assetStorageService.delete([asset.id])
+ }
+ if (agent != null) {
+ assetStorageService.delete([agent.id])
+ }
+ conditions.eventually {
+ assert brokerService.getUserConnections(keycloakTestSetup.serviceUser.id).isEmpty()
+ }
}
def "Check MQTT client protocol message match filter support"() {
@@ -519,6 +529,21 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert asset.getAttribute("temperature").flatMap { it.value }.map { it == 21.5 }.orElse(false)
assert asset.getAttribute("switchStatus").flatMap { it.value }.map { it == false }.orElse(false)
}
+
+
+ cleanup: "the agent and assets are deleted"
+ if (asset != null) {
+ assetStorageService.delete([asset.id])
+ }
+ if (testThing != null) {
+ assetStorageService.delete([testThing.id])
+ }
+ if (agent != null) {
+ assetStorageService.delete([agent.id])
+ }
+ conditions.eventually {
+ assert brokerService.getUserConnections(keycloakTestSetup.serviceUser.id).isEmpty()
+ }
}
def "Check MQTT client protocol wildcard subscriptions"() {
@@ -644,7 +669,7 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert protocol.wildcardTopicConsumerMap.get(wildcardTopic2).size() == 1
assert protocol.wildcardTopicConsumerMap.get(wildcardTopic2).containsKey(pressureTopic)
assert protocol.wildcardTopicConsumerMap.get(wildcardTopic2).get(pressureTopic).size() == 1
- def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
+ def connection = mqttBrokerService.getConnectionFromClientID(mqttAgentClientId)
assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 3
}
@@ -712,6 +737,26 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert asset.getAttribute("pressure").flatMap { it.value }.map { it == 1000 }.orElse(false)
assert asset.getAttribute("uvIndex").flatMap { it.value }.map { it == 4.0 }.orElse(false)
}
+
+ cleanup: "the agent and assets are deleted"
+ if (testThing1 != null) {
+ assetStorageService.delete([testThing1.id])
+ }
+ if (testThing2 != null) {
+ assetStorageService.delete([testThing2.id])
+ }
+ if (testThing3 != null) {
+ assetStorageService.delete([testThing3.id])
+ }
+ if (asset != null) {
+ assetStorageService.delete([asset.id])
+ }
+ if (agent != null) {
+ assetStorageService.delete([agent.id])
+ }
+ conditions.eventually {
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).isEmpty()
+ }
}
/**
@@ -807,6 +852,7 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
.setSecureMode(true)
.setCertificateAlias(aliasName)
// .setUsernamePassword(new UsernamePassword(keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser.username, keycloakTestSetup.serviceUser.secret))
+
and: "the agent is added to the asset service"
agent = assetStorageService.merge(agent)