Leveldb: Compaction

July 14, 2019
Database Leveldb

[TOC]

LevelDb通过周期性地利用归并排序对磁盘文件执行合并操作 (compaction):移除已删除和冗余数据,减少文件个数,保证读操作性能。

LevelDB中,Compaction分成两种,就如同Linux操作系统中的page fault分成minor和major一样:


compaction 整体流程

Compaction操作入口实际由DBImpl::MaybeScheduleCompaction控制。追踪该函数的调用场景,就可以知道触发Compaction的时机。 整体流程如下所示:

compaction_workflow

void DBImpl::MaybeScheduleCompaction() {
  mutex_.AssertHeld();
  if (bg_compaction_scheduled_) {
    // Already scheduled
  } else if (shutting_down_.Acquire_Load()) {
    // DB is being deleted; no more background compactions
  } else if (!bg_error_.ok()) {
    // Already got an error; no more changes
  } else if (imm_ == NULL &&
             manual_compaction_ == NULL &&
             !versions_->NeedsCompaction()) {
     /*防止无限递归,会判断需不需要再次Compaction,如果不需要就返回了*/
    // No work to be done
  } else {
    bg_compaction_scheduled_ = true;
    env_->Schedule(&DBImpl::BGWork, this);
  }
}

从这段代码可以看出发起Compction需要的条件,三个条件满足一个即可发起Compaction:

Minor Compaction

leveldb之Compaction (1) –从MemTable到SSTable文件

compaction_workflow

compaction_workflow

Major Compaction

leveldb之Compaction (2)–何时需要Compaction

Compaction文件选择

leveldb之Compaction(3)--选择参战文件

Leveldb: Block Cache

February 25, 2017
Database Leveldb

Leveldb: Storage SSTable

February 15, 2017
Database Leveldb

Leveldb: Storage MemTable

January 31, 2017
Database Leveldb
comments powered by Disqus