Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions container/src/main/java/org/openremote/container/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public synchronized void start() throws Exception {
LOG.log(INFO, "Starting service: " + service.getClass().getName());
service.start(Container.this);
}

LOG.log(INFO, "Starting runtime event producers");
for (ContainerService service : getServices()) {
service.startProducers(this);
}

LOG.log(INFO, "Starting runtime ingress");
for (ContainerService service : getServices()) {
service.startIngress(this);
}
} catch (Exception ex) {
LOG.log(ERROR, ">>> Runtime container startup failed", ex);
stop(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class MessageBrokerService implements ContainerService {
protected ProducerTemplate producerTemplate = context.createProducerTemplate();
protected FluentProducerTemplate fluentProducerTemplate = context.createFluentProducerTemplate();

private boolean isStarted = false;
private volatile boolean isStarted;

@Override
public int getPriority() {
Expand Down Expand Up @@ -151,12 +151,11 @@ public void start(Container container) throws Exception {

@Override
public void stop(Container container) throws Exception {
try {
if (context != null) {
context.stop();
}
} finally {
isStarted = false;
// We want to indicate service is stopped before actually stopping it so callers will not be
// able to push new events during shutdown
isStarted = false;
if (context != null) {
context.stop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ public void init(Container container) throws Exception {
}

@Override
public void start(Container container) throws Exception {
public void start(Container container) throws Exception {}

@Override
public void startIngress(Container container) throws Exception {
if (undertow != null) {
undertow.start();
LOG.log(INFO, "Webserver ready on http://" + host + ":" + port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.ServiceStatus;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.RouteConfigurationBuilder;
import org.openremote.container.message.MessageBrokerService;
Expand Down Expand Up @@ -91,6 +93,9 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
public static final String ATTRIBUTE_EVENT_ROUTE_CONFIG_ID = "attributeEvent";
public static final int PRIORITY = AssetStorageService.PRIORITY + 1000;
public static final String ATTRIBUTE_EVENT_PROCESSOR = "direct://AttributeEventProcessor";
private static final String ATTRIBUTE_EVENT_ROUTE_ID = "AttributeEvent-Processor";
private static final String ATTRIBUTE_EVENT_SEND_ENDPOINT =
ATTRIBUTE_EVENT_PROCESSOR + "?block=false";
private static final System.Logger LOG = System.getLogger(AssetProcessingService.class.getName());
protected final List<AttributeEventInterceptor> eventInterceptors = new ArrayList<>();
protected TimerService timerService;
Expand Down Expand Up @@ -329,7 +334,7 @@ public void configure() throws Exception {

// All user authorisation checks MUST have been carried out before events reach this queue
from(ATTRIBUTE_EVENT_PROCESSOR)
.routeId("AttributeEvent-Processor")
.routeId(ATTRIBUTE_EVENT_ROUTE_ID)
.routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID)
.threads()
.executorService(executorService)
Expand Down Expand Up @@ -382,24 +387,59 @@ public void addEventInterceptor(AttributeEventInterceptor eventInterceptor) {
eventInterceptors.sort(Comparator.comparingInt(AttributeEventInterceptor::getPriority));
}

/** Send internal attribute change events into the {@link #ATTRIBUTE_EVENT_PROCESSOR}. */
/**
* Send an internal attribute change event for asynchronous processing.
*
* @throws IllegalStateException if attribute event processing is unavailable
*/
public void sendAttributeEvent(AttributeEvent attributeEvent) {
sendAttributeEvent(attributeEvent, null);
}

/** Send internal attribute change events into the {@link #ATTRIBUTE_EVENT_PROCESSOR}. */
/**
* Send an internal attribute change event for asynchronous processing.
*
* @throws IllegalStateException if attribute event processing is unavailable
*/
public void sendAttributeEvent(AttributeEvent attributeEvent, String source) {
attributeEvent.setSource(source);
prepareAttributeEvent(attributeEvent, source).asyncSend();
}

/**
* Process an internal attribute change before continuing startup work that depends on the value.
*
* @throws IllegalStateException if attribute event processing is unavailable
* @throws AssetProcessingException if the attribute event cannot be processed
*/
public void sendAttributeEventAndWait(AttributeEvent attributeEvent, String source) {
Object result = prepareAttributeEvent(attributeEvent, source).request();
if (result instanceof RuntimeException failure) {
throw failure;
}
}

private FluentProducerTemplate prepareAttributeEvent(
AttributeEvent attributeEvent, String source) {
if (messageBrokerService == null
|| !messageBrokerService.isStarted()
|| messageBrokerService
.getContext()
.getRouteController()
.getRouteStatus(ATTRIBUTE_EVENT_ROUTE_ID)
!= ServiceStatus.Started) {
throw new IllegalStateException("Attribute event processor is not ready");
}

// Set event source time if not already set
attributeEvent.setSource(source);
if (attributeEvent.getTimestamp() <= 0) {
attributeEvent.setTimestamp(timerService.getCurrentTimeMillis());
}
messageBrokerService

// Fail promptly if the consumer stops between the readiness check and the send.
return messageBrokerService
.getFluentProducerTemplate()
.withBody(attributeEvent)
.to(ATTRIBUTE_EVENT_PROCESSOR)
.asyncSend();
.to(ATTRIBUTE_EVENT_SEND_ENDPOINT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public void start(Container container) throws Exception {
clientEventService.addSubscription(
AssetPredictedDatapointEvent.class, null, this::onPredictedDatapointsChanged);
clientEventService.addSubscription(AttributeEvent.class, null, this::onAttributeEvent);
}

@Override
public void startProducers(Container container) {
loadInitialAttributes();
scheduleNext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.security.IdentityProvider;
import org.openremote.manager.asset.AssetProcessingException;
import org.openremote.manager.asset.AssetProcessingService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.gateway.GatewayService;
Expand Down Expand Up @@ -71,7 +71,6 @@

protected static final Logger LOG = SyslogCategory.getLogger(API, ConnectionMonitorHandler.class);
protected MQTTBrokerService mqttBrokerService;
protected ExecutorService executorService;
protected AssetStorageService assetStorageService;
protected AssetProcessingService assetProcessingService;
protected GatewayService gatewayService;
Expand All @@ -82,13 +81,11 @@
@Override
public void init(Container container, Configuration serverConfiguration) throws Exception {
super.init(container, serverConfiguration);
executorService = container.getExecutor();
mqttBrokerService = container.getService(MQTTBrokerService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetProcessingService = container.getService(AssetProcessingService.class);
gatewayService = container.getService(GatewayService.class);
persistenceService = container.getService(PersistenceService.class);
MessageBrokerService messageBrokerService = container.getService(MessageBrokerService.class);
messageBrokerService
.getContext()
.addRoutes(
Expand Down Expand Up @@ -149,37 +146,48 @@

@Override
public void start(Container container) throws Exception {
// Don't do super start as don't need to publish or subscribe
// super.start(container);

// Don't block start
executorService.submit(
() -> {
// Get all assets that have attributes with user connected meta and initialise them
List<Asset<?>> assets =
assetStorageService.findAll(
new AssetQuery()
.attributes(
new AttributePredicate()
.meta(new NameValuePredicate(USER_CONNECTED, null))));

Map<String, List<Asset<?>>> realmAttributeMap =
assets.stream().collect(Collectors.groupingBy(Asset::getRealm));

realmAttributeMap.forEach(
(realm, realmAssets) -> {
List<Pair<String, Attribute<?>>> assetIdsAttrs =
realmAssets.stream()
.flatMap(
asset ->
asset.getAttributes().stream()
.filter(ConnectionMonitorHandler::attributeMatches)
.map(
attr ->
new Pair<String, Attribute<?>>(asset.getId(), attr)))
.toList();

addSessionAttributes(realm, assetIdsAttrs);
// This handler has no internal MQTT session to start.
}

@Override
public void startProducers(Container container) {
// Get all assets that have attributes with user connected meta and initialise them
List<Asset<?>> assets =
assetStorageService.findAll(
new AssetQuery()
.attributes(
new AttributePredicate().meta(new NameValuePredicate(USER_CONNECTED, null))));

Map<String, List<Asset<?>>> realmAttributeMap =
assets.stream().collect(Collectors.groupingBy(Asset::getRealm));

realmAttributeMap.forEach(
(realm, realmAssets) -> {
List<Pair<String, Attribute<?>>> assetIdsAttrs =
realmAssets.stream()
.flatMap(
asset ->
asset.getAttributes().stream()
.filter(ConnectionMonitorHandler::attributeMatches)
.map(attr -> new Pair<String, Attribute<?>>(asset.getId(), attr)))
.toList();

forEachSessionAttribute(
realm,
assetIdsAttrs,
(userID, attributeRef) -> {
registerSessionAttribute(userID, attributeRef);
boolean connected = !mqttBrokerService.getUserConnections(userID).isEmpty();
// Finish the initial write before the public acceptor can receive a connection.
try {
assetProcessingService.sendAttributeEventAndWait(
new AttributeEvent(attributeRef, connected), getClass().getSimpleName());
} catch (AssetProcessingException e) {
LOG.log(
Level.WARNING,
"Could not initialize connection status for " + attributeRef,
e);

Check warning on line 189 in manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Lambda should be used to defer string concatenation.

See more on https://sonarcloud.io/project/issues?id=openremote_openremote&issues=AaH0QtDiDTky2LpSYccT&open=AaH0QtDiDTky2LpSYccT&pullRequest=3593
}
});
});
}
Expand Down Expand Up @@ -265,6 +273,13 @@

protected void addSessionAttributes(
String realm, List<Pair<String, Attribute<?>>> assetIdsAttrs) {
forEachSessionAttribute(realm, assetIdsAttrs, this::addSessionAttribute);
}

private void forEachSessionAttribute(
String realm,
List<Pair<String, Attribute<?>>> assetIdsAttrs,
BiConsumer<String, AttributeRef> action) {
LOG.finest(
"Adding '"
+ assetIdsAttrs.size()
Expand All @@ -275,12 +290,8 @@
assetIdsAttrs.stream()
.map(assetIdAttr -> assetIdAttr.getValue().getMetaValue(USER_CONNECTED).orElse(null))
.filter(Objects::nonNull)
.map(ConnectionMonitorHandler::toServiceAccountUsername)
.distinct()
.map(
username ->
username.startsWith(User.SERVICE_ACCOUNT_PREFIX)
? username
: User.SERVICE_ACCOUNT_PREFIX + username)
.toList();

// Convert usernames to userIds
Expand All @@ -294,7 +305,7 @@
.ifPresent(
username -> {
String userID =
userIds.get(usernames.indexOf(User.SERVICE_ACCOUNT_PREFIX + username));
userIds.get(usernames.indexOf(toServiceAccountUsername(username)));

if (userID == null) {
LOG.warning(
Expand All @@ -303,22 +314,32 @@
+ ", username="
+ username);
} else {
addSessionAttribute(
action.accept(
userID,
new AttributeRef(assetIdAttr.key, assetIdAttr.getValue().getName()));
}
}));
}

private static String toServiceAccountUsername(String username) {
return username.startsWith(User.SERVICE_ACCOUNT_PREFIX)
? username
: User.SERVICE_ACCOUNT_PREFIX + username;
}

protected void addSessionAttribute(String userID, AttributeRef attributeRef) {
LOG.finest("Adding userID '" + userID + "' monitoring for attribute: " + attributeRef);
updateUserConnectedStatus(
userID,
Collections.singletonList(attributeRef),
!mqttBrokerService.getUserConnections(userID).isEmpty());
Set<AttributeRef> refs =
userIDAttributeRefs.computeIfAbsent(userID, ID -> ConcurrentHashMap.newKeySet());
refs.add(attributeRef);
registerSessionAttribute(userID, attributeRef);
}

private void registerSessionAttribute(String userID, AttributeRef attributeRef) {
userIDAttributeRefs
.computeIfAbsent(userID, id -> ConcurrentHashMap.newKeySet())
.add(attributeRef);
}

protected void removeSessionAttribute(String userID, AttributeRef attributeRef) {
Expand All @@ -334,6 +355,9 @@

protected void updateUserConnectedStatus(
String userID, Collection<AttributeRef> attributeRefs, boolean connected) {
if (!messageBrokerService.isStarted()) {
return;
}

// Only update statuses if this is the only connection
Set<RemotingConnection> connections = mqttBrokerService.getUserConnections(userID);
Expand Down
Loading
Loading