2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-10-31 19:54:05 +01:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2015-08-11 21:19:56 +02:00
|
|
|
#include <atomic>
|
2019-09-20 21:00:55 +02:00
|
|
|
#include <cinttypes>
|
2016-12-16 20:17:26 +01:00
|
|
|
#include <functional>
|
2017-08-25 01:05:16 +02:00
|
|
|
#include <map>
|
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"
|
2017-02-06 23:43:55 +01:00
|
|
|
#include "port/port.h"
|
2014-10-31 19:54:05 +01:00
|
|
|
#include "table/table_reader.h"
|
2019-08-29 23:06:07 +02:00
|
|
|
#include "util/string_util.h"
|
2014-10-31 19:54:05 +01:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
bool NewestFirstBySeqNo(FileMetaData* a, FileMetaData* b) {
|
2018-07-28 01:00:26 +02:00
|
|
|
if (a->fd.largest_seqno != b->fd.largest_seqno) {
|
|
|
|
return a->fd.largest_seqno > b->fd.largest_seqno;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2018-07-28 01:00:26 +02:00
|
|
|
if (a->fd.smallest_seqno != b->fd.smallest_seqno) {
|
|
|
|
return a->fd.smallest_seqno > b->fd.smallest_seqno;
|
2016-10-21 02:05:32 +02:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
// 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;
|
|
|
|
|
2017-12-07 20:52:12 +01:00
|
|
|
FileComparator() : internal_comparator(nullptr) {}
|
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
const FileOptions& file_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_;
|
2017-08-25 01:05:16 +02:00
|
|
|
int num_levels_;
|
2014-10-31 19:54:05 +01:00
|
|
|
LevelState* levels_;
|
2017-08-25 01:05:16 +02:00
|
|
|
// Store states of levels larger than num_levels_. We do this instead of
|
|
|
|
// storing them in levels_ to avoid regression in case there are no files
|
|
|
|
// on invalid levels. The version is not consistent if in the end the files
|
|
|
|
// on invalid levels don't cancel out.
|
|
|
|
std::map<int, std::unordered_set<uint64_t>> invalid_levels_;
|
|
|
|
// Whether there are invalid new files or invalid deletion on levels larger
|
|
|
|
// than num_levels_.
|
|
|
|
bool has_invalid_levels_;
|
2014-10-31 19:54:05 +01:00
|
|
|
FileComparator level_zero_cmp_;
|
|
|
|
FileComparator level_nonzero_cmp_;
|
|
|
|
|
|
|
|
public:
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
Rep(const FileOptions& file_options, Logger* info_log,
|
|
|
|
TableCache* table_cache,
|
2014-10-31 19:54:05 +01:00
|
|
|
VersionStorageInfo* base_vstorage)
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
: file_options_(file_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),
|
2017-08-25 01:05:16 +02:00
|
|
|
base_vstorage_(base_vstorage),
|
|
|
|
num_levels_(base_vstorage->num_levels()),
|
|
|
|
has_invalid_levels_(false) {
|
|
|
|
levels_ = new LevelState[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() {
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int level = 0; level < 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status CheckConsistency(VersionStorageInfo* vstorage) {
|
2016-10-08 02:21:45 +02:00
|
|
|
#ifdef NDEBUG
|
|
|
|
if (!vstorage->force_consistency_checks()) {
|
|
|
|
// Dont run consistency checks in release mode except if
|
|
|
|
// explicitly asked to
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::OK();
|
2016-10-08 02:21:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
2014-10-31 19:54:05 +01:00
|
|
|
// make sure the files are sorted correctly
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int level = 0; level < 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];
|
2019-08-29 23:06:07 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
auto pair = std::make_pair(&f1, &f2);
|
|
|
|
TEST_SYNC_POINT_CALLBACK("VersionBuilder::CheckConsistency", &pair);
|
|
|
|
#endif
|
2014-10-31 19:54:05 +01:00
|
|
|
if (level == 0) {
|
2016-10-08 02:21:45 +02:00
|
|
|
if (!level_zero_cmp_(f1, f2)) {
|
|
|
|
fprintf(stderr, "L0 files are not sorted properly");
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::Corruption("L0 files are not sorted properly");
|
2016-10-08 02:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 01:00:26 +02:00
|
|
|
if (f2->fd.smallest_seqno == f2->fd.largest_seqno) {
|
2016-10-21 02:05:32 +02:00
|
|
|
// This is an external file that we ingested
|
2018-07-28 01:00:26 +02:00
|
|
|
SequenceNumber external_file_seqno = f2->fd.smallest_seqno;
|
|
|
|
if (!(external_file_seqno < f1->fd.largest_seqno ||
|
2016-10-21 02:05:32 +02:00
|
|
|
external_file_seqno == 0)) {
|
2018-07-28 01:00:26 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"L0 file with seqno %" PRIu64 " %" PRIu64
|
|
|
|
" vs. file with global_seqno %" PRIu64 "\n",
|
|
|
|
f1->fd.smallest_seqno, f1->fd.largest_seqno,
|
2016-10-21 02:05:32 +02:00
|
|
|
external_file_seqno);
|
2019-09-20 21:00:55 +02:00
|
|
|
return Status::Corruption(
|
|
|
|
"L0 file with seqno " +
|
|
|
|
NumberToString(f1->fd.smallest_seqno) + " " +
|
|
|
|
NumberToString(f1->fd.largest_seqno) +
|
|
|
|
" vs. file with global_seqno" +
|
|
|
|
NumberToString(external_file_seqno) + " with fileNumber " +
|
|
|
|
NumberToString(f1->fd.GetNumber()));
|
2016-10-21 02:05:32 +02:00
|
|
|
}
|
2018-07-28 01:00:26 +02:00
|
|
|
} else if (f1->fd.smallest_seqno <= f2->fd.smallest_seqno) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"L0 files seqno %" PRIu64 " %" PRIu64 " vs. %" PRIu64
|
|
|
|
" %" PRIu64 "\n",
|
|
|
|
f1->fd.smallest_seqno, f1->fd.largest_seqno,
|
|
|
|
f2->fd.smallest_seqno, f2->fd.largest_seqno);
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::Corruption(
|
|
|
|
"L0 files seqno " + NumberToString(f1->fd.smallest_seqno) +
|
|
|
|
" " + NumberToString(f1->fd.largest_seqno) + " " +
|
|
|
|
NumberToString(f1->fd.GetNumber()) + " vs. " +
|
|
|
|
NumberToString(f2->fd.smallest_seqno) + " " +
|
|
|
|
NumberToString(f2->fd.largest_seqno) + " " +
|
|
|
|
NumberToString(f2->fd.GetNumber()));
|
2016-10-08 02:21:45 +02:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
} else {
|
2016-10-08 02:21:45 +02:00
|
|
|
if (!level_nonzero_cmp_(f1, f2)) {
|
|
|
|
fprintf(stderr, "L%d files are not sorted properly", level);
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::Corruption("L" + NumberToString(level) +
|
|
|
|
" files are not sorted properly");
|
2016-10-08 02:21:45 +02:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
|
|
|
|
// Make sure there is no overlap in levels > 0
|
|
|
|
if (vstorage->InternalComparator()->Compare(f1->largest,
|
|
|
|
f2->smallest) >= 0) {
|
2016-10-08 02:21:45 +02:00
|
|
|
fprintf(stderr, "L%d have overlapping ranges %s vs. %s\n", level,
|
|
|
|
(f1->largest).DebugString(true).c_str(),
|
|
|
|
(f2->smallest).DebugString(true).c_str());
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::Corruption(
|
|
|
|
"L" + NumberToString(level) + " have overlapping ranges " +
|
|
|
|
(f1->largest).DebugString(true) + " vs. " +
|
|
|
|
(f2->smallest).DebugString(true));
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::OK();
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status CheckConsistencyForDeletes(VersionEdit* /*edit*/, uint64_t number,
|
|
|
|
int level) {
|
2016-10-08 02:21:45 +02:00
|
|
|
#ifdef NDEBUG
|
|
|
|
if (!base_vstorage_->force_consistency_checks()) {
|
|
|
|
// Dont run consistency checks in release mode except if
|
|
|
|
// explicitly asked to
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::OK();
|
2016-10-08 02:21:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
2014-11-04 02:45:55 +01:00
|
|
|
// a file to be deleted better exist in the previous version
|
|
|
|
bool found = false;
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int l = 0; !found && l < num_levels_; l++) {
|
2014-11-04 02:45:55 +01:00
|
|
|
const std::vector<FileMetaData*>& base_files =
|
|
|
|
base_vstorage_->LevelFiles(l);
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < base_files.size(); i++) {
|
2014-11-04 02:45:55 +01:00
|
|
|
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
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int l = level + 1; !found && l < 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);
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::Corruption("not found " + NumberToString(number));
|
2014-11-04 02:45:55 +01:00
|
|
|
}
|
2019-08-29 23:06:07 +02:00
|
|
|
return Status::OK();
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
2017-08-25 01:05:16 +02:00
|
|
|
bool CheckConsistencyForNumLevels() {
|
|
|
|
// Make sure there are no files on or beyond num_levels().
|
|
|
|
if (has_invalid_levels_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto& level : invalid_levels_) {
|
|
|
|
if (level.second.size() > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
// Apply all of the edits in *edit to the current state.
|
2019-08-29 23:06:07 +02:00
|
|
|
Status Apply(VersionEdit* edit) {
|
|
|
|
Status s = CheckConsistency(base_vstorage_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
|
|
|
|
// 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;
|
2017-08-25 01:05:16 +02:00
|
|
|
if (level < num_levels_) {
|
|
|
|
levels_[level].deleted_files.insert(number);
|
|
|
|
CheckConsistencyForDeletes(edit, number, level);
|
|
|
|
|
|
|
|
auto exising = levels_[level].added_files.find(number);
|
|
|
|
if (exising != levels_[level].added_files.end()) {
|
|
|
|
UnrefFile(exising->second);
|
2017-10-17 18:58:24 +02:00
|
|
|
levels_[level].added_files.erase(exising);
|
2017-08-25 01:05:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-07 21:21:39 +02:00
|
|
|
if (invalid_levels_[level].erase(number) == 0) {
|
2017-08-25 01:05:16 +02:00
|
|
|
// Deleting an non-existing file on invalid level.
|
|
|
|
has_invalid_levels_ = true;
|
|
|
|
}
|
2015-01-07 19:29:21 +01:00
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add new files
|
|
|
|
for (const auto& new_file : edit->GetNewFiles()) {
|
|
|
|
const int level = new_file.first;
|
2017-08-25 01:05:16 +02:00
|
|
|
if (level < num_levels_) {
|
|
|
|
FileMetaData* f = new FileMetaData(new_file.second);
|
|
|
|
f->refs = 1;
|
|
|
|
|
|
|
|
assert(levels_[level].added_files.find(f->fd.GetNumber()) ==
|
|
|
|
levels_[level].added_files.end());
|
|
|
|
levels_[level].deleted_files.erase(f->fd.GetNumber());
|
|
|
|
levels_[level].added_files[f->fd.GetNumber()] = f;
|
|
|
|
} else {
|
|
|
|
uint64_t number = new_file.second.fd.GetNumber();
|
2019-10-07 21:21:39 +02:00
|
|
|
auto& lvls = invalid_levels_[level];
|
|
|
|
if (lvls.count(number) == 0) {
|
|
|
|
lvls.insert(number);
|
2017-08-25 01:05:16 +02:00
|
|
|
} else {
|
|
|
|
// Creating an already existing file on invalid level.
|
|
|
|
has_invalid_levels_ = true;
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2019-08-29 23:06:07 +02:00
|
|
|
return s;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the current state in *v.
|
2019-08-29 23:06:07 +02:00
|
|
|
Status SaveTo(VersionStorageInfo* vstorage) {
|
|
|
|
Status s = CheckConsistency(base_vstorage_);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = CheckConsistency(vstorage);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2014-10-31 19:54:05 +01:00
|
|
|
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int level = 0; level < 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);
|
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
|
2018-09-15 04:40:37 +02:00
|
|
|
FileMetaData* prev_added_file = nullptr;
|
2014-10-31 19:54:05 +01:00
|
|
|
for (const auto& added : added_files) {
|
2018-09-15 04:40:37 +02:00
|
|
|
if (level > 0 && prev_added_file != nullptr) {
|
2014-12-12 00:46:01 +01:00
|
|
|
assert(base_vstorage_->InternalComparator()->Compare(
|
2018-09-15 04:40:37 +02:00
|
|
|
prev_added_file->smallest, added->smallest) <= 0);
|
2014-12-12 00:46:01 +01:00
|
|
|
}
|
2018-09-15 04:40:37 +02:00
|
|
|
prev_added_file = added;
|
|
|
|
}
|
2014-12-12 00:46:01 +01:00
|
|
|
#endif
|
|
|
|
|
2018-09-15 04:40:37 +02:00
|
|
|
auto base_iter = base_files.begin();
|
|
|
|
auto base_end = base_files.end();
|
|
|
|
auto added_iter = added_files.begin();
|
|
|
|
auto added_end = added_files.end();
|
|
|
|
while (added_iter != added_end || base_iter != base_end) {
|
|
|
|
if (base_iter == base_end ||
|
|
|
|
(added_iter != added_end && cmp(*added_iter, *base_iter))) {
|
|
|
|
MaybeAddFile(vstorage, level, *added_iter++);
|
|
|
|
} else {
|
|
|
|
MaybeAddFile(vstorage, level, *base_iter++);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
s = CheckConsistency(vstorage);
|
|
|
|
return s;
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 00:41:31 +01:00
|
|
|
Status LoadTableHandlers(InternalStats* internal_stats, int max_threads,
|
|
|
|
bool prefetch_index_and_filter_in_cache,
|
|
|
|
bool is_initial_load,
|
|
|
|
const SliceTransform* prefix_extractor) {
|
2014-10-31 19:54:05 +01:00
|
|
|
assert(table_cache_ != nullptr);
|
2018-12-29 03:00:00 +01:00
|
|
|
|
|
|
|
size_t table_cache_capacity = table_cache_->get_cache()->GetCapacity();
|
|
|
|
bool always_load = (table_cache_capacity == TableCache::kInfiniteCapacity);
|
|
|
|
size_t max_load = port::kMaxSizet;
|
|
|
|
|
|
|
|
if (!always_load) {
|
|
|
|
// If it is initial loading and not set to always laoding all the
|
|
|
|
// files, we only load up to kInitialLoadLimit files, to limit the
|
|
|
|
// time reopening the DB.
|
|
|
|
const size_t kInitialLoadLimit = 16;
|
|
|
|
size_t load_limit;
|
|
|
|
// If the table cache is not 1/4 full, we pin the table handle to
|
|
|
|
// file metadata to avoid the cache read costs when reading the file.
|
|
|
|
// The downside of pinning those files is that LRU won't be followed
|
|
|
|
// for those files. This doesn't matter much because if number of files
|
|
|
|
// of the DB excceeds table cache capacity, eventually no table reader
|
|
|
|
// will be pinned and LRU will be followed.
|
|
|
|
if (is_initial_load) {
|
|
|
|
load_limit = std::min(kInitialLoadLimit, table_cache_capacity / 4);
|
|
|
|
} else {
|
|
|
|
load_limit = table_cache_capacity / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t table_cache_usage = table_cache_->get_cache()->GetUsage();
|
|
|
|
if (table_cache_usage >= load_limit) {
|
2019-03-27 00:41:31 +01:00
|
|
|
// TODO (yanqin) find a suitable status code.
|
|
|
|
return Status::OK();
|
2018-12-29 03:00:00 +01:00
|
|
|
} else {
|
|
|
|
max_load = load_limit - table_cache_usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:25:20 +02:00
|
|
|
// <file metadata, level>
|
|
|
|
std::vector<std::pair<FileMetaData*, int>> files_meta;
|
2019-03-27 00:41:31 +01:00
|
|
|
std::vector<Status> statuses;
|
2017-08-25 01:05:16 +02:00
|
|
|
for (int level = 0; level < 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;
|
2019-03-27 00:41:31 +01:00
|
|
|
// If the file has been opened before, just skip it.
|
|
|
|
if (!file_meta->table_reader_handle) {
|
|
|
|
files_meta.emplace_back(file_meta, level);
|
|
|
|
statuses.emplace_back(Status::OK());
|
|
|
|
}
|
2018-12-29 03:00:00 +01:00
|
|
|
if (files_meta.size() >= max_load) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (files_meta.size() >= max_load) {
|
|
|
|
break;
|
2015-08-11 21:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atomic<size_t> next_file_meta_idx(0);
|
2018-08-21 02:31:28 +02:00
|
|
|
std::function<void()> load_handlers_func([&]() {
|
2015-08-11 21:19:56 +02:00
|
|
|
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;
|
2019-03-27 00:41:31 +01:00
|
|
|
statuses[file_idx] = table_cache_->FindTable(
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
file_options_, *(base_vstorage_->InternalComparator()),
|
2018-05-21 23:33:55 +02:00
|
|
|
file_meta->fd, &file_meta->table_reader_handle, prefix_extractor,
|
|
|
|
false /*no_io */, true /* record_read_stats */,
|
|
|
|
internal_stats->GetFileReadHist(level), false, level,
|
|
|
|
prefetch_index_and_filter_in_cache);
|
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
|
|
|
}
|
2018-08-21 02:31:28 +02:00
|
|
|
});
|
2015-08-11 21:19:56 +02:00
|
|
|
|
2018-06-15 21:28:06 +02:00
|
|
|
std::vector<port::Thread> threads;
|
|
|
|
for (int i = 1; i < max_threads; i++) {
|
|
|
|
threads.emplace_back(load_handlers_func);
|
|
|
|
}
|
|
|
|
load_handlers_func();
|
|
|
|
for (auto& t : threads) {
|
|
|
|
t.join();
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2019-03-27 00:41:31 +01:00
|
|
|
for (const auto& s : statuses) {
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
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) {
|
2018-03-08 19:18:34 +01:00
|
|
|
// f is to-be-deleted table file
|
2015-12-07 19:51:08 +01:00
|
|
|
vstorage->RemoveCurrentStats(f);
|
2014-10-31 19:54:05 +01:00
|
|
|
} else {
|
2015-10-19 22:07:05 +02:00
|
|
|
vstorage->AddFile(level, f, info_log_);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
VersionBuilder::VersionBuilder(const FileOptions& file_options,
|
2014-10-31 19:54:05 +01:00
|
|
|
TableCache* table_cache,
|
2015-10-19 22:07:05 +02:00
|
|
|
VersionStorageInfo* base_vstorage,
|
|
|
|
Logger* info_log)
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
: rep_(new Rep(file_options, info_log, table_cache, base_vstorage)) {}
|
2017-08-25 01:05:16 +02:00
|
|
|
|
2014-10-31 19:54:05 +01:00
|
|
|
VersionBuilder::~VersionBuilder() { delete rep_; }
|
2017-08-25 01:05:16 +02:00
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status VersionBuilder::CheckConsistency(VersionStorageInfo* vstorage) {
|
|
|
|
return rep_->CheckConsistency(vstorage);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2017-08-25 01:05:16 +02:00
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status VersionBuilder::CheckConsistencyForDeletes(VersionEdit* edit,
|
|
|
|
uint64_t number, int level) {
|
|
|
|
return rep_->CheckConsistencyForDeletes(edit, number, level);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2017-08-25 01:05:16 +02:00
|
|
|
|
|
|
|
bool VersionBuilder::CheckConsistencyForNumLevels() {
|
|
|
|
return rep_->CheckConsistencyForNumLevels();
|
|
|
|
}
|
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status VersionBuilder::Apply(VersionEdit* edit) { return rep_->Apply(edit); }
|
2017-08-25 01:05:16 +02:00
|
|
|
|
2019-08-29 23:06:07 +02:00
|
|
|
Status VersionBuilder::SaveTo(VersionStorageInfo* vstorage) {
|
|
|
|
return rep_->SaveTo(vstorage);
|
2014-10-31 19:54:05 +01:00
|
|
|
}
|
2017-08-25 01:05:16 +02:00
|
|
|
|
2019-03-27 00:41:31 +01:00
|
|
|
Status VersionBuilder::LoadTableHandlers(
|
|
|
|
InternalStats* internal_stats, int max_threads,
|
|
|
|
bool prefetch_index_and_filter_in_cache, bool is_initial_load,
|
|
|
|
const SliceTransform* prefix_extractor) {
|
|
|
|
return rep_->LoadTableHandlers(internal_stats, max_threads,
|
|
|
|
prefetch_index_and_filter_in_cache,
|
|
|
|
is_initial_load, prefix_extractor);
|
2015-08-11 21:19:56 +02:00
|
|
|
}
|
2017-08-25 01:05:16 +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
|