h2-storage: test folder moved

H2MessagesStore: fixed retain messages
H2MessagesStore: code formater for travis rebuild
H2PersistentQueueTest: performance test disabled
This commit is contained in:
Andreas Mager
2017-08-23 13:32:45 +02:00
parent ed87abaaaa
commit 52415c5561
3 changed files with 11 additions and 22 deletions
@@ -13,18 +13,17 @@
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.moquette.persistence.h2;
import io.moquette.spi.IMatchingCondition;
import io.moquette.spi.IMessagesStore;
import io.moquette.spi.MessageGUID;
import io.moquette.spi.impl.subscriptions.Topic;
import org.h2.mvstore.Cursor;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
class H2MessagesStore implements IMessagesStore {
@@ -33,12 +32,7 @@ class H2MessagesStore implements IMessagesStore {
private final MVStore mvStore;
// maps clientID -> guid
private MVMap<Topic, MessageGUID> retainedStore;
// maps guid to message, it's message store
private MVMap<MessageGUID, StoredMessage> persistentMessageStore;
private MVMap<Topic, StoredMessage> new_retainedStore;
private MVMap<Topic, StoredMessage> retainedStore;
public H2MessagesStore(MVStore mvStore) {
this.mvStore = mvStore;
@@ -47,8 +41,6 @@ class H2MessagesStore implements IMessagesStore {
@Override
public void initStore() {
retainedStore = mvStore.openMap("retained");
persistentMessageStore = mvStore.openMap("oldPersistedMessages");
new_retainedStore = mvStore.openMap("persistedMessages");
LOG.info("Initialized message H2 store");
}
@@ -56,9 +48,9 @@ class H2MessagesStore implements IMessagesStore {
public void storeRetained(Topic topic, StoredMessage storedMessage) {
LOG.debug("Store retained message for topic={}, CId={}", topic, storedMessage.getClientID());
if (storedMessage.getClientID() == null) {
throw new IllegalArgumentException( "Message to be persisted must have a not null client ID");
throw new IllegalArgumentException("Message to be persisted must have a not null client ID");
}
new_retainedStore.put(topic, storedMessage);
retainedStore.put(topic, storedMessage);
}
@Override
@@ -66,19 +58,14 @@ class H2MessagesStore implements IMessagesStore {
LOG.debug("Scanning retained messages");
List<StoredMessage> results = new ArrayList<>();
Cursor<Topic, MessageGUID> mapCursor = retainedStore.cursor(null);
Cursor<Topic, StoredMessage> mapCursor = retainedStore.cursor(null);
while (mapCursor.hasNext()) {
final MessageGUID guid = mapCursor.getValue();
final Topic topic = mapCursor.getKey();
StoredMessage storedMsg = persistentMessageStore.get(guid);
if (condition.match(topic)) {
results.add(storedMsg);
}
Topic topic = mapCursor.next();
if (topic != null && condition.match(topic))
results.add(mapCursor.getValue());
}
if (LOG.isTraceEnabled()) {
LOG.trace("Retained messages have been scanned matchingMessages={}", results);
}
LOG.trace("Retained messages have been scanned matchingMessages={}", results);
return results;
}
@@ -0,0 +1,69 @@
/*
* 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.persistence.MessageStoreTCK;
import io.moquette.server.config.IConfig;
import io.moquette.server.config.MemoryConfig;
import org.junit.After;
import org.junit.Before;
import java.io.File;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static org.junit.Assert.assertFalse;
public class H2MessagesStoreTest extends MessageStoreTCK {
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();
messagesStore = storageService.messagesStore();
sessionsStore = storageService.sessionsStore();
}
@After
public void tearDown() {
if (storageService != null) {
storageService.close();
}
scheduler.shutdown();
cleanPersistenceFile(BrokerConstants.DEFAULT_PERSISTENT_PATH);
}
public static void cleanPersistenceFile(String fileName) {
File dbFile = new File(fileName);
if (dbFile.exists()) {
dbFile.delete();
}
assertFalse(dbFile.exists());
}
}
@@ -0,0 +1,98 @@
package io.moquette.persistence.h2;
import io.moquette.BrokerConstants;
import org.h2.mvstore.MVStore;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.*;
public class H2PersistentQueueTest {
private MVStore mvStore;
@Before
public void setUp() {
this.mvStore = new MVStore.Builder()
.fileName(BrokerConstants.DEFAULT_PERSISTENT_PATH)
.autoCommitDisabled()
.open();
}
@After
public void tearDown() {
File dbFile = new File(BrokerConstants.DEFAULT_PERSISTENT_PATH);
if (dbFile.exists()) {
dbFile.delete();
}
assertFalse(dbFile.exists());
}
@Test
public void testAdd() {
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
sut.add("Hello");
sut.add("world");
assertEquals("Hello", sut.peek());
assertEquals("Hello", sut.peek());
assertEquals("peek just return elements, doesn't remove them", 2, sut.size());
}
@Test
public void testPoll() {
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
sut.add("Hello");
sut.add("world");
assertEquals("Hello", sut.poll());
assertEquals("world", sut.poll());
assertTrue("after poll 2 elements inserted before, should be empty", sut.isEmpty());
}
@Ignore
@Test
public void testPerformance() {
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
int numIterations = 10000000;
for (int i=0; i < numIterations; i++) {
sut.add("Hello");
}
mvStore.commit();
for (int i=0; i < numIterations; i++) {
assertEquals("Hello", sut.poll());
}
assertTrue("should be empty", sut.isEmpty());
}
@Test
public void testReloadFromPersistedState() {
H2PersistentQueue<String> before = new H2PersistentQueue<>(this.mvStore, "test");
before.add("Hello");
before.add("crazy");
before.add("world");
assertEquals("Hello", before.poll());
this.mvStore.commit();
this.mvStore.close();
this.mvStore = new MVStore.Builder()
.fileName(BrokerConstants.DEFAULT_PERSISTENT_PATH)
.autoCommitDisabled()
.open();
//now reload the persisted state
H2PersistentQueue<String> after = new H2PersistentQueue<>(this.mvStore, "test");
assertEquals("crazy", after.poll());
assertEquals("world", after.poll());
assertTrue("should be empty", after.isEmpty());
}
}