Search in sources :

Example 1 with PagePositionImpl

use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.

the class XmlDataExporter method printPagedMessagesAsXML.

/**
 * Reads from the page files and prints messages as it finds them (making sure to check acks and transactions
 * from the journal).
 */
private void printPagedMessagesAsXML() {
    try {
        pagingmanager.start();
        SimpleString[] stores = pagingmanager.getStoreNames();
        for (SimpleString store : stores) {
            PagingStore pageStore = pagingmanager.getPageStore(store);
            if (pageStore != null) {
                File folder = pageStore.getFolder();
                ActiveMQServerLogger.LOGGER.debug("Reading page store " + store + " folder = " + folder);
                int pageId = (int) pageStore.getFirstPage();
                for (int i = 0; i < pageStore.getNumberOfPages(); i++) {
                    ActiveMQServerLogger.LOGGER.debug("Reading page " + pageId);
                    Page page = pageStore.createPage(pageId);
                    page.open();
                    List<PagedMessage> messages = page.read(storageManager);
                    page.close();
                    int messageId = 0;
                    for (PagedMessage message : messages) {
                        message.initMessage(storageManager);
                        long[] queueIDs = message.getQueueIDs();
                        List<String> queueNames = new ArrayList<>();
                        for (long queueID : queueIDs) {
                            PagePosition posCheck = new PagePositionImpl(pageId, messageId);
                            boolean acked = false;
                            Set<PagePosition> positions = cursorRecords.get(queueID);
                            if (positions != null) {
                                acked = positions.contains(posCheck);
                            }
                            if (!acked) {
                                PersistentQueueBindingEncoding queueBinding = queueBindings.get(queueID);
                                if (queueBinding != null) {
                                    SimpleString queueName = queueBinding.getQueueName();
                                    queueNames.add(queueName.toString());
                                }
                            }
                        }
                        if (queueNames.size() > 0 && (message.getTransactionID() == -1 || pgTXs.contains(message.getTransactionID()))) {
                            printSingleMessageAsXML(message.getMessage().toCore(), queueNames);
                        }
                        messageId++;
                    }
                    pageId++;
                }
            } else {
                ActiveMQServerLogger.LOGGER.debug("Page store was null");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PagePositionImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) XMLStreamException(javax.xml.stream.XMLStreamException) PersistentQueueBindingEncoding(org.apache.activemq.artemis.core.persistence.impl.journal.codec.PersistentQueueBindingEncoding) PagePosition(org.apache.activemq.artemis.core.paging.cursor.PagePosition) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) File(java.io.File)

Example 2 with PagePositionImpl

use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.

the class PagingLeakTest method testValidateLeak.

@Test
@BMRules(rules = { @BMRule(name = "newPosition", targetClass = "org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl", targetMethod = "<init>()", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.PagingLeakTest.newPosition()"), @BMRule(name = "finalPosition", targetClass = "org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl", targetMethod = "finalize", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.PagingLeakTest.deletePosition()") })
public void testValidateLeak() throws Throwable {
    System.out.println("location::" + getBindingsDir());
    List<PagePositionImpl> positions = new ArrayList<>();
    for (int i = 0; i < 300; i++) {
        positions.add(new PagePositionImpl(3, 3));
    }
    long timeout = System.currentTimeMillis() + 5000;
    while (pagePosInstances.get() != 300 && timeout > System.currentTimeMillis()) {
        forceGC();
    }
    // This is just to validate the rules are correctly applied on byteman
    assertEquals("You have changed something on PagePositionImpl in such way that these byteman rules are no longer working", 300, pagePosInstances.get());
    positions.clear();
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisfied() throws Exception {
            forceGC();
            return pagePosInstances.get() == 0;
        }
    }, 5000, 100);
    // This is just to validate the rules are correctly applied on byteman
    assertEquals("You have changed something on PagePositionImpl in such way that these byteman rules are no longer working", 0, pagePosInstances.get());
    final ArrayList<Exception> errors = new ArrayList<>();
    // A backup that will be waiting to be activated
    Configuration config = createDefaultNettyConfig();
    config.setJournalBufferTimeout_AIO(10).setJournalBufferTimeout_NIO(10);
    final ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true));
    server.start();
    AddressSettings settings = new AddressSettings().setPageSizeBytes(2 * 1024).setMaxSizeBytes(10 * 1024).setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    server.getAddressSettingsRepository().addMatch("#", settings);
    final SimpleString address = new SimpleString("pgdAddress");
    class Consumer extends Thread {

        final ServerLocator locator;

        final ClientSessionFactory sf;

        final ClientSession session;

        final ClientConsumer consumer;

        final int sleepTime;

        final int maxConsumed;

        Consumer(int sleepTime, String suffix, int maxConsumed) throws Exception {
            server.createQueue(address, RoutingType.MULTICAST, address.concat(suffix), null, true, false);
            this.sleepTime = sleepTime;
            locator = createInVMLocator(0);
            sf = locator.createSessionFactory();
            session = sf.createSession(true, true);
            consumer = session.createConsumer(address.concat(suffix));
            this.maxConsumed = maxConsumed;
        }

        @Override
        public void run() {
            try {
                session.start();
                long lastTime = System.currentTimeMillis();
                for (long i = 0; i < maxConsumed; i++) {
                    ClientMessage msg = consumer.receive(5000);
                    if (msg == null) {
                        errors.add(new Exception("didn't receive a message"));
                        return;
                    }
                    msg.acknowledge();
                    if (sleepTime > 0) {
                        Thread.sleep(sleepTime);
                    }
                    if (i % 1000 == 0) {
                        System.out.println("Consumed " + i + " events in " + (System.currentTimeMillis() - lastTime));
                        lastTime = System.currentTimeMillis();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    int numberOfMessages = 500;
    Consumer consumer1 = new Consumer(10, "-1", 150);
    Consumer consumer2 = new Consumer(0, "-2", numberOfMessages);
    final ServerLocator locator = createInVMLocator(0);
    final ClientSessionFactory sf = locator.createSessionFactory();
    final ClientSession session = sf.createSession(true, true);
    final ClientProducer producer = session.createProducer(address);
    byte[] b = new byte[1024];
    for (long i = 0; i < numberOfMessages; i++) {
        ClientMessage msg = session.createMessage(true);
        msg.getBodyBuffer().writeBytes(b);
        producer.send(msg);
        if (i == 100) {
            System.out.println("Starting consumers!!!");
            consumer1.start();
            consumer2.start();
        }
        if (i % 250 == 0) {
            validateInstances();
        }
    }
    System.out.println("Sent " + numberOfMessages);
    consumer1.join();
    consumer2.join();
    validateInstances();
    Throwable elast = null;
    for (Throwable e : errors) {
        e.printStackTrace();
        elast = e;
    }
    if (elast != null) {
        throw elast;
    }
}
Also used : Configuration(org.apache.activemq.artemis.core.config.Configuration) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) PagePositionImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Wait(org.apache.activemq.artemis.tests.util.Wait) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 3 with PagePositionImpl

use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl 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)

Example 4 with PagePositionImpl

use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.

the class CursorAckRecordEncoding method decode.

@Override
public void decode(ActiveMQBuffer buffer) {
    queueID = buffer.readLong();
    long pageNR = buffer.readLong();
    int messageNR = buffer.readInt();
    this.position = new PagePositionImpl(pageNR, messageNR);
}
Also used : PagePositionImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl)

Example 5 with PagePositionImpl

use of org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl in project activemq-artemis by apache.

the class PrintData method printPages.

private static void printPages(DescribeJournal describeJournal, StorageManager sm, PagingManager manager, PrintStream out, boolean safe) throws Exception {
    PageCursorsInfo cursorACKs = calculateCursorsInfo(describeJournal.getRecords());
    Set<Long> pgTXs = cursorACKs.getPgTXs();
    manager.start();
    SimpleString[] stores = manager.getStoreNames();
    for (SimpleString store : stores) {
        PagingStore pgStore = manager.getPageStore(store);
        File folder = null;
        if (pgStore != null) {
            folder = pgStore.getFolder();
        }
        out.println("####################################################################################################");
        out.println("Exploring store " + store + " folder = " + folder);
        int pgid = (int) pgStore.getFirstPage();
        for (int pg = 0; pg < pgStore.getNumberOfPages(); pg++) {
            out.println("*******   Page " + pgid);
            Page page = pgStore.createPage(pgid);
            page.open();
            List<PagedMessage> msgs = page.read(sm);
            page.close();
            int msgID = 0;
            for (PagedMessage msg : msgs) {
                msg.initMessage(sm);
                if (safe) {
                    try {
                        out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ", msg=" + msg.getMessage().getClass().getSimpleName() + "(safe data, size=" + msg.getMessage().getPersistentSize() + ")");
                    } catch (Exception e) {
                        out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ", msg=" + msg.getMessage().getClass().getSimpleName() + "(safe data)");
                    }
                } else {
                    out.print("pg=" + pgid + ", msg=" + msgID + ",pgTX=" + msg.getTransactionID() + ",userMessageID=" + (msg.getMessage().getUserID() != null ? msg.getMessage().getUserID() : "") + ", msg=" + msg.getMessage());
                }
                out.print(",Queues = ");
                long[] q = msg.getQueueIDs();
                for (int i = 0; i < q.length; i++) {
                    out.print(q[i]);
                    PagePosition posCheck = new PagePositionImpl(pgid, msgID);
                    boolean acked = false;
                    Set<PagePosition> positions = cursorACKs.getCursorRecords().get(q[i]);
                    if (positions != null) {
                        acked = positions.contains(posCheck);
                    }
                    if (acked) {
                        out.print(" (ACK)");
                    }
                    if (cursorACKs.getCompletePages(q[i]).contains(Long.valueOf(pgid))) {
                        out.println(" (PG-COMPLETE)");
                    }
                    if (i + 1 < q.length) {
                        out.print(",");
                    }
                }
                if (msg.getTransactionID() >= 0 && !pgTXs.contains(msg.getTransactionID())) {
                    out.print(", **PG_TX_NOT_FOUND**");
                }
                out.println();
                msgID++;
            }
            pgid++;
        }
    }
}
Also used : PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page) PagePositionImpl(org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl) PagePosition(org.apache.activemq.artemis.core.paging.cursor.PagePosition) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) File(java.io.File)

Aggregations

PagePositionImpl (org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl)5 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 PagingStore (org.apache.activemq.artemis.core.paging.PagingStore)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)2 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)2 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)2 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)2 Configuration (org.apache.activemq.artemis.core.config.Configuration)2 PagedMessage (org.apache.activemq.artemis.core.paging.PagedMessage)2 PagePosition (org.apache.activemq.artemis.core.paging.cursor.PagePosition)2 Page (org.apache.activemq.artemis.core.paging.impl.Page)2 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)2 Test (org.junit.Test)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1