fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
11 KiB
304 lines
11 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
10 years ago
|
// 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) 2012 Facebook.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file.
|
||
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
8 years ago
|
#include "utilities/checkpoint/checkpoint_impl.h"
|
||
10 years ago
|
|
||
|
#ifndef __STDC_FORMAT_MACROS
|
||
|
#define __STDC_FORMAT_MACROS
|
||
|
#endif
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <algorithm>
|
||
|
#include <string>
|
||
8 years ago
|
#include <vector>
|
||
|
|
||
10 years ago
|
#include "db/wal_manager.h"
|
||
8 years ago
|
#include "port/port.h"
|
||
10 years ago
|
#include "rocksdb/db.h"
|
||
|
#include "rocksdb/env.h"
|
||
10 years ago
|
#include "rocksdb/transaction_log.h"
|
||
8 years ago
|
#include "rocksdb/utilities/checkpoint.h"
|
||
10 years ago
|
#include "util/file_util.h"
|
||
8 years ago
|
#include "util/filename.h"
|
||
9 years ago
|
#include "util/sync_point.h"
|
||
10 years ago
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
Status Checkpoint::Create(DB* db, Checkpoint** checkpoint_ptr) {
|
||
|
*checkpoint_ptr = new CheckpointImpl(db);
|
||
|
return Status::OK();
|
||
|
}
|
||
|
|
||
8 years ago
|
Status Checkpoint::CreateCheckpoint(const std::string& checkpoint_dir,
|
||
|
uint64_t log_size_for_flush) {
|
||
10 years ago
|
return Status::NotSupported("");
|
||
|
}
|
||
|
|
||
|
// Builds an openable snapshot of RocksDB
|
||
8 years ago
|
Status CheckpointImpl::CreateCheckpoint(const std::string& checkpoint_dir,
|
||
|
uint64_t log_size_for_flush) {
|
||
8 years ago
|
DBOptions db_options = db_->GetDBOptions();
|
||
10 years ago
|
|
||
8 years ago
|
Status s = db_->GetEnv()->FileExists(checkpoint_dir);
|
||
9 years ago
|
if (s.ok()) {
|
||
10 years ago
|
return Status::InvalidArgument("Directory exists");
|
||
9 years ago
|
} else if (!s.IsNotFound()) {
|
||
|
assert(s.IsIOError());
|
||
|
return s;
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
ROCKS_LOG_INFO(
|
||
|
db_options.info_log,
|
||
|
"Started the snapshot process -- creating snapshot in directory %s",
|
||
|
checkpoint_dir.c_str());
|
||
|
std::string full_private_path = checkpoint_dir + ".tmp";
|
||
|
// create snapshot directory
|
||
|
s = db_->GetEnv()->CreateDir(full_private_path);
|
||
|
uint64_t sequence_number = 0;
|
||
|
if (s.ok()) {
|
||
|
db_->DisableFileDeletions();
|
||
|
s = CreateCustomCheckpoint(
|
||
|
db_options,
|
||
|
[&](const std::string& src_dirname, const std::string& fname,
|
||
|
FileType) {
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Hard Linking %s", fname.c_str());
|
||
|
return db_->GetEnv()->LinkFile(src_dirname + fname,
|
||
|
full_private_path + fname);
|
||
|
} /* link_file_cb */,
|
||
|
[&](const std::string& src_dirname, const std::string& fname,
|
||
|
uint64_t size_limit_bytes, FileType) {
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Copying %s", fname.c_str());
|
||
|
return CopyFile(db_->GetEnv(), src_dirname + fname,
|
||
|
full_private_path + fname, size_limit_bytes,
|
||
|
db_options.use_fsync);
|
||
|
} /* copy_file_cb */,
|
||
|
[&](const std::string& fname, const std::string& contents, FileType) {
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Creating %s", fname.c_str());
|
||
|
return CreateFile(db_->GetEnv(), full_private_path + fname, contents);
|
||
|
} /* create_file_cb */,
|
||
|
&sequence_number, log_size_for_flush);
|
||
|
// we copied all the files, enable file deletions
|
||
|
db_->EnableFileDeletions(false);
|
||
|
}
|
||
|
|
||
|
if (s.ok()) {
|
||
|
// move tmp private backup to real snapshot directory
|
||
|
s = db_->GetEnv()->RenameFile(full_private_path, checkpoint_dir);
|
||
|
}
|
||
|
if (s.ok()) {
|
||
|
unique_ptr<Directory> checkpoint_directory;
|
||
|
db_->GetEnv()->NewDirectory(checkpoint_dir, &checkpoint_directory);
|
||
|
if (checkpoint_directory != nullptr) {
|
||
|
s = checkpoint_directory->Fsync();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (s.ok()) {
|
||
|
// here we know that we succeeded and installed the new snapshot
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Snapshot DONE. All is good");
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Snapshot sequence number: %" PRIu64,
|
||
|
sequence_number);
|
||
|
} else {
|
||
|
// clean all the files we might have created
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Snapshot failed -- %s",
|
||
|
s.ToString().c_str());
|
||
|
// we have to delete the dir and all its children
|
||
|
std::vector<std::string> subchildren;
|
||
|
db_->GetEnv()->GetChildren(full_private_path, &subchildren);
|
||
|
for (auto& subchild : subchildren) {
|
||
|
std::string subchild_path = full_private_path + "/" + subchild;
|
||
|
Status s1 = db_->GetEnv()->DeleteFile(subchild_path);
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Delete file %s -- %s",
|
||
|
subchild_path.c_str(), s1.ToString().c_str());
|
||
|
}
|
||
|
// finally delete the private dir
|
||
|
Status s1 = db_->GetEnv()->DeleteDir(full_private_path);
|
||
|
ROCKS_LOG_INFO(db_options.info_log, "Delete dir %s -- %s",
|
||
|
full_private_path.c_str(), s1.ToString().c_str());
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status CheckpointImpl::CreateCustomCheckpoint(
|
||
|
const DBOptions& db_options,
|
||
|
std::function<Status(const std::string& src_dirname,
|
||
|
const std::string& src_fname, FileType type)>
|
||
|
link_file_cb,
|
||
|
std::function<Status(const std::string& src_dirname,
|
||
|
const std::string& src_fname,
|
||
|
uint64_t size_limit_bytes, FileType type)>
|
||
|
copy_file_cb,
|
||
|
std::function<Status(const std::string& fname, const std::string& contents,
|
||
|
FileType type)>
|
||
|
create_file_cb,
|
||
|
uint64_t* sequence_number, uint64_t log_size_for_flush) {
|
||
|
Status s;
|
||
|
std::vector<std::string> live_files;
|
||
|
uint64_t manifest_file_size = 0;
|
||
|
uint64_t min_log_num = port::kMaxUint64;
|
||
|
*sequence_number = db_->GetLatestSequenceNumber();
|
||
|
bool same_fs = true;
|
||
|
VectorLogPtr live_wal_files;
|
||
|
|
||
8 years ago
|
bool flush_memtable = true;
|
||
10 years ago
|
if (s.ok()) {
|
||
8 years ago
|
if (!db_options.allow_2pc) {
|
||
8 years ago
|
if (log_size_for_flush == port::kMaxUint64) {
|
||
|
flush_memtable = false;
|
||
|
} else if (log_size_for_flush > 0) {
|
||
|
// If out standing log files are small, we skip the flush.
|
||
|
s = db_->GetSortedWalFiles(live_wal_files);
|
||
8 years ago
|
|
||
8 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Don't flush column families if total log size is smaller than
|
||
|
// log_size_for_flush. We copy the log files instead.
|
||
|
// We may be able to cover 2PC case too.
|
||
|
uint64_t total_wal_size = 0;
|
||
|
for (auto& wal : live_wal_files) {
|
||
|
total_wal_size += wal->SizeFileBytes();
|
||
|
}
|
||
|
if (total_wal_size < log_size_for_flush) {
|
||
|
flush_memtable = false;
|
||
|
}
|
||
|
live_wal_files.clear();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// this will return live_files prefixed with "/"
|
||
8 years ago
|
s = db_->GetLiveFiles(live_files, &manifest_file_size, flush_memtable);
|
||
8 years ago
|
|
||
8 years ago
|
if (s.ok() && db_options.allow_2pc) {
|
||
8 years ago
|
// If 2PC is enabled, we need to get minimum log number after the flush.
|
||
|
// Need to refetch the live files to recapture the snapshot.
|
||
|
if (!db_->GetIntProperty(DB::Properties::kMinLogNumberToKeep,
|
||
|
&min_log_num)) {
|
||
|
return Status::InvalidArgument(
|
||
|
"2PC enabled but cannot fine the min log number to keep.");
|
||
|
}
|
||
|
// We need to refetch live files with flush to handle this case:
|
||
|
// A previous 000001.log contains the prepare record of transaction tnx1.
|
||
|
// The current log file is 000002.log, and sequence_number points to this
|
||
|
// file.
|
||
|
// After calling GetLiveFiles(), 000003.log is created.
|
||
|
// Then tnx1 is committed. The commit record is written to 000003.log.
|
||
|
// Now we fetch min_log_num, which will be 3.
|
||
|
// Then only 000002.log and 000003.log will be copied, and 000001.log will
|
||
|
// be skipped. 000003.log contains commit message of tnx1, but we don't
|
||
|
// have respective prepare record for it.
|
||
|
// In order to avoid this situation, we need to force flush to make sure
|
||
8 years ago
|
// all transactions committed before getting min_log_num will be flushed
|
||
8 years ago
|
// to SST files.
|
||
|
// We cannot get min_log_num before calling the GetLiveFiles() for the
|
||
|
// first time, because if we do that, all the logs files will be included,
|
||
|
// far more than needed.
|
||
8 years ago
|
s = db_->GetLiveFiles(live_files, &manifest_file_size, flush_memtable);
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
TEST_SYNC_POINT("CheckpointImpl::CreateCheckpoint:SavedLiveFiles1");
|
||
|
TEST_SYNC_POINT("CheckpointImpl::CreateCheckpoint:SavedLiveFiles2");
|
||
10 years ago
|
}
|
||
10 years ago
|
// if we have more than one column family, we need to also get WAL files
|
||
|
if (s.ok()) {
|
||
|
s = db_->GetSortedWalFiles(live_wal_files);
|
||
|
}
|
||
10 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t wal_size = live_wal_files.size();
|
||
10 years ago
|
|
||
|
// copy/hard link live_files
|
||
9 years ago
|
std::string manifest_fname, current_fname;
|
||
10 years ago
|
for (size_t i = 0; s.ok() && i < live_files.size(); ++i) {
|
||
|
uint64_t number;
|
||
|
FileType type;
|
||
|
bool ok = ParseFileName(live_files[i], &number, &type);
|
||
|
if (!ok) {
|
||
|
s = Status::Corruption("Can't parse file name. This is very bad");
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
// we should only get sst, options, manifest and current files here
|
||
10 years ago
|
assert(type == kTableFile || type == kDescriptorFile ||
|
||
9 years ago
|
type == kCurrentFile || type == kOptionsFile);
|
||
10 years ago
|
assert(live_files[i].size() > 0 && live_files[i][0] == '/');
|
||
9 years ago
|
if (type == kCurrentFile) {
|
||
|
// We will craft the current file manually to ensure it's consistent with
|
||
|
// the manifest number. This is necessary because current's file contents
|
||
|
// can change during checkpoint creation.
|
||
|
current_fname = live_files[i];
|
||
|
continue;
|
||
|
} else if (type == kDescriptorFile) {
|
||
|
manifest_fname = live_files[i];
|
||
|
}
|
||
10 years ago
|
std::string src_fname = live_files[i];
|
||
|
|
||
|
// rules:
|
||
|
// * if it's kTableFile, then it's shared
|
||
|
// * if it's kDescriptorFile, limit the size to manifest_file_size
|
||
|
// * always copy if cross-device link
|
||
|
if ((type == kTableFile) && same_fs) {
|
||
8 years ago
|
s = link_file_cb(db_->GetName(), src_fname, type);
|
||
10 years ago
|
if (s.IsNotSupported()) {
|
||
|
same_fs = false;
|
||
|
s = Status::OK();
|
||
|
}
|
||
|
}
|
||
|
if ((type != kTableFile) || (!same_fs)) {
|
||
8 years ago
|
s = copy_file_cb(db_->GetName(), src_fname,
|
||
|
(type == kDescriptorFile) ? manifest_file_size : 0,
|
||
|
type);
|
||
10 years ago
|
}
|
||
|
}
|
||
9 years ago
|
if (s.ok() && !current_fname.empty() && !manifest_fname.empty()) {
|
||
8 years ago
|
create_file_cb(current_fname, manifest_fname.substr(1) + "\n",
|
||
|
kCurrentFile);
|
||
9 years ago
|
}
|
||
8 years ago
|
ROCKS_LOG_INFO(db_options.info_log, "Number of log files %" ROCKSDB_PRIszt,
|
||
|
live_wal_files.size());
|
||
10 years ago
|
|
||
|
// Link WAL files. Copy exact size of last one because it is the only one
|
||
|
// that has changes after the last flush.
|
||
|
for (size_t i = 0; s.ok() && i < wal_size; ++i) {
|
||
|
if ((live_wal_files[i]->Type() == kAliveLogFile) &&
|
||
8 years ago
|
(!flush_memtable ||
|
||
8 years ago
|
live_wal_files[i]->StartSequence() >= *sequence_number ||
|
||
8 years ago
|
live_wal_files[i]->LogNumber() >= min_log_num)) {
|
||
10 years ago
|
if (i + 1 == wal_size) {
|
||
8 years ago
|
s = copy_file_cb(db_options.wal_dir, live_wal_files[i]->PathName(),
|
||
|
live_wal_files[i]->SizeFileBytes(), kLogFile);
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
if (same_fs) {
|
||
|
// we only care about live log files
|
||
8 years ago
|
s = link_file_cb(db_options.wal_dir, live_wal_files[i]->PathName(),
|
||
|
kLogFile);
|
||
10 years ago
|
if (s.IsNotSupported()) {
|
||
|
same_fs = false;
|
||
|
s = Status::OK();
|
||
|
}
|
||
|
}
|
||
|
if (!same_fs) {
|
||
8 years ago
|
s = copy_file_cb(db_options.wal_dir, live_wal_files[i]->PathName(), 0,
|
||
|
kLogFile);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
return s;
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
} // namespace rocksdb
|
||
|
|
||
|
#endif // ROCKSDB_LITE
|