Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,9 @@ public class MQTTService {
@Getter
private final WillMessageHandler willMessageHandler;

@Getter
private final RetainedMessageHandler retainedMessageHandler;

@Getter
@Setter
private SystemEventService eventService;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -105,14 +107,15 @@ 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();
this.metricsCollector = mqttService.getMetricsCollector();
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();
}
Expand Down Expand Up @@ -367,10 +370,14 @@ private CompletableFuture<Void> doSubscribe(MqttSubscribeMessage msg) {
return CompletableFuture.completedFuture(null);
}
List<CompletableFuture<Void>> futureList = new ArrayList<>(subTopics.size());
Optional<String> 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<List<String>> topicListFuture = PulsarTopicUtils.asyncGetTopicListFromTopicSubscription(
subTopic.topicName(), configuration.getDefaultTenant(), configuration.getDefaultNamespace(),
Expand All @@ -390,6 +397,7 @@ private CompletableFuture<Void> doSubscribe(MqttSubscribeMessage msg) {
});
futureList.add(completableFuture);
}
final Optional<String> finalRetainedTopic = retainedTopic;
return FutureUtil.waitForAll(futureList).thenAccept(v -> {
SubscribeAck subscribeAck = SubscribeAck
.builder()
Expand All @@ -399,7 +407,11 @@ private CompletableFuture<Void> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> publish(MqttPublishMessage msg) {
if (MqttUtils.isRetainedMessage(msg)) {
return retainedMessageHandler.addRetainedMessage(msg);
}
return writeToPulsarTopic(msg).thenAccept(__ -> {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -47,8 +47,14 @@ public CompletableFuture<Void> publish(MqttPublishMessage msg) {
final boolean isMqtt5 = MqttUtils.isMqtt5(protocolVersion);
final int packetId = msg.variableHeader().packetId();
final String topic = msg.variableHeader().topicName();
final CompletableFuture<Void> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
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.
*/
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, RetainedMessage> retainedMessages = new ConcurrentHashMap<>();

public RetainedMessageHandler(MQTTService mqttService) {
this.mqttService = mqttService;
this.eventListener = new RetainedMessageEventListener();
}

public CompletableFuture<Void> 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<String> 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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public CompletableFuture<Void> sendLWTEvent(LastWillMessageEvent event) {
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Void> sendRetainedEvent(RetainedMessageEvent event) {
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Void> sendEvent(MqttEvent event) {
return CompletableFuture.completedFuture(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@

public enum EventType {
CONNECT,
LAST_WILL_MESSAGE;
LAST_WILL_MESSAGE,
RETAINED_MESSAGE;
}
Loading