这次的项目我们重点关注RocksDB中的一个环节:Rate Limiter。其实Rate Limiter的思想在很多其他系统中也很常用。

在RocksDB中,后台会实时运行compaction和flush操作,这些都会对磁盘进行大量的写操作。可以通过Rate Limiter来控制最大写入速度的上限。因为在某些场景下,突发的大量写入会导致很大的read latency,从而影响系统性能。

Rate Limiter的基本原理是令牌桶算法:系统每秒往桶里丢数量为1/QPS的令牌(满了为止),写请求只有拿到了令牌才能处理。当桶里没有令牌时便可拒绝服务(阻塞)。它在RocksDB中的实现可以参考这里

Rate Limiter中可以调节的有以下几个参数

  • int64_t rate_bytes_per_sec:控制 compaction 和 flush 每秒总写入量的上限。一般情况下只需要调节这一个参数。
  • int64_t refill_period_us:控制 tokens 多久再次填满,譬如 rate_limit_bytes_per_sec 是 10MB/s,而 refill_period_us 是 100ms,那么每 100ms 的流量就是 1MB/s。
  • int32_t fairness:用来控制 high 和 low priority 的请求,防止 low priority 的请求饿死。

更详细的介绍可以直接看rate_limiter.h:

 // Create a RateLimiter object, which can be shared among RocksDB instances to control write rate of flush and compaction.
// @rate_bytes_per_sec: this is the only parameter you want to set most of the time. It controls the total write rate of compaction and flush in bytes per second. Currently, RocksDB does not enforce rate limit for anything other than flush and compaction, e.g. write to WAL.
// @refill_period_us: this controls how often tokens are refilled. For example, when rate_bytes_per_sec is set to 10MB/s and refill_period_us is set to 100ms, then 1MB is refilled every 100ms internally. Larger value can lead to burstier writes while smaller value introduces more CPU overhead. The default should work for most cases.
// @fairness: RateLimiter accepts high-pri requests and low-pri requests. A low-pri request is usually blocked in favor of hi-pri request. Currently, RocksDB assigns low-pri to request from compaction and high-pri to request from flush. Low-pri requests can get blocked if flush requests come in continuously. This fairness parameter grants low-pri requests permission by 1/fairness chance even though high-pri requests exist to avoid starvation. You should be good by leaving it at default 10.
// @mode: Mode indicates which types of operations count against the limit.
// @auto_tuned: Enables dynamic adjustment of rate limit within the range `[rate_bytes_per_sec / 20, rate_bytes_per_sec]`, according to the recent demand for background I/O.
extern RateLimiter* NewGenericRateLimiter(
int64_t rate_bytes_per_sec, int64_t refill_period_us = * ,
int32_t fairness = ,
RateLimiter::Mode mode = RateLimiter::Mode::kWritesOnly,
bool auto_tuned = false); } // namespace rocksdb

这里有个bool auto_tuned,这是RocksDB中带的一个Auto tune Rate Limiter的模块。因为这个rate_bytes_per_sec(写入速度的上限)是很难手动调节的,太大了没效果,太小了又会导致大量写操作无法继续。所以RocksDB提供了这个模块来自动调节。当开启这个模块时,参数rate_bytes_per_sec的含义就变成了定义写入速度上限的上限(In this case rate_bytes_per_sec will indicate the upper-bound of the window within which a rate limit will be picked dynamically.)。之后这个auto tuner会周期性监测I/O写入量,并相应增大/减小写入量上限的值(重新设置rate_bytes_per_sec_和refill_bytes_per_period_)。这里的benchmark显示Auto-tune Rate Limiter可以有效减少write突然增加的程度。


Rate Limiter的用法

可以通过NewGenericRateLimiter类来新建一个Rate Limiter。可以对每个RocksDB实例单独搞一个,也可以让所有实例共享一个来control the aggregated write rate of flush and compaction。

 RateLimiter* rate_limiter = NewGenericRateLimiter(
rate_bytes_per_sec /* int64_t */,
refill_period_us /* int64_t */,
fairness /* int32_t */);

这里面三个参数的含义上面介绍过啦。

Although tokens are refilled with a certain interval set by refill_period_us, the maximum bytes that can be granted in a single burst have to be bounded since we are not happy to see that tokens are accumulated for a long time and then consumed by a single burst request which definitely does not agree with our intention. GetSingleBurstBytes() returns this upper bound of tokens.

使用时,在每次写请求之前,都要申请token。如果当前请求无法满足(也就是被限速了),请求就会被阻塞,直到token被填充到足够完成请求。

 // block if tokens are not enough
rate_limiter->Request( /* bytes */, rocksdb::Env::IO_HIGH); // perform a write operation
Status s = db->Flush();

在运行过程中,也可以通过 SetBytesPerSecond(int64_t bytes_per_second) 动态修改Rate Limiter的流量。

Ref:

https://rocksdb.org.cn/doc/Rate-Limiter.html

https://github.com/facebook/rocksdb/wiki/Rate-Limiter

https://www.cnblogs.com/cchust/p/6007486.html

https://github.com/facebook/rocksdb/wiki/Statistics

RocksDB Compaction:

https://rimzy.net/category/graphs/

http://yinqiwen.github.io/Ardb/2014/09/13/ardb-practice.html

https://www.reddit.com/r/IAmA/comments/3de3cv/we_are_rocksdb_engineering_team_ask_us_anything/

https://www.cnblogs.com/pdev/p/11277784.html

http://mysql.taobao.org/monthly/2018/11/05/

http://mysql.taobao.org/monthly/2018/12/08/

https://github.com/facebook/rocksdb/wiki/Basic-Operations

..

RocksDB Rate Limiter源码解析的更多相关文章

  1. kube-proxy源码解析

    kubernetes离线安装包,仅需三步 kube-proxy源码解析 ipvs相对于iptables模式具备较高的性能与稳定性, 本文讲以此模式的源码解析为主,如果想去了解iptables模式的原理 ...

  2. Theano:LSTM源码解析

    最难读的Theano代码 这份LSTM代码的作者,感觉和前面Tutorial代码作者不是同一个人.对于Theano.Python的手法使用得非常娴熟. 尤其是在两重并行设计上: ①LSTM各个门之间并 ...

  3. OKHttp源码解析

    http://frodoking.github.io/2015/03/12/android-okhttp/ Android为我们提供了两种HTTP交互的方式:HttpURLConnection 和 A ...

  4. 第三十六节,目标检测之yolo源码解析

    在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的 ...

  5. TiKV 源码解析系列 - Raft 的优化

    本系列文章主要面向 TiKV 社区开发者,重点介绍 TiKV 的系统架构,源码结构,流程解析.目的是使得开发者阅读之后,能对 TiKV 项目有一个初步了解,更好的参与进入 TiKV 的开发中.本文是本 ...

  6. can-utils源码解析cansend

    前言 本文主要介绍socketCan中的发送函数cansend的源码解析. 代码 /* * cansend.c - simple command line tool to send CAN-frame ...

  7. [源码解析] Flink UDAF 背后做了什么

    [源码解析] Flink UDAF 背后做了什么 目录 [源码解析] Flink UDAF 背后做了什么 0x00 摘要 0x01 概念 1.1 概念 1.2 疑问 1.3 UDAF示例代码 0x02 ...

  8. [源码解析] 并行分布式框架 Celery 之架构 (2)

    [源码解析] 并行分布式框架 Celery 之架构 (2) 目录 [源码解析] 并行分布式框架 Celery 之架构 (2) 0x00 摘要 0x01 上文回顾 0x02 worker的思考 2.1 ...

  9. [源码解析] 并行分布式框架 Celery 之 worker 启动 (2)

    [源码解析] 并行分布式框架 Celery 之 worker 启动 (2) 目录 [源码解析] 并行分布式框架 Celery 之 worker 启动 (2) 0x00 摘要 0x01 前文回顾 0x2 ...

随机推荐

  1. Devexpress 10

    序言 Grid表格 资料 https://www.devexpresscn.com/

  2. 容器适配器————heap

    堆(heaps)不是容器,而是一种特别的数据组织方式.堆一般用来保存序列容器. 堆是一个完全二叉树,每个节点与其子节点位置相对.父节点总是大于或等于子节点,这种情况下被叫作大顶堆,或者父节点总是小于或 ...

  3. 当SQL Server出现当 MUST_CHANGE 为 ON (开)时

    取消账号强制策略时出现错误 当 MUST_CHANGE 为 ON (开)时,不能将 CHECK_POLICY 和 CHECK_EXPIRATION 选项设为 OFF (关). (Microsoft S ...

  4. BZOJ 4422 Cow Confinement (线段树、DP、扫描线、差分)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4422 我真服了..这题我能调一天半,最后还是对拍拍出来的...脑子还是有病啊 题解: ...

  5. Spring Boot教程(二十三)使用Swagger2构建强大的RESTful API文档(2)

    添加文档内容 在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容.如下所示,我们通 ...

  6. Httpwatch抓包

    一.下载Httpwatch 二.抓包 1.启动Httpwatch 打开浏览器-选择工具-Httpwatch professional(仅适用于IE和火狐40及以下浏览器) 2.开始抓包 点击“Reco ...

  7. (转)搭建自己的Nuget服务器

    转:https://www.cnblogs.com/knowledgesea/p/5500954.html 序言 你们公司有没有好多项目,有没有好多类库,你们的类库是在tfs中管理,还是svn或者gi ...

  8. 分布式工作流任务调度系统Easy Scheduler正式开源

    分布式工作流任务调度系统Easy Scheduler正式开源 1.背景 在多位技术小伙伴的努力下,经过近2年的研发迭代.内部业务剥离及重构,也经历一批种子用户试用一段时间后,EasyScheduler ...

  9. k8s网络通信

    K8s有三种网络, 1.节点网络 2.pod网络 3.service集群网络,由kube-proxy来负责

  10. Spring的Aop理解

    主要作用:解决代码复用,避免重复性编写代码. 比较典型的场景:日志打印,权限验证,事务处理 参考网址为:http://moon-walker.iteye.com/blog/2381532 spring ...