CompactionStyle
enum CompactionStyle : char {
// level based compaction style
kCompactionStyleLevel = 0x0,
// Universal compaction style
// Not supported in ROCKSDB_LITE.
kCompactionStyleUniversal = 0x1,
// FIFO compaction style
// Not supported in ROCKSDB_LITE
kCompactionStyleFIFO = 0x2,
// Disable background compaction. Compaction jobs are submitted
// via CompactFiles().
// Not supported in ROCKSDB_LITE
kCompactionStyleNone = 0x3,
};
rocksdb 有三种compact style:
- kCompactionStyleLevel:rocksdb默认的compact style。这种策略保证每层的key都是有序并且互不重叠,但层于层直接的key可能会重叠。当Ln层满足compact条件时,会根据CompactionPri策略(下面会介绍),挑选出Ln层的文件file1和Ln+1层中与file1有key重叠的file进行合并,生成file2,并将file2插入到Ln+1层中。该策略拥有最小的空间放大,但是带来很大的写放大和读放大。
- kCompactionStyleUniversal:这种style将磁盘上数据完全按照写入时间线去组织,只将相邻时间段内写入磁盘上的数据compact,很像在合并互不重叠(overlapping)的时间段。该策略写放大比较低,但是读放大和空间放大比较高。
- kCompactionStyleFIFO:通常用于几乎不修改的场景,比如消息队列、时序数据库等。
- kCompactionStyleNone:关闭自动compact,手动触发执行。
CompactionPri
// In Level-based compaction, it Determines which file from a level to be
// picked to merge to the next level. We suggest people try
// kMinOverlappingRatio first when you tune your database.
enum CompactionPri : char {
// Slightly prioritize larger files by size compensated by #deletes
kByCompensatedSize = 0x0,
// First compact files whose data's latest update time is oldest.
// Try this if you only update some hot keys in small ranges.
kOldestLargestSeqFirst = 0x1,
// First compact files whose range hasn't been compacted to the next level
// for the longest. If your updates are random across the key space,
// write amplification is slightly better with this option.
kOldestSmallestSeqFirst = 0x2,
// First compact files whose ratio between overlapping size in next level
// and its size is the smallest. It in many cases can optimize write
// amplification.
kMinOverlappingRatio = 0x3,
};
该参数控制每次compact,挑选哪些文件参与compact。
- kByCompensatedSize:优先选择被删除数据最多的file去compact。rocksdb的默认设置。被删除的数据越早被compact,越可以减少空间放大,同时降低读放大。
- kOldestLargestSeqFirst:优先选择冷数据file去compact。将冷数据compact到下一层,而将热数据保留在上层,可以有效降低读放大问题。适用有冷热数据之分的场景。
- kOldestSmallestSeqFirst:优先选择key范围密集的file去compact。file中key范围越密集,就会有更少的重叠file在下一level,从而降低compact开销。适用于key空间均匀分布的场景。
- kMinOverlappingRatio:优先选择和下层key范围重叠率最小的文件去compact。可以降低写放大。
网友评论