diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/AbstractQosPublishHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/AbstractQosPublishHandler.java index 029ed7aaa..44f251515 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/AbstractQosPublishHandler.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/AbstractQosPublishHandler.java @@ -17,6 +17,7 @@ import io.netty.channel.Channel; import io.netty.handler.codec.mqtt.MqttPublishMessage; import io.streamnative.pulsar.handlers.mqtt.exception.MQTTNoMatchingSubscriberException; +import io.streamnative.pulsar.handlers.mqtt.support.RetainedMessageHandler; import io.streamnative.pulsar.handlers.mqtt.utils.MessagePublishContext; import io.streamnative.pulsar.handlers.mqtt.utils.PulsarTopicUtils; import java.util.Optional; @@ -34,12 +35,14 @@ public abstract class AbstractQosPublishHandler implements QosPublishHandler { protected final PulsarService pulsarService; + protected final RetainedMessageHandler retainedMessageHandler; protected final MQTTServerConfiguration configuration; protected final Channel channel; - protected AbstractQosPublishHandler(PulsarService pulsarService, MQTTServerConfiguration configuration, + protected AbstractQosPublishHandler(MQTTService mqttService, MQTTServerConfiguration configuration, Channel channel) { - this.pulsarService = pulsarService; + this.pulsarService = mqttService.getPulsarService(); + this.retainedMessageHandler = mqttService.getRetainedMessageHandler(); this.configuration = configuration; this.channel = channel; } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/MQTTService.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/MQTTService.java index b1f4305e2..d5841febd 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/MQTTService.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/MQTTService.java @@ -15,6 +15,7 @@ import io.streamnative.pulsar.handlers.mqtt.support.MQTTMetricsCollector; import io.streamnative.pulsar.handlers.mqtt.support.MQTTMetricsProvider; +import io.streamnative.pulsar.handlers.mqtt.support.RetainedMessageHandler; import io.streamnative.pulsar.handlers.mqtt.support.WillMessageHandler; import io.streamnative.pulsar.handlers.mqtt.support.event.DisableEventCenter; import io.streamnative.pulsar.handlers.mqtt.support.event.PulsarEventCenter; @@ -69,6 +70,9 @@ public class MQTTService { @Getter private final WillMessageHandler willMessageHandler; + @Getter + private final RetainedMessageHandler retainedMessageHandler; + @Getter @Setter private SystemEventService eventService; @@ -94,6 +98,7 @@ public MQTTService(BrokerService brokerService, MQTTServerConfiguration serverCo serverConfiguration.getEventCenterCallbackPoolThreadNum()); } this.willMessageHandler = new WillMessageHandler(this); + this.retainedMessageHandler = new RetainedMessageHandler(this); } public boolean isSystemTopicEnabled() { diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyProtocolMethodProcessor.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyProtocolMethodProcessor.java index 4e3cbc0e0..ef0eca363 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyProtocolMethodProcessor.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyProtocolMethodProcessor.java @@ -43,6 +43,7 @@ import io.streamnative.pulsar.handlers.mqtt.support.handler.AckHandler; import io.streamnative.pulsar.handlers.mqtt.support.systemtopic.ConnectEvent; import io.streamnative.pulsar.handlers.mqtt.support.systemtopic.SystemEventService; +import io.streamnative.pulsar.handlers.mqtt.utils.MqttUtils; import io.streamnative.pulsar.handlers.mqtt.utils.NettyUtils; import io.streamnative.pulsar.handlers.mqtt.utils.PulsarTopicUtils; import java.net.InetSocketAddress; @@ -249,7 +250,7 @@ public void processSubscribe(MqttSubscribeMessage msg) { private void registerTopicListener(MqttSubscribeMessage msg) { for (MqttTopicSubscription subscription : msg.payload().topicSubscriptions()) { String topicFilter = subscription.topicName(); - if (PulsarTopicUtils.isRegexFilter(topicFilter)) { + if (MqttUtils.isRegexFilter(topicFilter)) { connection.addTopicChangeListener(new PulsarTopicChangeListener() { @Override public void onTopicLoad(TopicName changedTopicName) { diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyService.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyService.java index fd358cb1a..cadb9de1d 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyService.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/proxy/MQTTProxyService.java @@ -74,6 +74,7 @@ public MQTTProxyService(MQTTService mqttService, MQTTProxyConfiguration proxyCon new DisabledSystemEventService(); this.eventService.addListener(connectionManager.getEventListener()); this.eventService.addListener(mqttService.getWillMessageHandler().getEventListener()); + this.eventService.addListener(mqttService.getRetainedMessageHandler().getEventListener()); mqttService.setEventService(eventService); this.acceptorGroup = EventLoopUtil.newEventLoopGroup(proxyConfig.getMqttProxyNumAcceptorThreads(), false, acceptorThreadFactory); diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/DefaultProtocolMethodProcessorImpl.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/DefaultProtocolMethodProcessorImpl.java index c83474a16..37fdadd8d 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/DefaultProtocolMethodProcessorImpl.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/DefaultProtocolMethodProcessorImpl.java @@ -53,6 +53,7 @@ import io.streamnative.pulsar.handlers.mqtt.support.event.PulsarEventCenter; import io.streamnative.pulsar.handlers.mqtt.support.event.PulsarTopicChangeListener; import io.streamnative.pulsar.handlers.mqtt.support.handler.AckHandler; +import io.streamnative.pulsar.handlers.mqtt.utils.MqttMessageUtils; import io.streamnative.pulsar.handlers.mqtt.utils.MqttUtils; import io.streamnative.pulsar.handlers.mqtt.utils.NettyUtils; import io.streamnative.pulsar.handlers.mqtt.utils.PulsarTopicUtils; @@ -97,6 +98,7 @@ public class DefaultProtocolMethodProcessorImpl extends AbstractCommonProtocolMe private final MQTTConnectionManager connectionManager; private final MQTTSubscriptionManager mqttSubscriptionManager; private final WillMessageHandler willMessageHandler; + private final RetainedMessageHandler retainedMessageHandler; private Connection connection; private final PulsarEventCenter eventCenter; @@ -105,7 +107,7 @@ public DefaultProtocolMethodProcessorImpl(MQTTService mqttService, ChannelHandle mqttService.getServerConfiguration().isMqttAuthenticationEnabled(), ctx); this.pulsarService = mqttService.getPulsarService(); this.configuration = mqttService.getServerConfiguration(); - this.qosPublishHandlers = new QosPublishHandlersImpl(pulsarService, configuration, ctx.channel()); + this.qosPublishHandlers = new QosPublishHandlersImpl(mqttService, configuration, ctx.channel()); this.packetIdGenerator = PacketIdGenerator.newNonZeroGenerator(); this.outstandingPacketContainer = new OutstandingPacketContainerImpl(); this.authorizationService = mqttService.getAuthorizationService(); @@ -113,6 +115,7 @@ public DefaultProtocolMethodProcessorImpl(MQTTService mqttService, ChannelHandle this.connectionManager = mqttService.getConnectionManager(); this.mqttSubscriptionManager = mqttService.getSubscriptionManager(); this.willMessageHandler = mqttService.getWillMessageHandler(); + this.retainedMessageHandler = mqttService.getRetainedMessageHandler(); this.serverCnx = new MQTTServerCnx(pulsarService, ctx); this.eventCenter = mqttService.getEventCenter(); } @@ -367,10 +370,14 @@ private CompletableFuture doSubscribe(MqttSubscribeMessage msg) { return CompletableFuture.completedFuture(null); } List> futureList = new ArrayList<>(subTopics.size()); + Optional retainedTopic = Optional.empty(); for (MqttTopicSubscription subTopic : subTopics) { - if (PulsarTopicUtils.isRegexFilter(subTopic.topicName())) { + if (MqttUtils.isRegexFilter(subTopic.topicName())) { registerRegexTopicFilterListener(subTopic); } + if (!retainedTopic.isPresent()) { + retainedTopic = retainedMessageHandler.getRetainedTopic(subTopic.topicName()); + } metricsCollector.addSub(subTopic.topicName()); CompletableFuture> topicListFuture = PulsarTopicUtils.asyncGetTopicListFromTopicSubscription( subTopic.topicName(), configuration.getDefaultTenant(), configuration.getDefaultNamespace(), @@ -390,6 +397,7 @@ private CompletableFuture doSubscribe(MqttSubscribeMessage msg) { }); futureList.add(completableFuture); } + final Optional finalRetainedTopic = retainedTopic; return FutureUtil.waitForAll(futureList).thenAccept(v -> { SubscribeAck subscribeAck = SubscribeAck .builder() @@ -399,7 +407,11 @@ private CompletableFuture doSubscribe(MqttSubscribeMessage msg) { .map(MqttTopicSubscription::qualityOfService) .collect(Collectors.toList())) .build(); - ackHandler.sendSubscribeAck(connection, subscribeAck); + ackHandler.sendSubscribeAck(connection, subscribeAck).addListener(listener -> { + finalRetainedTopic.map(topic -> + channel.writeAndFlush(MqttMessageUtils + .createRetainedMessage(retainedMessageHandler.getRetainedMessage(topic)))); + }); }).exceptionally(ex -> { Throwable realCause = FutureUtil.unwrapCompletionException(ex); if (realCause instanceof BrokerServiceException.TopicNotFoundException) { diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos0PublishHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos0PublishHandler.java index e07382c86..63b2f57ee 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos0PublishHandler.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos0PublishHandler.java @@ -17,21 +17,25 @@ import io.netty.handler.codec.mqtt.MqttPublishMessage; import io.streamnative.pulsar.handlers.mqtt.AbstractQosPublishHandler; import io.streamnative.pulsar.handlers.mqtt.MQTTServerConfiguration; +import io.streamnative.pulsar.handlers.mqtt.MQTTService; +import io.streamnative.pulsar.handlers.mqtt.utils.MqttUtils; import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; -import org.apache.pulsar.broker.PulsarService; /** * Publish handler implementation for Qos 0. */ @Slf4j public class Qos0PublishHandler extends AbstractQosPublishHandler { - public Qos0PublishHandler(PulsarService pulsarService, MQTTServerConfiguration configuration, Channel channel) { - super(pulsarService, configuration, channel); + public Qos0PublishHandler(MQTTService mqttService, MQTTServerConfiguration configuration, Channel channel) { + super(mqttService, configuration, channel); } @Override public CompletableFuture publish(MqttPublishMessage msg) { + if (MqttUtils.isRetainedMessage(msg)) { + return retainedMessageHandler.addRetainedMessage(msg); + } return writeToPulsarTopic(msg).thenAccept(__ -> {}); } } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos1PublishHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos1PublishHandler.java index c6021999f..4e071d9ac 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos1PublishHandler.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos1PublishHandler.java @@ -18,6 +18,7 @@ import io.streamnative.pulsar.handlers.mqtt.AbstractQosPublishHandler; import io.streamnative.pulsar.handlers.mqtt.Connection; import io.streamnative.pulsar.handlers.mqtt.MQTTServerConfiguration; +import io.streamnative.pulsar.handlers.mqtt.MQTTService; import io.streamnative.pulsar.handlers.mqtt.exception.MQTTNoMatchingSubscriberException; import io.streamnative.pulsar.handlers.mqtt.messages.ack.PublishAck; import io.streamnative.pulsar.handlers.mqtt.messages.codes.mqtt5.Mqtt5PubReasonCode; @@ -26,7 +27,6 @@ import io.streamnative.pulsar.handlers.mqtt.utils.NettyUtils; import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; -import org.apache.pulsar.broker.PulsarService; import org.apache.pulsar.broker.service.BrokerServiceException; import org.apache.pulsar.common.util.FutureUtil; @@ -36,8 +36,8 @@ @Slf4j public class Qos1PublishHandler extends AbstractQosPublishHandler { - public Qos1PublishHandler(PulsarService pulsarService, MQTTServerConfiguration configuration, Channel channel) { - super(pulsarService, configuration, channel); + public Qos1PublishHandler(MQTTService mqttService, MQTTServerConfiguration configuration, Channel channel) { + super(mqttService, configuration, channel); } @Override @@ -47,8 +47,14 @@ public CompletableFuture publish(MqttPublishMessage msg) { final boolean isMqtt5 = MqttUtils.isMqtt5(protocolVersion); final int packetId = msg.variableHeader().packetId(); final String topic = msg.variableHeader().topicName(); + final CompletableFuture ret; + if (MqttUtils.isRetainedMessage(msg)) { + ret = retainedMessageHandler.addRetainedMessage(msg); + } else { + ret = writeToPulsarTopic(msg, isMqtt5).thenApply(__ -> null); + } // we need to check if subscription exist when protocol version is mqtt 5.x - return writeToPulsarTopic(msg, isMqtt5) + return ret .thenCompose(__ -> { PublishAck publishAck = PublishAck.builder() .success(true) diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos2PublishHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos2PublishHandler.java index f2e3a2706..fe51fa6f3 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos2PublishHandler.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/Qos2PublishHandler.java @@ -17,9 +17,9 @@ import io.netty.handler.codec.mqtt.MqttPublishMessage; import io.streamnative.pulsar.handlers.mqtt.AbstractQosPublishHandler; import io.streamnative.pulsar.handlers.mqtt.MQTTServerConfiguration; +import io.streamnative.pulsar.handlers.mqtt.MQTTService; import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; -import org.apache.pulsar.broker.PulsarService; /** * Publish handler implementation for Qos 2. @@ -27,8 +27,8 @@ @Slf4j public class Qos2PublishHandler extends AbstractQosPublishHandler { - public Qos2PublishHandler(PulsarService pulsarService, MQTTServerConfiguration configuration, Channel channel) { - super(pulsarService, configuration, channel); + public Qos2PublishHandler(MQTTService mqttService, MQTTServerConfiguration configuration, Channel channel) { + super(mqttService, configuration, channel); } @Override diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/QosPublishHandlersImpl.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/QosPublishHandlersImpl.java index 8f93ecc2b..70ba9d789 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/QosPublishHandlersImpl.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/QosPublishHandlersImpl.java @@ -15,9 +15,9 @@ import io.netty.channel.Channel; import io.streamnative.pulsar.handlers.mqtt.MQTTServerConfiguration; +import io.streamnative.pulsar.handlers.mqtt.MQTTService; import io.streamnative.pulsar.handlers.mqtt.QosPublishHandler; import io.streamnative.pulsar.handlers.mqtt.QosPublishHandlers; -import org.apache.pulsar.broker.PulsarService; /** * Default implementation of QosPublishHandlers. @@ -28,10 +28,10 @@ public class QosPublishHandlersImpl implements QosPublishHandlers { private final QosPublishHandler qos1Handler; private final QosPublishHandler qos2Handler; - public QosPublishHandlersImpl(PulsarService pulsarService, MQTTServerConfiguration configuration, Channel channel) { - this.qos0Handler = new Qos0PublishHandler(pulsarService, configuration, channel); - this.qos1Handler = new Qos1PublishHandler(pulsarService, configuration, channel); - this.qos2Handler = new Qos2PublishHandler(pulsarService, configuration, channel); + public QosPublishHandlersImpl(MQTTService mqttService, MQTTServerConfiguration configuration, Channel channel) { + this.qos0Handler = new Qos0PublishHandler(mqttService, configuration, channel); + this.qos1Handler = new Qos1PublishHandler(mqttService, configuration, channel); + this.qos2Handler = new Qos2PublishHandler(mqttService, configuration, channel); } @Override diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/RetainedMessageHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/RetainedMessageHandler.java new file mode 100644 index 000000000..bb627ba6d --- /dev/null +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/RetainedMessageHandler.java @@ -0,0 +1,94 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.streamnative.pulsar.handlers.mqtt.support; + +import static io.streamnative.pulsar.handlers.mqtt.support.systemtopic.EventType.RETAINED_MESSAGE; +import io.netty.handler.codec.mqtt.MqttPublishMessage; +import io.streamnative.pulsar.handlers.mqtt.MQTTService; +import io.streamnative.pulsar.handlers.mqtt.TopicFilterImpl; +import io.streamnative.pulsar.handlers.mqtt.support.systemtopic.EventListener; +import io.streamnative.pulsar.handlers.mqtt.support.systemtopic.MqttEvent; +import io.streamnative.pulsar.handlers.mqtt.support.systemtopic.RetainedMessageEvent; +import io.streamnative.pulsar.handlers.mqtt.utils.MqttMessageUtils; +import io.streamnative.pulsar.handlers.mqtt.utils.MqttUtils; +import io.streamnative.pulsar.handlers.mqtt.utils.RetainedMessage; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class RetainedMessageHandler { + + @Getter + private final EventListener eventListener; + private final MQTTService mqttService; + private final Map retainedMessages = new ConcurrentHashMap<>(); + + public RetainedMessageHandler(MQTTService mqttService) { + this.mqttService = mqttService; + this.eventListener = new RetainedMessageEventListener(); + } + + public CompletableFuture addRetainedMessage(MqttPublishMessage retainedMessage) { + if (mqttService.isSystemTopicEnabled()) { + RetainedMessageEvent event = RetainedMessageEvent + .builder() + .retainedMessage(MqttMessageUtils.createRetainedMessage(retainedMessage)) + .build(); + mqttService.getEventService().sendRetainedEvent(event); + } else { + // Standalone mode + addRetainedMessage(MqttMessageUtils.createRetainedMessage(retainedMessage)); + } + return CompletableFuture.completedFuture(null); + } + + public Optional getRetainedTopic(String topic) { + if (MqttUtils.isRegexFilter(topic)) { + TopicFilterImpl topicFilter = new TopicFilterImpl(topic); + return retainedMessages.keySet().stream().filter(topicFilter::test).findFirst(); + } + return retainedMessages.keySet().stream().filter(key -> key.equals(topic)).findFirst(); + } + + public RetainedMessage getRetainedMessage(String topic) { + return retainedMessages.get(topic); + } + + private void addRetainedMessage(RetainedMessage msg) { + String topicName = msg.getTopic(); + if (msg.getPayload().length == 0) { + retainedMessages.remove(topicName); + } else { + retainedMessages.put(topicName, msg); + } + } + + class RetainedMessageEventListener implements EventListener { + + @Override + public void onChange(MqttEvent event) { + if (event.getEventType() == RETAINED_MESSAGE) { + RetainedMessageEvent retainedEvent = (RetainedMessageEvent) event.getSourceEvent(); + if (log.isDebugEnabled()) { + log.debug("add retained message : {}", retainedEvent.getRetainedMessage()); + } + addRetainedMessage(retainedEvent.getRetainedMessage()); + } + } + } +} diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/WillMessageHandler.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/WillMessageHandler.java index 0c9e74ea2..3ec3f5cbe 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/WillMessageHandler.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/WillMessageHandler.java @@ -80,6 +80,9 @@ public void onChange(MqttEvent event) { if (event.getEventType() == LAST_WILL_MESSAGE) { LastWillMessageEvent lwtEvent = (LastWillMessageEvent) event.getSourceEvent(); if (!lwtEvent.getAddress().equals(advertisedAddress)) { + if (log.isDebugEnabled()) { + log.debug("fire will message : {}", lwtEvent.getWillMessage()); + } fireWillMessage("", lwtEvent.getWillMessage()); } } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/DisabledSystemEventService.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/DisabledSystemEventService.java index 1a5c35521..376621ebc 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/DisabledSystemEventService.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/DisabledSystemEventService.java @@ -42,6 +42,11 @@ public CompletableFuture sendLWTEvent(LastWillMessageEvent event) { return CompletableFuture.completedFuture(null); } + @Override + public CompletableFuture sendRetainedEvent(RetainedMessageEvent event) { + return CompletableFuture.completedFuture(null); + } + @Override public CompletableFuture sendEvent(MqttEvent event) { return CompletableFuture.completedFuture(null); diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/EventType.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/EventType.java index 90359939d..8e34c0325 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/EventType.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/EventType.java @@ -15,5 +15,6 @@ public enum EventType { CONNECT, - LAST_WILL_MESSAGE; + LAST_WILL_MESSAGE, + RETAINED_MESSAGE; } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/MqttEventUtils.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/MqttEventUtils.java new file mode 100644 index 000000000..8037af7a6 --- /dev/null +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/MqttEventUtils.java @@ -0,0 +1,64 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.streamnative.pulsar.handlers.mqtt.support.systemtopic; + +import org.apache.bookkeeper.common.util.JsonUtil; + +/** + * Event message utils. + */ +public class MqttEventUtils { + + public static MqttEvent getMqttEvent(LastWillMessageEvent event, ActionType actionType) { + MqttEvent.MqttEventBuilder builder = MqttEvent.builder(); + try { + return builder + .key(event.getClientId() + "-LWT") + .eventType(EventType.LAST_WILL_MESSAGE) + .actionType(actionType) + .sourceEvent(JsonUtil.toJson(event)) + .build(); + } catch (JsonUtil.ParseJsonException e) { + throw new IllegalArgumentException(e); + } + } + + public static MqttEvent getMqttEvent(RetainedMessageEvent event, ActionType actionType) { + MqttEvent.MqttEventBuilder builder = MqttEvent.builder(); + try { + return builder + .key(event.getRetainedMessage().getTopic()) + .eventType(EventType.RETAINED_MESSAGE) + .actionType(actionType) + .sourceEvent(JsonUtil.toJson(event)) + .build(); + } catch (JsonUtil.ParseJsonException e) { + throw new IllegalArgumentException(e); + } + } + + public static MqttEvent getMqttEvent(ConnectEvent event, ActionType actionType) { + MqttEvent.MqttEventBuilder builder = MqttEvent.builder(); + try { + return builder + .key(event.getClientId()) + .eventType(EventType.CONNECT) + .actionType(actionType) + .sourceEvent(JsonUtil.toJson(event)) + .build(); + } catch (JsonUtil.ParseJsonException e) { + throw new IllegalArgumentException(e); + } + } +} diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/RetainedMessageEvent.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/RetainedMessageEvent.java new file mode 100644 index 000000000..efffef314 --- /dev/null +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/RetainedMessageEvent.java @@ -0,0 +1,29 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.streamnative.pulsar.handlers.mqtt.support.systemtopic; + +import io.streamnative.pulsar.handlers.mqtt.utils.RetainedMessage; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class RetainedMessageEvent extends SourceEvent { + + private RetainedMessage retainedMessage; +} diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemEventService.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemEventService.java index fb0a44e3a..f71eeec8d 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemEventService.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemEventService.java @@ -27,5 +27,7 @@ public interface SystemEventService { CompletableFuture sendLWTEvent(LastWillMessageEvent event); + CompletableFuture sendRetainedEvent(RetainedMessageEvent event); + CompletableFuture sendEvent(MqttEvent event); } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemTopicBasedSystemEventService.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemTopicBasedSystemEventService.java index 1a88e9b4c..33110c7cb 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemTopicBasedSystemEventService.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/support/systemtopic/SystemTopicBasedSystemEventService.java @@ -13,6 +13,7 @@ */ package io.streamnative.pulsar.handlers.mqtt.support.systemtopic; +import static io.streamnative.pulsar.handlers.mqtt.support.systemtopic.MqttEventUtils.getMqttEvent; import com.google.common.annotations.Beta; import java.util.ArrayList; import java.util.List; @@ -87,6 +88,17 @@ public CompletableFuture sendLWTEvent(LastWillMessageEvent event) { }); } + @Override + public CompletableFuture sendRetainedEvent(RetainedMessageEvent event) { + checkReader(); + return sendEvent(getMqttEvent(event, ActionType.INSERT)) + .thenRun(() -> { + if (log.isDebugEnabled()) { + log.debug("send retained event : {}", event); + } + }); + } + @Override public CompletableFuture sendEvent(MqttEvent event) { CompletableFuture> writerFuture = systemTopicClient.newWriterAsync(); @@ -106,34 +118,6 @@ public CompletableFuture sendEvent(MqttEvent event) { }); } - private MqttEvent getMqttEvent(LastWillMessageEvent event, ActionType actionType) { - MqttEvent.MqttEventBuilder builder = MqttEvent.builder(); - try { - return builder - .key(event.getClientId() + "-LWT") - .eventType(EventType.LAST_WILL_MESSAGE) - .actionType(actionType) - .sourceEvent(JsonUtil.toJson(event)) - .build(); - } catch (JsonUtil.ParseJsonException e) { - throw new IllegalArgumentException(e); - } - } - - private MqttEvent getMqttEvent(ConnectEvent event, ActionType actionType) { - MqttEvent.MqttEventBuilder builder = MqttEvent.builder(); - try { - return builder - .key(event.getClientId()) - .eventType(EventType.CONNECT) - .actionType(actionType) - .sourceEvent(JsonUtil.toJson(event)) - .build(); - } catch (JsonUtil.ParseJsonException e) { - throw new IllegalArgumentException(e); - } - } - protected CompletableFuture> createReader() { CompletableFuture> result = new CompletableFuture<>(); Backoff backoff = new Backoff(1, TimeUnit.SECONDS, @@ -227,6 +211,11 @@ private void refreshCache(Message msg) { LastWillMessageEvent.class); value.setSourceEvent(lwtEvent); break; + case RETAINED_MESSAGE: + RetainedMessageEvent retainedEvent = JsonUtil.fromJson((String) value.getSourceEvent(), + RetainedMessageEvent.class); + value.setSourceEvent(retainedEvent); + break; default: break; } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttMessageUtils.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttMessageUtils.java index 59e3b546a..21fd8ae94 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttMessageUtils.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttMessageUtils.java @@ -13,6 +13,7 @@ */ package io.streamnative.pulsar.handlers.mqtt.utils; +import static com.google.common.base.Preconditions.checkArgument; import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; @@ -98,6 +99,28 @@ public static WillMessage createWillMessage(MqttConnectMessage msg) { return new WillMessage(willTopic, willMessage, qos, retained); } + public static RetainedMessage createRetainedMessage(MqttPublishMessage msg) { + checkArgument(msg.fixedHeader().isRetain(), "Must be retained msg"); + final byte[] payload = new byte[msg.payload().readableBytes()]; + msg.payload().markReaderIndex(); + msg.payload().readBytes(payload); + msg.payload().resetReaderIndex(); + final String topicName = msg.variableHeader().topicName(); + final MqttQoS qos = msg.fixedHeader().qosLevel(); + return new RetainedMessage(topicName, payload, qos); + } + + public static MqttPublishMessage createRetainedMessage(RetainedMessage msg) { + checkArgument(msg != null, "Msg should not be null"); + return MessageBuilder.publish() + .messageId(-1) + .payload(Unpooled.copiedBuffer(msg.getPayload())) + .topicName(msg.getTopic()) + .qos(msg.getQos()) + .retained(true) + .build(); + } + public static MqttPublishMessage createMqttWillMessage(WillMessage willMessage) { return MessageBuilder.publish() .topicName(willMessage.getTopic()) diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttUtils.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttUtils.java index 3bcf6faf5..fd392f28f 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttUtils.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/MqttUtils.java @@ -14,8 +14,10 @@ package io.streamnative.pulsar.handlers.mqtt.utils; import io.netty.handler.codec.mqtt.MqttConnectMessage; +import io.netty.handler.codec.mqtt.MqttPublishMessage; import io.netty.handler.codec.mqtt.MqttQoS; import io.netty.handler.codec.mqtt.MqttVersion; +import io.streamnative.pulsar.handlers.mqtt.TopicFilter; /** * Some Mqtt protocol utilities. @@ -49,4 +51,13 @@ public static boolean isQosSupported(MqttConnectMessage msg) { MqttQoS mqttQoS = MqttQoS.valueOf(willQos); return mqttQoS == MqttQoS.AT_LEAST_ONCE || mqttQoS == MqttQoS.AT_MOST_ONCE; } + + public static boolean isRetainedMessage(MqttPublishMessage msg) { + return msg.fixedHeader().isRetain(); + } + + public static boolean isRegexFilter(String topicFilter) { + return topicFilter.contains(TopicFilter.SINGLE_LEVEL) + || topicFilter.contains(TopicFilter.MULTI_LEVEL); + } } diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/PulsarTopicUtils.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/PulsarTopicUtils.java index 5fa18eb66..dca754601 100644 --- a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/PulsarTopicUtils.java +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/PulsarTopicUtils.java @@ -13,6 +13,7 @@ */ package io.streamnative.pulsar.handlers.mqtt.utils; +import static io.streamnative.pulsar.handlers.mqtt.utils.MqttUtils.isRegexFilter; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import io.netty.handler.codec.mqtt.MqttSubscribeMessage; @@ -41,6 +42,7 @@ import org.apache.pulsar.common.policies.data.AutoTopicCreationOverride; import org.apache.pulsar.common.util.FutureUtil; + /** * Pulsar topic utils. */ @@ -212,11 +214,6 @@ public static CompletableFuture> asyncGetTopicsForSubscribeMsg(Mqtt ).orElseGet(()-> CompletableFuture.completedFuture(Collections.emptyList())); } - public static boolean isRegexFilter(String topicFilter) { - return topicFilter.contains(TopicFilter.SINGLE_LEVEL) - || topicFilter.contains(TopicFilter.MULTI_LEVEL); - } - public static CompletableFuture> asyncGetTopicListFromTopicSubscription(String topicFilter, String defaultTenant, String defaultNamespace, PulsarService pulsarService, String defaultTopicDomain) { if (isRegexFilter(topicFilter)) { diff --git a/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/RetainedMessage.java b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/RetainedMessage.java new file mode 100644 index 000000000..dd09a3483 --- /dev/null +++ b/mqtt-impl/src/main/java/io/streamnative/pulsar/handlers/mqtt/utils/RetainedMessage.java @@ -0,0 +1,39 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.streamnative.pulsar.handlers.mqtt.utils; + +import io.netty.handler.codec.mqtt.MqttQoS; +import lombok.Getter; +import lombok.Setter; + +/** + * Retained Message. + */ +@Getter +@Setter +public class RetainedMessage { + + String topic; + byte[] payload; + MqttQoS qos; + + public RetainedMessage() { + } + + public RetainedMessage(final String topic, final byte[] payload, final MqttQoS qos) { + this.topic = topic; + this.payload = payload; + this.qos = qos; + } +} diff --git a/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/base/SimpleIntegrationTest.java b/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/base/SimpleIntegrationTest.java index 39ae9c797..887ce3436 100644 --- a/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/base/SimpleIntegrationTest.java +++ b/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/base/SimpleIntegrationTest.java @@ -668,4 +668,28 @@ public void testNonPersistentTopic() throws Exception { received.ack(); connection.disconnect(); } + + @Test + public void testRetainedMessage() throws Exception { + String topicName = "persistent://public/default/a"; + MQTT mqtt = createMQTTClient(); + BlockingConnection connection = mqtt.blockingConnection(); + connection.connect(); + String message1 = "Retained Message"; + String message2 = "Hello Message"; + admin.topics().createNonPartitionedTopic(topicName); + Topic[] topics = { new Topic(topicName, QoS.AT_LEAST_ONCE) }; + connection.publish(topicName, message1.getBytes(), QoS.AT_LEAST_ONCE, true); + connection.subscribe(topics); + connection.publish(topicName, message2.getBytes(), QoS.AT_LEAST_ONCE, false); + Message received1 = connection.receive(5, TimeUnit.SECONDS); + Assert.assertNotNull(received1); + received1.ack(); + Assert.assertEquals(new String(received1.getPayload()), message1); + Message received2 = connection.receive(5, TimeUnit.SECONDS); + Assert.assertNotNull(received2); + received2.ack(); + Assert.assertEquals(new String(received2.getPayload()), message2); + connection.disconnect(); + } } diff --git a/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/proxy/ProxyTest.java b/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/proxy/ProxyTest.java index 6800903c8..34a29a1ae 100644 --- a/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/proxy/ProxyTest.java +++ b/tests/src/test/java/io/streamnative/pulsar/handlers/mqtt/mqtt3/fusesource/proxy/ProxyTest.java @@ -376,4 +376,43 @@ public void testLastWillMessageInCluster() throws Exception { // Assert.assertEquals(rev2.getTopic(), willTopic); consumer2.disconnect(); } + + @Test + public void testRetainedMessageInCluster() throws Exception { + String topicName = "topic-retained-message"; + int numPartitions = 40; + admin.topics().createPartitionedTopic(topicName, numPartitions); + Set ownerBroker = new HashSet<>(); + for (int i = 0; i < numPartitions; i++) { + String partitionedTopic = topicName + "-partition-" + i; + String broker = admin.lookups().lookupTopic(partitionedTopic); + ownerBroker.add(broker); + } + Assert.assertTrue(ownerBroker.size() >= 2); + List brokers = new ArrayList<>(ownerBroker); + String broker1 = brokers.get(0); + // + String retainedTopic = "retained-message-topic"; + String retainedMessage = "retained message"; + MQTT mqtt1 = createMQTT(Integer.parseInt(broker1.substring(broker1.lastIndexOf(":") + 1)) + 5); + mqtt1.setClientId("ab-ab-ab"); + BlockingConnection producer = mqtt1.blockingConnection(); + producer.connect(); + producer.publish(retainedTopic, retainedMessage.getBytes(StandardCharsets.UTF_8), QoS.AT_LEAST_ONCE, true); + producer.disconnect(); + // Wait for the retained message to be sent out and reader received. + Thread.sleep(5000); + // + String broker2 = brokers.get(1); + MQTT mqtt2 = createMQTT(Integer.parseInt(broker2.substring(broker2.lastIndexOf(":") + 1)) + 5); + mqtt2.setClientId("cd-cd-cd"); + BlockingConnection consumer2 = mqtt2.blockingConnection(); + consumer2.connect(); + Topic[] topic = { new Topic(retainedTopic, QoS.AT_LEAST_ONCE)}; + consumer2.subscribe(topic); + Message rev2 = consumer2.receive(10, TimeUnit.SECONDS); + Assert.assertNotNull(rev2); + Assert.assertEquals(new String(rev2.getPayload()), retainedMessage); + consumer2.disconnect(); + } }