How to read data from the ring buffer?

Source: Internet
Author: User

Http://mechanitis.blogspot.com/2011/06/dissecting-disruptor-how-do-i-read-from.html author is Trisha Gee, a female engineer at Lmax.

This is the next blog of the disruptor mode series developed by Lmax.

From the previous blog, we understand what ring buffer andHow good is it?. Unfortunately, I have not mentioned how to read and write data when using disruptor.

Consumerbarrier and consumer

Here I will introduce it in turn, because in general this section is easier to understand. Assuming that some magic has already filled the data in the ring buffer, how can we read the data from the ring buffer?

(Well, I began to regret using paint/gimp. Even though this is a good excuse to buy a drawing board, if I continue writing it... the authority in the UML field is probably cursing my name too .)

A consumer (consumer) is a thread that wants to extract data from the ring buffer. It can access the consumerbarrier object. This object is created by ringbuffer and represents the consumer interacting with it. Just as the ring buffer clearly requires a sequence number to locate the next available node, the consumer needs to know the sequence number-each consumer needs to find the next sequence number to access. In the preceding example, after the consumer processes all the data before the serial number 8 in the ring buffer, the next serial number it expects to access is 9.

The consumer can call the waitfor () method of the consumerbarrier object and pass the next sequence number required by it:

JavaCode

    1. Final long availableseq = consumerbarrier. waitfor (nextsequence );
 
Final long availableseq = consumerbarrier. waitfor (nextsequence );

Consumerbarrier returns the maximum accessible serial number of ringbuffer-12 in the preceding example. Consumerbarrier holds a waitstrategy value to determine how it waits for this sequence number. I will not describe its details for the moment. The Code has summarized the advantages and disadvantages of each waitstrategy.

What should we do next?

Next, the consumer will continue to commit and wait for more data to be written into the ring buffer. In addition, after data is written, the consumer will receive notifications-nodes 9, 10, 11, and 12 have been written. Now that the serial number is 12, the consumer can instruct consumerbarrier to get the data in these serial numbers.

After the data is obtained, the consumer updates its own cursor (cursor ).

You should have already felt that, how does this help smooth the latency curve spike-instead of asking one node by one, "Can I get the next data? How is it now? What about now ?", The consumer just needs to simply say "tell me when the number you get is bigger than this". The function return value will tell it how many new data nodes can be read. Because these new nodes have indeed been written (the serial number of the ring buffer itself has been updated), and the only operation the consumer performs on these nodes is read rather than write, so no locks are needed for access. This is really good. Not only can the code be more secure and simple, but it does not need to be locked for extremely fast speed.

Another additional benefit is that you can use multiple consumer to read the same ringbuffer without locking or using another queue to coordinate different threads. In this way, you can implement real concurrent data processing under the coordination of disruptor.

Batchconsumer is the sample code of a consumer. If you implement batchhandler, you can use batchconsumer to complete the complex work I mentioned above. It is easy to implement the function of processing nodes in batches (such as the nodes 9-12 above) without reading each node separately.

Update: note that disruptor 2.0 is named differently from this document. If you are confused about the class name, please read my change summary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.