Patent Publication Number: US-9847950-B1

Title: Messaging system thread pool

Description:
FIELD 
     The invention relates to a messaging system. More particularly, the invention relates to a messaging system thread pool. 
     BACKGROUND 
     The Advanced Message Queuing Protocol (AMQP) is an open standard application layer protocol for message-oriented middleware. The defining features of AMQP are message orientation, queuing, routing (including point-to-point and publish-and-subscribe), reliability, and security. 
     AMQP mandates the behavior of the messaging provider and client to the extent that implementations from different vendors are interoperable, in the same way as SMTP, HTTP, FTP, etc. have created interoperable systems. Unlike JMS, which defines an API and a set of behaviors that a messaging implementation must provide, AMQP is a wire-level protocol. A wire-level protocol is a description of the format of the data that is sent across the network as a stream of bytes. 
     Consequently, any tool that can create and interpret messages that conform to this data format can interoperate with any other compliant tool irrespective of implementation language. 
     AMQP is a binary, application layer protocol, designed to support a wide variety of messaging applications and communication patterns efficiently. It provides flow controlled, message-oriented communication with message-delivery guarantees such as at-most-once (where each message is delivered once or never), at-least-once (where each message is certain to be delivered, but may do so multiple times), and exactly-once (where the message will always certainly arrive and do so only once), and authentication and/or encryption based on SASL and/or TLS. It assumes an underlying reliable transport layer protocol such as Transmission Control Protocol (TCP). 
     RabbitMQ is open source message broker software (sometimes called message-oriented middleware) that implements AMQP. RabbitMQ is used for messaging, for example, in Flexera Flexnet® Advanced Lifecycle Management (ALM). It is used to queue and process/replicate data to Flexnet Operations (LLM) and other components in the system, such as Usage Analytics Service (UAS) and License Fulfillment Service (LFS). In use, RabbitMQ includes a publisher and consumer. In one implementation of ALM, data from all tenants flows through two queues.  FIG. 1  is a block schematic diagram showing a messaging environment, such as a RabbitMQ environment. In  FIG. 1 , a notification is sent from a multi-tenant app  10  via a message dispatcher  11 . In this implementation, each queue  12   a ,  12   b  has its own consumer  13   a ,  13   b , which routes the messages as processed data  14 ,  15 . 
     This approach is problematic because there is so much data at times that the single consumer cannot keep up during peak periods. Furthermore, all data is processed serially, so other tenants are negatively impacted if one tenant generated lots of data/messages that had to be processed first. Thus, one tenant with heavy traffic could flood the queue and cause delays in processing messages from other tenants 
     This bottleneck is particularly a problem for data replicated to Cloud components because sending each message over the Internet takes a significant amount of time. Thus, in another implementation each tenant&#39;s data flows through a different queue and has its own consumer (1 tenant=1 consumer=1 thread), as shown in  FIG. 2 , which is a block schematic diagram showing a messaging environment where each tenant&#39;s data flows through a different queue  20  and has its own consumer  21 . This implementation provides one queue per tenant. 
     If the same architecture shown in  FIG. 2  is applied to another part of the system, the system could spin up so many consumer threads that it could adversely affect the system, e.g. where # of threads=# of tenants*2, and that number would continue to grow as more tenants were added. 
       FIG. 3  is a block schematic diagram showing a messaging environment where each tenant&#39;s data flows through a different queue  20 ,  30  for replication. The provision of one consumer  21 ,  31  per thread results in an inordinate number of threads. 
     SUMMARY 
     A presently preferred embodiment of the invention provides a thread pool of consumers that poll the queues. A thread manager controls the number of active threads. This approach limits the number of threads, but is still able to keep up with the volume of traffic. 
    
    
     
       DRAWINGS 
         FIG. 1  is a block schematic diagram showing a messaging environment; 
         FIG. 2  is a block schematic diagram showing a messaging environment where each tenant&#39;s data flows through a different queue and has its own consumer; 
         FIG. 3  is a block schematic diagram showing a messaging environment where each tenant&#39;s data flows through a different queue for replication; 
         FIG. 4  shows a messaging environment having one queue per tenant with a thread pool of consumers that poll the queues according to the invention; 
         FIG. 5  is a block schematic diagram showing a messaging environment in which a queue manager controls the number of active threads according to the invention; 
         FIG. 6  is a flow diagram showing a thread manager algorithm in a messaging environment according to the invention; 
         FIG. 7  is a flow diagram showing a worker thread algorithm in a messaging environment according to the invention; 
         FIG. 8  is a flow diagram showing a GetNextQueue( ) algorithm in a messaging environment according to the invention; and 
         FIG. 9  is a block schematic diagram showing a machine in the example form of a computer system within which a set of instructions for causing the machine to perform one or more of the methodologies discussed herein may be executed. 
     
    
    
     DESCRIPTION 
     A presently preferred embodiment of the invention provides a thread pool of consumers that poll the queues. This approach limits the number of threads, but is still able to keep up with the volume of traffic. 
       FIG. 4  shows a messaging environment, such as a RabbitMQ environment, having one queue per tenant with a thread pool of consumers  40   a ,  40   b  that poll the queues according to the invention. A traditional model using RabbitMQ spins up a single thread per queue per consumer. Because this is not practical, as discussed above, embodiments of the invention skip the thread if another thread is already processing. A thread manager (discussed below) controls the number of active threads. 
     Those skilled in the art will appreciate that the invention may be practiced with many other messaging systems and is not, therefore, limited to its use with RabbitMQ. For example, the invention is applicable to enterprise servers comprising multiple producers and consumers having small transaction payloads, such as messages or notifications. 
     In embodiments of the invention, thread pooling involves a thread manager that periodically looks at all threads to determine status, e.g. Is the thread busy? The QueueManager keeps track of which queues have an actively polling worker thread. The thread manager periodically checks this count and compares it against the total number of threads in the pool. If the counts are equal, then all threads are busy and the system can then spin up another thread, if the maximum number of allowed threads has not been reached. If all currently existing threads are busy, then the thread manager adds a thread; if the currently existing threads are not all busy, then the thread manager takes no action. 
     In embodiments of the invention, the worker/consumer threads poll the queues in a round-robin fashion. For example, if a first message producer has 1000 messages, then that message producer is assigned a thread for its first message. If a second producer has one message, then the second producer is assigned the next thread for its message. Round-robin fashion assures that each producer has equal access to threads in strict order without regard to message volume a producer may have. In this way, no producer is able to create a bottleneck due to its volume of traffic. Because the size of the thread pool is maintained dynamically by the thread manager, in most circumstances the proper number of threads is maintained for the amount of traffic and the number of producers actively using the messaging system. Embodiments of the invention use a maximum of ten threads in each pool, but the number of threads for a given application of this system could be different. 
     Each tenant&#39;s thread is assigned to a queue from a thread pool. Each consumer thread polls the queues in a round-robin fashion. As noted above, with a round-robin approach each consumer thread is assigned to a queue in equal portions and in circular order, handling all threads without priority. If a queue has any messages, that consumer thread processes the data. There is always a minimum of two threads, one manager and one consumer thread. The thread manager periodically monitors the state of all running consumer threads and, if all consumer threads are busy, e.g., connected to a queue and processing data, then it spins up another thread to help with the current load. Conversely, if a worker thread makes it round-robin through the entire queue list twice without finding any data, then it shuts down, unless it is the last consumer thread, which never shuts down. Thus, the number of threads dynamically scales up and down proportional to the volume of traffic, while still processing each queue in FIFO order. 
       FIG. 5  is a block schematic diagram showing a messaging environment in which a message  52  is processed. In  FIG. 5 , a queue manager  50  controls the number of active threads for a plurality of queues  54  associated with a plurality of tenants  55   a - 55   c . Each consumer thread polls the queues round-robin, i.e., it skips a thread if another thread is already processing. The thread manager  51  controls the number of active threads in the worker thread pool  53 . 
       FIG. 6  is a flow diagram showing a thread manager algorithm in a messaging environment according to the invention. 
     In  FIG. 6 , the thread manager starts. ( 600 ) In an embodiment of the invention, the thread manager waits thirty seconds ( 601 ). Those skilled in the art will appreciate that other wait periods may be used when practicing the invention. The thread manager tests to see if all current threads are busy ( 602 ). In embodiments of the invention, when the threads are not all busy, the thread manager waits another thirty seconds; when the threads are all busy, the thread manager determines if the current number of threads is less than the maximum number of threads that can be supported by the system at any time ( 603 ). When the current number of threads is not less than the maximum number of threads, the thread manager waits another thirty seconds; when the current number of threads is less than the maximum number of threads, the thread manager spawns a new worker thread ( 604 ). Those skilled in the art will appreciate that the wait time may be chosen as appropriate. 
       FIG. 7  is a flow diagram showing a worker thread algorithm in a messaging environment according to the invention. 
     In  FIG. 7 , a thread starts ( 700 ) 
     When the consecutive number of tries in which there is no data is less than or equal to 2*the number of queues ( 701 ), the queue manager gets a next queue (GetNextQueue(( )) ( 702 ). 
     The queue manager is a (thread safe) data structure that keeps track of which queues currently have a worker thread polling it and methods to get the count and next available queue to poll. With the queue manager, a worker thread can call the queue manager to get the next available queue, skipping the ones that already have threads polling the queue, as per  FIG. 8 ; and it can get the count of queues currently being polled when the thread manager decides if it needs to spin up another thread. 
     When the consecutive number of tries in which there is no data is less than or equal to 2*the number of queues, the queue manager determines if the number of threads is equal to one ( 707 ). When the number of threads is not equal to one, the thread is ended ( 708 ); when the number of threads is equal to one, the thread manager determines if the consecutive number of tries in which there is no data is less than or equal to 2*the number of queues. Processing then continues as above. 
     When the consecutive number of tries in which there is no data is not less than or equal to 2*the number of queues, the queue manager determines if the queue has any data ( 703 ). 
     When the queue does not have any data, the queue manager determines if the consecutive number of tries in which there is no data the thread manager then determines if the consecutive number of tries in which there is no data is less than or equal to 2*the number of queues ( 704 ), and processing continues as above; when the queue has data, the queue manager determines if the consecutive number of tries in which there is no data the thread manager equals zero ( 705 ), and then processes the queue data ( 706 ) and then determines if the consecutive number of tries in which there is no data is less than or equal to 2*the number of queues. Processing then continues as above. 
     In embodiment of the invention, there is always at least one worker thread running. If a thread iterates through  2 *the number of queues with no data found, then the thread dies, and the thread pool is thus increased by that thread, i.e., the thread that dies is now available should it be needed. 
       FIG. 8  is a flow diagram showing a GetNextQueue( ) algorithm in a messaging environment according to the invention. 
     In  FIG. 8 , the system commences a GetNextQueue( )( 801 ). 
     The current queue is set to be equal to the current queue plus one ( 802 ). 
     When the current queue is greater than the total number of available queues in the system ( 803 ), the current queue is set to one, ( 806 ) i.e., it is reset to the beginning; when the current queue is not greater than the total number of available queues in the system, the system determines if another thread is processing in the current queue ( 804 ). 
     When another thread is processing in the current queue, the current queue is set to be equal to the current queue plus one ( 802 ) and processing continues as set forth above; when another thread is not processing in the current queue, the process is done ( 805 ). 
     The system iterates through the queue list sequentially. When the system gets to the ends of the queue list, it restarts from the beginning of the list. 
     Computer Implementation 
       FIG. 9  is a block diagram of a computer system that may be used to implement certain features of some of the embodiments of the invention. The computer system may be a server computer, a client computer, a personal computer (PC), a user device, a tablet PC, a laptop computer, a personal digital assistant (PDA), a cellular telephone, an iPhone, an iPad, a Blackberry, a processor, a telephone, a web appliance, a network router, switch or bridge, a console, a hand-held console, a (hand-held) gaming device, a music player, any portable, mobile, hand-held device, wearable device, or any machine capable of executing a set of instructions, sequential or otherwise, that specify actions to be taken by that machine. 
     The computing system  90  may include one or more central processing units (“processors”)  95 , memory  91 , input/output devices  94 , e.g., keyboard and pointing devices, touch devices, display devices, storage devices  92 , e.g., disk drives, and network adapters  93 , e.g., network interfaces, that are connected to an interconnect  96 . 
     In  FIG. 9 , the interconnect is illustrated as an abstraction that represents any one or more separate physical buses, point-to-point connections, or both connected by appropriate bridges, adapters, or controllers. The interconnect, therefore, may include, for example a system bus, a peripheral component interconnect (PCI) bus or PCI-Express bus, a HyperTransport or industry standard architecture (ISA) bus, a small computer system interface (SCSI) bus, a universal serial bus (USB), IIC (12C) bus, or an Institute of Electrical and Electronics Engineers (IEEE) standard 1394 bus, also referred to as Firewire. 
     The memory  91  and storage devices  92  are computer-readable storage media that may store instructions that implement at least portions of the various embodiments of the invention. In addition, the data structures and message structures may be stored or transmitted via a data transmission medium, e.g., a signal on a communications link. Various communications links may be used, e.g., the Internet, a local area network, a wide area network, or a point-to-point dial-up connection. Thus, computer readable media can include computer-readable storage media, e.g. non-transitory media, and computer-readable transmission media. 
     The instructions stored in memory  91  can be implemented as software and/or firmware to program one or more processors to carry out the actions described above. In some embodiments of the invention, such software or firmware may be initially provided to the processing system  90  by downloading it from a remote system through the computing system, e.g., via the network adapter  93 . 
     The various embodiments of the invention introduced herein can be implemented by, for example, programmable circuitry, e.g., one or more microprocessors, programmed with software and/or firmware, entirely in special-purpose hardwired, i.e., non-programmable, circuitry, or in a combination of such forms. Special-purpose hardwired circuitry may be in the form of, for example, one or more ASICs, PLDs, FPGAs, etc. 
     Although the invention is described herein with reference to the preferred embodiment, one skilled in the art will readily appreciate that other applications may be substituted for those set forth herein without departing from the spirit and scope of the present invention. Accordingly, the invention should only be limited by the Claims included below.