Search in sources :

Example 1 with MessageStore

use of org.apache.qpid.server.store.MessageStore in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecoverer method recover.

@Override
public ListenableFuture<Void> recover(QueueManagingVirtualHost<?> virtualHost) {
    EventLogger eventLogger = virtualHost.getEventLogger();
    MessageStore store = virtualHost.getMessageStore();
    MessageStore.MessageStoreReader storeReader = store.newMessageStoreReader();
    MessageStoreLogSubject logSubject = new MessageStoreLogSubject(virtualHost.getName(), store.getClass().getSimpleName());
    Map<Queue<?>, Integer> queueRecoveries = new TreeMap<>();
    Map<Long, ServerMessage<?>> recoveredMessages = new HashMap<>();
    Map<Long, StoredMessage<?>> unusedMessages = new TreeMap<>();
    Map<UUID, Integer> unknownQueuesWithMessages = new HashMap<>();
    Map<Queue<?>, Integer> queuesWithUnknownMessages = new HashMap<>();
    eventLogger.message(logSubject, MessageStoreMessages.RECOVERY_START());
    storeReader.visitMessages(new MessageVisitor(recoveredMessages, unusedMessages));
    eventLogger.message(logSubject, TransactionLogMessages.RECOVERY_START(null, false));
    try {
        storeReader.visitMessageInstances(new MessageInstanceVisitor(virtualHost, store, queueRecoveries, recoveredMessages, unusedMessages, unknownQueuesWithMessages, queuesWithUnknownMessages));
    } finally {
        if (!unknownQueuesWithMessages.isEmpty()) {
            unknownQueuesWithMessages.forEach((queueId, count) -> {
                LOGGER.info("Discarded {} entry(s) associated with queue id '{}' as a queue with this " + "id does not appear in the configuration.", count, queueId);
            });
        }
        if (!queuesWithUnknownMessages.isEmpty()) {
            queuesWithUnknownMessages.forEach((queue, count) -> {
                LOGGER.info("Discarded {} entry(s) associated with queue '{}' as the referenced message " + "does not exist.", count, queue.getName());
            });
        }
    }
    for (Map.Entry<Queue<?>, Integer> entry : queueRecoveries.entrySet()) {
        Queue<?> queue = entry.getKey();
        Integer deliveredCount = entry.getValue();
        eventLogger.message(logSubject, TransactionLogMessages.RECOVERED(deliveredCount, queue.getName()));
        eventLogger.message(logSubject, TransactionLogMessages.RECOVERY_COMPLETE(queue.getName(), true));
        queue.completeRecovery();
    }
    for (Queue<?> q : virtualHost.getChildren(Queue.class)) {
        if (!queueRecoveries.containsKey(q)) {
            q.completeRecovery();
        }
    }
    storeReader.visitDistributedTransactions(new DistributedTransactionVisitor(virtualHost, eventLogger, logSubject, recoveredMessages, unusedMessages));
    for (StoredMessage<?> m : unusedMessages.values()) {
        LOGGER.debug("Message id '{}' is orphaned, removing", m.getMessageNumber());
        m.remove();
    }
    if (unusedMessages.size() > 0) {
        LOGGER.info("Discarded {} orphaned message(s).", unusedMessages.size());
    }
    eventLogger.message(logSubject, TransactionLogMessages.RECOVERY_COMPLETE(null, false));
    eventLogger.message(logSubject, MessageStoreMessages.RECOVERED(recoveredMessages.size() - unusedMessages.size()));
    eventLogger.message(logSubject, MessageStoreMessages.RECOVERY_COMPLETE());
    return Futures.immediateFuture(null);
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) EventLogger(org.apache.qpid.server.logging.EventLogger) HashMap(java.util.HashMap) ServerMessage(org.apache.qpid.server.message.ServerMessage) TreeMap(java.util.TreeMap) StoredMessage(org.apache.qpid.server.store.StoredMessage) MessageStoreLogSubject(org.apache.qpid.server.logging.subjects.MessageStoreLogSubject) UUID(java.util.UUID) Queue(org.apache.qpid.server.model.Queue) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with MessageStore

use of org.apache.qpid.server.store.MessageStore in project qpid-broker-j by apache.

the class AbstractVirtualHostTest method testOpenSucceeds.

public void testOpenSucceeds() {
    Map<String, Object> attributes = Collections.<String, Object>singletonMap(AbstractVirtualHost.NAME, getTestName());
    final MessageStore store = mock(MessageStore.class);
    when(store.newMessageStoreReader()).thenReturn(mock(MessageStore.MessageStoreReader.class));
    AbstractVirtualHost host = new AbstractVirtualHost(attributes, _node) {

        @Override
        protected MessageStore createMessageStore() {
            return store;
        }

        @Override
        protected void onExceptionInOpen(final RuntimeException e) {
            fail("open failed");
        }
    };
    host.open();
    assertEquals("Unexpected host state", State.ACTIVE, host.getState());
    verify(store, atLeastOnce()).openMessageStore(host);
    host.close();
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 3 with MessageStore

use of org.apache.qpid.server.store.MessageStore in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecovererTest method testRecoveryOfMessageInstanceForNonExistingQueue.

public void testRecoveryOfMessageInstanceForNonExistingQueue() {
    final UUID queueId = UUID.randomUUID();
    final Transaction transaction = mock(Transaction.class);
    final long messageId = 1;
    final StoredMessage<StorableMessageMetaData> storedMessage = createMockStoredMessage(messageId);
    MessageStore store = new NullMessageStore() {

        @Override
        public void visitMessages(MessageHandler handler) throws StoreException {
            handler.handle(storedMessage);
        }

        @Override
        public void visitMessageInstances(MessageInstanceHandler handler) throws StoreException {
            handler.handle(new TestMessageEnqueueRecord(queueId, messageId));
        }

        @Override
        public Transaction newTransaction() {
            return transaction;
        }
    };
    when(_virtualHost.getMessageStore()).thenReturn(store);
    SynchronousMessageStoreRecoverer recoverer = new SynchronousMessageStoreRecoverer();
    recoverer.recover(_virtualHost);
    verify(transaction).dequeueMessage(argThat(new MessageEnqueueRecordMatcher(queueId, messageId)));
    verify(transaction, times(1)).commitTranAsync((Void) null);
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) Transaction(org.apache.qpid.server.store.Transaction) MessageHandler(org.apache.qpid.server.store.handler.MessageHandler) MessageInstanceHandler(org.apache.qpid.server.store.handler.MessageInstanceHandler) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) UUID(java.util.UUID) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData)

Example 4 with MessageStore

use of org.apache.qpid.server.store.MessageStore in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecovererTest method testRecoveryOfSingleMessageOnSingleQueue.

@SuppressWarnings("unchecked")
public void testRecoveryOfSingleMessageOnSingleQueue() {
    final Queue<?> queue = createRegisteredMockQueue();
    final long messageId = 1;
    final StoredMessage<StorableMessageMetaData> storedMessage = createMockStoredMessage(messageId);
    MessageStore store = new NullMessageStore() {

        @Override
        public void visitMessages(MessageHandler handler) throws StoreException {
            handler.handle(storedMessage);
        }

        @Override
        public void visitMessageInstances(MessageInstanceHandler handler) throws StoreException {
            handler.handle(new TestMessageEnqueueRecord(queue.getId(), messageId));
        }
    };
    when(_virtualHost.getMessageStore()).thenReturn(store);
    SynchronousMessageStoreRecoverer recoverer = new SynchronousMessageStoreRecoverer();
    recoverer.recover(_virtualHost);
    ServerMessage<?> message = storedMessage.getMetaData().getType().createMessage(storedMessage);
    verify(queue, times(1)).recover(eq(message), any(MessageEnqueueRecord.class));
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) MessageHandler(org.apache.qpid.server.store.handler.MessageHandler) MessageInstanceHandler(org.apache.qpid.server.store.handler.MessageInstanceHandler) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData)

Example 5 with MessageStore

use of org.apache.qpid.server.store.MessageStore in project qpid-broker-j by apache.

the class SynchronousMessageStoreRecovererTest method testRecoveryOfSingleDequeueWithDistributedTransaction.

public void testRecoveryOfSingleDequeueWithDistributedTransaction() {
    final UUID queueId = UUID.randomUUID();
    final Queue<?> queue = createRegisteredMockQueue(queueId);
    final Transaction transaction = mock(Transaction.class);
    final StoredMessage<StorableMessageMetaData> storedMessage = createMockStoredMessage(1);
    final long messageId = storedMessage.getMessageNumber();
    Transaction.DequeueRecord dequeueRecord = createMockDequeueRecord(queueId, messageId);
    QueueEntry queueEntry = mock(QueueEntry.class);
    when(queueEntry.acquire()).thenReturn(true);
    when(queue.getMessageOnTheQueue(messageId)).thenReturn(queueEntry);
    final long format = 1;
    final byte[] globalId = new byte[] { 0 };
    final byte[] branchId = new byte[] { 0 };
    final EnqueueRecord[] enqueues = {};
    final Transaction.DequeueRecord[] dequeues = { dequeueRecord };
    MessageStore store = new NullMessageStore() {

        @Override
        public void visitMessages(MessageHandler handler) throws StoreException {
            handler.handle(storedMessage);
        }

        @Override
        public void visitMessageInstances(MessageInstanceHandler handler) throws StoreException {
            // We need the message to be enqueued onto the queue so that later the distributed transaction
            // can dequeue it.
            handler.handle(new TestMessageEnqueueRecord(queue.getId(), messageId));
        }

        @Override
        public void visitDistributedTransactions(DistributedTransactionHandler handler) throws StoreException {
            handler.handle(new Transaction.StoredXidRecord() {

                @Override
                public long getFormat() {
                    return format;
                }

                @Override
                public byte[] getGlobalId() {
                    return globalId;
                }

                @Override
                public byte[] getBranchId() {
                    return branchId;
                }
            }, enqueues, dequeues);
        }

        @Override
        public Transaction newTransaction() {
            return transaction;
        }
    };
    DtxRegistry dtxRegistry = new DtxRegistry(_virtualHost);
    when(_virtualHost.getMessageStore()).thenReturn(store);
    when(_virtualHost.getDtxRegistry()).thenReturn(dtxRegistry);
    SynchronousMessageStoreRecoverer recoverer = new SynchronousMessageStoreRecoverer();
    recoverer.recover(_virtualHost);
    DtxBranch branch = dtxRegistry.getBranch(new Xid(format, globalId, branchId));
    assertNotNull("Expected dtx branch to be created", branch);
    branch.commit();
    verify(queueEntry, times(1)).delete();
    verify(transaction).commitTran();
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) MessageHandler(org.apache.qpid.server.store.handler.MessageHandler) EnqueueRecord(org.apache.qpid.server.store.Transaction.EnqueueRecord) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord) UUID(java.util.UUID) StorableMessageMetaData(org.apache.qpid.server.store.StorableMessageMetaData) DistributedTransactionHandler(org.apache.qpid.server.store.handler.DistributedTransactionHandler) NullMessageStore(org.apache.qpid.server.store.NullMessageStore) QueueEntry(org.apache.qpid.server.queue.QueueEntry) Xid(org.apache.qpid.server.txn.Xid) Transaction(org.apache.qpid.server.store.Transaction) DtxRegistry(org.apache.qpid.server.txn.DtxRegistry) MessageInstanceHandler(org.apache.qpid.server.store.handler.MessageInstanceHandler) DtxBranch(org.apache.qpid.server.txn.DtxBranch)

Aggregations

MessageStore (org.apache.qpid.server.store.MessageStore)20 MessageEnqueueRecord (org.apache.qpid.server.store.MessageEnqueueRecord)7 NullMessageStore (org.apache.qpid.server.store.NullMessageStore)6 Transaction (org.apache.qpid.server.store.Transaction)6 MessageHandler (org.apache.qpid.server.store.handler.MessageHandler)6 MessageInstanceHandler (org.apache.qpid.server.store.handler.MessageInstanceHandler)6 UUID (java.util.UUID)5 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)5 StorableMessageMetaData (org.apache.qpid.server.store.StorableMessageMetaData)5 ServerMessage (org.apache.qpid.server.message.ServerMessage)3 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)2 VirtualHost (org.apache.qpid.server.model.VirtualHost)2 QueueEntry (org.apache.qpid.server.queue.QueueEntry)2 StoredMessage (org.apache.qpid.server.store.StoredMessage)2 TestMemoryMessageStore (org.apache.qpid.server.store.TestMemoryMessageStore)2 EnqueueRecord (org.apache.qpid.server.store.Transaction.EnqueueRecord)2 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)2 DistributedTransactionHandler (org.apache.qpid.server.store.handler.DistributedTransactionHandler)2 File (java.io.File)1 AccessControlException (java.security.AccessControlException)1