将 LevelDB RocksDB 系统放置在内存(全内存架构)

1024程序员节,冒个泡…

!本文基于人工和使用GPT-4的Copilot混合生成!

LevelDB 全内存架构

一般意义上,我们认为LevelDB 将MemTable和ImmTable放置在内存,底下的SSTable放置在硬盘(SSD、HDD、PM)上。但 LevelDB 同时也提供了全内存LevelDB 的测试方案,即将所有LevelDB 的组件都放置在内存中。

memenv提供了一个完全基于内存的文件系统接口。这对于单元测试和性能分析是很有用的,使用memenv,就无需在单元测试中去做文件的创建和删除,同时性能分析中也可以去除磁盘的影响,可以方便地了解到各种操作在CPU方面的性能,同时也可以与磁盘文件做性能对比分析以了解IO方面的开销。通过这个InMemoryEnv,用户就可以将LevelDB架在内存中。

示例代码:

#include "leveldb/db.h"
#include "helpers/memenv/memenv.h"

int main() {
  leveldb::DB* db;
  leveldb::Options options;
  options.create_if_missing = true;

  // 创建一个新的内存环境
  leveldb::Env* memenv = leveldb::NewMemEnv(leveldb::Env::Default());

  // 使用内存环境
  options.env = memenv;

  // 打开数据库
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  // 使用数据库...

  delete db;
  delete memenv;

  return 0;
}

在LevelDB中,你可以使用leveldb::NewMemEnv函数来创建一个新的InMemoryEnv实例,然后将这个实例作为你的数据库环境。在这个示例中,我们首先创建了一个新的InMemoryEnv实例,然后将这个实例设置为我们的数据库环境。然后,我们就可以像平常一样使用数据库了。注意,由于我们使用的是内存环境,所以所有的数据都将存储在内存中,而不是硬盘上。

RocksDB 全内存架构

一开始以为RocksDB全内存设置也和LevelDB的设置方式一样,结果发现不work,代码如下:

// std::unique_ptr<ROCKSDB_NAMESPACE::Env> mem_env(ROCKSDB_NAMESPACE::NewMemEnv(ROCKSDB_NAMESPACE::Env::Default()));
// options.env = mem_env.get();

查阅RocksDB 文档:How to persist in memory RocksDB database

在不考虑宕机Recover 的情况下,将RocksDB 架在内存中最简单的步骤如下:

  • You need to create a virtual file system that uses your RAM as storage space. This way, you can store your RocksDB data files in memory, which will make them faster to access and modify. To do this, you need to use the mount command with the appropriate options. For example, you can use the following command to mount a 4GB tmpfs file system to /mnt/rocksdb: sudo mount -t tmpfs -o size=4G tmpfs /mnt/rocksdb

  • Then, you can set your RocksDB directory to /mnt/rocksdb in your code or configuration file. This will make RocksDB use the in-memory file system for storing its data files.

这块内存VFS会占用内存,因此,当不需要在内存中继续测试的时候,我们可以将这块VFS占用的内存释放,操作如下:

  • Ensure no processes are using the tmpfs or ramfs: Before you can unmount the tmpfs or ramfs, you need to make sure that no processes are using it. You can use the lsof command to check for any open files in the mount point. For example, lsof /mnt/rocksdb.

  • Unmount the tmpfs or ramfs: Once you have ensured that no processes are using the tmpfs or ramfs, you can unmount it using the umount command. For example, sudo umount /mnt/rocksdb.

  • After these steps, the RAM used by the tmpfs or ramfs will be released back to the system and can be used by other processes.Please note that any data stored in the tmpfs or ramfs will be lost once it is unmounted. So make sure to backup any important data before unmounting.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值