Design and implementation of Time wheel (Timewheel)

Source: Internet
Author: User

First, preface

Due to the need for work, a time wheel for controlling event timeout discards is implemented, as this is a relatively independent interface, and a summary is shared.

First look at the requirements, this time round need to have the following features:

1) can add the event, and attach its time-out period;

2) If the event is performed properly, it can be shown that it is removed from the time wheel, without having to wait for the time wheel to automatically remove it;

3) If the event has not been completed after the set time-out, the time wheel needs to remove it and send a timeout message to the system.

Based on this demand, the following design and implementation of the corresponding.

second, the design of the time wheel

Based on the previous requirements, two entities can be abstracted: clocks and slots, where the clock is responsible for moving the pointer around, acquiring, etc., and the slots are used to store events that fall at various points in time. There are also three behaviors required: Add, delete for normal execution, delete for timeout, and send message. With this abstraction, two classes of Clocker and slots are designed, respectively, to represent the clock and slot, and for several behaviors, you need to put in a master control class, designed as Timewheel, exposing the interface to external calls. So the static structure of the system comes out.

Notice that when the event times out, a message needs to be sent out for the event to perform related processing, which is an interface-oriented programming approach. We first define an interface expiration, there is only one method expired method. Specific events need to implement this interface, so that in our time round, as long as the timeout, directly with the incoming event to invoke the expired method can be used to send the message!

In the combination of several departments, there is the main frame class diagram.

  

For the Clocker class, it has to be a separate thread to complete as it controls the time to move around. The time to move around, it is best not to thread.sleep to simulate; You can use an empty blocking queue, and then each time you call the poll method, set the time interval is the time-out period, so the effect is better. For a new event, with a timeout, you need to find the position of the pointer where it should be, usually by dividing the time-out period by the predefined interval, getting an offset, plus the current position.

For the main class Timewheel, because at the end of the event, it is possible for the event to be actively removed from the time wheel, so its Add method needs to return the location of the slot where the event is located, so that when the remove, the location information, it is convenient to find the slots sitting Delete the event from.

When the time-out expires, you need to clear all the elements in the corresponding slot, this work can be done in the slot class, the corresponding slot all the elements are taken out to the temporary variable, the current slot is emptied, so that there will be no event backlog in the slot. For the removed element, call the corresponding expired method.

Based on this design, it has been able to meet the needs of the system, and the operation is relatively good.

Third, optimize

For the above design, I think there will be a modified point, that is, for the timeout element execution expired method is executed in the Timewheel thread, this will have the problem:

1) Unsure of the time it takes to execute the expired method, which may affect the main thread;

2) If an exception occurs in the expired method, the main thread may be affected.

Based on this consideration, we can put these events into a blocking queue, then another thread, specifically to the queue to take elements, execute the expired method. This would be a good way to isolate the expired operation from the main thread. For such a design, that is, the typical producer-consumer model , the main thread timewheel is responsible for production data, design a release thread responsible for consumption data, the warehouse with the blocking queue to assume, so that better solve the problem, played a role in optimization.

  

  

Design and implementation of Time wheel (Timewheel)

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.