2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00: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.
|
|
|
|
//
|
2015-04-09 23:11:15 +00:00
|
|
|
// Repairer does best effort recovery to recover as much data as possible after
|
|
|
|
// a disaster without compromising consistency. It does not guarantee bringing
|
|
|
|
// the database to a time consistent state.
|
|
|
|
//
|
|
|
|
// Repair process is broken into 4 phases:
|
|
|
|
// (a) Find files
|
|
|
|
// (b) Convert logs to tables
|
|
|
|
// (c) Extract metadata
|
|
|
|
// (d) Write Descriptor
|
|
|
|
//
|
|
|
|
// (a) Find files
|
|
|
|
//
|
|
|
|
// The repairer goes through all the files in the directory, and classifies them
|
|
|
|
// based on their file name. Any file that cannot be identified by name will be
|
|
|
|
// ignored.
|
|
|
|
//
|
|
|
|
// (b) Convert logs to table
|
|
|
|
//
|
|
|
|
// Every log file that is active is replayed. All sections of the file where the
|
|
|
|
// checksum does not match is skipped over. We intentionally give preference to
|
|
|
|
// data consistency.
|
|
|
|
//
|
|
|
|
// (c) Extract metadata
|
|
|
|
//
|
|
|
|
// We scan every table to compute
|
|
|
|
// (1) smallest/largest for the table
|
|
|
|
// (2) largest sequence number in the table
|
|
|
|
//
|
|
|
|
// If we are unable to scan the file, then we ignore the table.
|
|
|
|
//
|
|
|
|
// (d) Write Descriptor
|
|
|
|
//
|
|
|
|
// We generate descriptor contents:
|
|
|
|
// - log number is set to zero
|
|
|
|
// - next-file-number is set to 1 + largest file number we found
|
|
|
|
// - last-sequence-number is set to largest sequence# found across
|
|
|
|
// all tables (see 2c)
|
|
|
|
// - compaction pointers are cleared
|
|
|
|
// - every table file is added at level 0
|
2011-03-18 22:37:00 +00:00
|
|
|
//
|
|
|
|
// Possible optimization 1:
|
|
|
|
// (a) Compute total size and use to pick appropriate max-level M
|
|
|
|
// (b) Sort tables by largest sequence# in the table
|
|
|
|
// (c) For each table: if it overlaps earlier table, place in level-0,
|
|
|
|
// else place in level-M.
|
2015-04-09 23:11:15 +00:00
|
|
|
// (d) We can provide options for time consistent recovery and unsafe recovery
|
|
|
|
// (ignore checksum failure when applicable)
|
2011-03-18 22:37:00 +00:00
|
|
|
// Possible optimization 2:
|
2011-04-20 22:48:11 +00:00
|
|
|
// Store per-table metadata (smallest, largest, largest-seq#, ...)
|
|
|
|
// in the table's meta section to speed up ScanTable.
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-04-15 20:39:26 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-09-05 06:14:37 +00:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
2014-06-27 23:01:59 +00:00
|
|
|
#define __STDC_FORMAT_MACROS
|
2014-09-05 06:14:37 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-27 23:01:59 +00:00
|
|
|
#include <inttypes.h>
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/builder.h"
|
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/log_reader.h"
|
|
|
|
#include "db/log_writer.h"
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/table_cache.h"
|
|
|
|
#include "db/version_edit.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "options/cf_options.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
2016-06-24 23:29:43 +00:00
|
|
|
#include "rocksdb/options.h"
|
2016-06-21 01:01:03 +00:00
|
|
|
#include "rocksdb/write_buffer_manager.h"
|
2015-10-12 22:06:38 +00:00
|
|
|
#include "table/scoped_arena_iterator.h"
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
#include "util/file_reader_writer.h"
|
2017-04-04 01:27:24 +00:00
|
|
|
#include "util/filename.h"
|
2016-06-24 23:29:43 +00:00
|
|
|
#include "util/string_util.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Repairer {
|
|
|
|
public:
|
2016-06-24 23:29:43 +00:00
|
|
|
Repairer(const std::string& dbname, const DBOptions& db_options,
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
|
|
|
const ColumnFamilyOptions& default_cf_opts,
|
|
|
|
const ColumnFamilyOptions& unknown_cf_opts, bool create_unknown_cfs)
|
2011-03-18 22:37:00 +00:00
|
|
|
: dbname_(dbname),
|
2016-06-24 23:29:43 +00:00
|
|
|
env_(db_options.env),
|
2016-06-24 18:19:40 +00:00
|
|
|
env_options_(),
|
2016-06-24 23:29:43 +00:00
|
|
|
db_options_(SanitizeOptions(dbname_, db_options)),
|
2017-09-28 00:37:08 +00:00
|
|
|
immutable_db_options_(ImmutableDBOptions(db_options_)),
|
2016-06-24 23:29:43 +00:00
|
|
|
icmp_(default_cf_opts.comparator),
|
2018-04-06 02:49:06 +00:00
|
|
|
default_cf_opts_(
|
|
|
|
SanitizeOptions(immutable_db_options_, default_cf_opts)),
|
2016-06-24 23:29:43 +00:00
|
|
|
default_cf_iopts_(
|
2018-04-06 02:49:06 +00:00
|
|
|
ImmutableCFOptions(immutable_db_options_, default_cf_opts_)),
|
|
|
|
unknown_cf_opts_(
|
|
|
|
SanitizeOptions(immutable_db_options_, unknown_cf_opts)),
|
2016-06-24 23:29:43 +00:00
|
|
|
create_unknown_cfs_(create_unknown_cfs),
|
[CF] Rethink table cache
Summary:
Adapting table cache to column families is interesting. We want table cache to be global LRU, so if some column families are use not as often as others, we want them to be evicted from cache. However, current TableCache object also constructs tables on its own. If table is not found in the cache, TableCache automatically creates new table. We want each column family to be able to specify different table factory.
To solve the problem, we still have a single LRU, but we provide the LRUCache object to TableCache on construction. We have one TableCache per column family, but the underyling cache is shared by all TableCache objects.
This allows us to have a global LRU, but still be able to support different table factories for different column families. Also, in the future it will also be able to support different directories for different column families.
Test Plan: make check
Reviewers: dhruba, haobo, kailiu, sdong
CC: leveldb
Differential Revision: https://reviews.facebook.net/D15915
2014-02-05 17:07:55 +00:00
|
|
|
raw_table_cache_(
|
|
|
|
// TableCache can be small since we expect each table to be opened
|
|
|
|
// once.
|
2016-06-24 23:29:43 +00:00
|
|
|
NewLRUCache(10, db_options_.table_cache_numshardbits)),
|
|
|
|
table_cache_(new TableCache(default_cf_iopts_, env_options_,
|
|
|
|
raw_table_cache_.get())),
|
|
|
|
wb_(db_options_.db_write_buffer_size),
|
|
|
|
wc_(db_options_.delayed_write_rate),
|
2016-09-23 23:34:04 +00:00
|
|
|
vset_(dbname_, &immutable_db_options_, env_options_,
|
|
|
|
raw_table_cache_.get(), &wb_, &wc_),
|
2011-03-18 22:37:00 +00:00
|
|
|
next_file_number_(1) {
|
2016-06-24 23:29:43 +00:00
|
|
|
for (const auto& cfd : column_families) {
|
|
|
|
cf_name_to_opts_[cfd.name] = cfd.options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ColumnFamilyOptions* GetColumnFamilyOptions(
|
|
|
|
const std::string& cf_name) {
|
|
|
|
if (cf_name_to_opts_.find(cf_name) == cf_name_to_opts_.end()) {
|
|
|
|
if (create_unknown_cfs_) {
|
|
|
|
return &unknown_cf_opts_;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &cf_name_to_opts_[cf_name];
|
2016-06-24 18:19:40 +00:00
|
|
|
}
|
A new call back to TablePropertiesCollector to allow users know the entry is add, delete or merge
Summary:
Currently users have no idea a key is add, delete or merge from TablePropertiesCollector call back. Add a new function to add it.
Also refactor the codes so that
(1) make table property collector and internal table property collector two separate data structures with the later one now exposed
(2) table builders only receive internal table properties
Test Plan: Add cases in table_properties_collector_test to cover both of old and new ways of using TablePropertiesCollector.
Reviewers: yhchiang, igor.sugak, rven, igor
Reviewed By: rven, igor
Subscribers: meyering, yoshinorim, maykov, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D35373
2015-04-06 17:04:30 +00:00
|
|
|
|
2016-06-24 18:19:40 +00:00
|
|
|
// Adds a column family to the VersionSet with cf_options_ and updates
|
|
|
|
// manifest.
|
|
|
|
Status AddColumnFamily(const std::string& cf_name, uint32_t cf_id) {
|
2016-06-24 23:29:43 +00:00
|
|
|
const auto* cf_opts = GetColumnFamilyOptions(cf_name);
|
|
|
|
if (cf_opts == nullptr) {
|
|
|
|
return Status::Corruption("Encountered unknown column family with name=" +
|
|
|
|
cf_name + ", id=" + ToString(cf_id));
|
|
|
|
}
|
|
|
|
Options opts(db_options_, *cf_opts);
|
2016-09-14 04:11:59 +00:00
|
|
|
MutableCFOptions mut_cf_opts(opts);
|
2016-06-24 18:19:40 +00:00
|
|
|
|
|
|
|
VersionEdit edit;
|
2016-06-24 23:29:43 +00:00
|
|
|
edit.SetComparatorName(opts.comparator->Name());
|
2016-06-24 18:19:40 +00:00
|
|
|
edit.SetLogNumber(0);
|
|
|
|
edit.SetColumnFamily(cf_id);
|
|
|
|
ColumnFamilyData* cfd;
|
|
|
|
cfd = nullptr;
|
|
|
|
edit.AddColumnFamily(cf_name);
|
|
|
|
|
|
|
|
mutex_.Lock();
|
2016-06-24 23:29:43 +00:00
|
|
|
Status status = vset_.LogAndApply(cfd, mut_cf_opts, &edit, &mutex_,
|
|
|
|
nullptr /* db_directory */,
|
|
|
|
false /* new_descriptor_log */, cf_opts);
|
2016-06-24 18:19:40 +00:00
|
|
|
mutex_.Unlock();
|
|
|
|
return status;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Repairer() {
|
|
|
|
delete table_cache_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Run() {
|
|
|
|
Status status = FindFiles();
|
2016-06-24 18:19:40 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
// Discard older manifests and start a fresh one
|
|
|
|
for (size_t i = 0; i < manifests_.size(); i++) {
|
|
|
|
ArchiveFile(dbname_ + "/" + manifests_[i]);
|
|
|
|
}
|
|
|
|
// Just create a DBImpl temporarily so we can reuse NewDB()
|
2016-06-24 23:29:43 +00:00
|
|
|
DBImpl* db_impl = new DBImpl(db_options_, dbname_);
|
2016-06-24 18:19:40 +00:00
|
|
|
status = db_impl->NewDB();
|
|
|
|
delete db_impl;
|
|
|
|
}
|
2017-08-08 17:42:38 +00:00
|
|
|
|
2016-06-24 18:19:40 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
// Recover using the fresh manifest created by NewDB()
|
2016-06-24 23:29:43 +00:00
|
|
|
status =
|
|
|
|
vset_.Recover({{kDefaultColumnFamilyName, default_cf_opts_}}, false);
|
2016-06-24 18:19:40 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
if (status.ok()) {
|
2016-06-24 20:12:13 +00:00
|
|
|
// Need to scan existing SST files first so the column families are
|
|
|
|
// created before we process WAL files
|
|
|
|
ExtractMetaData();
|
|
|
|
|
|
|
|
// ExtractMetaData() uses table_fds_ to know which SST files' metadata to
|
|
|
|
// extract -- we need to clear it here since metadata for existing SST
|
|
|
|
// files has been extracted already
|
|
|
|
table_fds_.clear();
|
2011-03-18 22:37:00 +00:00
|
|
|
ConvertLogFilesToTables();
|
|
|
|
ExtractMetaData();
|
2016-06-24 18:19:40 +00:00
|
|
|
status = AddTables();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
if (status.ok()) {
|
2014-06-27 23:01:59 +00:00
|
|
|
uint64_t bytes = 0;
|
2011-04-20 22:48:11 +00:00
|
|
|
for (size_t i = 0; i < tables_.size(); i++) {
|
2014-06-13 22:54:19 +00:00
|
|
|
bytes += tables_[i].meta.fd.GetFileSize();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"**** Repaired rocksdb %s; "
|
|
|
|
"recovered %" ROCKSDB_PRIszt " files; %" PRIu64
|
2018-05-11 18:14:20 +00:00
|
|
|
" bytes. "
|
2017-03-16 02:22:52 +00:00
|
|
|
"Some data may have been lost. "
|
|
|
|
"****",
|
|
|
|
dbname_.c_str(), tables_.size(), bytes);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct TableInfo {
|
|
|
|
FileMetaData meta;
|
2016-06-24 20:12:13 +00:00
|
|
|
uint32_t column_family_id;
|
|
|
|
std::string column_family_name;
|
2013-06-14 05:09:08 +00:00
|
|
|
SequenceNumber min_sequence;
|
2011-03-18 22:37:00 +00:00
|
|
|
SequenceNumber max_sequence;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string const dbname_;
|
|
|
|
Env* const env_;
|
2016-06-24 18:19:40 +00:00
|
|
|
const EnvOptions env_options_;
|
2016-06-24 23:29:43 +00:00
|
|
|
const DBOptions db_options_;
|
2016-09-23 23:34:04 +00:00
|
|
|
const ImmutableDBOptions immutable_db_options_;
|
2016-06-24 23:29:43 +00:00
|
|
|
const InternalKeyComparator icmp_;
|
|
|
|
const ColumnFamilyOptions default_cf_opts_;
|
|
|
|
const ImmutableCFOptions default_cf_iopts_; // table_cache_ holds reference
|
|
|
|
const ColumnFamilyOptions unknown_cf_opts_;
|
|
|
|
const bool create_unknown_cfs_;
|
[CF] Rethink table cache
Summary:
Adapting table cache to column families is interesting. We want table cache to be global LRU, so if some column families are use not as often as others, we want them to be evicted from cache. However, current TableCache object also constructs tables on its own. If table is not found in the cache, TableCache automatically creates new table. We want each column family to be able to specify different table factory.
To solve the problem, we still have a single LRU, but we provide the LRUCache object to TableCache on construction. We have one TableCache per column family, but the underyling cache is shared by all TableCache objects.
This allows us to have a global LRU, but still be able to support different table factories for different column families. Also, in the future it will also be able to support different directories for different column families.
Test Plan: make check
Reviewers: dhruba, haobo, kailiu, sdong
CC: leveldb
Differential Revision: https://reviews.facebook.net/D15915
2014-02-05 17:07:55 +00:00
|
|
|
std::shared_ptr<Cache> raw_table_cache_;
|
2011-03-18 22:37:00 +00:00
|
|
|
TableCache* table_cache_;
|
2016-06-21 01:01:03 +00:00
|
|
|
WriteBufferManager wb_;
|
2016-06-24 18:19:40 +00:00
|
|
|
WriteController wc_;
|
|
|
|
VersionSet vset_;
|
2016-06-24 23:29:43 +00:00
|
|
|
std::unordered_map<std::string, ColumnFamilyOptions> cf_name_to_opts_;
|
2016-06-24 18:19:40 +00:00
|
|
|
InstrumentedMutex mutex_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
std::vector<std::string> manifests_;
|
2014-07-02 16:54:20 +00:00
|
|
|
std::vector<FileDescriptor> table_fds_;
|
2011-03-18 22:37:00 +00:00
|
|
|
std::vector<uint64_t> logs_;
|
|
|
|
std::vector<TableInfo> tables_;
|
|
|
|
uint64_t next_file_number_;
|
|
|
|
|
|
|
|
Status FindFiles() {
|
|
|
|
std::vector<std::string> filenames;
|
2014-07-02 16:54:20 +00:00
|
|
|
bool found_file = false;
|
2017-08-08 17:42:38 +00:00
|
|
|
std::vector<std::string> to_search_paths;
|
|
|
|
|
2016-06-24 23:29:43 +00:00
|
|
|
for (size_t path_id = 0; path_id < db_options_.db_paths.size(); path_id++) {
|
2017-08-08 17:42:38 +00:00
|
|
|
to_search_paths.push_back(db_options_.db_paths[path_id].path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// search wal_dir if user uses a customize wal_dir
|
2017-09-22 19:37:59 +00:00
|
|
|
bool same = false;
|
|
|
|
Status status = env_->AreFilesSame(db_options_.wal_dir, dbname_, &same);
|
|
|
|
if (status.IsNotSupported()) {
|
|
|
|
same = db_options_.wal_dir == dbname_;
|
|
|
|
status = Status::OK();
|
|
|
|
} else if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!same) {
|
|
|
|
to_search_paths.push_back(db_options_.wal_dir);
|
2017-08-08 17:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t path_id = 0; path_id < to_search_paths.size(); path_id++) {
|
2017-09-22 19:37:59 +00:00
|
|
|
status = env_->GetChildren(to_search_paths[path_id], &filenames);
|
2014-07-02 16:54:20 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (!filenames.empty()) {
|
|
|
|
found_file = true;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-07-02 16:54:20 +00:00
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
|
|
|
if (ParseFileName(filenames[i], &number, &type)) {
|
|
|
|
if (type == kDescriptorFile) {
|
|
|
|
manifests_.push_back(filenames[i]);
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2014-07-02 16:54:20 +00:00
|
|
|
if (number + 1 > next_file_number_) {
|
|
|
|
next_file_number_ = number + 1;
|
|
|
|
}
|
|
|
|
if (type == kLogFile) {
|
|
|
|
logs_.push_back(number);
|
|
|
|
} else if (type == kTableFile) {
|
2015-12-28 22:28:19 +00:00
|
|
|
table_fds_.emplace_back(number, static_cast<uint32_t>(path_id),
|
|
|
|
0);
|
2014-07-02 16:54:20 +00:00
|
|
|
} else {
|
|
|
|
// Ignore other files
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-02 16:54:20 +00:00
|
|
|
if (!found_file) {
|
|
|
|
return Status::Corruption(dbname_, "repair found no files");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertLogFilesToTables() {
|
2011-04-20 22:48:11 +00:00
|
|
|
for (size_t i = 0; i < logs_.size(); i++) {
|
2017-08-08 17:42:38 +00:00
|
|
|
// we should use LogFileName(wal_dir, logs_[i]) here. user might uses wal_dir option.
|
|
|
|
std::string logname = LogFileName(db_options_.wal_dir, logs_[i]);
|
2011-03-18 22:37:00 +00:00
|
|
|
Status status = ConvertLogToTable(logs_[i]);
|
|
|
|
if (!status.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"Log #%" PRIu64 ": ignoring conversion error: %s",
|
|
|
|
logs_[i], status.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
ArchiveFile(logname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ConvertLogToTable(uint64_t log) {
|
|
|
|
struct LogReporter : public log::Reader::Reporter {
|
|
|
|
Env* env;
|
2013-01-20 10:07:13 +00:00
|
|
|
std::shared_ptr<Logger> info_log;
|
2011-03-18 22:37:00 +00:00
|
|
|
uint64_t lognum;
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual void Corruption(size_t bytes, const Status& s) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
// We print error messages for corruption, but continue repairing.
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(info_log, "Log #%" PRIu64 ": dropping %d bytes; %s",
|
|
|
|
lognum, static_cast<int>(bytes), s.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Open the log file
|
2017-08-08 17:42:38 +00:00
|
|
|
std::string logname = LogFileName(db_options_.wal_dir, log);
|
2013-01-20 10:07:13 +00:00
|
|
|
unique_ptr<SequentialFile> lfile;
|
2017-05-23 01:40:41 +00:00
|
|
|
Status status = env_->NewSequentialFile(
|
|
|
|
logname, &lfile, env_->OptimizeForLogRead(env_options_));
|
2011-03-18 22:37:00 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
unique_ptr<SequentialFileReader> lfile_reader(
|
|
|
|
new SequentialFileReader(std::move(lfile)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Create the log reader.
|
|
|
|
LogReporter reporter;
|
|
|
|
reporter.env = env_;
|
2016-06-24 23:29:43 +00:00
|
|
|
reporter.info_log = db_options_.info_log;
|
2011-03-18 22:37:00 +00:00
|
|
|
reporter.lognum = log;
|
2016-06-24 18:19:40 +00:00
|
|
|
// We intentionally make log::Reader do checksumming so that
|
2011-03-18 22:37:00 +00:00
|
|
|
// corruptions cause entire commits to be skipped instead of
|
|
|
|
// propagating bad information (like overly large sequence
|
|
|
|
// numbers).
|
2016-06-24 23:29:43 +00:00
|
|
|
log::Reader reader(db_options_.info_log, std::move(lfile_reader), &reporter,
|
2015-10-08 17:06:16 +00:00
|
|
|
true /*enable checksum*/, 0 /*initial_offset*/, log);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2016-06-24 20:12:13 +00:00
|
|
|
// Initialize per-column family memtables
|
|
|
|
for (auto* cfd : *vset_.GetColumnFamilySet()) {
|
|
|
|
cfd->CreateNewMemtable(*cfd->GetLatestMutableCFOptions(),
|
|
|
|
kMaxSequenceNumber);
|
|
|
|
}
|
|
|
|
auto cf_mems = new ColumnFamilyMemTablesImpl(vset_.GetColumnFamilySet());
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Read all the records and add to a memtable
|
|
|
|
std::string scratch;
|
|
|
|
Slice record;
|
|
|
|
WriteBatch batch;
|
|
|
|
int counter = 0;
|
|
|
|
while (reader.ReadRecord(&record, &scratch)) {
|
2016-03-30 17:35:22 +00:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2011-03-18 22:37:00 +00:00
|
|
|
reporter.Corruption(
|
|
|
|
record.size(), Status::Corruption("log record too small"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WriteBatchInternal::SetContents(&batch, record);
|
2016-06-24 20:12:13 +00:00
|
|
|
status = WriteBatchInternal::InsertInto(&batch, cf_mems, nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
counter += WriteBatchInternal::Count(&batch);
|
|
|
|
} else {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log, "Log #%" PRIu64 ": ignoring %s",
|
|
|
|
log, status.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
status = Status::OK(); // Keep going with rest of file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 20:12:13 +00:00
|
|
|
// Dump a table for each column family with entries in this log file.
|
|
|
|
for (auto* cfd : *vset_.GetColumnFamilySet()) {
|
|
|
|
// Do not record a version edit for this conversion to a Table
|
|
|
|
// since ExtractMetaData() will also generate edits.
|
|
|
|
MemTable* mem = cfd->mem();
|
|
|
|
if (mem->IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileMetaData meta;
|
|
|
|
meta.fd = FileDescriptor(next_file_number_++, 0, 0);
|
2014-09-05 00:40:41 +00:00
|
|
|
ReadOptions ro;
|
|
|
|
ro.total_order_seek = true;
|
|
|
|
Arena arena;
|
|
|
|
ScopedArenaIterator iter(mem->NewIterator(ro, &arena));
|
2017-07-05 19:02:00 +00:00
|
|
|
int64_t _current_time = 0;
|
|
|
|
status = env_->GetCurrentTime(&_current_time); // ignore error
|
2017-06-28 00:02:20 +00:00
|
|
|
const uint64_t current_time = static_cast<uint64_t>(_current_time);
|
2017-10-18 16:09:31 +00:00
|
|
|
SnapshotChecker* snapshot_checker = DisableGCSnapshotChecker::Instance();
|
2017-11-10 17:25:26 +00:00
|
|
|
|
|
|
|
auto write_hint = cfd->CalculateSSTWriteHint(0);
|
2015-10-08 23:57:35 +00:00
|
|
|
status = BuildTable(
|
2016-06-24 20:12:13 +00:00
|
|
|
dbname_, env_, *cfd->ioptions(), *cfd->GetLatestMutableCFOptions(),
|
2017-09-28 00:37:08 +00:00
|
|
|
env_options_, table_cache_, iter.get(),
|
2016-11-19 22:14:35 +00:00
|
|
|
std::unique_ptr<InternalIterator>(mem->NewRangeTombstoneIterator(ro)),
|
2016-11-01 03:35:54 +00:00
|
|
|
&meta, cfd->internal_comparator(),
|
|
|
|
cfd->int_tbl_prop_collector_factories(), cfd->GetID(), cfd->GetName(),
|
2017-10-06 17:26:38 +00:00
|
|
|
{}, kMaxSequenceNumber, snapshot_checker, kNoCompression,
|
|
|
|
CompressionOptions(), false, nullptr /* internal_stats */,
|
|
|
|
TableFileCreationReason::kRecovery, nullptr /* event_logger */,
|
|
|
|
0 /* job_id */, Env::IO_HIGH, nullptr /* table_properties */,
|
2017-11-10 17:25:26 +00:00
|
|
|
-1 /* level */, current_time, write_hint);
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_INFO(db_options_.info_log,
|
|
|
|
"Log #%" PRIu64 ": %d ops saved to Table #%" PRIu64 " %s",
|
|
|
|
log, counter, meta.fd.GetNumber(),
|
|
|
|
status.ToString().c_str());
|
2016-06-24 20:12:13 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
if (meta.fd.GetFileSize() > 0) {
|
|
|
|
table_fds_.push_back(meta.fd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 20:12:13 +00:00
|
|
|
delete cf_mems;
|
2011-03-18 22:37:00 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtractMetaData() {
|
2014-07-02 16:54:20 +00:00
|
|
|
for (size_t i = 0; i < table_fds_.size(); i++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
TableInfo t;
|
2014-07-02 16:54:20 +00:00
|
|
|
t.meta.fd = table_fds_[i];
|
2011-03-18 22:37:00 +00:00
|
|
|
Status status = ScanTable(&t);
|
|
|
|
if (!status.ok()) {
|
2014-07-02 16:54:20 +00:00
|
|
|
std::string fname = TableFileName(
|
2016-06-24 23:29:43 +00:00
|
|
|
db_options_.db_paths, t.meta.fd.GetNumber(), t.meta.fd.GetPathId());
|
2014-08-13 18:57:40 +00:00
|
|
|
char file_num_buf[kFormatFileNumberBufSize];
|
|
|
|
FormatFileNumber(t.meta.fd.GetNumber(), t.meta.fd.GetPathId(),
|
|
|
|
file_num_buf, sizeof(file_num_buf));
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log, "Table #%s: ignoring %s",
|
|
|
|
file_num_buf, status.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
ArchiveFile(fname);
|
|
|
|
} else {
|
|
|
|
tables_.push_back(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ScanTable(TableInfo* t) {
|
2016-06-24 23:29:43 +00:00
|
|
|
std::string fname = TableFileName(
|
|
|
|
db_options_.db_paths, t->meta.fd.GetNumber(), t->meta.fd.GetPathId());
|
2011-03-18 22:37:00 +00:00
|
|
|
int counter = 0;
|
2014-07-02 16:54:20 +00:00
|
|
|
uint64_t file_size;
|
|
|
|
Status status = env_->GetFileSize(fname, &file_size);
|
|
|
|
t->meta.fd = FileDescriptor(t->meta.fd.GetNumber(), t->meta.fd.GetPathId(),
|
|
|
|
file_size);
|
2016-06-24 20:12:13 +00:00
|
|
|
std::shared_ptr<const TableProperties> props;
|
|
|
|
if (status.ok()) {
|
|
|
|
status = table_cache_->GetTableProperties(env_options_, icmp_, t->meta.fd,
|
|
|
|
&props);
|
|
|
|
}
|
|
|
|
if (status.ok()) {
|
|
|
|
t->column_family_id = static_cast<uint32_t>(props->column_family_id);
|
|
|
|
if (t->column_family_id ==
|
|
|
|
TablePropertiesCollectorFactory::Context::kUnknownColumnFamily) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(
|
|
|
|
db_options_.info_log,
|
2016-06-24 20:12:13 +00:00
|
|
|
"Table #%" PRIu64
|
|
|
|
": column family unknown (probably due to legacy format); "
|
|
|
|
"adding to default column family id 0.",
|
|
|
|
t->meta.fd.GetNumber());
|
|
|
|
t->column_family_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vset_.GetColumnFamilySet()->GetColumnFamily(t->column_family_id) ==
|
|
|
|
nullptr) {
|
|
|
|
status =
|
|
|
|
AddColumnFamily(props->column_family_name, t->column_family_id);
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 17:49:25 +00:00
|
|
|
ColumnFamilyData* cfd = nullptr;
|
2016-06-24 20:12:13 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
cfd = vset_.GetColumnFamilySet()->GetColumnFamily(t->column_family_id);
|
|
|
|
if (cfd->GetName() != props->column_family_name) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(
|
|
|
|
db_options_.info_log,
|
2016-06-24 20:12:13 +00:00
|
|
|
"Table #%" PRIu64
|
|
|
|
": inconsistent column family name '%s'; expected '%s' for column "
|
|
|
|
"family id %" PRIu32 ".",
|
|
|
|
t->meta.fd.GetNumber(), props->column_family_name.c_str(),
|
|
|
|
cfd->GetName().c_str(), t->column_family_id);
|
|
|
|
status = Status::Corruption(dbname_, "inconsistent column family name");
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
if (status.ok()) {
|
2015-10-12 22:06:38 +00:00
|
|
|
InternalIterator* iter = table_cache_->NewIterator(
|
2016-11-16 01:18:32 +00:00
|
|
|
ReadOptions(), env_options_, cfd->internal_comparator(), t->meta.fd,
|
2018-05-21 21:33:55 +00:00
|
|
|
nullptr /* range_del_agg */,
|
|
|
|
cfd->GetLatestMutableCFOptions()->prefix_extractor.get());
|
2011-03-18 22:37:00 +00:00
|
|
|
bool empty = true;
|
|
|
|
ParsedInternalKey parsed;
|
2013-06-14 05:09:08 +00:00
|
|
|
t->min_sequence = 0;
|
2011-03-18 22:37:00 +00:00
|
|
|
t->max_sequence = 0;
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
Slice key = iter->key();
|
|
|
|
if (!ParseInternalKey(key, &parsed)) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(db_options_.info_log,
|
|
|
|
"Table #%" PRIu64 ": unparsable key %s",
|
|
|
|
t->meta.fd.GetNumber(), EscapeString(key).c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
if (empty) {
|
|
|
|
empty = false;
|
|
|
|
t->meta.smallest.DecodeFrom(key);
|
2017-02-10 18:51:08 +00:00
|
|
|
t->min_sequence = parsed.sequence;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
t->meta.largest.DecodeFrom(key);
|
2013-06-14 05:09:08 +00:00
|
|
|
if (parsed.sequence < t->min_sequence) {
|
|
|
|
t->min_sequence = parsed.sequence;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
if (parsed.sequence > t->max_sequence) {
|
|
|
|
t->max_sequence = parsed.sequence;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!iter->status().ok()) {
|
|
|
|
status = iter->status();
|
|
|
|
}
|
|
|
|
delete iter;
|
2016-06-24 20:12:13 +00:00
|
|
|
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_INFO(db_options_.info_log, "Table #%" PRIu64 ": %d entries %s",
|
|
|
|
t->meta.fd.GetNumber(), counter,
|
|
|
|
status.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-06-24 18:19:40 +00:00
|
|
|
Status AddTables() {
|
2016-06-24 20:12:13 +00:00
|
|
|
std::unordered_map<uint32_t, std::vector<const TableInfo*>> cf_id_to_tables;
|
2011-03-18 22:37:00 +00:00
|
|
|
SequenceNumber max_sequence = 0;
|
2011-04-20 22:48:11 +00:00
|
|
|
for (size_t i = 0; i < tables_.size(); i++) {
|
2016-06-24 20:12:13 +00:00
|
|
|
cf_id_to_tables[tables_[i].column_family_id].push_back(&tables_[i]);
|
2011-03-18 22:37:00 +00:00
|
|
|
if (max_sequence < tables_[i].max_sequence) {
|
|
|
|
max_sequence = tables_[i].max_sequence;
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 01:18:01 +00:00
|
|
|
vset_.SetLastAllocatedSequence(max_sequence);
|
2017-12-01 07:39:56 +00:00
|
|
|
vset_.SetLastPublishedSequence(max_sequence);
|
2016-06-24 18:19:40 +00:00
|
|
|
vset_.SetLastSequence(max_sequence);
|
|
|
|
|
2016-06-24 20:12:13 +00:00
|
|
|
for (const auto& cf_id_and_tables : cf_id_to_tables) {
|
|
|
|
auto* cfd =
|
|
|
|
vset_.GetColumnFamilySet()->GetColumnFamily(cf_id_and_tables.first);
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.SetComparatorName(cfd->user_comparator()->Name());
|
|
|
|
edit.SetLogNumber(0);
|
|
|
|
edit.SetNextFile(next_file_number_);
|
|
|
|
edit.SetColumnFamily(cfd->GetID());
|
|
|
|
|
|
|
|
// TODO(opt): separate out into multiple levels
|
|
|
|
for (const auto* table : cf_id_and_tables.second) {
|
|
|
|
edit.AddFile(0, table->meta.fd.GetNumber(), table->meta.fd.GetPathId(),
|
|
|
|
table->meta.fd.GetFileSize(), table->meta.smallest,
|
|
|
|
table->meta.largest, table->min_sequence,
|
|
|
|
table->max_sequence, table->meta.marked_for_compaction);
|
|
|
|
}
|
2017-10-10 20:07:00 +00:00
|
|
|
assert(next_file_number_ > 0);
|
|
|
|
vset_.MarkFileNumberUsed(next_file_number_ - 1);
|
2016-06-24 20:12:13 +00:00
|
|
|
mutex_.Lock();
|
|
|
|
Status status = vset_.LogAndApply(
|
|
|
|
cfd, *cfd->GetLatestMutableCFOptions(), &edit, &mutex_,
|
|
|
|
nullptr /* db_directory */, false /* new_descriptor_log */);
|
|
|
|
mutex_.Unlock();
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2016-06-24 20:12:13 +00:00
|
|
|
return Status::OK();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile(const std::string& fname) {
|
|
|
|
// Move into another directory. E.g., for
|
|
|
|
// dir/foo
|
|
|
|
// rename to
|
|
|
|
// dir/lost/foo
|
|
|
|
const char* slash = strrchr(fname.c_str(), '/');
|
|
|
|
std::string new_dir;
|
2013-03-01 02:04:58 +00:00
|
|
|
if (slash != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
new_dir.assign(fname.data(), slash - fname.data());
|
|
|
|
}
|
|
|
|
new_dir.append("/lost");
|
|
|
|
env_->CreateDir(new_dir); // Ignore error
|
|
|
|
std::string new_file = new_dir;
|
|
|
|
new_file.append("/");
|
2013-03-01 02:04:58 +00:00
|
|
|
new_file.append((slash == nullptr) ? fname.c_str() : slash + 1);
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s = env_->RenameFile(fname, new_file);
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_INFO(db_options_.info_log, "Archiving %s: %s\n", fname.c_str(),
|
|
|
|
s.ToString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
};
|
2016-06-24 23:29:43 +00:00
|
|
|
|
|
|
|
Status GetDefaultCFOptions(
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
|
|
|
ColumnFamilyOptions* res) {
|
|
|
|
assert(res != nullptr);
|
|
|
|
auto iter = std::find_if(column_families.begin(), column_families.end(),
|
|
|
|
[](const ColumnFamilyDescriptor& cfd) {
|
|
|
|
return cfd.name == kDefaultColumnFamilyName;
|
|
|
|
});
|
|
|
|
if (iter == column_families.end()) {
|
|
|
|
return Status::InvalidArgument(
|
|
|
|
"column_families", "Must contain entry for default column family");
|
|
|
|
}
|
|
|
|
*res = iter->options;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2016-06-24 18:19:40 +00:00
|
|
|
} // anonymous namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2016-06-24 23:29:43 +00:00
|
|
|
Status RepairDB(const std::string& dbname, const DBOptions& db_options,
|
2017-09-28 00:37:08 +00:00
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families
|
|
|
|
) {
|
2016-06-24 23:29:43 +00:00
|
|
|
ColumnFamilyOptions default_cf_opts;
|
|
|
|
Status status = GetDefaultCFOptions(column_families, &default_cf_opts);
|
|
|
|
if (status.ok()) {
|
2017-09-28 00:37:08 +00:00
|
|
|
Repairer repairer(dbname, db_options, column_families,
|
|
|
|
default_cf_opts,
|
2016-06-24 23:29:43 +00:00
|
|
|
ColumnFamilyOptions() /* unknown_cf_opts */,
|
|
|
|
false /* create_unknown_cfs */);
|
|
|
|
status = repairer.Run();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status RepairDB(const std::string& dbname, const DBOptions& db_options,
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
|
|
|
const ColumnFamilyOptions& unknown_cf_opts) {
|
|
|
|
ColumnFamilyOptions default_cf_opts;
|
|
|
|
Status status = GetDefaultCFOptions(column_families, &default_cf_opts);
|
|
|
|
if (status.ok()) {
|
2017-09-28 00:37:08 +00:00
|
|
|
Repairer repairer(dbname, db_options,
|
|
|
|
column_families, default_cf_opts,
|
2016-06-24 23:29:43 +00:00
|
|
|
unknown_cf_opts, true /* create_unknown_cfs */);
|
|
|
|
status = repairer.Run();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
Status RepairDB(const std::string& dbname, const Options& options) {
|
2016-06-24 23:29:43 +00:00
|
|
|
DBOptions db_options(options);
|
|
|
|
ColumnFamilyOptions cf_options(options);
|
2017-09-28 00:37:08 +00:00
|
|
|
Repairer repairer(dbname, db_options,
|
|
|
|
{}, cf_options /* default_cf_opts */,
|
2016-06-24 23:29:43 +00:00
|
|
|
cf_options /* unknown_cf_opts */,
|
|
|
|
true /* create_unknown_cfs */);
|
2011-03-18 22:37:00 +00:00
|
|
|
return repairer.Run();
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2014-04-15 20:39:26 +00:00
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|