Added tracking of closed session time and expired session cleaner for clients that never reconnects but had clean session flag = false
This commit is contained in:
@@ -27,6 +27,7 @@ import org.h2.mvstore.MVStore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
@@ -43,6 +44,7 @@ public class H2SessionsStore implements ISessionsStore, ISubscriptionsStore {
|
||||
private Map<String, Set<Integer>> inFlightIds;
|
||||
// maps clientID->[MessageId -> guid]
|
||||
private ConcurrentMap<String, ConcurrentMap<Integer, StoredMessage>> secondPhaseStore;
|
||||
private MVMap<LocalDateTime, Set<String>> sessionsClosingTimes;
|
||||
|
||||
H2SessionsStore(MVStore mvStore) {
|
||||
this.mvStore = mvStore;
|
||||
@@ -54,6 +56,7 @@ public class H2SessionsStore implements ISessionsStore, ISubscriptionsStore {
|
||||
this.outboundFlightMessages = mvStore.openMap("outboundFlight");
|
||||
this.inFlightIds = mvStore.openMap("inflightPacketIDs");
|
||||
this.secondPhaseStore = mvStore.openMap("secondPhase");
|
||||
this.sessionsClosingTimes = mvStore.openMap("sessionsCreationTimes");
|
||||
LOG.info("Initialized sessions H2 store");
|
||||
}
|
||||
|
||||
@@ -290,4 +293,23 @@ public class H2SessionsStore implements ISessionsStore, ISubscriptionsStore {
|
||||
this.secondPhaseStore.remove(clientID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void trackSessionClose(LocalDateTime when, String clientID) {
|
||||
this.sessionsClosingTimes.putIfAbsent(when, new HashSet<>());
|
||||
this.sessionsClosingTimes.computeIfPresent(when, (key, oldSet) -> {
|
||||
oldSet.add(clientID);
|
||||
return oldSet;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> sessionOlderThan(LocalDateTime queryPin) {
|
||||
final Set<String> results = new HashSet<>();
|
||||
LocalDateTime keyBefore = this.sessionsClosingTimes.lowerKey(queryPin);
|
||||
while (keyBefore != null) {
|
||||
results.addAll(this.sessionsClosingTimes.get(keyBefore));
|
||||
keyBefore = this.sessionsClosingTimes.lowerKey(keyBefore);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class H2MessagesStoreTest extends MessageStoreTCK {
|
||||
storageService.initStore();
|
||||
messagesStore = storageService.messagesStore();
|
||||
sessionsStore = storageService.sessionsStore();
|
||||
this.sessionsRepository = new SessionsRepository(sessionsStore);
|
||||
this.sessionsRepository = new SessionsRepository(sessionsStore, null);
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2017 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.h2;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import io.moquette.server.config.IConfig;
|
||||
import io.moquette.server.config.MemoryConfig;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static io.moquette.persistence.h2.H2MessagesStoreTest.cleanPersistenceFile;
|
||||
import static java.time.LocalDateTime.now;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class H2SessionsStoreTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(H2SessionsStoreTest.class);
|
||||
|
||||
private H2PersistentStore storageService;
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
scheduler = Executors.newScheduledThreadPool(1);
|
||||
|
||||
cleanPersistenceFile(BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
Properties props = new Properties();
|
||||
props.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
IConfig conf = new MemoryConfig(props);
|
||||
storageService = new H2PersistentStore(conf, scheduler);
|
||||
storageService.initStore();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (storageService != null) {
|
||||
storageService.close();
|
||||
}
|
||||
|
||||
scheduler.shutdown();
|
||||
cleanPersistenceFile(BrokerConstants.DEFAULT_PERSISTENT_PATH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryByExpirationDate() {
|
||||
H2SessionsStore sessionsStore = (H2SessionsStore) storageService.sessionsStore();
|
||||
LocalDateTime sessionCreationTime = LocalDateTime.of(2017, 10, 1, 10, 0, 0);
|
||||
sessionsStore.trackSessionClose(sessionCreationTime, "Sensor1");
|
||||
|
||||
LocalDateTime session2CreationTime = LocalDateTime.of(2017, 11, 1, 10, 0, 0);
|
||||
sessionsStore.trackSessionClose(session2CreationTime, "Sensor2");
|
||||
|
||||
//Exercise
|
||||
LocalDateTime queryPin = LocalDateTime.of(2017, 10, 20, 10, 0, 0);
|
||||
Set<String> sessions = sessionsStore.sessionOlderThan(queryPin);
|
||||
|
||||
//Verify
|
||||
assertThat(sessions).contains("Sensor1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListAllSessionsOlderThan() {
|
||||
H2SessionsStore sessionsStore = (H2SessionsStore) storageService.sessionsStore();
|
||||
LocalDateTime sessionCreationTime = LocalDateTime.of(2017, 10, 1, 10, 0, 0);
|
||||
sessionsStore.trackSessionClose(sessionCreationTime, "Sensor1");
|
||||
|
||||
LocalDateTime session2CreationTime = LocalDateTime.of(2017, 10, 2, 10, 0, 0);
|
||||
sessionsStore.trackSessionClose(session2CreationTime, "Sensor2");
|
||||
|
||||
//Exercise
|
||||
LocalDateTime queryPin = LocalDateTime.of(2017, 10, 20, 10, 0, 0);
|
||||
Set<String> sessions = sessionsStore.sessionOlderThan(queryPin);
|
||||
|
||||
//Verify
|
||||
assertThat(sessions).contains("Sensor1", "Sensor2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetrieve1000ExpiredSessionPerformance() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
H2SessionsStore sessionsStore = (H2SessionsStore) storageService.sessionsStore();
|
||||
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
final Future<?> future1 = executorService.submit(inserterForOffset(sessionsStore, 0));
|
||||
final Future<?> future2 = executorService.submit(inserterForOffset(sessionsStore, 50000));
|
||||
future1.get(2, TimeUnit.SECONDS);
|
||||
future2.get(2, TimeUnit.SECONDS);
|
||||
// //prepare the fixture
|
||||
// for (int i = 0; i < 100000; i++) {
|
||||
// final LocalDateTime timePin = now().minusSeconds(1);
|
||||
// sessionsStore.trackSessionClose(timePin, "Sensor" + i);
|
||||
// }
|
||||
|
||||
//Exercise
|
||||
LOG.warn("Start search");
|
||||
Set<String> sessions = sessionsStore.sessionOlderThan(now());
|
||||
LOG.warn("Finish search");
|
||||
|
||||
//Verify
|
||||
assertThat(sessions.size()).isEqualTo(100000);
|
||||
}
|
||||
|
||||
private Runnable inserterForOffset(H2SessionsStore sessionsStore, int start) {
|
||||
return () -> {
|
||||
//prepare the fixture
|
||||
for (int i = start; i < start + 50000; i++) {
|
||||
final LocalDateTime timePin = now().minusSeconds(1);
|
||||
sessionsStore.trackSessionClose(timePin, "Sensor" + i);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user