Added QueueRepository implemented in memory and on H2
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package io.moquette.broker;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
public interface IQueueRepository {
|
||||
|
||||
Queue<SessionRegistry.EnqueuedMessage> createQueue(String cli, boolean clean);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.moquette.broker;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class MemoryQueueRepository implements IQueueRepository {
|
||||
|
||||
@Override
|
||||
public Queue<SessionRegistry.EnqueuedMessage> createQueue(String cli, boolean clean) {
|
||||
return new ConcurrentLinkedQueue<>();
|
||||
}
|
||||
}
|
||||
@@ -161,18 +161,21 @@ public class Server {
|
||||
authorizatorPolicy = initializeAuthorizatorPolicy(authorizatorPolicy, config);
|
||||
|
||||
final ISubscriptionsRepository subscriptionsRepository;
|
||||
final IQueueRepository queueRepository;
|
||||
if (persistencePath != null && !persistencePath.isEmpty()) {
|
||||
LOG.trace("Configuring H2 subscriptions store to {}", persistencePath);
|
||||
h2Builder = new H2Builder(config, scheduler);
|
||||
subscriptionsRepository = h2Builder.initStore().subscriptionsRepository();
|
||||
h2Builder = new H2Builder(config, scheduler).initStore();
|
||||
subscriptionsRepository = h2Builder.subscriptionsRepository();
|
||||
queueRepository = h2Builder.queueRepository();
|
||||
} else {
|
||||
LOG.trace("Configuring in-memory subscriptions store");
|
||||
subscriptionsRepository = new MemorySubscriptionsRepository();
|
||||
queueRepository = new MemoryQueueRepository();
|
||||
}
|
||||
|
||||
ISubscriptionsDirectory subscriptions = new CTrieSubscriptionDirectory();
|
||||
subscriptions.init(subscriptionsRepository);
|
||||
SessionRegistry sessions = new SessionRegistry(subscriptions, interceptor);
|
||||
SessionRegistry sessions = new SessionRegistry(subscriptions, queueRepository);
|
||||
dispatcher = new PostOffice(subscriptions, authorizatorPolicy, new MemoryRetainedRepository(), sessions,
|
||||
interceptor);
|
||||
final BrokerConfiguration brokerConfig = new BrokerConfiguration(config);
|
||||
|
||||
@@ -97,10 +97,8 @@ class Session {
|
||||
private final AtomicInteger inflightSlots = new AtomicInteger(INFLIGHT_WINDOW_SIZE); // this should be configurable
|
||||
|
||||
Session(String clientId, boolean clean, Will will, Queue<SessionRegistry.EnqueuedMessage> sessionQueue) {
|
||||
this.clientId = clientId;
|
||||
this.clean = clean;
|
||||
this(clean, clientId, sessionQueue);
|
||||
this.will = will;
|
||||
this.sessionQueue = sessionQueue;
|
||||
}
|
||||
|
||||
Session(boolean clean, String clientId, Queue<SessionRegistry.EnqueuedMessage> sessionQueue) {
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package io.moquette.broker;
|
||||
|
||||
import io.moquette.broker.Session.SessionStatus;
|
||||
import io.moquette.interception.BrokerInterceptor;
|
||||
import io.moquette.broker.subscriptions.ISubscriptionsDirectory;
|
||||
import io.moquette.broker.subscriptions.Subscription;
|
||||
import io.moquette.broker.subscriptions.Topic;
|
||||
@@ -32,9 +31,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
class SessionRegistry {
|
||||
public class SessionRegistry {
|
||||
|
||||
abstract static class EnqueuedMessage {
|
||||
public abstract static class EnqueuedMessage {
|
||||
}
|
||||
|
||||
static class PublishedMessage extends EnqueuedMessage {
|
||||
@@ -61,10 +60,12 @@ class SessionRegistry {
|
||||
|
||||
private final ConcurrentMap<String, Session> pool = new ConcurrentHashMap<>();
|
||||
private final ISubscriptionsDirectory subscriptionsDirectory;
|
||||
private final IQueueRepository queueRepository;
|
||||
private final ConcurrentMap<String, Queue<SessionRegistry.EnqueuedMessage>> queues = new ConcurrentHashMap<>();
|
||||
|
||||
SessionRegistry(ISubscriptionsDirectory subscriptionsDirectory, BrokerInterceptor m_interceptor) {
|
||||
SessionRegistry(ISubscriptionsDirectory subscriptionsDirectory, IQueueRepository queueRepository) {
|
||||
this.subscriptionsDirectory = subscriptionsDirectory;
|
||||
this.queueRepository = queueRepository;
|
||||
}
|
||||
|
||||
void bindToSession(MQTTConnection mqttConnection, MqttConnectMessage msg, String clientId) {
|
||||
@@ -178,9 +179,9 @@ class SessionRegistry {
|
||||
}
|
||||
|
||||
private Session createNewSession(MQTTConnection mqttConnection, MqttConnectMessage msg, String clientId) {
|
||||
final Queue<SessionRegistry.EnqueuedMessage> sessionQueue =
|
||||
queues.computeIfAbsent(clientId, (String cli) -> new ConcurrentLinkedQueue<>());
|
||||
final boolean clean = msg.variableHeader().isCleanSession();
|
||||
final Queue<SessionRegistry.EnqueuedMessage> sessionQueue =
|
||||
queues.computeIfAbsent(clientId, (String cli) -> queueRepository.createQueue(cli, clean));
|
||||
final Session newSession;
|
||||
if (msg.variableHeader().isWillFlag()) {
|
||||
final Session.Will will = createWill(msg);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.moquette.persistence;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import io.moquette.broker.IQueueRepository;
|
||||
import io.moquette.broker.ISubscriptionsRepository;
|
||||
import io.moquette.broker.config.IConfig;
|
||||
import org.h2.mvstore.MVStore;
|
||||
@@ -52,4 +53,8 @@ public class H2Builder {
|
||||
public void closeStore() {
|
||||
mvStore.close();
|
||||
}
|
||||
|
||||
public IQueueRepository queueRepository() {
|
||||
return new H2QueueRepository(mvStore);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2018 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
package io.moquette.persistence;
|
||||
|
||||
import org.h2.mvstore.MVMap;
|
||||
import org.h2.mvstore.MVStore;
|
||||
|
||||
import java.util.AbstractQueue;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
class H2PersistentQueue<T> extends AbstractQueue<T> {
|
||||
|
||||
private final MVMap<Long, T> queueMap;
|
||||
private final MVMap<String, Long> metadataMap;
|
||||
private final AtomicLong head;
|
||||
private final AtomicLong tail;
|
||||
|
||||
H2PersistentQueue(MVStore store, String queueName) {
|
||||
if (queueName == null || queueName.isEmpty()) {
|
||||
throw new IllegalArgumentException("queueName parameter can't be empty or null");
|
||||
}
|
||||
this.queueMap = store.openMap("queue_" + queueName);
|
||||
this.metadataMap = store.openMap("queue_" + queueName + "_meta");
|
||||
|
||||
//setup head index
|
||||
long headIdx = 0L;
|
||||
if (this.metadataMap.containsKey("head")) {
|
||||
headIdx = this.metadataMap.get("head");
|
||||
} else {
|
||||
this.metadataMap.put("head", headIdx);
|
||||
}
|
||||
this.head = new AtomicLong(headIdx);
|
||||
|
||||
//setup tail index
|
||||
long tailIdx = 0L;
|
||||
if (this.metadataMap.containsKey("tail")) {
|
||||
tailIdx = this.metadataMap.get("tail");
|
||||
} else {
|
||||
this.metadataMap.put("tail", tailIdx);
|
||||
}
|
||||
this.tail = new AtomicLong(tailIdx);
|
||||
}
|
||||
|
||||
static void dropQueue(MVStore store, String queueName) {
|
||||
store.removeMap(store.openMap("queue_" + queueName));
|
||||
store.removeMap(store.openMap("queue_" + queueName + "_meta"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.head.intValue() - this.tail.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offer(T t) {
|
||||
if (t == null) {
|
||||
throw new NullPointerException("Inserted element can't be null");
|
||||
}
|
||||
final long nextHead = head.getAndIncrement();
|
||||
this.queueMap.put(nextHead, t);
|
||||
this.metadataMap.put("head", nextHead + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T poll() {
|
||||
if (head.equals(tail)) {
|
||||
return null;
|
||||
}
|
||||
final long nextTail = tail.getAndIncrement();
|
||||
final T tail = this.queueMap.get(nextTail);
|
||||
queueMap.remove(nextTail);
|
||||
this.metadataMap.put("tail", nextTail + 1);
|
||||
return tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T peek() {
|
||||
if (head.equals(tail)) {
|
||||
return null;
|
||||
}
|
||||
return this.queueMap.get(tail.get());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.moquette.persistence;
|
||||
|
||||
import io.moquette.broker.IQueueRepository;
|
||||
import io.moquette.broker.SessionRegistry;
|
||||
import org.h2.mvstore.MVStore;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class H2QueueRepository implements IQueueRepository {
|
||||
|
||||
private MVStore mvStore;
|
||||
|
||||
public H2QueueRepository(MVStore mvStore) {
|
||||
this.mvStore = mvStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queue<SessionRegistry.EnqueuedMessage> createQueue(String cli, boolean clean) {
|
||||
if (!clean) {
|
||||
return new H2PersistentQueue<>(mvStore, cli);
|
||||
}
|
||||
return new ConcurrentLinkedQueue<>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user