RocksDB table properties 源码

使用场景

TiKV 中的使用:

  1. TiDB GC 中用来通过 table collector 统计文件中 mvcc 版本信息,min mvcc ts ,delete和 put 数量等,来优化 filter 的触发时机,避免需要 gc 的数据太少的时候依然触发 gc filter。

  1. RawKV V1TTL 场景:通过收集文件中 min ts,避免不必要的 filter 触发。

  1. 其他:RocksDB 本身 NewCompactOnDeletionCollectorFactory ,来处理 delete 量远多余 put 的情况;

使用方式

  1. 自定义一个 table properties ,里面自定义指标

  2. 自定义一个 rocksdb table collector 来做自定义指标的具体统计逻辑

  3. 一个 DB 可以加载多个 table collector

主要实现 table collector 的 Finish 和 Add 两个函数;

Finish 和 Add 在 RocksDB 中的调用

  class TablePropertiesCollector {
 public:
  virtual ~TablePropertiesCollector() {}

  // DEPRECATE User defined collector should implement AddUserKey(), though
  //           this old function still works for backward compatible reason.
  // Add() will be called when a new key/value pair is inserted into the table.
  // @params key    the user key that is inserted into the table.
  // @params value  the value that is inserted into the table.
  virtual Status Add(const Slice& /*key*/, const Slice& /*value*/) {
    return Status::InvalidArgument(
        "TablePropertiesCollector::Add() deprecated.");
  }

  

  // Finish() will be called when a table has already been built and is ready
  // for writing the properties block.
  // @params properties  User will add their collected statistics to
  // `properties`.
  virtual Status Finish(UserCollectedProperties* properties) = 0;


  //...
};

RocksDB中的调用链路

用户定义的table collector 加载:

CompactionJob::ProcessKeyValueCompaction 
  CompactionJob::OpenCompactionOutputFile 
    TableBuilder* NewTableBuilder 
        BlockBasedTableFactory::NewTableBuilder 
            BlockBasedTableBuilder::BlockBasedTableBuilder 
                Rep 

Finish 函数调用链路

构造 sst 结束的时候执行

CompactionJob::ProcessKeyValueCompaction
    CompactionJob::FinishCompactionOutputFile
        GetTableProperties
            collector->Finish

Add 函数调用链路

每遍历一个 entry,执行一次所有加载的 table collector 的 Add 。

CompactionJob::ProcessKeyValueCompaction
    CompactionJob::FinishCompactionOutputFile   //这里遍历所有要固化的数据,写文件
        //遍历过程中调用下面代码
        BlockBasedTableBuilder::Add
            NotifyCollectTableCollectorsOnAdd 
            //这里是和 table collector 相关的代码,可以设置多个 table colloector 
            //NotifyCollectTableCollectorsOnAdd内部每个 colloctor 都会执行一次Add

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值