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

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

Java Kafka消费者实现过程

《JavaKafka消费者实现过程》Kafka消费者通过KafkaConsumer类实现,核心机制包括偏移量管理、消费者组协调、批量拉取消息及多线程处理,手动提交offset确保数据可靠性,自动提交... 目录基础KafkaConsumer类分析关键代码与核心算法2.1 订阅与分区分配2.2 拉取消息2.3

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python利用PySpark和Kafka实现流处理引擎构建指南

《Python利用PySpark和Kafka实现流处理引擎构建指南》本文将深入解剖基于Python的实时处理黄金组合:Kafka(分布式消息队列)与PySpark(分布式计算引擎)的化学反应,并构建一... 目录引言:数据洪流时代的生存法则第一章 Kafka:数据世界的中央神经系统消息引擎核心设计哲学高吞吐

MySQL按时间维度对亿级数据表进行平滑分表

《MySQL按时间维度对亿级数据表进行平滑分表》本文将以一个真实的4亿数据表分表案例为基础,详细介绍如何在不影响线上业务的情况下,完成按时间维度分表的完整过程,感兴趣的小伙伴可以了解一下... 目录引言一、为什么我们需要分表1.1 单表数据量过大的问题1.2 分表方案选型二、分表前的准备工作2.1 数据评估