Search in sources :

Example 6 with RoutingContext

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

the class PageCursorStressTest method testConsumeLivePageMultiThread.

@Test
public void testConsumeLivePageMultiThread() throws Exception {
    final PagingStoreImpl pageStore = lookupPageStore(ADDRESS);
    pageStore.startPaging();
    final int NUM_TX = 100;
    final int MSGS_TX = 100;
    final int TOTAL_MSG = NUM_TX * MSGS_TX;
    final int messageSize = 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);
    final StorageManager storage = this.server.getStorageManager();
    final AtomicInteger exceptions = new AtomicInteger(0);
    Thread t1 = new Thread() {

        @Override
        public void run() {
            try {
                int count = 0;
                for (int txCount = 0; txCount < NUM_TX; txCount++) {
                    Transaction tx = null;
                    if (txCount % 2 == 0) {
                        tx = new TransactionImpl(storage);
                    }
                    RoutingContext ctx = generateCTX(tx);
                    for (int i = 0; i < MSGS_TX; i++) {
                        // System.out.println("Sending " + count);
                        ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, count);
                        Message msg = new CoreMessage(i, buffer.writerIndex());
                        msg.putIntProperty("key", count++);
                        msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
                        Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
                    }
                    if (tx != null) {
                        tx.commit();
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                exceptions.incrementAndGet();
            }
        }
    };
    t1.start();
    LinkedListIterator<PagedReference> iterator = cursor.iterator();
    for (int i = 0; i < TOTAL_MSG; i++) {
        assertEquals(0, exceptions.get());
        PagedReference ref = null;
        for (int repeat = 0; repeat < 5; repeat++) {
            ref = iterator.next();
            if (ref == null) {
                Thread.sleep(1000);
            } else {
                break;
            }
        }
        assertNotNull(ref);
        ref.acknowledge();
        assertNotNull(ref);
        System.out.println("Consuming " + ref.getMessage().getIntProperty("key"));
    // assertEquals(i, ref.getMessage().getIntProperty("key").intValue());
    }
    assertEquals(0, exceptions.get());
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) PageSubscription(org.apache.activemq.artemis.core.paging.cursor.PageSubscription) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) PagedReference(org.apache.activemq.artemis.core.paging.cursor.PagedReference) RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PageCursorProvider(org.apache.activemq.artemis.core.paging.cursor.PageCursorProvider) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 7 with RoutingContext

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

the class PageCursorStressTest method addMessages.

private int addMessages(final int start, final int numMessages, final int messageSize) throws Exception {
    PagingStoreImpl pageStore = lookupPageStore(ADDRESS);
    pageStore.startPaging();
    RoutingContext ctx = generateCTX();
    for (int i = start; i < start + numMessages; i++) {
        if (i % 100 == 0)
            System.out.println("Paged " + i);
        ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
        Message msg = new CoreMessage(i, buffer.writerIndex());
        msg.putIntProperty("key", i);
        // to be used on tests that are validating filters
        msg.putBooleanProperty("even", i % 2 == 0);
        msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
        Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
    }
    return pageStore.getNumberOfPages();
}
Also used : RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext) 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) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 8 with RoutingContext

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

the class QueueImpl method moveBetweenSnFQueues.

@SuppressWarnings({ "ArrayToString", "ArrayToStringConcatenation" })
private void moveBetweenSnFQueues(final SimpleString queueSuffix, final Transaction tx, final MessageReference ref) throws Exception {
    Message copyMessage = makeCopy(ref, false, false);
    byte[] oldRouteToIDs = null;
    String targetNodeID;
    Binding targetBinding;
    // remove the old route
    for (SimpleString propName : copyMessage.getPropertyNames()) {
        if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
            oldRouteToIDs = (byte[]) copyMessage.removeProperty(propName.toString());
            // don't use Arrays.toString(..) here
            final String hashcodeToString = oldRouteToIDs.toString();
            logger.debug("Removed property from message: " + propName + " = " + hashcodeToString + " (" + ByteBuffer.wrap(oldRouteToIDs).getLong() + ")");
            // there should only be one of these properties so potentially save some loop iterations
            break;
        }
    }
    ByteBuffer oldBuffer = ByteBuffer.wrap(oldRouteToIDs);
    RoutingContext routingContext = new RoutingContextImpl(tx);
    /* this algorithm will look at the old route and find the new remote queue bindings where the messages should go
       * and route them there directly
       */
    while (oldBuffer.hasRemaining()) {
        long oldQueueID = oldBuffer.getLong();
        // look at all the bindings
        Pair<String, Binding> result = locateTargetBinding(queueSuffix, copyMessage, oldQueueID);
        targetBinding = result.getB();
        targetNodeID = result.getA();
        if (targetBinding == null) {
            ActiveMQServerLogger.LOGGER.unableToFindTargetQueue(targetNodeID);
        } else {
            logger.debug("Routing on binding: " + targetBinding);
            targetBinding.route(copyMessage, routingContext);
        }
    }
    postOffice.processRoute(copyMessage, routingContext, false);
    ref.handled();
    acknowledge(tx, ref);
    storageManager.afterCompleteOperations(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
            ActiveMQServerLogger.LOGGER.ioErrorRedistributing(errorCode, errorMessage);
        }

        @Override
        public void done() {
            deliverAsync();
        }
    });
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) RemoteQueueBinding(org.apache.activemq.artemis.core.server.cluster.RemoteQueueBinding) Message(org.apache.activemq.artemis.api.core.Message) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ByteBuffer(java.nio.ByteBuffer) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext)

Aggregations

RoutingContext (org.apache.activemq.artemis.core.server.RoutingContext)8 Message (org.apache.activemq.artemis.api.core.Message)6 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)4 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)3 RoutingContextImpl (org.apache.activemq.artemis.core.server.impl.RoutingContextImpl)3 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)3 PagingStoreImpl (org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl)2 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)2 ByteBuffer (java.nio.ByteBuffer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pair (org.apache.activemq.artemis.api.core.Pair)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)1 PageCursorProvider (org.apache.activemq.artemis.core.paging.cursor.PageCursorProvider)1 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)1 PagedReference (org.apache.activemq.artemis.core.paging.cursor.PagedReference)1 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)1 Binding (org.apache.activemq.artemis.core.postoffice.Binding)1 Bindings (org.apache.activemq.artemis.core.postoffice.Bindings)1 LocalQueueBinding (org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding)1