Condensed the looping on subscription on separate class

This commit is contained in:
andsel
2016-12-13 23:43:02 +01:00
parent b58cd086ea
commit 2b554ca475
8 changed files with 96 additions and 208 deletions
@@ -0,0 +1,78 @@
package io.moquette.spi.impl;
import io.moquette.parser.proto.messages.AbstractMessage;
import io.moquette.parser.proto.messages.PublishMessage;
import io.moquette.server.ConnectionDescriptor;
import io.moquette.spi.ClientSession;
import io.moquette.spi.IMessagesStore;
import io.moquette.spi.ISessionsStore;
import io.moquette.spi.MessageGUID;
import io.moquette.spi.impl.subscriptions.Subscription;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import static io.moquette.spi.impl.ProtocolProcessor.lowerQosToTheSubscriptionDesired;
class MetaPublisher {
private static final Logger LOG = LoggerFactory.getLogger(MetaPublisher.class);
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
private final ISessionsStore m_sessionsStore;
private final IMessagesStore m_messagesStore;
private final Qos0Publisher qos0Publisher;
private final Qos2Publisher qos2Publisher;
public MetaPublisher(ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors, ISessionsStore sessionsStore,
IMessagesStore messagesStore) {
this.connectionDescriptors = connectionDescriptors;
this.m_sessionsStore = sessionsStore;
this.m_messagesStore = messagesStore;
this.qos0Publisher = new Qos0Publisher(connectionDescriptors, sessionsStore);
this.qos2Publisher = new Qos2Publisher(connectionDescriptors, sessionsStore, m_messagesStore);
}
void publish2Subscribers(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) {
final String topic = pubMsg.getTopic();
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
final ByteBuffer origMessage = pubMsg.getMessage();
//if QoS 1 or 2 store the message
MessageGUID guid = null;
if (publishingQos != AbstractMessage.QOSType.MOST_ONE) {
guid = m_messagesStore.storePublishForFuture(pubMsg);
}
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
for (final Subscription sub : topicMatchingSubscriptions) {
AbstractMessage.QOSType qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
ClientSession targetSession = m_sessionsStore.sessionForClient(sub.getClientId());
boolean targetIsActive = this.connectionDescriptors.containsKey(sub.getClientId());
LOG.debug("Broker republishing to client <{}> topic <{}> qos <{}>, active {}",
sub.getClientId(), sub.getTopicFilter(), qos, targetIsActive);
ByteBuffer message = origMessage.duplicate();
if (targetIsActive) {
//QoS 0
if (qos == AbstractMessage.QOSType.MOST_ONE) {
this.qos0Publisher.publishQos0(targetSession, topic, message);
} else {
//QoS 1 or 2
int messageId = targetSession.nextPacketId();
targetSession.inFlightAckWaiting(guid, messageId);
this.qos2Publisher.publishQos2(targetSession, topic, qos, message, false, messageId);
}
} else {
if (!targetSession.isCleanSession()) {
//store the message in targetSession queue to deliver
targetSession.enqueueToDeliver(guid);
}
}
}
}
}
@@ -136,7 +136,7 @@ public class ProtocolProcessor {
private Qos0PublishHandler qos0PublishHandler; private Qos0PublishHandler qos0PublishHandler;
private Qos1PublishHandler qos1PublishHandler; private Qos1PublishHandler qos1PublishHandler;
private Qos2PublishHandler qos2PublishHandler; private Qos2PublishHandler qos2PublishHandler;
private Qos2Publisher qos2Publisher; private MetaPublisher metaPublisher;
private InternalRepublisher internalRepublisher; private InternalRepublisher internalRepublisher;
//maps clientID to Will testament, if specified on CONNECT //maps clientID to Will testament, if specified on CONNECT
@@ -194,9 +194,9 @@ public class ProtocolProcessor {
this.qos1PublishHandler = new Qos1PublishHandler(m_authorizator, subscriptions, m_messagesStore, this.qos1PublishHandler = new Qos1PublishHandler(m_authorizator, subscriptions, m_messagesStore,
m_interceptor, this.connectionDescriptors, m_sessionsStore, m_server_port); m_interceptor, this.connectionDescriptors, m_sessionsStore, m_server_port);
this.qos2Publisher = new Qos2Publisher(connectionDescriptors, sessionsStore, m_messagesStore); this.metaPublisher = new MetaPublisher(connectionDescriptors, sessionsStore, m_messagesStore);
this.qos2PublishHandler = new Qos2PublishHandler(m_authorizator, subscriptions, m_messagesStore, this.qos2PublishHandler = new Qos2PublishHandler(m_authorizator, subscriptions, m_messagesStore,
m_interceptor, this.connectionDescriptors, m_sessionsStore, m_server_port, this.qos2Publisher); m_interceptor, this.connectionDescriptors, m_sessionsStore, m_server_port/*, this.qos2Publisher*/);
this.internalRepublisher = new InternalRepublisher(this.connectionDescriptors); this.internalRepublisher = new InternalRepublisher(this.connectionDescriptors);
} }
@@ -488,7 +488,7 @@ public class ProtocolProcessor {
guid = m_messagesStore.storePublishForFuture(toStoreMsg); guid = m_messagesStore.storePublishForFuture(toStoreMsg);
} }
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic); List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
this.qos2Publisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions); this.metaPublisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions);
if (!msg.isRetainFlag()) { if (!msg.isRetainFlag()) {
return; return;
@@ -522,7 +522,7 @@ public class ProtocolProcessor {
String topic = tobeStored.getTopic(); String topic = tobeStored.getTopic();
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic); List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
this.qos2Publisher.publish2Subscribers(tobeStored, topicMatchingSubscriptions); this.metaPublisher.publish2Subscribers(tobeStored, topicMatchingSubscriptions);
} }
static QOSType lowerQosToTheSubscriptionDesired(Subscription sub, QOSType qos) { static QOSType lowerQosToTheSubscriptionDesired(Subscription sub, QOSType qos) {
@@ -25,7 +25,7 @@ class Qos0PublishHandler {
private final SubscriptionsStore subscriptions; private final SubscriptionsStore subscriptions;
private final IMessagesStore m_messagesStore; private final IMessagesStore m_messagesStore;
private final BrokerInterceptor m_interceptor; private final BrokerInterceptor m_interceptor;
private final Qos0Publisher qos0Publisher; private final MetaPublisher metaPublisher;
public Qos0PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions, public Qos0PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String, IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String,
@@ -34,7 +34,7 @@ class Qos0PublishHandler {
this.subscriptions = subscriptions; this.subscriptions = subscriptions;
this.m_messagesStore = messagesStore; this.m_messagesStore = messagesStore;
this.m_interceptor = interceptor; this.m_interceptor = interceptor;
this.qos0Publisher = new Qos0Publisher(connectionDescriptors, sessionsStore); this.metaPublisher = new MetaPublisher(connectionDescriptors, sessionsStore, messagesStore);
} }
void receivedPublishQos0(Channel channel, PublishMessage msg) { void receivedPublishQos0(Channel channel, PublishMessage msg) {
@@ -55,7 +55,7 @@ class Qos0PublishHandler {
LOG.trace("subscription tree {}", subscriptions.dumpTree()); LOG.trace("subscription tree {}", subscriptions.dumpTree());
} }
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic); List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
qos0Publisher.publish2Subscribers_qos0(toStoreMsg, topicMatchingSubscriptions); this.metaPublisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions);
if (msg.isRetainFlag()) { if (msg.isRetainFlag()) {
//QoS == 0 && retain => clean old retained //QoS == 0 && retain => clean old retained
@@ -26,30 +26,7 @@ class Qos0Publisher {
this.m_sessionsStore = sessionsStore; this.m_sessionsStore = sessionsStore;
} }
void publish2Subscribers_qos0(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) { void publishQos0(ClientSession clientsession, String topic, ByteBuffer message) {
final String topic = pubMsg.getTopic();
final ByteBuffer origMessage = pubMsg.getMessage();
// LOG.debug("publish2Subscribers republishing to existing subscribers that matches the topic {}", topic);
// if (LOG.isTraceEnabled()) {
// LOG.trace("content <{}>", DebugUtils.payload2Str(origMessage));
// LOG.trace("subscription tree {}", subscriptions.dumpTree());
// }
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
for (final Subscription sub : topicMatchingSubscriptions) {
boolean targetIsActive = connectionDescriptors.containsKey(sub.getClientId());
LOG.debug("Broker republishing to client <{}> topic <{}> qos <{}>, active {}",
sub.getClientId(), sub.getTopicFilter(), AbstractMessage.QOSType.MOST_ONE, targetIsActive);
if (targetIsActive) {
ClientSession targetSession = m_sessionsStore.sessionForClient(sub.getClientId());
ByteBuffer message = origMessage.duplicate();
publishQos0(targetSession, topic, message);
}
}
}
private void publishQos0(ClientSession clientsession, String topic, ByteBuffer message) {
String clientId = clientsession.clientID; String clientId = clientsession.clientID;
LOG.debug("publishQos0 invoked clientId <{}> on topic <{}>", clientId, topic); LOG.debug("publishQos0 invoked clientId <{}> on topic <{}>", clientId, topic);
@@ -28,7 +28,7 @@ class Qos1PublishHandler {
private final BrokerInterceptor m_interceptor; private final BrokerInterceptor m_interceptor;
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors; private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
private final String brokerPort; private final String brokerPort;
private final Qos1Publisher qos1Publisher; private final MetaPublisher metaPublisher;
public Qos1PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions, public Qos1PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String, IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String,
@@ -39,7 +39,7 @@ class Qos1PublishHandler {
this.m_interceptor = interceptor; this.m_interceptor = interceptor;
this.connectionDescriptors = connectionDescriptors; this.connectionDescriptors = connectionDescriptors;
this.brokerPort = brokerPort; this.brokerPort = brokerPort;
this.qos1Publisher = new Qos1Publisher(connectionDescriptors, sessionsStore, messagesStore); this.metaPublisher = new MetaPublisher(connectionDescriptors, sessionsStore, messagesStore);
} }
void receivedPublishQos1(Channel channel, PublishMessage msg) { void receivedPublishQos1(Channel channel, PublishMessage msg) {
@@ -60,7 +60,7 @@ class Qos1PublishHandler {
LOG.trace("subscription tree {}", subscriptions.dumpTree()); LOG.trace("subscription tree {}", subscriptions.dumpTree());
} }
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic); List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
this.qos1Publisher.publish2Subscribers_qos1(toStoreMsg, topicMatchingSubscriptions); this.metaPublisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions);
//send PUBACK //send PUBACK
final Integer messageID = msg.getMessageID(); final Integer messageID = msg.getMessageID();
@@ -1,121 +0,0 @@
package io.moquette.spi.impl;
import io.moquette.parser.proto.messages.AbstractMessage;
import io.moquette.parser.proto.messages.PublishMessage;
import io.moquette.server.ConnectionDescriptor;
import io.moquette.spi.ClientSession;
import io.moquette.spi.IMessagesStore;
import io.moquette.spi.ISessionsStore;
import io.moquette.spi.MessageGUID;
import io.moquette.spi.impl.subscriptions.Subscription;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import static io.moquette.spi.impl.ProtocolProcessor.lowerQosToTheSubscriptionDesired;
class Qos1Publisher {
private static final Logger LOG = LoggerFactory.getLogger(Qos1Publisher.class);
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
private final ISessionsStore m_sessionsStore;
private final IMessagesStore m_messagesStore;
public Qos1Publisher(ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors, ISessionsStore sessionsStore,
IMessagesStore messagesStore) {
this.connectionDescriptors = connectionDescriptors;
this.m_sessionsStore = sessionsStore;
m_messagesStore = messagesStore;
}
void publish2Subscribers_qos1(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) {
final String topic = pubMsg.getTopic();
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
final ByteBuffer origMessage = pubMsg.getMessage();
//if QoS 1 or 2 store the message
MessageGUID guid = m_messagesStore.storePublishForFuture(pubMsg);
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
for (final Subscription sub : topicMatchingSubscriptions) {
AbstractMessage.QOSType qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
ClientSession targetSession = m_sessionsStore.sessionForClient(sub.getClientId());
boolean targetIsActive = this.connectionDescriptors.containsKey(sub.getClientId());
LOG.debug("Broker republishing to client <{}> topic <{}> qos <{}>, active {}",
sub.getClientId(), sub.getTopicFilter(), qos, targetIsActive);
ByteBuffer message = origMessage.duplicate();
if (qos == AbstractMessage.QOSType.MOST_ONE && targetIsActive) {
//QoS 0
publishQos1(targetSession, topic, qos, message, false, null);
} else {
//QoS 1 or 2
//if the target subscription is not clean session and is not connected => store it
if (!targetSession.isCleanSession() && !targetIsActive) {
//store the message in targetSession queue to deliver
targetSession.enqueueToDeliver(guid);
} else {
//publish
if (targetIsActive) {
int messageId = targetSession.nextPacketId();
targetSession.inFlightAckWaiting(guid, messageId);
publishQos1(targetSession, topic, qos, message, false, messageId);
}
}
}
}
}
private void publishQos1(ClientSession clientsession, String topic, AbstractMessage.QOSType qos,
ByteBuffer message, boolean retained, Integer messageID) {
String clientId = clientsession.clientID;
LOG.debug("directSend invoked clientId <{}> on topic <{}> QoS {} retained {} messageID {}",
clientId, topic, qos, retained, messageID);
PublishMessage pubMessage = new PublishMessage();
pubMessage.setRetainFlag(retained);
pubMessage.setTopicName(topic);
pubMessage.setQos(qos);
pubMessage.setPayload(message);
LOG.info("send publish message to <{}> on topic <{}>", clientId, topic);
if (LOG.isDebugEnabled()) {
LOG.debug("content <{}>", DebugUtils.payload2Str(message));
}
//set the PacketIdentifier only for QoS > 0
if (pubMessage.getQos() != AbstractMessage.QOSType.MOST_ONE) {
pubMessage.setMessageID(messageID);
} else {
if (messageID != null) {
throw new RuntimeException("Internal bad error, trying to forwardPublish a QoS 0 message " +
"with PacketIdentifier: " + messageID);
}
}
if (connectionDescriptors == null) {
throw new RuntimeException("Internal bad error, found connectionDescriptors to null while it should be " +
"initialized, somewhere it's overwritten!!");
}
if (connectionDescriptors.get(clientId) == null) {
//TODO while we were publishing to the target client, that client disconnected,
// could happen is not an error HANDLE IT
throw new RuntimeException(String.format("Can't find a ConnectionDescriptor for client <%s> in cache <%s>",
clientId, connectionDescriptors));
}
Channel channel = connectionDescriptors.get(clientId).channel;
LOG.trace("Session for clientId {}", clientId);
if (channel.isWritable()) {
LOG.debug("channel is writable");
//if channel is writable don't enqueue
channel.writeAndFlush(pubMessage);
} else {
//enqueue to the client session
LOG.debug("enqueue to client session");
clientsession.enqueue(pubMessage);
}
}
}
@@ -30,12 +30,12 @@ class Qos2PublishHandler {
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors; private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
private final ISessionsStore m_sessionsStore; private final ISessionsStore m_sessionsStore;
private final String brokerPort; private final String brokerPort;
private final Qos2Publisher qos2Publisher; private final MetaPublisher metaPublisher;
public Qos2PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions, public Qos2PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String, IMessagesStore messagesStore, BrokerInterceptor interceptor, ConcurrentMap<String,
ConnectionDescriptor> connectionDescriptors, ISessionsStore sessionsStore, String brokerPort, ConnectionDescriptor> connectionDescriptors, ISessionsStore sessionsStore, String brokerPort/*,
Qos2Publisher qos2Publisher) { Qos2Publisher qos2Publisher*/) {
this.m_authorizator = authorizator; this.m_authorizator = authorizator;
this.subscriptions = subscriptions; this.subscriptions = subscriptions;
this.m_messagesStore = messagesStore; this.m_messagesStore = messagesStore;
@@ -43,7 +43,7 @@ class Qos2PublishHandler {
this.connectionDescriptors = connectionDescriptors; this.connectionDescriptors = connectionDescriptors;
this.m_sessionsStore = sessionsStore; this.m_sessionsStore = sessionsStore;
this.brokerPort = brokerPort; this.brokerPort = brokerPort;
this.qos2Publisher = qos2Publisher; this.metaPublisher = new MetaPublisher(connectionDescriptors, sessionsStore, messagesStore);
} }
void receivedPublishQos2(Channel channel, PublishMessage msg) { void receivedPublishQos2(Channel channel, PublishMessage msg) {
@@ -95,7 +95,7 @@ class Qos2PublishHandler {
LOG.trace("content <{}>", DebugUtils.payload2Str(evt.getMessage())); LOG.trace("content <{}>", DebugUtils.payload2Str(evt.getMessage()));
LOG.trace("subscription tree {}", subscriptions.dumpTree()); LOG.trace("subscription tree {}", subscriptions.dumpTree());
} }
this.qos2Publisher.publish2Subscribers(evt, topicMatchingSubscriptions); this.metaPublisher.publish2Subscribers(evt, topicMatchingSubscriptions);
if (evt.isRetained()) { if (evt.isRetained()) {
if (!evt.getMessage().hasRemaining()) { if (!evt.getMessage().hasRemaining()) {
@@ -6,18 +6,13 @@ import io.moquette.server.ConnectionDescriptor;
import io.moquette.spi.ClientSession; import io.moquette.spi.ClientSession;
import io.moquette.spi.IMessagesStore; import io.moquette.spi.IMessagesStore;
import io.moquette.spi.ISessionsStore; import io.moquette.spi.ISessionsStore;
import io.moquette.spi.MessageGUID;
import io.moquette.spi.impl.subscriptions.Subscription;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import static io.moquette.spi.impl.ProtocolProcessor.lowerQosToTheSubscriptionDesired;
class Qos2Publisher { class Qos2Publisher {
private static final Logger LOG = LoggerFactory.getLogger(Qos2Publisher.class); private static final Logger LOG = LoggerFactory.getLogger(Qos2Publisher.class);
@@ -32,48 +27,7 @@ class Qos2Publisher {
m_messagesStore = messagesStore; m_messagesStore = messagesStore;
} }
void publish2Subscribers(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) { void publishQos2(ClientSession clientsession, String topic, AbstractMessage.QOSType qos,
final String topic = pubMsg.getTopic();
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
final ByteBuffer origMessage = pubMsg.getMessage();
//if QoS 1 or 2 store the message
MessageGUID guid = null;
if (publishingQos != AbstractMessage.QOSType.MOST_ONE) {
guid = m_messagesStore.storePublishForFuture(pubMsg);
}
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
for (final Subscription sub : topicMatchingSubscriptions) {
AbstractMessage.QOSType qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
ClientSession targetSession = m_sessionsStore.sessionForClient(sub.getClientId());
boolean targetIsActive = this.connectionDescriptors.containsKey(sub.getClientId());
LOG.debug("Broker republishing to client <{}> topic <{}> qos <{}>, active {}",
sub.getClientId(), sub.getTopicFilter(), qos, targetIsActive);
ByteBuffer message = origMessage.duplicate();
if (targetIsActive) {
//QoS 0
if (qos == AbstractMessage.QOSType.MOST_ONE) {
publishQos2(targetSession, topic, qos, message, false, null);
} else {
//QoS 1 or 2
int messageId = targetSession.nextPacketId();
targetSession.inFlightAckWaiting(guid, messageId);
publishQos2(targetSession, topic, qos, message, false, messageId);
}
} else {
if (!targetSession.isCleanSession()) {
//store the message in targetSession queue to deliver
targetSession.enqueueToDeliver(guid);
}
}
}
}
private void publishQos2(ClientSession clientsession, String topic, AbstractMessage.QOSType qos,
ByteBuffer message, boolean retained, Integer messageID) { ByteBuffer message, boolean retained, Integer messageID) {
String clientId = clientsession.clientID; String clientId = clientsession.clientID;
LOG.debug("directSend invoked clientId <{}> on topic <{}> QoS {} retained {} messageID {}", LOG.debug("directSend invoked clientId <{}> on topic <{}> QoS {} retained {} messageID {}",