Search in sources :

Example 6 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class ServerConsumerImpl method close.

@Override
public void close(final boolean failed) throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("ServerConsumerImpl::" + this + " being closed with failed=" + failed, new Exception("trace"));
    }
    if (server.hasBrokerPlugins()) {
        server.callBrokerPlugins(plugin -> plugin.beforeCloseConsumer(this, failed));
    }
    setStarted(false);
    LargeMessageDeliverer del = largeMessageDeliverer;
    if (del != null) {
        del.finish();
    }
    removeItself();
    LinkedList<MessageReference> refs = cancelRefs(failed, false, null);
    Iterator<MessageReference> iter = refs.iterator();
    Transaction tx = new TransactionImpl(storageManager);
    while (iter.hasNext()) {
        MessageReference ref = iter.next();
        if (logger.isTraceEnabled()) {
            logger.trace("ServerConsumerImpl::" + this + " cancelling reference " + ref);
        }
        ref.getQueue().cancel(tx, ref, true);
    }
    tx.rollback();
    messageQueue.recheckRefCount(session.getSessionContext());
    if (!browseOnly) {
        TypedProperties props = new TypedProperties();
        props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, binding.getAddress());
        props.putSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME, binding.getClusterName());
        props.putSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME, binding.getRoutingName());
        props.putSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING, filter == null ? null : filter.getFilterString());
        props.putIntProperty(ManagementHelper.HDR_DISTANCE, binding.getDistance());
        props.putIntProperty(ManagementHelper.HDR_CONSUMER_COUNT, messageQueue.getConsumerCount());
        // HORNETQ-946
        props.putSimpleStringProperty(ManagementHelper.HDR_USER, SimpleString.toSimpleString(session.getUsername()));
        props.putSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS, SimpleString.toSimpleString(((ServerSessionImpl) session).getRemotingConnection().getRemoteAddress()));
        props.putSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME, SimpleString.toSimpleString(session.getName()));
        Notification notification = new Notification(null, CoreNotificationType.CONSUMER_CLOSED, props);
        managementService.sendNotification(notification);
    }
    if (server.hasBrokerPlugins()) {
        server.callBrokerPlugins(plugin -> plugin.afterCloseConsumer(this, failed));
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) Notification(org.apache.activemq.artemis.core.server.management.Notification)

Example 7 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class ServerConsumerImpl method acknowledge.

@Override
public synchronized void acknowledge(Transaction tx, final long messageID) throws Exception {
    if (browseOnly) {
        return;
    }
    // Acknowledge acknowledges all refs delivered by the consumer up to and including the one explicitly
    // acknowledged
    // We use a transaction here as if the message is not found, we should rollback anything done
    // This could eventually happen on retries during transactions, and we need to make sure we don't ACK things we are not supposed to acknowledge
    boolean startedTransaction = false;
    if (tx == null) {
        startedTransaction = true;
        tx = new TransactionImpl(storageManager);
    }
    try {
        MessageReference ref;
        do {
            synchronized (lock) {
                ref = deliveringRefs.poll();
            }
            if (logger.isTraceEnabled()) {
                logger.trace("ACKing ref " + ref + " on tx= " + tx + ", consumer=" + this);
            }
            if (ref == null) {
                ActiveMQIllegalStateException ils = ActiveMQMessageBundle.BUNDLE.consumerNoReference(id, messageID, messageQueue.getName());
                tx.markAsRollbackOnly(ils);
                throw ils;
            }
            ref.acknowledge(tx);
            acks++;
        } while (ref.getMessageID() != messageID);
        if (startedTransaction) {
            tx.commit();
        }
    } catch (ActiveMQException e) {
        if (startedTransaction) {
            tx.rollback();
        } else {
            tx.markAsRollbackOnly(e);
        }
        throw e;
    } catch (Throwable e) {
        ActiveMQServerLogger.LOGGER.errorAckingMessage((Exception) e);
        ActiveMQException activeMQIllegalStateException = new ActiveMQIllegalStateException(e.getMessage());
        if (startedTransaction) {
            tx.rollback();
        } else {
            tx.markAsRollbackOnly(activeMQIllegalStateException);
        }
        throw activeMQIllegalStateException;
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)

Example 8 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class PostOfficeJournalLoader method recoverPendingPageCounters.

/**
 * This method will recover the counters after failures making sure the page counter doesn't get out of sync
 *
 * @param pendingNonTXPageCounter
 * @throws Exception
 */
@Override
public void recoverPendingPageCounters(List<PageCountPending> pendingNonTXPageCounter) throws Exception {
    // We need a structure of the following
    // Address -> PageID -> QueueID -> List<PageCountPending>
    // The following loop will sort the records according to the hierarchy we need
    Transaction txRecoverCounter = new TransactionImpl(storageManager);
    Map<SimpleString, Map<Long, Map<Long, List<PageCountPending>>>> perAddressMap = generateMapsOnPendingCount(queues, pendingNonTXPageCounter, txRecoverCounter);
    for (Map.Entry<SimpleString, Map<Long, Map<Long, List<PageCountPending>>>> addressPageMapEntry : perAddressMap.entrySet()) {
        PagingStore store = pagingManager.getPageStore(addressPageMapEntry.getKey());
        Map<Long, Map<Long, List<PageCountPending>>> perPageMap = addressPageMapEntry.getValue();
        // We have already generated this before, so it can't be null
        assert (perPageMap != null);
        for (Long pageId : perPageMap.keySet()) {
            Map<Long, List<PageCountPending>> perQueue = perPageMap.get(pageId);
            // This can't be true!
            assert (perQueue != null);
            if (store.checkPageFileExists(pageId.intValue())) {
                // on this case we need to recalculate the records
                Page pg = store.createPage(pageId.intValue());
                pg.open();
                List<PagedMessage> pgMessages = pg.read(storageManager);
                Map<Long, AtomicInteger> countsPerQueueOnPage = new HashMap<>();
                Map<Long, AtomicLong> sizePerQueueOnPage = new HashMap<>();
                for (PagedMessage pgd : pgMessages) {
                    if (pgd.getTransactionID() <= 0) {
                        for (long q : pgd.getQueueIDs()) {
                            AtomicInteger countQ = countsPerQueueOnPage.get(q);
                            AtomicLong sizeQ = sizePerQueueOnPage.get(q);
                            if (countQ == null) {
                                countQ = new AtomicInteger(0);
                                countsPerQueueOnPage.put(q, countQ);
                            }
                            if (sizeQ == null) {
                                sizeQ = new AtomicLong(0);
                                sizePerQueueOnPage.put(q, sizeQ);
                            }
                            countQ.incrementAndGet();
                            if (pgd.getPersistentSize() > 0) {
                                sizeQ.addAndGet(pgd.getPersistentSize());
                            }
                        }
                    }
                }
                for (Map.Entry<Long, List<PageCountPending>> entry : perQueue.entrySet()) {
                    for (PageCountPending record : entry.getValue()) {
                        logger.debug("Deleting pg tempCount " + record.getID());
                        storageManager.deletePendingPageCounter(txRecoverCounter.getID(), record.getID());
                    }
                    PageSubscriptionCounter counter = store.getCursorProvider().getSubscription(entry.getKey()).getCounter();
                    AtomicInteger value = countsPerQueueOnPage.get(entry.getKey());
                    AtomicLong sizeValue = sizePerQueueOnPage.get(entry.getKey());
                    if (value == null) {
                        logger.debug("Page " + entry.getKey() + " wasn't open, so we will just ignore");
                    } else {
                        logger.debug("Replacing counter " + value.get());
                        counter.increment(txRecoverCounter, value.get(), sizeValue.get());
                    }
                }
            } else {
                // on this case the page file didn't exist, we just remove all the records since the page is already gone
                logger.debug("Page " + pageId + " didn't exist on address " + addressPageMapEntry.getKey() + ", so we are just removing records");
                for (List<PageCountPending> records : perQueue.values()) {
                    for (PageCountPending record : records) {
                        logger.debug("Removing pending page counter " + record.getID());
                        storageManager.deletePendingPageCounter(txRecoverCounter.getID(), record.getID());
                        txRecoverCounter.setContainsPersistent();
                    }
                }
            }
        }
    }
    txRecoverCounter.commit();
}
Also used : PageCountPending(org.apache.activemq.artemis.core.persistence.impl.PageCountPending) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) HashMap(java.util.HashMap) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PageSubscriptionCounter(org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter) Page(org.apache.activemq.artemis.core.paging.impl.Page) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore)

Example 9 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class QueueImpl method purgeAfterRollback.

private void purgeAfterRollback(LinkedList<MessageReference> refs) {
    try {
        Transaction transaction = new TransactionImpl(storageManager);
        for (MessageReference reference : refs) {
            // post ack will decrement this, so need to inc
            incDelivering(reference);
            acknowledge(transaction, reference, AckReason.KILLED);
        }
        transaction.commit();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) BindingsTransactionImpl(org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with TransactionImpl

use of org.apache.activemq.artemis.core.transaction.impl.TransactionImpl in project activemq-artemis by apache.

the class QueueImpl method deleteReference.

@Override
public synchronized boolean deleteReference(final long messageID) throws Exception {
    boolean deleted = false;
    Transaction tx = new TransactionImpl(storageManager);
    try (LinkedListIterator<MessageReference> iter = iterator()) {
        while (iter.hasNext()) {
            MessageReference ref = iter.next();
            if (ref.getMessage().getMessageID() == messageID) {
                incDelivering(ref);
                acknowledge(tx, ref);
                iter.remove();
                refRemoved(ref);
                deleted = true;
                break;
            }
        }
        if (!deleted) {
            // Look in scheduled deliveries
            deleted = scheduledDeliveryHandler.removeReferenceWithID(messageID) != null ? true : false;
        }
        tx.commit();
        return deleted;
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) BindingsTransactionImpl(org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference)

Aggregations

TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)31 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)24 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)11 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)11 Test (org.junit.Test)10 Queue (org.apache.activemq.artemis.core.server.Queue)9 Message (org.apache.activemq.artemis.api.core.Message)8 PageSubscriptionCounter (org.apache.activemq.artemis.core.paging.cursor.PageSubscriptionCounter)7 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)5 PageSubscription (org.apache.activemq.artemis.core.paging.cursor.PageSubscription)5 DuplicateIDCache (org.apache.activemq.artemis.core.postoffice.DuplicateIDCache)5 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)5 Map (java.util.Map)4 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)4 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)4 LinkedList (java.util.LinkedList)3 NoSuchElementException (java.util.NoSuchElementException)3