Kafka时间轮(TimerWheel)--算法简介

2023-10-13 22:59

本文主要是介绍Kafka时间轮(TimerWheel)--算法简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、简介

一个简单的时间轮是一个定时器任务桶的循环列表。

  1. 让u作为时间单位。
  2. 尺寸为n的时间轮有n个桶,可以在n*u的时间间隔内保存定时器任务。
  3. 每个bucket保存属于相应时间范围的计时器任务。

在开始时,

  1. 第一个桶保存[0,u)的任务,第二个桶保存[u,2u),…的任务…,[u*(n-1),u*n)的第n个bucket。
  2. 每个时间单位u的间隔,计时器滴答作响移动到下一个bucket,然后使其中的所有计时器任务过期。

因此,计时器从不插入任务到当前时间的存储桶,因为它已经过期

计时器会立即运行已过期的任务。清空的bucket可用于下一轮,因此如果当前为时间t的bucket,它在一个tick之后变成[t+u*n,t+(n+1)*u)的bucket。

二、时间轮复杂度分析 

  • 时间轮的插入/删除(启动定时器/停止定时器)成本为O(1)
  • 优先级队列基于定时器,如java.util.concurrent.DelayQueue和java.util.Timer,具有O(logn)插入/删除成本。

三、简单时间轮

简单时间轮的一个主要缺点是,它假设计时器请求在从当前时间开始的n*u的时间间隔。
如果定时器请求超出该间隔,会产生溢出。

四、分级时间轮

  1. 分级时间轮会处理上面这种溢出,这是一种等级制度有组织的时间轮。
  2. 最低级别的时间分辨率最好。随着向上移动层次结构,时间分辨率变得更粗糙。
  3. 如果一个轮子在一个级别上的分辨率是u并且大小为n,下一级的分辨率应为n*u。
  4. 在每个级别上,溢出为委托给更高一级的轮子。
  5. 当较高级别的轮子发出滴答声时,它会重新插入计时器任务到较低级别。
  6. 溢流轮可以按需创建。当一个桶溢出存储桶过期,其中的所有任务都会递归地重新插入计时器。
  7. 这些任务会被移动到更细粒度的轮子或被执行。插入(启动计时器)成本为O(m),其中m是轮子的数量,与请求的数量相比,这个数量通常很小,并且删除(停止定时器)成本仍然是O(1)

https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X3BuZy8xd0JaQ0dpYVlxQkc4ZmlhSDVEcGNYbDRnWDlkU0poRnpGWHpkZWczUVVnZG9KMjZSdFlRcGliRnNGSzBJWGhpY285eGljUHNaRHZSblljaWNxdWw1N1AwdFAwZy82NDA?x-oss-process=image/format,png

五、示例

假设u是1,n是3。如果开始时间是c,则不同级别的桶是:

时间轮层级bucket
1[c,c][c+1,c+1][c+2,c+2]
2[c,c+2][c+3,c+5][c+6,c+8]
3[c,c+8][c+9,c+17][c+18,c+26]

前序bucket到期时间为后序bucket开始时间。

  1. 因此,在时间=c+1时,存储桶[c,c]、[c,c+2]和[c,c+8]到期。
  2. 级别1的时钟移动到c+1,并创建[c+3,c+3]。
  3. 级别2和级别3的时钟保持在c,因为它们的时钟分别以3和9为单位移动。因此,在级别2和级别3中不会创建新的bucket

注意,级别2中的bucket[c,c+2]不会接收任何任务,因为该范围已经在级别1中涵盖。
级别3中的bucket[c,c+8]也是如此,因为它的范围在级别2中涵盖。

这有点浪费,但简化了实现。

时间轮层级bucket
1[c+1,c+1][c+2,c+2][c+3,c+3]
2[c,c+2][c+3,c+5][c+6,c+8]
3[c,c+8][c+9,c+17][c+18,c+26]

在时间=c+2时,[c+1,c+1]是新到期的, 级别1移动到c+2,并创建[c+4,c+4],

时间轮层级bucket
1[c+2,c+2][c+3,c+3][c+4,c+4]
2[c,c+2][c+3,c+5][c+6,c+8]
3[c,c+8][c+9,c+17][c+18,c+26]

    

在时间=c+3时,[c+2,c+2]是新到期的; 级别2移动到c+3,并创建[c+5,c+5]和[c+9,c+11]。
3级停留在c。

时间轮层级bucket
1[c+3,c+3][c+4,c+4][c+5,c+5]
2[c+3,c+5][c+6,c+8][c+9,c+11]
3[c,c+8][c+9,c+17][c+18,c+26]

当操作在超时之前完成时,分级正时轮工作得特别好。即使一切都超时了,当计时器中有很多项目时,它仍然具有优势。其插入成本(包括重新插入)和删除成本分别为O(m)O(1),而优先级为基于队列的计时器为插入和删除取O(log N),其中N是队列中的项目数。

============================= 英文版  ================================== 


Hierarchical Timing Wheels

A simple timing wheel is a circular list of buckets of timer tasks. Let u be the time unit.
A timing wheel with size n has n buckets and can hold timer tasks in n * u time interval.
Each bucket holds timer tasks that fall into the corresponding time range. At the beginning,
the first bucket holds tasks for [0, u), the second bucket holds tasks for [u, 2u), …,
the n-th bucket for [u * (n -1), u * n). Every interval of time unit u, the timer ticks and
moved to the next bucket then expire all timer tasks in it. So, the timer never insert a task
into the bucket for the current time since it is already expired. The timer immediately runs
the expired task. The emptied bucket is then available for the next round, so if the current
bucket is for the time t, it becomes the bucket for [t + u * n, t + (n + 1) * u) after a tick.
A timing wheel has O(1) cost for insert/delete (start-timer/stop-timer) whereas priority queue
based timers, such as java.util.concurrent.DelayQueue and java.util.Timer, have O(log n)
insert/delete cost.

A major drawback of a simple timing wheel is that it assumes that a timer request is within
the time interval of n * u from the current time. If a timer request is out of this interval,
it is an overflow. A hierarchical timing wheel deals with such overflows. It is a hierarchically
organized timing wheels. The lowest level has the finest time resolution. As moving up the
hierarchy, time resolutions become coarser. If the resolution of a wheel at one level is u and
the size is n, the resolution of the next level should be n * u. At each level overflows are
delegated to the wheel in one level higher. When the wheel in the higher level ticks, it reinsert
timer tasks to the lower level. An overflow wheel can be created on-demand. When a bucket in an
overflow bucket expires, all tasks in it are reinserted into the timer recursively. The tasks
are then moved to the finer grain wheels or be executed. The insert (start-timer) cost is O(m)
where m is the number of wheels, which is usually very small compared to the number of requests
in the system, and the delete (stop-timer) cost is still O(1).

Example
Let's say that u is 1 and n is 3. If the start time is c,
then the buckets at different levels are:

level    buckets
1        [c,c]   [c+1,c+1]  [c+2,c+2]
2        [c,c+2] [c+3,c+5]  [c+6,c+8]
3        [c,c+8] [c+9,c+17] [c+18,c+26]

The bucket expiration is at the time of bucket beginning.
So at time = c+1, buckets [c,c], [c,c+2] and [c,c+8] are expired.
Level 1's clock moves to c+1, and [c+3,c+3] is created.
Level 2 and level3's clock stay at c since their clocks move in unit of 3 and 9, respectively.
So, no new buckets are created in level 2 and 3.

Note that bucket [c,c+2] in level 2 won't receive any task since that range is already covered in level 1.
The same is true for the bucket [c,c+8] in level 3 since its range is covered in level 2.
This is a bit wasteful, but simplifies the implementation.

1        [c+1,c+1]  [c+2,c+2]  [c+3,c+3]
2        [c,c+2]    [c+3,c+5]  [c+6,c+8]
3        [c,c+8]    [c+9,c+17] [c+18,c+26]

At time = c+2, [c+1,c+1] is newly expired.
Level 1 moves to c+2, and [c+4,c+4] is created,

1        [c+2,c+2]  [c+3,c+3]  [c+4,c+4]
2        [c,c+2]    [c+3,c+5]  [c+6,c+8]
3        [c,c+8]    [c+9,c+17] [c+18,c+26]

At time = c+3, [c+2,c+2] is newly expired.
Level 2 moves to c+3, and [c+5,c+5] and [c+9,c+11] are created.
Level 3 stay at c.

1        [c+3,c+3]  [c+4,c+4]  [c+5,c+5]
2        [c+3,c+5]  [c+6,c+8]  [c+9,c+11]
3        [c,c+8]    [c+9,c+17] [c+18,c+26]

The hierarchical timing wheels works especially well when operations are completed before they time out.
Even when everything times out, it still has advantageous when there are many items in the timer.
Its insert cost (including reinsert) and delete cost are O(m) and O(1), respectively while priority
queue based timers takes O(log N) for both insert and delete where N is the number of items in the queue.

这篇关于Kafka时间轮(TimerWheel)--算法简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/206430

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依