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

相关文章

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Kafka拦截器的神奇操作方法

《Kafka拦截器的神奇操作方法》Kafka拦截器是一种强大的机制,用于在消息发送和接收过程中插入自定义逻辑,它们可以用于消息定制、日志记录、监控、业务逻辑集成、性能统计和异常处理等,本文介绍Kafk... 目录前言拦截器的基本概念Kafka 拦截器的定义和基本原理:拦截器是 Kafka 消息传递的不可或缺

Golang的CSP模型简介(最新推荐)

《Golang的CSP模型简介(最新推荐)》Golang采用了CSP(CommunicatingSequentialProcesses,通信顺序进程)并发模型,通过goroutine和channe... 目录前言一、介绍1. 什么是 CSP 模型2. Goroutine3. Channel4. Channe

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j