2014-10-31 19:54:05 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/version_builder.h"
|
|
|
|
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <algorithm>
|
2015-08-11 21:19:56 +02:00
|
|
|
#include <atomic>
|
2014-10-31 19:54:05 +01:00
|
|
|
#include <set>
|
2015-08-11 21:19:56 +02:00
|
|
|
#include <thread>
|
2014-12-10 05:33:43 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2015-09-02 19:25:20 +02:00
|
|
|
#include <utility>
|
2014-10-31 19:54:05 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2015-09-02 19:25:20 +02:00
|
|
|
#include "db/internal_stats.h"
|
2014-10-31 19:54:05 +01:00
|
|
|
#include "db/table_cache.h"
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "table/table_reader.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
bool NewestFirstBySeqNo(FileMetaData* a, FileMetaData* b) {
|
|
|
|
if (a->smallest_seqno != b->smallest_seqno) {
|
|
|
|
return a->smallest_seqno > b->smallest_seqno;
|
|
|
|
}
|
|
|
|
if (a->largest_seqno != b->largest_seqno) {
|
|
|
|
return a->largest_seqno > b->largest_seqno;
|
|
|
|
}
|
|
|
|
// Break ties by file number
|
|
|
|
return a->fd.GetNumber() > b->fd.GetNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool BySmallestKey(FileMetaData* a, FileMetaData* b,
|
|
|
|
const InternalKeyComparator* cmp) {
|
|
|
|
int r = cmp->Compare(a->smallest, b->smallest);
|
|
|
|
if (r != 0) {
|
|
|
|
return (r < 0);
|
|
|
|
}
|
|
|
|
// Break ties by file number
|
|
|
|
return (a->fd.GetNumber() < b->fd.GetNumber());
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class VersionBuilder::Rep {
|
|
|
|
private:
|
|
|
|
// Helper to sort files_ in v
|
|
|
|
// kLevel0 -- NewestFirstBySeqNo
|
|
|
|
// kLevelNon0 -- BySmallestKey
|
|
|
|
struct FileComparator {
|
2014-11-04 02:45:55 +01:00
|
|
|
enum SortMethod { kLevel0 = 0, kLevelNon0 = 1, } sort_method;
|
2014-10-31 19:54:05 +01:00
|
|
|
const InternalKeyComparator* internal_comparator;
|
|
|
|
|
|
|
|
bool operator()(FileMetaData* f1, FileMetaData* f2) const {
|
|
|
|
switch (sort_method) {
|
|
|
|
case kLevel0:
|
|
|
|
return NewestFirstBySeqNo(f1, f2);
|
|
|
|
case kLevelNon0:
|
|
|
|
return BySmallestKey(f1, f2, internal_comparator);
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LevelState {
|
2014-12-10 05:33:43 +01:00
|
|
|
std::unordered_set<uint64_t> deleted_files;
|
|
|
|
// Map from file number to file meta data.
|
|
|
|
std::unordered_map<uint64_t, FileMetaData*> added_files;
|
2014-10-31 19:54:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const EnvOptions& env_options_;
|
2015-10-19 22:07:05 +02:00
|
|
|
Logger* info_log_;
|
2014-10-31 19:54:05 +01:00
|
|
|
TableCache* table_cache_;
|
|
|
|
VersionStorageInfo* base_vstorage_;
|
|
|
|
LevelState* levels_;
|
|
|
|
FileComparator level_zero_cmp_;
|
|
|
|
FileComparator level_nonzero_cmp_;
|
|
|
|
|
|
|
|
public:
|
2015-10-19 22:07:05 +02:00
|
|
|
Rep(const EnvOptions& env_options, Logger* info_log, TableCache* table_cache,
|
2014-10-31 19:54:05 +01:00
|
|
|
VersionStorageInfo* base_vstorage)
|
|
|
|
: env_options_(env_options),
|
2015-10-19 22:07:05 +02:00
|
|
|
info_log_(info_log),
|
2014-10-31 19:54:05 +01:00
|
|
|
table_cache_(table_cache),
|
|
|
|
base_vstorage_(base_vstorage) {
|
2014-11-04 02:45:55 +01:00
|
|
|
levels_ = new LevelState[base_vstorage_->num_levels()];
|
2014-10-31 19:54:05 +01:00
|
|
|
level_zero_cmp_.sort_method = FileComparator::kLevel0;
|
|
|
|
level_nonzero_cmp_.sort_method = FileComparator::kLevelNon0;
|
|
|
|
level_nonzero_cmp_.internal_comparator =
|
|
|
|
base_vstorage_->InternalComparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Rep() {
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int level = 0; level < base_vstorage_->num_levels(); level++) {
|
2014-12-10 05:33:43 +01:00
|
|
|
const auto& added = levels_[level].added_files;
|
|
|
|
for (auto& pair : added) {
|
2015-01-07 19:29:21 +01:00
|
|
|
UnrefFile(pair.second);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] levels_;
|
|
|
|
}
|
|
|
|
|
2015-01-07 19:29:21 +01:00
|
|
|
void UnrefFile(FileMetaData* f) {
|
|
|
|
f->refs--;
|
|
|
|
if (f->refs <= 0) {
|
|
|
|
if (f->table_reader_handle) {
|
|
|
|
assert(table_cache_ != nullptr);
|
|
|
|
table_cache_->ReleaseHandle(f->table_reader_handle);
|
|
|
|
f->table_reader_handle = nullptr;
|
|
|
|
}
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
void CheckConsistency(VersionStorageInfo* vstorage) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// make sure the files are sorted correctly
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int level = 0; level < vstorage->num_levels(); level++) {
|
2014-10-31 19:54:05 +01:00
|
|
|
auto& level_files = vstorage->LevelFiles(level);
|
|
|
|
for (size_t i = 1; i < level_files.size(); i++) {
|
|
|
|
auto f1 = level_files[i - 1];
|
|
|
|
auto f2 = level_files[i];
|
|
|
|
if (level == 0) {
|
|
|
|
assert(level_zero_cmp_(f1, f2));
|
|
|
|
assert(f1->largest_seqno > f2->largest_seqno);
|
|
|
|
} else {
|
|
|
|
assert(level_nonzero_cmp_(f1, f2));
|
|
|
|
|
|
|
|
// Make sure there is no overlap in levels > 0
|
|
|
|
if (vstorage->InternalComparator()->Compare(f1->largest,
|
|
|
|
f2->smallest) >= 0) {
|
|
|
|
fprintf(stderr, "overlapping ranges in same level %s vs. %s\n",
|
|
|
|
(f1->largest).DebugString().c_str(),
|
|
|
|
(f2->smallest).DebugString().c_str());
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckConsistencyForDeletes(VersionEdit* edit, uint64_t number,
|
|
|
|
int level) {
|
|
|
|
#ifndef NDEBUG
|
2014-11-04 02:45:55 +01:00
|
|
|
// a file to be deleted better exist in the previous version
|
|
|
|
bool found = false;
|
|
|
|
for (int l = 0; !found && l < base_vstorage_->num_levels(); l++) {
|
|
|
|
const std::vector<FileMetaData*>& base_files =
|
|
|
|
base_vstorage_->LevelFiles(l);
|
|
|
|
for (unsigned int i = 0; i < base_files.size(); i++) {
|
|
|
|
FileMetaData* f = base_files[i];
|
|
|
|
if (f->fd.GetNumber() == number) {
|
|
|
|
found = true;
|
|
|
|
break;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 02:45:55 +01:00
|
|
|
}
|
|
|
|
// if the file did not exist in the previous version, then it
|
|
|
|
// is possibly moved from lower level to higher level in current
|
|
|
|
// version
|
|
|
|
for (int l = level + 1; !found && l < base_vstorage_->num_levels(); l++) {
|
2014-12-10 05:33:43 +01:00
|
|
|
auto& level_added = levels_[l].added_files;
|
|
|
|
auto got = level_added.find(number);
|
|
|
|
if (got != level_added.end()) {
|
|
|
|
found = true;
|
|
|
|
break;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2014-11-04 02:45:55 +01:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
// maybe this file was added in a previous edit that was Applied
|
|
|
|
if (!found) {
|
2014-12-10 05:33:43 +01:00
|
|
|
auto& level_added = levels_[level].added_files;
|
|
|
|
auto got = level_added.find(number);
|
|
|
|
if (got != level_added.end()) {
|
|
|
|
found = true;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2014-11-04 02:45:55 +01:00
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
fprintf(stderr, "not found %" PRIu64 "\n", number);
|
|
|
|
}
|
|
|
|
assert(found);
|
2014-10-31 19:54:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all of the edits in *edit to the current state.
|
|
|
|
void Apply(VersionEdit* edit) {
|
|
|
|
CheckConsistency(base_vstorage_);
|
|
|
|
|
|
|
|
// Delete files
|
|
|
|
const VersionEdit::DeletedFileSet& del = edit->GetDeletedFiles();
|
|
|
|
for (const auto& del_file : del) {
|
|
|
|
const auto level = del_file.first;
|
|
|
|
const auto number = del_file.second;
|
|
|
|
levels_[level].deleted_files.insert(number);
|
|
|
|
CheckConsistencyForDeletes(edit, number, level);
|
2015-01-07 19:29:21 +01:00
|
|
|
|
|
|
|
auto exising = levels_[level].added_files.find(number);
|
|
|
|
if (exising != levels_[level].added_files.end()) {
|
|
|
|
UnrefFile(exising->second);
|
|
|
|
levels_[level].added_files.erase(number);
|
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add new files
|
|
|
|
for (const auto& new_file : edit->GetNewFiles()) {
|
|
|
|
const int level = new_file.first;
|
|
|
|
FileMetaData* f = new FileMetaData(new_file.second);
|
|
|
|
f->refs = 1;
|
|
|
|
|
2014-12-10 05:33:43 +01:00
|
|
|
assert(levels_[level].added_files.find(f->fd.GetNumber()) ==
|
|
|
|
levels_[level].added_files.end());
|
2014-10-31 19:54:05 +01:00
|
|
|
levels_[level].deleted_files.erase(f->fd.GetNumber());
|
2014-12-10 05:33:43 +01:00
|
|
|
levels_[level].added_files[f->fd.GetNumber()] = f;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the current state in *v.
|
|
|
|
void SaveTo(VersionStorageInfo* vstorage) {
|
|
|
|
CheckConsistency(base_vstorage_);
|
|
|
|
CheckConsistency(vstorage);
|
|
|
|
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int level = 0; level < base_vstorage_->num_levels(); level++) {
|
2014-10-31 19:54:05 +01:00
|
|
|
const auto& cmp = (level == 0) ? level_zero_cmp_ : level_nonzero_cmp_;
|
|
|
|
// Merge the set of added files with the set of pre-existing files.
|
|
|
|
// Drop any deleted files. Store the result in *v.
|
|
|
|
const auto& base_files = base_vstorage_->LevelFiles(level);
|
|
|
|
auto base_iter = base_files.begin();
|
|
|
|
auto base_end = base_files.end();
|
2014-12-10 05:33:43 +01:00
|
|
|
const auto& unordered_added_files = levels_[level].added_files;
|
|
|
|
vstorage->Reserve(level,
|
|
|
|
base_files.size() + unordered_added_files.size());
|
|
|
|
|
|
|
|
// Sort added files for the level.
|
2014-12-12 00:46:01 +01:00
|
|
|
std::vector<FileMetaData*> added_files;
|
|
|
|
added_files.reserve(unordered_added_files.size());
|
2014-12-10 05:33:43 +01:00
|
|
|
for (const auto& pair : unordered_added_files) {
|
|
|
|
added_files.push_back(pair.second);
|
|
|
|
}
|
|
|
|
std::sort(added_files.begin(), added_files.end(), cmp);
|
2014-10-31 19:54:05 +01:00
|
|
|
|
2014-12-12 00:46:01 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
FileMetaData* prev_file = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
for (const auto& added : added_files) {
|
2014-12-12 00:46:01 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (level > 0 && prev_file != nullptr) {
|
|
|
|
assert(base_vstorage_->InternalComparator()->Compare(
|
|
|
|
prev_file->smallest, added->smallest) <= 0);
|
|
|
|
}
|
|
|
|
prev_file = added;
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
// Add all smaller files listed in base_
|
|
|
|
for (auto bpos = std::upper_bound(base_iter, base_end, added, cmp);
|
2014-11-04 02:45:55 +01:00
|
|
|
base_iter != bpos; ++base_iter) {
|
2014-10-31 19:54:05 +01:00
|
|
|
MaybeAddFile(vstorage, level, *base_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeAddFile(vstorage, level, added);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add remaining base files
|
|
|
|
for (; base_iter != base_end; ++base_iter) {
|
|
|
|
MaybeAddFile(vstorage, level, *base_iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckConsistency(vstorage);
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:25:20 +02:00
|
|
|
void LoadTableHandlers(InternalStats* internal_stats, int max_threads) {
|
2014-10-31 19:54:05 +01:00
|
|
|
assert(table_cache_ != nullptr);
|
2015-09-02 19:25:20 +02:00
|
|
|
// <file metadata, level>
|
|
|
|
std::vector<std::pair<FileMetaData*, int>> files_meta;
|
2014-11-04 02:45:55 +01:00
|
|
|
for (int level = 0; level < base_vstorage_->num_levels(); level++) {
|
2014-12-10 05:33:43 +01:00
|
|
|
for (auto& file_meta_pair : levels_[level].added_files) {
|
|
|
|
auto* file_meta = file_meta_pair.second;
|
2014-10-31 19:54:05 +01:00
|
|
|
assert(!file_meta->table_reader_handle);
|
2015-09-02 19:25:20 +02:00
|
|
|
files_meta.emplace_back(file_meta, level);
|
2015-08-11 21:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atomic<size_t> next_file_meta_idx(0);
|
|
|
|
std::function<void()> load_handlers_func = [&]() {
|
|
|
|
while (true) {
|
|
|
|
size_t file_idx = next_file_meta_idx.fetch_add(1);
|
|
|
|
if (file_idx >= files_meta.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:25:20 +02:00
|
|
|
auto* file_meta = files_meta[file_idx].first;
|
|
|
|
int level = files_meta[file_idx].second;
|
|
|
|
table_cache_->FindTable(env_options_,
|
|
|
|
*(base_vstorage_->InternalComparator()),
|
|
|
|
file_meta->fd, &file_meta->table_reader_handle,
|
|
|
|
false /*no_io */, true /* record_read_stats */,
|
|
|
|
internal_stats->GetFileReadHist(level));
|
2014-10-31 19:54:05 +01:00
|
|
|
if (file_meta->table_reader_handle != nullptr) {
|
|
|
|
// Load table_reader
|
|
|
|
file_meta->fd.table_reader = table_cache_->GetTableReaderFromHandle(
|
|
|
|
file_meta->table_reader_handle);
|
2014-11-04 02:45:55 +01:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2015-08-11 21:19:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (max_threads <= 1) {
|
|
|
|
load_handlers_func();
|
|
|
|
} else {
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
for (int i = 0; i < max_threads; i++) {
|
|
|
|
threads.emplace_back(load_handlers_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& t : threads) {
|
|
|
|
t.join();
|
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaybeAddFile(VersionStorageInfo* vstorage, int level, FileMetaData* f) {
|
|
|
|
if (levels_[level].deleted_files.count(f->fd.GetNumber()) > 0) {
|
|
|
|
// File is deleted: do nothing
|
|
|
|
} else {
|
2015-10-19 22:07:05 +02:00
|
|
|
vstorage->AddFile(level, f, info_log_);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
VersionBuilder::VersionBuilder(const EnvOptions& env_options,
|
|
|
|
TableCache* table_cache,
|
2015-10-19 22:07:05 +02:00
|
|
|
VersionStorageInfo* base_vstorage,
|
|
|
|
Logger* info_log)
|
|
|
|
: rep_(new Rep(env_options, info_log, table_cache, base_vstorage)) {}
|
2014-10-31 19:54:05 +01:00
|
|
|
VersionBuilder::~VersionBuilder() { delete rep_; }
|
|
|
|
void VersionBuilder::CheckConsistency(VersionStorageInfo* vstorage) {
|
|
|
|
rep_->CheckConsistency(vstorage);
|
|
|
|
}
|
|
|
|
void VersionBuilder::CheckConsistencyForDeletes(VersionEdit* edit,
|
|
|
|
uint64_t number, int level) {
|
|
|
|
rep_->CheckConsistencyForDeletes(edit, number, level);
|
|
|
|
}
|
|
|
|
void VersionBuilder::Apply(VersionEdit* edit) { rep_->Apply(edit); }
|
|
|
|
void VersionBuilder::SaveTo(VersionStorageInfo* vstorage) {
|
|
|
|
rep_->SaveTo(vstorage);
|
|
|
|
}
|
2015-09-02 19:25:20 +02:00
|
|
|
void VersionBuilder::LoadTableHandlers(InternalStats* internal_stats,
|
|
|
|
int max_threads) {
|
|
|
|
rep_->LoadTableHandlers(internal_stats, max_threads);
|
2015-08-11 21:19:56 +02:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
void VersionBuilder::MaybeAddFile(VersionStorageInfo* vstorage, int level,
|
|
|
|
FileMetaData* f) {
|
|
|
|
rep_->MaybeAddFile(vstorage, level, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|