Search in sources :

Example 56 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingTest method testTwoQueues.

@Test
public void testTwoQueues() throws Exception {
    boolean persistentMessages = true;
    clearDataRecreateServerDirs();
    Configuration config = createDefaultInVMConfig().setJournalSyncNonTransactional(false);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    final int messageSize = 1024;
    final int numberOfMessages = 1000;
    try {
        ServerLocator locator = createInVMNonHALocator().setClientFailureCheckPeriod(120000).setConnectionTTL(5000000).setCallTimeout(120000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
        ClientSessionFactory sf = locator.createSessionFactory();
        ClientSession session = sf.createSession(false, false, false);
        session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS.concat("=1"), null, true);
        session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS.concat("=2"), null, true);
        ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
        ClientMessage message = null;
        byte[] body = new byte[messageSize];
        for (int i = 0; i < numberOfMessages; i++) {
            message = session.createMessage(persistentMessages);
            ActiveMQBuffer bodyLocal = message.getBodyBuffer();
            bodyLocal.writeBytes(body);
            message.putIntProperty("propTest", i % 2 == 0 ? 1 : 2);
            producer.send(message);
            if (i % 1000 == 0) {
                session.commit();
            }
        }
        session.commit();
        session.start();
        for (int msg = 1; msg <= 2; msg++) {
            ClientConsumer consumer = session.createConsumer(PagingTest.ADDRESS.concat("=" + msg));
            for (int i = 0; i < numberOfMessages; i++) {
                message = consumer.receive(5000);
                assertNotNull(message);
                message.acknowledge();
                // assertEquals(msg, message.getIntProperty("propTest").intValue());
                System.out.println("i = " + i + " msg = " + message.getIntProperty("propTest"));
            }
            session.commit();
            assertNull(consumer.receiveImmediate());
            consumer.close();
        }
        PagingStore store = server.getPagingManager().getPageStore(ADDRESS);
        store.getCursorProvider().cleanup();
        long timeout = System.currentTimeMillis() + 5000;
        while (store.isPaging() && timeout > System.currentTimeMillis()) {
            Thread.sleep(100);
        }
        store.getCursorProvider().cleanup();
        waitForNotPaging(server.locateQueue(PagingTest.ADDRESS.concat("=1")));
        sf.close();
        locator.close();
    } finally {
        try {
            server.stop();
        } catch (Throwable ignored) {
        }
    }
}
Also used : DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 57 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingTest method testPageAndDepageRapidly.

@Test
public void testPageAndDepageRapidly() throws Exception {
    boolean persistentMessages = true;
    clearDataRecreateServerDirs();
    Configuration config = createDefaultInVMConfig().setJournalSyncNonTransactional(false).setJournalFileSize(10 * 1024 * 1024);
    server = createServer(true, config, 100 * 1024, 1024 * 1024 / 2);
    server.start();
    final int messageSize = 51527;
    final int numberOfMessages = 200;
    locator = createInVMNonHALocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, true);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    final AtomicInteger errors = new AtomicInteger(0);
    Thread consumeThread = new Thread() {

        @Override
        public void run() {
            ClientSession sessionConsumer = null;
            try {
                sessionConsumer = sf.createSession(false, false);
                sessionConsumer.start();
                ClientConsumer cons = sessionConsumer.createConsumer(ADDRESS);
                for (int i = 0; i < numberOfMessages; i++) {
                    ClientMessage msg = cons.receive(PagingTest.RECEIVE_TIMEOUT);
                    assertNotNull(msg);
                    msg.acknowledge();
                    if (i % 20 == 0) {
                        sessionConsumer.commit();
                    }
                }
                sessionConsumer.commit();
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                try {
                    sessionConsumer.close();
                } catch (ActiveMQException e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
            }
        }
    };
    consumeThread.start();
    ClientMessage message = null;
    byte[] body = new byte[messageSize];
    for (int i = 0; i < numberOfMessages; i++) {
        message = session.createMessage(persistentMessages);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        message.putIntProperty(new SimpleString("id"), i);
        producer.send(message);
        Thread.sleep(50);
    }
    consumeThread.join();
    assertEquals(0, errors.get());
    long timeout = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < timeout && (server.getPagingManager().getPageStore(ADDRESS).isPaging() || server.getPagingManager().getPageStore(ADDRESS).getNumberOfPages() != 1)) {
        Thread.sleep(1);
    }
    // It's async, so need to wait a bit for it happening
    assertFalse(server.getPagingManager().getPageStore(ADDRESS).isPaging());
    assertEquals(1, server.getPagingManager().getPageStore(ADDRESS).getNumberOfPages());
}
Also used : DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 58 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingTest method internaltestSendReceivePaging.

private void internaltestSendReceivePaging(final boolean persistentMessages) throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = createDefaultInVMConfig().setJournalSyncNonTransactional(false);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    final int numberOfIntegers = 256;
    final int numberOfMessages = 1000;
    locator = createInVMNonHALocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(null, null, false, true, true, false, 0);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    Queue queue = server.locateQueue(ADDRESS);
    queue.getPageSubscription().getPagingStore().startPaging();
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    ClientMessage message = null;
    byte[] body = new byte[numberOfIntegers * 4];
    ByteBuffer bb = ByteBuffer.wrap(body);
    for (int j = 1; j <= numberOfIntegers; j++) {
        bb.putInt(j);
    }
    for (int i = 0; i < numberOfMessages; i++) {
        message = session.createMessage(persistentMessages);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        message.putIntProperty(new SimpleString("id"), i);
        producer.send(message);
    }
    session.close();
    sf.close();
    locator.close();
    server.stop();
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    locator = createInVMNonHALocator();
    sf = createSessionFactory(locator);
    session = sf.createSession(null, null, false, true, true, false, 0);
    ClientConsumer consumer = session.createConsumer(PagingTest.ADDRESS);
    session.start();
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage message2 = consumer.receive(PagingTest.RECEIVE_TIMEOUT);
        Assert.assertNotNull(message2);
        Assert.assertEquals(i, message2.getIntProperty("id").intValue());
        assertEquals(body.length, message2.getBodySize());
        message2.acknowledge();
        Assert.assertNotNull(message2);
        if (i % 1000 == 0) {
            session.commit();
        }
        try {
            assertBodiesEqual(body, message2.getBodyBuffer());
        } catch (AssertionError e) {
            PagingTest.log.info("Expected buffer:" + ActiveMQTestBase.dumpBytesHex(body, 40));
            PagingTest.log.info("Arriving buffer:" + ActiveMQTestBase.dumpBytesHex(message2.getBodyBuffer().toByteBuffer().array(), 40));
            throw e;
        }
    }
    consumer.close();
    session.close();
}
Also used : DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Queue(org.apache.activemq.artemis.core.server.Queue) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ByteBuffer(java.nio.ByteBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 59 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingTest method testSendOverBlockingNoFlowControl.

@Test
public void testSendOverBlockingNoFlowControl() throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = createDefaultInVMConfig().setJournalSyncNonTransactional(false);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.getAddressSettingsRepository().getMatch("#").setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
    server.start();
    final int biggerMessageSize = 10 * 1024;
    final int numberOfMessages = 500;
    locator = createInVMNonHALocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setProducerWindowSize(-1).setMinLargeMessageSize(1024 * 1024);
    sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, false, false);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    ClientMessage message = null;
    byte[] body = new byte[biggerMessageSize];
    ByteBuffer bb = ByteBuffer.wrap(body);
    for (int j = 1; j <= biggerMessageSize; j++) {
        bb.put(getSamplebyte(j));
    }
    for (int i = 0; i < numberOfMessages; i++) {
        message = session.createMessage(true);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        message.putIntProperty(new SimpleString("id"), i);
        producer.send(message);
        if (i % 10 == 0) {
            session.commit();
        }
    }
    session.commit();
    session.start();
    ClientConsumer cons = session.createConsumer(ADDRESS);
    for (int i = 0; i < numberOfMessages; i++) {
        message = cons.receive(5000);
        assertNotNull(message);
        message.acknowledge();
        if (i % 10 == 0) {
            session.commit();
        }
    }
    session.commit();
}
Also used : DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ByteBuffer(java.nio.ByteBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 60 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingTest method testRestartWithComplete.

// The pages are complete, and this is simulating a scenario where the server crashed before deleting the pages.
@Test
public void testRestartWithComplete() throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = createDefaultInVMConfig();
    final AtomicBoolean mainCleanup = new AtomicBoolean(true);
    class InterruptedCursorProvider extends PageCursorProviderImpl {

        InterruptedCursorProvider(PagingStore pagingStore, StorageManager storageManager, ArtemisExecutor executor, int maxCacheSize) {
            super(pagingStore, storageManager, executor, maxCacheSize);
        }

        @Override
        public void cleanup() {
            if (mainCleanup.get()) {
                super.cleanup();
            } else {
                try {
                    pagingStore.unlock();
                } catch (Throwable ignored) {
                }
            }
        }
    }
    server = new ActiveMQServerImpl(config, ManagementFactory.getPlatformMBeanServer(), new ActiveMQSecurityManagerImpl()) {

        @Override
        protected PagingStoreFactoryNIO getPagingStoreFactory() {
            return new PagingStoreFactoryNIO(this.getStorageManager(), this.getConfiguration().getPagingLocation(), this.getConfiguration().getJournalBufferTimeout_NIO(), this.getScheduledPool(), this.getExecutorFactory(), this.getConfiguration().isJournalSyncNonTransactional(), null) {

                @Override
                public PageCursorProvider newCursorProvider(PagingStore store, StorageManager storageManager, AddressSettings addressSettings, ArtemisExecutor executor) {
                    return new InterruptedCursorProvider(store, storageManager, executor, addressSettings.getPageCacheMaxSize());
                }
            };
        }
    };
    addServer(server);
    AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(PagingTest.PAGE_SIZE).setMaxSizeBytes(PagingTest.PAGE_MAX).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    server.getAddressSettingsRepository().addMatch("#", defaultSetting);
    server.start();
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, true, 0);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    Queue queue = server.locateQueue(ADDRESS);
    queue.getPageSubscription().getPagingStore().startPaging();
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    ClientMessage message;
    for (int i = 0; i < 20; i++) {
        message = session.createMessage(true);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(new byte[100 * 4]);
        message.putIntProperty(new SimpleString("idi"), i);
        producer.send(message);
        session.commit();
        if (i < 19) {
            queue.getPageSubscription().getPagingStore().forceAnotherPage();
        }
    }
    Assert.assertEquals(20, queue.getPageSubscription().getPagingStore().getCurrentWritingPage());
    // This will force a scenario where the pages are cleaned up. When restarting we need to check if the current page is complete
    // if it is complete we must move to another page avoiding races on cleanup
    // which could happen during a crash / restart
    long tx = server.getStorageManager().generateID();
    for (int i = 1; i <= 20; i++) {
        server.getStorageManager().storePageCompleteTransactional(tx, queue.getID(), new PagePositionImpl(i, 1));
    }
    server.getStorageManager().commit(tx);
    session.close();
    sf.close();
    server.stop();
    mainCleanup.set(false);
    logger.trace("Server restart");
    server.start();
    queue = server.locateQueue(ADDRESS);
    locator = createInVMNonHALocator();
    sf = createSessionFactory(locator);
    session = sf.createSession(null, null, false, false, true, false, 0);
    producer = session.createProducer(PagingTest.ADDRESS);
    for (int i = 0; i < 10; i++) {
        message = session.createMessage(true);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(new byte[100 * 4]);
        message.putIntProperty(new SimpleString("newid"), i);
        producer.send(message);
        session.commit();
        if (i == 5) {
            queue.getPageSubscription().getPagingStore().forceAnotherPage();
        }
    }
    mainCleanup.set(true);
    queue = server.locateQueue(ADDRESS);
    queue.getPageSubscription().cleanupEntries(false);
    queue.getPageSubscription().getPagingStore().getCursorProvider().cleanup();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    session.start();
    for (int i = 0; i < 10; i++) {
        message = consumer.receive(5000);
        Assert.assertNotNull(message);
        Assert.assertEquals(i, message.getIntProperty("newid").intValue());
        message.acknowledge();
    }
    server.stop();
// Thread.sleep(5000);
}
Also used : ActiveMQSecurityManagerImpl(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManagerImpl) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) ArtemisExecutor(org.apache.activemq.artemis.utils.actors.ArtemisExecutor) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) PageCursorProviderImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PageCursorProviderImpl) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) PagePositionImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PagingStoreFactoryNIO(org.apache.activemq.artemis.core.paging.impl.PagingStoreFactoryNIO) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) PageCursorProvider(org.apache.activemq.artemis.core.paging.cursor.PageCursorProvider) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) Queue(org.apache.activemq.artemis.core.server.Queue) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)150 Test (org.junit.Test)81 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)49 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)44 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)39 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)38 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 ByteBuffer (java.nio.ByteBuffer)34 Configuration (org.apache.activemq.artemis.core.config.Configuration)34 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)27 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)25 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)25 ArrayList (java.util.ArrayList)21 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)21 Queue (org.apache.activemq.artemis.core.server.Queue)18 HashMap (java.util.HashMap)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)15 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)15 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)14