美文网首页程序员
Tuning RocksDB - Write Stalls

Tuning RocksDB - Write Stalls

作者: siddontang | 来源:发表于2017-03-22 21:11 被阅读1095次

RocksDB 使用 LSM 的方式用来提升写入的性能,但如果写入过快,超过了 RocksDB 处理的极限,RocksDB 就会考虑对写入进行降速处理。这个在 TiKV 调优的时候遇到过很多次,当我们持续大量插入数据的时候,会发现到了某一个时间,性能就突然下降了,如果突然出现了这样的情况,我们都会从 LOG 文件里或者 statistics 上面来确认是否出现了 write stall。

Where Stall

通常 write stall 会在几个地方出现

Too many memtables

当需要等待被 flush 到 level 0 的 memtable 到了或者超过了 max_write_buffer_number,RocksDB 就会完全 stop 写入,直到 flush 结束。同时,当 max_write_buffer_number 大于等于 3,需要 flush 的 memtable 数量已经大于等于 max_writer_buffer_number - 1 的时候,RocksDB 就会 stall 写入。我们可以在 LOG 里面看到如下的信息:

Stopping writes because we have 5 immutable memtables (waiting for flush), max_write_buffer_number is set to 5

Stalling writes because we have 4 immutable memtables (waiting for flush), max_write_buffer_number is set to 5

Too many level-0 SST files

当 level 0 的 SST file 的数量达到 level0_slowdown_writes_tigger 的时候,RocksDB 就会 stall 写入。当 level 0 的 SST file 的数量达到 level0_stop_writes_trigger 的时候,RocksDB 就会 stop 写入,直到 level 0 到 level 1 之间的 compaction 完成,level 0 SST file 的数量减少之后。我们可以在 LOG 里面看到如下的信息:

Stalling writes because we have 4 level-0 files

Stopping writes because we have 20 level-0 files

Too many pending compaction bytes

当预计的 compaction 数据的大小达到了 sofe_pending_compaction_bytes 之后,RocksDB 会 stall 写入。当达到了 hard_pending_compaction_bytes 之后,则会 stop 写入。我们可以在 LOG 里面看到如下的信息:

Stalling writes because of estimated pending compaction bytes 500000000

Stopping writes because of estimated pending compaction bytes 1000000000

Mitigate Stall

需要注意的是,很多时候,受限于机器的性能,我们并不能杜绝 stall,只能通过配置尽量的改善。

当发生 stall 的时候,RocksDB 会降低写入的速度到 delayed_write_rate,甚至有可能比这个更低。另外需要注意的是 slowdown/stop trigger 或者 pending compaction limit 都是针对不同的 CF 的,但 stall 是针对整个 DB 的,如果程序里面有多个 CF,一个 CF 出现了 stall 的情况,整个 DB 都会 stall。

如果 stall 是因为 pending flush memtable 不及时导致的,我们可以尝试:

  • 增大 max_background_flushes ,这样就能有更多的线程同时 flush memtable。
  • 增大 max_write_buffer_number ,用更小的 memtable 来提升 flush 的速度。

如果 stall 是因为 level 0 或者 pending compaction 太多导致,我们就需要考虑提升 compaction 的速度。另外,也可以减小写放大,因为写放大越小,需要 compaction 的数据量就越小。所以我们可以尝试:

  • 增大 max_background_compactions,用更多的线程来进行 compaction。
  • 增大 write_buffer_size,这样就能有更大的 memtable,用来减少写放大
  • 增加 min_write_buffer_number_to_merge,在 flush 之前先将 memtable merge,减少写入 key 的数量,但这样会影响从 memtable read 的性能。

小结

对于写性能要求非常高的系统来说,write stall 是一个绕不过去的坎,所以我们只能在不同的场景下面通过配置来在 write,read 和 space 这三个上面做平衡。所以需要更加深入的去理解 RocksDB 那一坨参数以及相关的含义。

相关文章

  • Tuning RocksDB - Write Stalls

    RocksDB 使用 LSM 的方式用来提升写入的性能,但如果写入过快,超过了 RocksDB 处理的极限,Roc...

  • 自动调优 RocksDB

    最近看到一篇 Paper,Auto-tuning RocksDB,顿时两眼放光。RocksDB 以配置多,难优化而...

  • Tuning RocksDB - Statistics

    RocksDB 是一个性能非常强悍的 Key-Value 存储引擎,很多项目包括我们的 TiKV 都使用它来存储数...

  • Tuning RocksDB - Options

    在先前我们讨论了 RocksDB 的 statistics 和 write stall,但这些只能让我们发现问题,...

  • Tuning RocksDB - Prefix Extracto

    在 TiKV 里面,我们在 Write 和 Raft column family (CF) 上面使用了 prefi...

  • RocksDB源码分析 Write

    写入流程 将一条或者多条操作的记录封装到WriteBatch 将记录对应的日志写到WAL文件中 将WriteBat...

  • rocksdb 的write stall

    为什么需要write stall 我们知道, 当flush/compaction赶不上write rate的速度时...

  • rocksdb系列之write stall

    为什么需要write stall 我们知道, 当flush/compaction赶不上write rate的速度时...

  • RocksDB 的 Group Write 机制

    RocksDB 是 LSM-tree 结构的 KV 存储,写入的数据先通过 WAL 持久化,再写入到 memtab...

  • RocksDB系列六:Write Ahead Log

    对RocksDB的每一次update都会写入两个位置:1) 内存表(内存数据结构,后续会flush到SST fil...

网友评论

    本文标题:Tuning RocksDB - Write Stalls

    本文链接:https://www.haomeiwen.com/subject/cqjanttx.html