Search in sources :

Example 1 with RoutingContextImpl

use of org.apache.activemq.artemis.core.server.impl.RoutingContextImpl in project activemq-artemis by apache.

the class PostOfficeImpl method redistribute.

/**
 * The redistribution can't process the route right away as we may be dealing with a large message which will need to be processed on a different thread
 */
@Override
public Pair<RoutingContext, Message> redistribute(final Message message, final Queue originatingQueue, final Transaction tx) throws Exception {
    // We have to copy the message and store it separately, otherwise we may lose remote bindings in case of restart before the message
    // arrived the target node
    // as described on https://issues.jboss.org/browse/JBPAPP-6130
    Message copyRedistribute = message.copy(storageManager.generateID());
    Bindings bindings = addressManager.getBindingsForRoutingAddress(originatingQueue.getAddress());
    if (bindings != null) {
        RoutingContext context = new RoutingContextImpl(tx);
        boolean routed = bindings.redistribute(copyRedistribute, originatingQueue, context);
        if (routed) {
            return new Pair<>(context, copyRedistribute);
        }
    }
    return null;
}
Also used : RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext) LargeServerMessage(org.apache.activemq.artemis.core.server.LargeServerMessage) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) Pair(org.apache.activemq.artemis.api.core.Pair)

Example 2 with RoutingContextImpl

use of org.apache.activemq.artemis.core.server.impl.RoutingContextImpl in project activemq-artemis by apache.

the class PagingStoreImplTest method testConcurrentPaging.

protected void testConcurrentPaging(final SequentialFileFactory factory, final int numberOfThreads) throws Exception {
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    final int MAX_SIZE = 1024 * 10;
    final AtomicLong messageIdGenerator = new AtomicLong(0);
    final AtomicInteger aliveProducers = new AtomicInteger(numberOfThreads);
    final CountDownLatch latchStart = new CountDownLatch(numberOfThreads);
    final ConcurrentHashMap<Long, Message> buffers = new ConcurrentHashMap<>();
    final ArrayList<Page> readPages = new ArrayList<>();
    AddressSettings settings = new AddressSettings().setPageSizeBytes(MAX_SIZE).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    final PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    // Marked the store to be paged
    storeImpl.startPaging();
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    final SimpleString destination = new SimpleString("test");
    class WriterThread extends Thread {

        Exception e;

        @Override
        public void run() {
            try {
                boolean firstTime = true;
                while (true) {
                    long id = messageIdGenerator.incrementAndGet();
                    // Each thread will Keep paging until all the messages are depaged.
                    // This is possible because the depage thread is not actually reading the pages.
                    // Just using the internal API to remove it from the page file system
                    Message msg = createMessage(id, storeImpl, destination, createRandomBuffer(id, 5));
                    final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
                    if (storeImpl.page(msg, ctx2.getTransaction(), ctx2.getContextListing(storeImpl.getStoreName()), lock)) {
                        buffers.put(id, msg);
                    } else {
                        break;
                    }
                    if (firstTime) {
                        // We have at least one data paged. So, we can start depaging now
                        latchStart.countDown();
                        firstTime = false;
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                this.e = e1;
            } finally {
                aliveProducers.decrementAndGet();
            }
        }
    }
    final class ReaderThread extends Thread {

        Exception e;

        @Override
        public void run() {
            try {
                // Wait every producer to produce at least one message
                ActiveMQTestBase.waitForLatch(latchStart);
                while (aliveProducers.get() > 0) {
                    Page page = storeImpl.depage();
                    if (page != null) {
                        readPages.add(page);
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                this.e = e1;
            }
        }
    }
    WriterThread[] producerThread = new WriterThread[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        producerThread[i] = new WriterThread();
        producerThread[i].start();
    }
    ReaderThread consumer = new ReaderThread();
    consumer.start();
    for (int i = 0; i < numberOfThreads; i++) {
        producerThread[i].join();
        if (producerThread[i].e != null) {
            throw producerThread[i].e;
        }
    }
    consumer.join();
    if (consumer.e != null) {
        throw consumer.e;
    }
    final ConcurrentMap<Long, Message> buffers2 = new ConcurrentHashMap<>();
    for (Page page : readPages) {
        page.open();
        List<PagedMessage> msgs = page.read(new NullStorageManager());
        page.close();
        for (PagedMessage msg : msgs) {
            long id = msg.getMessage().toCore().getBodyBuffer().readLong();
            msg.getMessage().toCore().getBodyBuffer().resetReaderIndex();
            Message msgWritten = buffers.remove(id);
            buffers2.put(id, msg.getMessage());
            Assert.assertNotNull(msgWritten);
            Assert.assertEquals(msg.getMessage().getAddress(), msgWritten.getAddress());
            ActiveMQTestBase.assertEqualsBuffers(10, msgWritten.toCore().getBodyBuffer(), msg.getMessage().toCore().getBodyBuffer());
        }
    }
    Assert.assertEquals(0, buffers.size());
    List<String> files = factory.listFiles("page");
    Assert.assertTrue(files.size() != 0);
    for (String file : files) {
        SequentialFile fileTmp = factory.createSequentialFile(file);
        fileTmp.open();
        Assert.assertTrue("The page file size (" + fileTmp.size() + ") shouldn't be > " + MAX_SIZE, fileTmp.size() <= MAX_SIZE);
        fileTmp.close();
    }
    PagingStore storeImpl2 = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, new SimpleString("test"), settings, getExecutorFactory().getExecutor(), true);
    storeImpl2.start();
    int numberOfPages = storeImpl2.getNumberOfPages();
    Assert.assertTrue(numberOfPages != 0);
    storeImpl2.startPaging();
    storeImpl2.startPaging();
    Assert.assertEquals(numberOfPages, storeImpl2.getNumberOfPages());
    long lastMessageId = messageIdGenerator.incrementAndGet();
    Message lastMsg = createMessage(lastMessageId, storeImpl, destination, createRandomBuffer(lastMessageId, 5));
    storeImpl2.forceAnotherPage();
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    storeImpl2.page(lastMsg, ctx.getTransaction(), ctx.getContextListing(storeImpl2.getStoreName()), lock);
    buffers2.put(lastMessageId, lastMsg);
    Page lastPage = null;
    while (true) {
        Page page = storeImpl2.depage();
        if (page == null) {
            break;
        }
        lastPage = page;
        page.open();
        List<PagedMessage> msgs = page.read(new NullStorageManager());
        page.close();
        for (PagedMessage msg : msgs) {
            long id = msg.getMessage().toCore().getBodyBuffer().readLong();
            Message msgWritten = buffers2.remove(id);
            Assert.assertNotNull(msgWritten);
            Assert.assertEquals(msg.getMessage().getAddress(), msgWritten.getAddress());
            ActiveMQTestBase.assertEqualsByteArrays(msgWritten.toCore().getBodyBuffer().writerIndex(), msgWritten.toCore().getBodyBuffer().toByteBuffer().array(), msg.getMessage().toCore().getBodyBuffer().toByteBuffer().array());
        }
    }
    lastPage.open();
    List<PagedMessage> lastMessages = lastPage.read(new NullStorageManager());
    lastPage.close();
    Assert.assertEquals(1, lastMessages.size());
    lastMessages.get(0).getMessage().toCore().getBodyBuffer().resetReaderIndex();
    Assert.assertEquals(lastMessages.get(0).getMessage().toCore().getBodyBuffer().readLong(), lastMessageId);
    Assert.assertEquals(0, buffers2.size());
    Assert.assertEquals(0, storeImpl.getAddressSize());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) Page(org.apache.activemq.artemis.core.paging.impl.Page) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) PagingStoreFactory(org.apache.activemq.artemis.core.paging.PagingStoreFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore)

Example 3 with RoutingContextImpl

use of org.apache.activemq.artemis.core.server.impl.RoutingContextImpl in project activemq-artemis by apache.

the class BindingsImplTest method internalTest.

private void internalTest(final boolean route) throws Exception {
    final FakeBinding fake = new FakeBinding(new SimpleString("a"));
    final Bindings bind = new BindingsImpl(null, null, null);
    bind.addBinding(fake);
    bind.addBinding(new FakeBinding(new SimpleString("a")));
    bind.addBinding(new FakeBinding(new SimpleString("a")));
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                bind.removeBinding(fake);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    Queue queue = new FakeQueue(new SimpleString("a"));
    t.start();
    for (int i = 0; i < 100; i++) {
        if (route) {
            bind.route(new CoreMessage(i, 100), new RoutingContextImpl(new FakeTransaction()));
        } else {
            bind.redistribute(new CoreMessage(i, 100), queue, new RoutingContextImpl(new FakeTransaction()));
        }
    }
}
Also used : RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BindingsImpl(org.apache.activemq.artemis.core.postoffice.impl.BindingsImpl) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) Queue(org.apache.activemq.artemis.core.server.Queue) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage)

Example 4 with RoutingContextImpl

use of org.apache.activemq.artemis.core.server.impl.RoutingContextImpl in project activemq-artemis by apache.

the class PageCursorStressTest method testConsumeLivePage.

@Test
public void testConsumeLivePage() throws Exception {
    PagingStoreImpl pageStore = lookupPageStore(ADDRESS);
    pageStore.startPaging();
    final int NUM_MESSAGES = 100;
    final int messageSize = 1024 * 1024;
    PageCursorProvider cursorProvider = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider();
    System.out.println("cursorProvider = " + cursorProvider);
    PageSubscription cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    System.out.println("Cursor: " + cursor);
    RoutingContextImpl ctx = generateCTX();
    LinkedListIterator<PagedReference> iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES; i++) {
        // if (i % 100 == 0)
        System.out.println("read/written " + i);
        ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
        Message msg = new CoreMessage(i, buffer.writerIndex());
        msg.putIntProperty("key", i);
        msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
        Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
        assertNull(iterator.next());
    }
    OperationContextImpl.clearContext();
    ctx = generateCTX();
    pageStore = lookupPageStore(ADDRESS);
    cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES * 2; i++) {
        if (i % 100 == 0)
            System.out.println("Paged " + i);
        if (i >= NUM_MESSAGES) {
            ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
            Message msg = new CoreMessage(i, buffer.writerIndex());
            msg.putIntProperty("key", i);
            msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
            Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        }
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
    }
    OperationContextImpl.clearContext();
    pageStore = lookupPageStore(ADDRESS);
    cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES * 3; i++) {
        if (i % 100 == 0)
            System.out.println("Paged " + i);
        if (i >= NUM_MESSAGES * 2 - 1) {
            ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
            Message msg = new CoreMessage(i, buffer.writerIndex());
            msg.putIntProperty("key", i + 1);
            msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
            Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        }
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        cursor.ack(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
    }
    PagedReference readMessage = iterator.next();
    assertEquals(NUM_MESSAGES * 3, readMessage.getMessage().getIntProperty("key").intValue());
    cursor.ack(readMessage);
    server.getStorageManager().waitOnOperations();
    pageStore.flushExecutors();
    assertFalse(pageStore.isPaging());
    server.stop();
    createServer();
    assertFalse(pageStore.isPaging());
    waitCleanup();
    assertFalse(lookupPageStore(ADDRESS).isPaging());
}
Also used : RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) PagedReference(org.apache.activemq.artemis.core.paging.cursor.PagedReference) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) PageSubscription(org.apache.activemq.artemis.core.paging.cursor.PageSubscription) PageCursorProvider(org.apache.activemq.artemis.core.paging.cursor.PageCursorProvider) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 5 with RoutingContextImpl

use of org.apache.activemq.artemis.core.server.impl.RoutingContextImpl in project activemq-artemis by apache.

the class PostOfficeImpl method routeQueueInfo.

private void routeQueueInfo(final Message message, final Queue queue, final boolean applyFilters) throws Exception {
    if (!applyFilters || queue.getFilter() == null || queue.getFilter().match(message)) {
        RoutingContext context = new RoutingContextImpl(null);
        queue.route(message, context);
        processRoute(message, context, false);
    }
}
Also used : RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext)

Aggregations

RoutingContextImpl (org.apache.activemq.artemis.core.server.impl.RoutingContextImpl)12 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)8 Message (org.apache.activemq.artemis.api.core.Message)7 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 PagedMessage (org.apache.activemq.artemis.core.paging.PagedMessage)6 PagingStoreImpl (org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl)6 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PagingStoreFactory (org.apache.activemq.artemis.core.paging.PagingStoreFactory)5 Page (org.apache.activemq.artemis.core.paging.impl.Page)5 NullStorageManager (org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager)5 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)4 SequentialFileFactory (org.apache.activemq.artemis.core.io.SequentialFileFactory)4 NIOSequentialFileFactory (org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory)4 PagingStore (org.apache.activemq.artemis.core.paging.PagingStore)4 FakeSequentialFileFactory (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory)4 RoutingContext (org.apache.activemq.artemis.core.server.RoutingContext)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 SequentialFile (org.apache.activemq.artemis.core.io.SequentialFile)2