leveldb学习记录-各组件的默认大小

memtable的大小

1
2
3
4
5
6
7
8
9
// Amount of data to build up in memory (backed by an unsorted log
// on disk) before converting to a sorted on-disk file.
//
// Larger values increase performance, especially during bulk loads.
// Up to two write buffers may be held in memory at the same time,
// so you may wish to adjust this parameter to control memory usage.
// Also, a larger write buffer will result in a longer recovery time
// the next time the database is opened.
size_t write_buffer_size = 4 * 1024 * 1024;//memtable的最大size

表示一个memtable的最大大小,默认为==4MB==

sstable的大小

1
2
3
4
5
6
7
8
9
// Leveldb will write up to this amount of bytes to a file before
// switching to a new one.
// Most clients should leave this parameter alone. However if your
// filesystem is more efficient with larger files, you could
// consider increasing the value. The downside will be longer
// compactions and hence longer latency/performance hiccups.
// Another reason to increase this parameter might be when you are
// initially populating a large database.
size_t max_file_size = 2 * 1024 * 1024;//最大文件尺寸

sstable默认为2MB

level0的文件大小

level 0 的文件是无序的,每次查找 level 0都需要查找所有文件,因此需要严格控制个数。

当文件数目 >= config::kL0_SlowdownWritesTrigger 时,通过 sleep 延缓写入,当文件数目 >= config::kL0_StopWritesTrigger时,则完全停止写入,等待后台 compaction.这两个数字分别是 8 和 12.

由于 memtable 是通过 BuildTable 一次性生成一个文件,因此文件大小 ~ 4M左右.

compact 会有一个 score 的筛选,其中 level 0 的计算公式为:

1
2
score = v->files_[level].size() /
static_cast<double>(config::kL0_CompactionTrigger);

score >= 1.0 时,就会触发往下层的文件归并操作,因此可以认为 level 0 比较理想的个数应当 < config::kL0_CompactionTrigger,该值为 4.

level 1+ 的文件大小及个数

level 最大一共是 7层,范围是[0, kNumLevels = 7).

各 level 的文件都是归并生成的,在DoCompactionWork生成文件,当文件大小超过MaxOutputFileSize时,则重新打开新文件写入。这个大小对所有 level 都是一样的,默认值是options->max_file_size = 2M.

每次的文件总大小是MaxBytesForLevel,随着 level 增大,从10M1T.

level single file size totla file size
0 4M 4M*(4 or 8 or 12)
1 2M 10M
2 2M 100M
3 2M 1G
4 2M 10G
5 2M 100G
6 2M 1T

sstable data block的默认大小为4K

1
2
3
4
5
// Approximate size of user data packed per block.  Note that the
// block size specified here corresponds to uncompressed data. The
// actual size of the unit read from disk may be smaller if
// compression is enabled. This parameter can be changed dynamically.
size_t block_size = 4 * 1024;//sstable中block的size