翻译 MemTable

网址: https://github.com/facebook/rocksdb/wiki/MemTable

有道

MemTable

MemTable is an in-memory data structure holding data before they are flushed to SST files. It serves both read and write - new writes always insert data to memtable, and reads has to query memtable before reading from SST files, because data in memtable is newer. Once a memtable is full, it becomes immutable and replaced by a new memtable. A background thread will flush the content of the memtable into a SST file, after which the memtable can be destroyed.
MemTable是一个内存中的数据结构,在数据被刷新到SST文件之前,它保存着数据。它同时服务于读和写——新写总是插入数据到memtable,而读必须在从SST文件读取之前查询memtable,因为memtable中的数据是更新的。一旦一个记忆表被填满,它就成为不可变的,并被一个新的记忆表所取代。一个后台线程将内存表的内容刷新到一个SST文件中,然后可以销毁内存表。

The most important options that affect memtable behavior are:
影响memtable行为的最重要的选项有:

  • AdvancedColumnFamilyOptions::memtable_factory: The factory object of memtable. By specifying factory object user can change the underlying implementation of memtable, and provide implementation specific options (Default: SkipListFactory).
    AdvancedColumnFamilyOptions::memtable_factory: memtable的工厂对象。通过指定工厂对象,用户可以改变memtable的底层实现,并提供实现特定的选项(默认:SkipListFactory)。
  • ColumnFamilyOptions::write_buffer_size: Size of a single memtable (Default: 64MB).
    ColumnFamilyOptions::write_buffer_size:单个memtable的大小(默认为64MB)。
  • DBOptions::db_write_buffer_size: Total size of memtables across column families. This can be used to manage the total memory used by memtables. (Default: 0 (Disabled))
    DBOptions::db_write_buffer_size:跨列族memtables的总大小。这可以用来管理memtables使用的总内存。(默认值:0(禁用))
  • DBOptions::write_buffer_manager: Instead of specifying a total size of memtables, user can provide their own write buffer manager to control the overall memtable memory usage. Overrides db_write_buffer_size. (Default: nullptr)
    DBOptions::write_buffer_manager:用户可以提供自己的写缓冲区管理器来控制整个memtable内存的使用,而不是指定memtable的总大小。覆盖db_write_buffer_size。(默认值:nullptr)
  • AdvancedColumnFamilyOptions::max_write_buffer_number: The maximum number of memtables build up in memory, before they flush to SST files. (Default: 2)
    AdvancedColumnFamilyOptions::max_write_buffer_number:在内存中建立的memtables的最大数量,在它们刷新到SST文件之前。(默认值:2)
  • AdvancedColumnFamilyOptions::max_write_buffer_size_to_maintain: The amount of write history to maintain in memory, in bytes. This includes the current memtable size, sealed but unflushed memtables, and flushed memtables that are kept around. RocksDB will try to keep at least this much history in memory - if dropping a flushed memtable would result in history falling below this threshold, it would not be dropped. (Default: 0)
    AdvancedColumnFamilyOptions::max_write_buffer_size_to_maintain:要在内存中维护的写历史数量,以字节为单位。这包括当前的memtable大小,密封但未刷新的memtable,以及保存在周围的刷新的memtable。RocksDB会尝试在内存中保留至少这么多的历史记录——如果删除一个刷新的memtable会导致历史记录低于这个阈值,那么它就不会被删除。(默认值:0)

The default implementation of memtable is based on skiplist. Other than the default memtable implementation, users can use other types of memtable implementation, for example HashLinkList, HashSkipList or Vector, to speed-up some queries.
memtable的默认实现是基于skiplist的。除了默认的记忆表实现,用户可以使用其他类型的记忆表实现,例如HashLinkList, HashSkipList或Vector,以加快一些查询。

Skiplist MemTable

Skiplist-based memtable provides general good performance to both read and write, random access and sequential scan. Besides, it provides some other useful features that other memtable implementations don't currently support, like Concurrent Insert and Insert with Hint.
基于skiplist的memtable在读写、随机访问和顺序扫描方面都提供了良好的性能。此外,它还提供了一些其他memtable实现目前不支持的有用特性,比如并发插入和带提示的插入。

HashSkiplist MemTable

As their names imply, HashSkipList organizes data in a hash table with each hash bucket to be a skip list, while HashLinkList organizes data in a hash table with each hash bucket as a sorted single linked list. Both types are built to reduce number of comparisons when doing queries. One good use case is to combine them with PlainTable SST format and store data in RAMFS.
顾名思义,HashSkipList将数据组织在哈希表中,每个哈希桶都是一个跳跃列表,而HashLinkList将数据组织在哈希表中,每个哈希桶都是一个排序的单链列表。构建这两种类型都是为了减少执行查询时的比较次数。一个很好的用例是将它们与PlainTable SST格式结合起来,并在RAMFS中存储数据。

When doing a look-up or inserting a key, target key's prefix is retrieved using Options.prefix_extractor, which is used to find the hash bucket. Inside a hash bucket, all the comparisons are done using whole (internal) keys, just as SkipList based memtable.
在进行查找或插入键时,使用Options检索目标键的前缀。Prefix_extractor,用于查找散列桶。在一个哈希bucket中,所有的比较都是使用整个(内部)键来完成的,就像基于SkipList的memtable一样。

The biggest limitation of the hash based memtables is that doing scan across multiple prefixes requires copy and sort, which is very slow and memory costly.
基于散列的memtable的最大限制是跨多个前缀进行扫描需要复制和排序,这非常慢,而且占用内存。

Flush

There are three scenarios where memtable flush can be triggered:
有三种场景可以触发memtable刷新:

  • Memtable size exceeds ColumnFamilyOptions::write_buffer_size after a write.
    Memtable大小超过ColumnFamilyOptions::write_buffer_size。
  • Total memtable size across all column families exceeds DBOptions::db_write_buffer_size, or DBOptions::write_buffer_manager signals a flush. In this scenario the largest memtable will be flushed.
    所有列族的memtable总大小超过DBOptions::db_write_buffer_size,或者DBOptions::write_buffer_manager表示刷新。在这种情况下,最大的memtable将被刷新。
  • Total WAL file size exceeds DBOptions::max_total_wal_size. In this scenario the memtable with the oldest data will be flushed, in order to allow the WAL file with data from this memtable to be purged.
    总WAL文件大小超过DBOptions::max_total_wal_size。在这个场景中,包含最旧数据的memtable将被刷新,以便清除包含该memtable数据的WAL文件。

As a result, a memtable can be flushed before it is full. This is one reason the generated SST file can be smaller than the corresponding memtable. Compression is another factor to make SST file smaller than corresponding memtable, since data in memtable is uncompressed.
因此,可以在记忆表满之前刷新它。这就是生成的SST文件比相应的memtable要小的原因之一。压缩是使SST文件小于memtable的另一个因素,因为memtable中的数据是未压缩的。

Concurrent Insert

Without support of concurrent insert to memtables, concurrent writes to RocksDB from multiple threads will apply to memtable sequentially. Concurrent memtable insert is enabled by default and can be turn off via DBOptions::allow_concurrent_memtable_write option, although only skiplist-based memtable supports the feature.
如果不支持对memtable的并发插入,多个线程对RocksDB的并发写操作将依次应用于memtable。并发memtable插入在默认情况下是启用的,可以通过DBOptions::allow_concurrent_memtable_write选项关闭,尽管只有基于skiplist的memtable支持该特性。

Insert with Hint

In-place Update

In place updates can be enabled by toggling on the bool inplace_update_support flag. However, this flag is by default set to false because this thread-safe in-place update support is not compatible with concurrent memtable writes. Note that the bool allow_concurrent_memtable_write is set to true by default.
可以通过切换bool inplace_update_support标志来启用本地更新。然而,该标志默认设置为false,因为这种线程安全的就地更新支持与并发的memtable写入不兼容。注意,bool allow_concurrent_memtable_write默认设置为true。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容