TerarkDB兼容 RocksDB 的 KV 存储引擎

联合创作 · 2023-10-01 01:44

TerarkDB 是字节跳动开源的 RocksDB 的替代品,具有优化的尾部延迟、吞吐量和压缩等。在大多数情况下,你可以将现有的 RocksDB 实例迁移到 TerarkDB,没有任何问题。

  • TerarkDB 仅在 Linux 平台下测试和生产就绪
  • 除 C/C++ 之外的语言绑定尚未完全测试。
  • 现有数据可以直接从 RocksDB 迁移到 TerarkDB,但不能迁移回 RocksDB。
  • TerarkDB 是从 RocksDB v5.18.3 分支出来的

性能概览

  • RocksDB v6.12
  • 服务器
    • Intel(R) Xeon(R) Gold 5218 CPU @ 2.30GHz(2 插槽,32 核 64 线程)
    • 376 GB 动态内存
    • NVMe TLC 固态硬盘(3.5 TB)
  • 工作台工具和工作负载
    • 利用 db_bench
    • 10 个客户端线程,每个线程 20GB 请求
    • 键 = 24 字节,值 = 2000 字节
    • heavy_write 意味着 90% 的写操作
    • heavy_read 意味着 90% 的读取操作

用法

  • 基于块的表(BlockBasedTable
#include <cassert>
#include "rocksdb/db.h"

rocksdb::DB* db;
rocksdb::Options options;

// Your options here
options.create_if_missing = true;
options.wal_bytes_per_sync = 32768;
options.bytes_per_sync = 32768;

// Open DB
auto status = rocksdb::DB::Open(options, "/tmp/testdb", &db);

// Operations
std::string value;
auto s = db->Put(rocksdb::WriteOptions(), "key1", "value1");
s = db->Get(rocksdb::ReadOptions(), "key1", &value);
assert(s.ok());
assert("value1" == value);

s = db->Delete(rocksdb::WriteOptions(), "key1");
assert(s.ok());

或手动设置表格格式和表格选项:

#include <cassert>
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/table.h"

rocksdb::DB* db;
rocksdb::Options options;

// Your db options here
options.create_if_missing = true;
options.wal_bytes_per_sync = 32768;
options.bytes_per_sync = 32768;

// Manually specify target table and table options
rocksdb::BlockBasedTableOptions table_options;
table_options.block_cache =
    rocksdb::NewLRUCache(32ULL << 30, 8, false);
table_options.block_size = 8ULL << 10;
options.table_factory = std::shared_ptr<rocksdb::TableFactory>
                          (NewBlockBasedTableFactory(table_options));

// Open DB
auto status = rocksdb::DB::Open(options, "/tmp/testdb2", &db);

// Operations
std::string value;
auto s = db->Put(rocksdb::WriteOptions(), "key1", "value1");
s = db->Get(rocksdb::ReadOptions(), "key1", &value);
assert(s.ok());
assert("value1" == value);

s = db->Delete(rocksdb::WriteOptions(), "key1");
assert(s.ok());
  • Terark 压缩表格(TerarkZipTable
#include <cassert>
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/table.h"
#include "table/terark_zip_table.h"

rocksdb::DB* db;
rocksdb::Options options;

// Your db options here
options.create_if_missing = true;
options.wal_bytes_per_sync = 32768;
options.bytes_per_sync = 32768;

// TerarkZipTable need a `fallback` options because you can indicate which LSM level you want to start using TerarkZipTable
// For example, by setting tzt_options.terarkZipMinLevel = 2, TerarkDB will use your fallback Table on level 0 and 1.
std::shared_ptr<rocksdb::TableFactory> table_factory;
rocksdb::BlockBasedTableOptions blockbased_options;
blockbased_options.block_size = 8ULL << 10;
table_factory.reset(NewBlockBasedTableFactory(blockbased_options));

rocksdb::TerarkZipTableOptions tzt_options;
// TerarkZipTable requires a temp directory other than data directory, a slow device is acceptable
tzt_options.localTempDir = "/tmp";
tzt_options.indexNestLevel = 3;
tzt_options.sampleRatio = 0.01;
tzt_options.terarkZipMinLevel = 2; // Start using TerarkZipTable from level 2

table_factory.reset(
    rocksdb::NewTerarkZipTableFactory(tzt_options, table_factory));

options.table_factory = table_factory;

// Open DB
auto status = rocksdb::DB::Open(options, "/tmp/testdb2", &db);

// Operations
std::string value;
auto s = db->Put(rocksdb::WriteOptions(), "key1", "value1");
s = db->Get(rocksdb::ReadOptions(), "key1", &value);
assert(s.ok());
assert("value1" == value);

s = db->Delete(rocksdb::WriteOptions(), "key1");
assert(s.ok());

实际性能改进

TerarkDB 已经部署在字节跳动的许多应用程序中,在大多数情况下,TerarkDB 可以帮助减少延迟峰值并极大地提高吞吐量。

磁盘写入(Disk Write

获取延迟(Get Latency )

浏览 12
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报