Time parsing algorithm wheel (Netty HashedWheelTimer source interpretation)

1. Background

Wheel time algorithm can be used to efficiently perform a number of scheduled tasks.

A typical scenario in Netty is determine if a connection is idle, if idle (such as a client due to network causes the heartbeat to the server can not be delivered), the server will automatically disconnect and release resources. Thanks Netty NIO excellent performance, can maintain server-based Netty developed a large number of long connection, a single 8-core 16G cloud host can be maintained simultaneously connected hundreds of thousands of long, timely Qiadiao inactive connection is especially important.

2, Introduction to Algorithms

                                            

Pirates Online dpi, a time round the algorithm may be described by FIG. Suppose the size of a time round a turn 8,1s grid, each grid point to a linked list, holds the task to be performed.

Suppose, currently located in 2, after the task is now to add a point of 3s, the 3 + 2 = 5, a node is added to the task list points to the fifth grid, the identification round = 0.

Suppose, currently located in 2, after the task is now to add a point 10s, the (2 + 10)% 8 = 4, a node is added in the fourth grid point to Task, and identifies round = 1, the time when the second wheel after the 4th time frame that will perform the task.

Performing only a time round round = 0 of tasks, and other tasks will round on the grid minus 1.

Algorithm principle is very easy to understand, but read the source code implementation is still useful.

3, parsing source code

1, construction method

parameter:

1)threadFactory

Used to generate the worker thread

2) tickDuration and unit

Each grid intervals default 100ms

3)ticksPerWheel

There are several grid lap down, default 512, in particular, if the incoming power N is not 2, it is adjusted to be greater than or equal to the first parameter of the N-th power of 2, the benefits can be optimized hash value calculated

4)leakDetection

If false, it will only work when the thread is not tracking a background thread resource leaks, this parameter can be ignored

5)maxPendingTimeouts

The maximum number of pending default -1 means no limit

Note: You can see the end of the constructor is executed, the worker thread does not start, then it should be launched in the first to add a task, we continue to look at ways to add tasks newTimeout

2、newTimeout

首先,通过一个原子变量来计数当前的任务数,如果设置最大pending且超过了,则会直接throw Exception

其次,便是调用start方法来正式启用worker线程,为了防止重复调用,使用了一个原子操作,并且调用完毕之后会CountDownLatch.await阻塞住,直到线程完全起来才返回

 

可以看到,方法是public的,也即用户可以显示的调用,而无需等待第一次添加任务时再启动

最后,便是包装一个HashedWheelTimeout对象(计算出了deadline),丢给队列,等待工作线程处理,那么接下来的重点就是看worker线程的实现了

 

3、Worker线程

工作线程启动的第一步是初始化startTime,并调用countDown来通知start方法,初始化结束了

其次便是一个循环,循环内的行为就是每隔一段跳一格的操作了,我们看具体的操作:

1)首先调用waitForNextTick()

 

首先计算一下当前tick下的deadline,减去startTime,得到sleepTimeMs,随后sleep一下。这里面有几个小细节:

计算sleepTimeMs先加999999,应该是不足1ms的,补足1ms

因为每次执行定时任务消耗的时候是不受控制的,因此算出来的sleepTimeMs可能为负,这个时候就可以直接返回了执行下一个格子里的任务了

如果currentTime==Long.MIN_VALUE,会直接返回一个负数,这个应该是为了处理时间轮执行了很长时间导致的long值溢出,具体了解的可以评论里告诉,不胜感激

下面还有一个,如果是windows平台,先除以10再乘以10,是因为windows平台下最小调度单位是10ms,如果不处理成10ms的倍数,可能导致sleep更不准了

最后,如果线程被打断了,并且是shutdown状态,会直接返回负数,并在随后的while判断中挑出循环

2)随后调用processCanceldTasks()

该方法是为了处理那些被取消的任务,任务存放在一个queue中

 

3)transferTimeoutsToBuckets()

该方法是从timeouts(就是前面newTimeout是放进去的那个queue)的queue中取出任务,放到格子里(HashedWheelBucket是一个链表),为了防止这个操作销毁太多时间,导致更多的任务时间不准,因此一次最多操作10w个。几个注意点:

计算stopIndes时,含义是取模,因为mask是2的N次方减1,因此%和&可以等价操作,即x % (mask + 1) == x & mask,这个技巧在jdk的集合类中也被使用到

为了防止出现任务延迟太久,因此在计算模之前,还先取max in (calculated, tick),从而让那些本应该在 过去执行的任务,在这期先快速执行掉

 

4)expireTimeouts(deadline)

这是HashedWheelBucket的一个方法,就是来执行该格子里那些已经过期的任务

这步的操作比较简单,就是一次遍历链表,如果remainingRounds(剩下的圈数)小于等于0,那么就把他移除并执行expire方法(即TimerTask的run方法);如果任务被取消了,则直接移除;否则remainingRounds减一,等待下一圈

 

5)如果中间时间轮的状态不再是started,那么就会跳出循环,并依次取出各个bucket上的未执行且没有被取消的任务,stop方法会返回这个列表

4、总结

  时间轮算法理解起来很简单,实现也似乎不难,但是通过阅读源码,可以看到,其中还是有很多很多的小细节需要注意,这个就不容易了

  而且通过阅读源码,可以看到,整个时间轮的调度都是在一个线程里完成的,因此对于那些耗时较大的定时任务,如果直接扔进去处理显然会影响其他任务的正常执行,例子如下:

                                              

 

                                                                        

 

 

转载:https://sq.163yun.com/blog/article/177510753845874688

Guess you like

Origin blog.csdn.net/demon7552003/article/details/92054262