Search in sources :

Example 1 with PagePosition

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

the class PageSubscriptionImpl method destroy.

/**
 * All the data associated with the cursor should go away here
 */
@Override
public void destroy() throws Exception {
    final long tx = store.generateID();
    try {
        boolean isPersistent = false;
        synchronized (consumedPages) {
            for (PageCursorInfo cursor : consumedPages.values()) {
                for (PagePosition info : cursor.acks) {
                    if (info.getRecordID() >= 0) {
                        isPersistent = true;
                        store.deleteCursorAcknowledgeTransactional(tx, info.getRecordID());
                    }
                }
                PagePosition completeInfo = cursor.getCompleteInfo();
                if (completeInfo != null && completeInfo.getRecordID() >= 0) {
                    store.deletePageComplete(completeInfo.getRecordID());
                    cursor.setCompleteInfo(null);
                }
            }
        }
        if (isPersistent) {
            store.commit(tx);
        }
        cursorProvider.close(this);
    } catch (Exception e) {
        try {
            store.rollback(tx);
        } catch (Exception ignored) {
        // exception of the exception.. nothing that can be done here
        }
    }
}
Also used : PagePosition(org.apache.activemq.artemis.core.paging.cursor.PagePosition) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 2 with PagePosition

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

the class PageSubscriptionImpl method cleanupEntries.

/**
 * It will cleanup all the records for completed pages
 */
@Override
public void cleanupEntries(final boolean completeDelete) throws Exception {
    if (completeDelete) {
        counter.delete();
    }
    if (logger.isTraceEnabled()) {
        logger.trace("cleanupEntries", new Exception("trace"));
    }
    Transaction tx = new TransactionImpl(store);
    boolean persist = false;
    final ArrayList<PageCursorInfo> completedPages = new ArrayList<>();
    // First get the completed pages using a lock
    synchronized (consumedPages) {
        // lastAckedPosition = null means no acks were done yet, so we are not ready to cleanup
        if (lastAckedPosition == null) {
            return;
        }
        for (Entry<Long, PageCursorInfo> entry : consumedPages.entrySet()) {
            PageCursorInfo info = entry.getValue();
            if (info.isDone() && !info.isPendingDelete()) {
                Page currentPage = pageStore.getCurrentPage();
                if (currentPage != null && entry.getKey() == pageStore.getCurrentPage().getPageId() && currentPage.isLive()) {
                    logger.trace("We can't clear page " + entry.getKey() + " now since it's the current page");
                } else {
                    info.setPendingDelete();
                    completedPages.add(entry.getValue());
                }
            }
        }
    }
    for (PageCursorInfo infoPG : completedPages) {
        // first will mark the page as complete
        if (isPersistent()) {
            PagePosition completePage = new PagePositionImpl(infoPG.getPageId(), infoPG.getNumberOfMessages());
            infoPG.setCompleteInfo(completePage);
            store.storePageCompleteTransactional(tx.getID(), this.getId(), completePage);
            if (!persist) {
                persist = true;
                tx.setContainsPersistent();
            }
        }
        // it will delete the page ack records
        for (PagePosition pos : infoPG.acks) {
            if (pos.getRecordID() >= 0) {
                store.deleteCursorAcknowledgeTransactional(tx.getID(), pos.getRecordID());
                if (!persist) {
                    // only need to set it once
                    tx.setContainsPersistent();
                    persist = true;
                }
            }
        }
        infoPG.acks.clear();
        infoPG.acks = Collections.synchronizedSet(new LinkedHashSet<PagePosition>());
        infoPG.removedReferences.clear();
        infoPG.removedReferences = new ConcurrentHashSet<>();
    }
    tx.addOperation(new TransactionOperationAbstract() {

        @Override
        public void afterCommit(final Transaction tx1) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    if (!completeDelete) {
                        cursorProvider.scheduleCleanup();
                    }
                }
            });
        }
    });
    tx.commit();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Page(org.apache.activemq.artemis.core.paging.impl.Page) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) TransactionOperationAbstract(org.apache.activemq.artemis.core.transaction.TransactionOperationAbstract) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) AtomicLong(java.util.concurrent.atomic.AtomicLong) PagePosition(org.apache.activemq.artemis.core.paging.cursor.PagePosition)

Example 3 with PagePosition

use of org.apache.activemq.artemis.core.paging.cursor.PagePosition 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 4 with PagePosition

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

Example 5 with PagePosition

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

the class PrintData method calculateCursorsInfo.

/**
 * Calculate the acks on the page system
 */
private static PageCursorsInfo calculateCursorsInfo(List<RecordInfo> records) throws Exception {
    PageCursorsInfo cursorInfo = new PageCursorsInfo();
    for (RecordInfo record : records) {
        byte[] data = record.data;
        ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(data);
        if (record.userRecordType == JournalRecordIds.ACKNOWLEDGE_CURSOR) {
            CursorAckRecordEncoding encoding = new CursorAckRecordEncoding();
            encoding.decode(buff);
            Set<PagePosition> set = cursorInfo.getCursorRecords().get(encoding.queueID);
            if (set == null) {
                set = new HashSet<>();
                cursorInfo.getCursorRecords().put(encoding.queueID, set);
            }
            set.add(encoding.position);
        } else if (record.userRecordType == JournalRecordIds.PAGE_CURSOR_COMPLETE) {
            CursorAckRecordEncoding encoding = new CursorAckRecordEncoding();
            encoding.decode(buff);
            Long queueID = Long.valueOf(encoding.queueID);
            Long pageNR = Long.valueOf(encoding.position.getPageNr());
            if (!cursorInfo.getCompletePages(queueID).add(pageNR)) {
                System.err.println("Page " + pageNR + " has been already set as complete on queue " + queueID);
            }
        } else if (record.userRecordType == JournalRecordIds.PAGE_TRANSACTION) {
            if (record.isUpdate) {
                PageUpdateTXEncoding pageUpdate = new PageUpdateTXEncoding();
                pageUpdate.decode(buff);
                cursorInfo.getPgTXs().add(pageUpdate.pageTX);
            } else {
                PageTransactionInfoImpl pageTransactionInfo = new PageTransactionInfoImpl();
                pageTransactionInfo.decode(buff);
                pageTransactionInfo.setRecordID(record.id);
                cursorInfo.getPgTXs().add(pageTransactionInfo.getTransactionID());
            }
        }
    }
    return cursorInfo;
}
Also used : RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) CursorAckRecordEncoding(org.apache.activemq.artemis.core.persistence.impl.journal.codec.CursorAckRecordEncoding) PageTransactionInfoImpl(org.apache.activemq.artemis.core.paging.impl.PageTransactionInfoImpl) PagePosition(org.apache.activemq.artemis.core.paging.cursor.PagePosition) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) PageUpdateTXEncoding(org.apache.activemq.artemis.core.persistence.impl.journal.codec.PageUpdateTXEncoding)

Aggregations

PagePosition (org.apache.activemq.artemis.core.paging.cursor.PagePosition)8 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)4 PagedMessage (org.apache.activemq.artemis.core.paging.PagedMessage)3 Page (org.apache.activemq.artemis.core.paging.impl.Page)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 PagingStore (org.apache.activemq.artemis.core.paging.PagingStore)2 PagePositionImpl (org.apache.activemq.artemis.core.paging.cursor.impl.PagePositionImpl)2 LinkedHashSet (java.util.LinkedHashSet)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)1 PageCache (org.apache.activemq.artemis.core.paging.cursor.PageCache)1 PageTransactionInfoImpl (org.apache.activemq.artemis.core.paging.impl.PageTransactionInfoImpl)1 CursorAckRecordEncoding (org.apache.activemq.artemis.core.persistence.impl.journal.codec.CursorAckRecordEncoding)1 PageUpdateTXEncoding (org.apache.activemq.artemis.core.persistence.impl.journal.codec.PageUpdateTXEncoding)1 PersistentQueueBindingEncoding (org.apache.activemq.artemis.core.persistence.impl.journal.codec.PersistentQueueBindingEncoding)1 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)1