RocksDB学习笔记

阅读量: searchstar 2023-07-28 13:52:54
Categories: Tags:

Version是在某个瞬间各种SSTable的snapshot。

SuperVersion是最新的Version加上

ColumnFamilyData和SuperVersion互相保存了对方的引用。在ColumnFamilyData::InstallSuperVersion中,会先调用SuperVersion::Init将SuperVersion的ref变成1,并且让这个SuperVersion引用自己,让自己的引用计数+1。

Get的时候如果不是在snapshot上get,那么先拿thread local的SuperVersion,再在上面Get。在ColumnFamilyData::InstallSuperVersion中会更新thread local SuperVersion。

在hold DB mutex的情况下,可以直接调用ColumnFamilyData::GetSuperVersion拿最新的SuperVersion。

rocksdb.stats

std::string rocksdb_stats;
db->GetProperty("rocksdb.stats", &rocksdb_stats)

其中Compaction Stats里每行是以该level为output level的compaction的stats,Rn是non output level的读取量,Rnp1是output level的读取量,其中p1应该是plus 1的意思。RnRnp1相加就是Read

值得注意的是,flush到L0的I/O也被算进了L0的write里。

Secondary cache

官方文档:https://github.com/facebook/rocksdb/wiki/SecondaryCache-(Experimental)

官方讲解:https://rocksdb.org/blog/2021/05/27/rocksdb-secondary-cache.html

CompressedSecondaryCacheOptions (in-memory)

  rocksdb::CompressedSecondaryCacheOptions secondary_cache_opts;
  secondary_cache_opts.capacity = 字节数;
  secondary_cache_opts.compression_type = CompressionType的某一项;
  rocksdb::LRUCacheOptions lru_cache_opts;
  lru_cache_opts.capacity = 字节数;
  lru_cache_opts.secondary_cache =
      NewCompressedSecondaryCache(secondary_cache_opts);
  rocksdb::BlockBasedTableOptions table_options;
  table_options.block_cache = rocksdb::NewLRUCache(lru_cache_opts);
  rocksdb::Options options;
  options.table_factory.reset(
      rocksdb::NewBlockBasedTableFactory(table_options));

RocksCachelibWrapper

https://github.com/facebook/CacheLib/blob/main/cachelib/adaptor/rocks_secondary_cache/CachelibWrapper.h

facebook::rocks_secondary_cache::RocksCachelibWrapper

可惜其中有一部分没有开源,所以无法编译:https://github.com/facebook/CacheLib/issues/278

其实只要把CachelibWrapper.hCachelibWrapper.cpp这两个文件拷贝到自己的project里,然后把里面引用了facebook内部代码的代码删掉即可:https://github.com/seekstar/RocksCachelibWrapper

相关:

https://github.com/facebook/rocksdb/issues/8347

https://github.com/facebook/CacheLib/pull/184

Snapshot

官方文档:https://github.com/facebook/rocksdb/wiki/Snapshot

RocksDB的snapshot相当于对当时的DB状态做一个快照。在take snapshot时,将当前sequence number保存下来。在snapshot上读取的时候,忽略sequence number大于snapshot的sequence number的record。

在flush和compaction时,如果一个record对某个snapshot是可见的,即使在有新版本的情况下也会保留它。