You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This Page is Work In Progress

 

Introduction

For Qpid for Java version 6.2 we removed the queuerunner which was responsible for assigning messages from the queue to consumers and instead moved to a model where the consumers pull messages from the queue (QPID-7514).

This page is to explain how the new model works, what benefits it brings, what corner cases to think of, and general reasoning behind the implementation.

Why Remove Queuerunner?

The problems with queuerunner were manifold

  • unclear threading model
    This lead to hard to test code with many sleeps and sporadic test failures
  • over-reliance on StateChangeListeners
    Made the code hard to debug and reason about

High-level Overview

The main players are

  • Queue
    • QueueConsumerManager
  • ConsumerTarget
    • (Queue-)Consumers

The ConsumerTarget is the broker-side representation of a consuming client. Due to multi-queue consumers a ConsumerTarget has one or more Consumers associated with one queue each.

The responsibility of the Queue is to notify the Consumers when there is work for them (i.e., messages the consumer is interested in).

 The Consumers responsibility is to notify the Queue when it is ready to do some work and when the time has come to pull messages of the queue and process them (i.e. send them to the consuming client).

Consumers are always invoked from the consuming connection's IO-Thread whereas the Queue might be invoked from different threads (producing connection's IO-Thread, Housekeeping thread for held or TTLed messages, a consuming connection's IO-Thread in case for message reject).


Simple Flow

  1. Message arrives on the Queue
  2. The Queue notifies some interested Consumers that there is work to be done
  3. The Consumers notify their ConsumerTarget that they would like to do work
  4. The ConsumerTargets notify their Session that they would like to do work
  5. The Sessions notify their Connections that they would like to do work
  6. The Connections schedule themselves
  7. The Scheduler kicks off a IO-Thread to process the work of a Connection
  8. The Connection iterates over its Sessions that want to do work
  9. The Sessions iterate over its ConsumerTargets that want to do work
  10. The ConsumerTargets iterate over its Consumers that want to do work
  11. The Consumer tries to pulls a message from the Queue
  12. If successful the message is put on the IO-buffer to be sent down the wire

Design Goals

  • Avoid spurious wake-ups
  • Reduce delay incurred by wake-ups
  • Ensure some level of fairness between consumers

Corner Cases and Things to Remember

  • Multi-Queue consumers
  • consumer priorities
  • out-of-order queues
  • queue browsers
  • fairness

QueueConsumerManager internals

The QueueConsumerManager (QCM for short) keeps track of the state of Consumers from the perspective of the Queue. It decides which Consumer to notify of work. To do this in a performant way it maintains a number of lists and moves Consumers between those lists to indicate state change. The lists it maintains are:

  • All
  • NonAcquiring
  • NotInterested
  • Interested
  • Notified

Typically we want these lists to be thread-safe and give us O(1) access/deletion if we know the element. Unfortunately there does not exist a data structure in the Java standard library with those characteristics which is why they are based on our own data structure QueueConsumerNodeList.

QueueConsumerNodeList

The QueueConsumerNodeList is the underlying data structure of all of QCM's lists. It is thread-safe and allows O(1) appending and given you have a pointer to an entry O(1) deletion. It is essentially a singly linked list. To achieve O(1) deletion entries are marked for deletion but only actually removed upon the next iteration.  The rational being that, to delete an entry you would need to update the previous entry's "next" pointer but to get to the previous element you would need a doubly linked list which it impossible to maintain in a thread-safe way without locking. Special care must be taken when removing elements from the tail since we keep an explicit reference to it in the QueueConsumerNodeList to achieve O(1) appending. The data structure in the QueueConsumerNodeList are called QueueConsumerNodeListEntries which themselves have a reference to a QueueConsumerNode which is the persistent entity that moves between QCM's lists and has a reference to the QueueConsumer. The QueueConsumer itself also has a reference to the QueueConsumerNode to enable O(1) deletion given a Consumer. This tightly couples the QueueConsumer and QCM classes.

The "All" List

The All list contains all Consumers registered with the Queue. This is necessary to be able to iterate over all consumers in a thread-safe way without locking. The danger of using several lists instead of a single All list is that you might miss a Consumer if it moves between lists during iteration.

The "NonAcquiring" List

This is a list of Consumers that do not acquire messages for example Queue Browsers. These need to be handled separately because they should always be notified about new messages. Where they kept in the same list as the acquiring consumers we would have to iterate of the entire list to make sure we did not miss a non-acquiring consumer.

The "NotInterested" List

This list contains all acquiring Consumers that indicated to the Queue that they currently are not interested in doing any work (i.e., taking messages). This typically happens when a Consumer/Connection is suspended due to FlowControl/TCP backpressure.

The "Interested" List

This is the default list for acquiring Consumers. It signifies that they are ready to process messages. When a new message arrives on the Queue it will notify Consumers from this list.

The "Notified" List

Once an acquiring Consumer is notified that there is work to do it is moved from the "Interested" list to the "Notified" list. The QCM expects such a Consumer to either indicate that it is no longer interested (e.g., it became suspended in the meantime and therefore will not do the work we expected it to) or call AbstractQueue#deliverSingleMessage. The Consumer should remain in the "Notified" list and continue to call deliverSingleMessage until deliverSingleMessage cannot deliver a message to it any more in which case it is moved back to the "Interested" list. This is to avoid unnecessary wakeups.

Handling Consumer Priorities

When deciding which Consumer to notify the QCM should take consumer priorities into account. To do this in a performant way it maintains a QueueConsumerNodeList per consumer priority in a list of PriorityConsumerListPairs.

 

  • No labels