mirror of
https://github.com/facebook/rocksdb.git
synced 2024-12-03 14:52:53 +00:00
16c21afc06
Summary:
**Context/Summary:**
b26b395e0a
propagates `CleanStagingDirectory()` status to `CreateCheckpoint()`. However, we didn't return early when `Status s = db_->GetEnv()->FileExists(full_private_path);` return non-NotFound non-ok stratus in `CleanStagingDirectory()`. Therefore we can proceed to the next step when `full_private_path` doesn't exist.
```
Verification failed: Checkpoint failed: Operation aborted: Failed to clean the temporary directory /dev/shm/rocksdb.J4Su/rocksdb_crashtest_blackbox/.checkpoint28.tmp needed before checkpoint creation : NotFound:
db_stress: db_stress_tool/db_stress_test_base.cc:549: void rocksdb::StressTest::ProcessStatus(rocksdb::SharedState*, std::string, const rocksdb::Status&, bool) const: Assertion `false' failed.
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12919
Test Plan:
Below failed before the fix and passes after
```
./db_stress --WAL_size_limit_MB=1 --WAL_ttl_seconds=0 --acquire_snapshot_one_in=100 --adaptive_readahead=1 --adm_policy=1 --advise_random_on_open=0 --allow_data_in_errors=True --allow_fallocate=1 --async_io=1 --auto_readahead_size=0 --avoid_flush_during_recovery=1 --avoid_flush_during_shutdown=0 --avoid_unnecessary_blocking_io=1 --backup_max_size=104857600 --backup_one_in=100000 --batch_protection_bytes_per_key=8 --bgerror_resume_retry_interval=1000000 --block_align=0 --block_protection_bytes_per_key=4 --block_size=16384 --bloom_before_level=2 --bloom_bits=4 --bottommost_compression_type=snappy --bottommost_file_compaction_delay=0 --bytes_per_sync=0 --cache_index_and_filter_blocks=1 --cache_index_and_filter_blocks_with_high_priority=0 --cache_size=8388608 --cache_type=auto_hyper_clock_cache --charge_compression_dictionary_building_buffer=0 --charge_file_metadata=1 --charge_filter_construction=1 --charge_table_reader=1 --check_multiget_consistency=0 --check_multiget_entity_consistency=0 --checkpoint_one_in=10000 --checksum_type=kxxHash64 --clear_column_family_one_in=0 --compact_files_one_in=1000 --compact_range_one_in=1000000 --compaction_pri=3 --compaction_readahead_size=1048576 --compaction_ttl=0 --compress_format_version=2 --compressed_secondary_cache_ratio=0.0 --compressed_secondary_cache_size=0 --compression_checksum=0 --compression_max_dict_buffer_bytes=0 --compression_max_dict_bytes=0 --compression_parallel_threads=1 --compression_type=none --compression_use_zstd_dict_trainer=0 --compression_zstd_max_train_bytes=0 --continuous_verification_interval=0 --daily_offpeak_time_utc= --data_block_index_type=0 --db=/dev/shm/rocksdb.J4Su/rocksdb_crashtest_blackbox --db_write_buffer_size=134217728 --default_temperature=kUnknown --default_write_temperature=kHot --delete_obsolete_files_period_micros=21600000000 --delpercent=4 --delrangepercent=1 --destroy_db_initially=0 --detect_filter_construct_corruption=0 --disable_file_deletions_one_in=1000000 --disable_manual_compaction_one_in=10000 --disable_wal=0 --dump_malloc_stats=0 --enable_checksum_handoff=0 --enable_compaction_filter=0 --enable_custom_split_merge=0 --enable_do_not_compress_roles=1 --enable_index_compression=1 --enable_memtable_insert_with_hint_prefix_extractor=0 --enable_pipelined_write=0 --enable_sst_partitioner_factory=0 --enable_thread_tracking=0 --enable_write_thread_adaptive_yield=1 --error_recovery_with_no_fault_injection=1 --exclude_wal_from_write_fault_injection=0 --expected_values_dir=/dev/shm/rocksdb.J4Su/rocksdb_crashtest_expected --fail_if_options_file_error=1 --fifo_allow_compaction=1 --file_checksum_impl=xxh64 --fill_cache=1 --flush_one_in=1000000 --format_version=6 --get_all_column_family_metadata_one_in=1000000 --get_current_wal_file_one_in=0 --get_live_files_apis_one_in=1000000 --get_properties_of_all_tables_one_in=1000000 --get_property_one_in=1000000 --get_sorted_wal_files_one_in=0 --hard_pending_compaction_bytes_limit=274877906944 --high_pri_pool_ratio=0.5 --index_block_restart_interval=13 --index_shortening=0 --index_type=3 --ingest_external_file_one_in=0 --initial_auto_readahead_size=16384 --inplace_update_support=0 --iterpercent=10 --key_len_percent_dist=1,30,69 --key_may_exist_one_in=100000 --last_level_temperature=kWarm --level_compaction_dynamic_level_bytes=1 --lock_wal_one_in=1000000 --log_file_time_to_roll=0 --log_readahead_size=0 --long_running_snapshots=0 --low_pri_pool_ratio=0 --lowest_used_cache_tier=0 --manifest_preallocation_size=5120 --manual_wal_flush_one_in=1000 --mark_for_compaction_one_file_in=10 --max_auto_readahead_size=16384 --max_background_compactions=20 --max_bytes_for_level_base=10485760 --max_key=2500000 --max_key_len=3 --max_log_file_size=0 --max_manifest_file_size=1073741824 --max_sequential_skip_in_iterations=8 --max_total_wal_size=0 --max_write_batch_group_size_bytes=16 --max_write_buffer_number=10 --max_write_buffer_size_to_maintain=0 --memtable_insert_hint_per_batch=0 --memtable_max_range_deletions=100 --memtable_prefix_bloom_size_ratio=0.1 --memtable_protection_bytes_per_key=4 --memtable_whole_key_filtering=0 --memtablerep=skip_list --metadata_charge_policy=1 --metadata_read_fault_one_in=32 --metadata_write_fault_one_in=128 --min_write_buffer_number_to_merge=2 --mmap_read=1 --mock_direct_io=False --nooverwritepercent=1 --num_file_reads_for_auto_readahead=2 --open_files=500000 --open_metadata_read_fault_one_in=8 --open_metadata_write_fault_one_in=0 --open_read_fault_one_in=0 --open_write_fault_one_in=16 --ops_per_thread=100000000 --optimize_filters_for_hits=0 --optimize_filters_for_memory=1 --optimize_multiget_for_io=0 --paranoid_file_checks=1 --partition_filters=0 --partition_pinning=0 --pause_background_one_in=10000 --periodic_compaction_seconds=0 --prefix_size=7 --prefixpercent=5 --prepopulate_block_cache=1 --preserve_internal_time_seconds=36000 --progress_reports=0 --promote_l0_one_in=0 --read_amp_bytes_per_bit=0 --read_fault_one_in=0 --readahead_size=0 --readpercent=45 --recycle_log_file_num=1 --reopen=0 --report_bg_io_stats=0 --reset_stats_one_in=10000 --sample_for_compression=5 --secondary_cache_fault_one_in=32 --set_options_one_in=10000 --skip_stats_update_on_db_open=1 --snapshot_hold_ops=100000 --soft_pending_compaction_bytes_limit=68719476736 --sqfc_name=foo --sqfc_version=0 --sst_file_manager_bytes_per_sec=104857600 --sst_file_manager_bytes_per_truncate=0 --stats_dump_period_sec=600 --stats_history_buffer_size=1048576 --strict_bytes_per_sync=1 --subcompactions=3 --sync=0 --sync_fault_injection=0 --table_cache_numshardbits=6 --target_file_size_base=524288 --target_file_size_multiplier=2 --test_batches_snapshots=0 --test_cf_consistency=1 --top_level_index_pinning=1 --uncache_aggressiveness=0 --universal_max_read_amp=0 --unpartitioned_pinning=0 --use_adaptive_mutex=0 --use_adaptive_mutex_lru=1 --use_attribute_group=0 --use_delta_encoding=1 --use_direct_io_for_flush_and_compaction=0 --use_direct_reads=0 --use_full_merge_v1=0 --use_get_entity=1 --use_merge=0 --use_multi_cf_iterator=0 --use_multi_get_entity=0 --use_multiget=0 --use_put_entity_one_in=0 --use_sqfc_for_range_queries=1 --use_timed_put_one_in=1 --use_write_buffer_manager=1 --user_timestamp_size=0 --value_size_mult=32 --verification_only=0 --verify_checksum=1 --verify_checksum_one_in=1000 --verify_compression=1 --verify_db_one_in=100000 --verify_file_checksums_one_in=1000 --verify_iterator_with_expected_state_one_in=0 --verify_sst_unique_id_in_manifest=1 --wal_bytes_per_sync=0 --wal_compression=none --write_buffer_size=1048576 --write_dbid_to_manifest=0 --write_fault_one_in=128 --writepercent=35
```
Reviewed By: cbi42
Differential Revision: D60938952
Pulled By: hx235
fbshipit-source-id: 5696cd6b00f33c9f9a256944fecb4e2f4d52a2e6
502 lines
18 KiB
C++
502 lines
18 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// 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).
|
|
//
|
|
// Copyright (c) 2012 Facebook.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
|
|
#include "utilities/checkpoint/checkpoint_impl.h"
|
|
|
|
#include <algorithm>
|
|
#include <cinttypes>
|
|
#include <string>
|
|
#include <tuple>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
#include "db/wal_manager.h"
|
|
#include "file/file_util.h"
|
|
#include "file/filename.h"
|
|
#include "logging/logging.h"
|
|
#include "port/port.h"
|
|
#include "rocksdb/db.h"
|
|
#include "rocksdb/env.h"
|
|
#include "rocksdb/metadata.h"
|
|
#include "rocksdb/options.h"
|
|
#include "rocksdb/transaction_log.h"
|
|
#include "rocksdb/types.h"
|
|
#include "rocksdb/utilities/checkpoint.h"
|
|
#include "test_util/sync_point.h"
|
|
#include "util/cast_util.h"
|
|
#include "util/file_checksum_helper.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
Status Checkpoint::Create(DB* db, Checkpoint** checkpoint_ptr) {
|
|
*checkpoint_ptr = new CheckpointImpl(db);
|
|
return Status::OK();
|
|
}
|
|
|
|
Status Checkpoint::CreateCheckpoint(const std::string& /*checkpoint_dir*/,
|
|
uint64_t /*log_size_for_flush*/,
|
|
uint64_t* /*sequence_number_ptr*/) {
|
|
return Status::NotSupported("");
|
|
}
|
|
|
|
Status CheckpointImpl::CleanStagingDirectory(
|
|
const std::string& full_private_path, Logger* info_log) {
|
|
std::vector<std::string> subchildren;
|
|
Status s = db_->GetEnv()->FileExists(full_private_path);
|
|
if (s.IsNotFound()) {
|
|
// Nothing to clean
|
|
return Status::OK();
|
|
} else if (!s.ok()) {
|
|
return s;
|
|
}
|
|
assert(s.ok());
|
|
ROCKS_LOG_INFO(info_log, "File exists %s -- %s", full_private_path.c_str(),
|
|
s.ToString().c_str());
|
|
|
|
s = db_->GetEnv()->GetChildren(full_private_path, &subchildren);
|
|
if (s.ok()) {
|
|
for (auto& subchild : subchildren) {
|
|
Status del_s;
|
|
std::string subchild_path = full_private_path + "/" + subchild;
|
|
del_s = db_->GetEnv()->DeleteFile(subchild_path);
|
|
ROCKS_LOG_INFO(info_log, "Delete file %s -- %s", subchild_path.c_str(),
|
|
del_s.ToString().c_str());
|
|
if (!del_s.ok() && s.ok()) {
|
|
s = del_s;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Then delete the private dir
|
|
if (s.ok()) {
|
|
s = db_->GetEnv()->DeleteDir(full_private_path);
|
|
ROCKS_LOG_INFO(info_log, "Delete dir %s -- %s", full_private_path.c_str(),
|
|
s.ToString().c_str());
|
|
}
|
|
return s;
|
|
}
|
|
|
|
Status Checkpoint::ExportColumnFamily(
|
|
ColumnFamilyHandle* /*handle*/, const std::string& /*export_dir*/,
|
|
ExportImportFilesMetaData** /*metadata*/) {
|
|
return Status::NotSupported("");
|
|
}
|
|
|
|
// Builds an openable snapshot of RocksDB
|
|
Status CheckpointImpl::CreateCheckpoint(const std::string& checkpoint_dir,
|
|
uint64_t log_size_for_flush,
|
|
uint64_t* sequence_number_ptr) {
|
|
DBOptions db_options = db_->GetDBOptions();
|
|
|
|
Status file_exists_s = db_->GetEnv()->FileExists(checkpoint_dir);
|
|
if (file_exists_s.ok()) {
|
|
return Status::InvalidArgument("Directory exists");
|
|
} else if (!file_exists_s.IsNotFound()) {
|
|
assert(file_exists_s.IsIOError());
|
|
return file_exists_s;
|
|
} else {
|
|
assert(file_exists_s.IsNotFound());
|
|
};
|
|
|
|
Status s;
|
|
ROCKS_LOG_INFO(
|
|
db_options.info_log,
|
|
"Started the snapshot process -- creating snapshot in directory %s",
|
|
checkpoint_dir.c_str());
|
|
|
|
size_t final_nonslash_idx = checkpoint_dir.find_last_not_of('/');
|
|
if (final_nonslash_idx == std::string::npos) {
|
|
// npos means it's only slashes or empty. Non-empty means it's the root
|
|
// directory, but it shouldn't be because we verified above the directory
|
|
// doesn't exist.
|
|
assert(checkpoint_dir.empty());
|
|
s.PermitUncheckedError();
|
|
return Status::InvalidArgument("invalid checkpoint directory name");
|
|
}
|
|
|
|
std::string full_private_path =
|
|
checkpoint_dir.substr(0, final_nonslash_idx + 1) + ".tmp";
|
|
ROCKS_LOG_INFO(db_options.info_log,
|
|
"Snapshot process -- using temporary directory %s",
|
|
full_private_path.c_str());
|
|
|
|
s = CleanStagingDirectory(full_private_path, db_options.info_log.get());
|
|
if (!s.ok()) {
|
|
return Status::Aborted(
|
|
"Failed to clean the temporary directory " + full_private_path +
|
|
" needed before checkpoint creation : " + s.ToString());
|
|
}
|
|
|
|
// create snapshot directory
|
|
s = db_->GetEnv()->CreateDir(full_private_path);
|
|
uint64_t sequence_number = 0;
|
|
if (s.ok()) {
|
|
// enable file deletions
|
|
s = db_->DisableFileDeletions();
|
|
const bool disabled_file_deletions = s.ok();
|
|
|
|
if (s.ok() || s.IsNotSupported()) {
|
|
s = CreateCustomCheckpoint(
|
|
[&](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_->GetFileSystem()->LinkFile(
|
|
src_dirname + "/" + fname, full_private_path + "/" + fname,
|
|
IOOptions(), nullptr);
|
|
} /* link_file_cb */,
|
|
[&](const std::string& src_dirname, const std::string& fname,
|
|
uint64_t size_limit_bytes, FileType,
|
|
const std::string& /* checksum_func_name */,
|
|
const std::string& /* checksum_val */,
|
|
const Temperature temperature) {
|
|
ROCKS_LOG_INFO(db_options.info_log, "Copying %s", fname.c_str());
|
|
return CopyFile(db_->GetFileSystem(), src_dirname + "/" + fname,
|
|
temperature, full_private_path + "/" + fname,
|
|
temperature, size_limit_bytes, db_options.use_fsync,
|
|
nullptr);
|
|
} /* 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_->GetFileSystem(),
|
|
full_private_path + "/" + fname, contents,
|
|
db_options.use_fsync);
|
|
} /* create_file_cb */,
|
|
&sequence_number, log_size_for_flush);
|
|
|
|
// we copied all the files, enable file deletions
|
|
if (disabled_file_deletions) {
|
|
Status ss = db_->EnableFileDeletions();
|
|
assert(ss.ok());
|
|
ss.PermitUncheckedError();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (s.ok()) {
|
|
// move tmp private backup to real snapshot directory
|
|
s = db_->GetEnv()->RenameFile(full_private_path, checkpoint_dir);
|
|
}
|
|
if (s.ok()) {
|
|
std::unique_ptr<FSDirectory> checkpoint_directory;
|
|
s = db_->GetFileSystem()->NewDirectory(checkpoint_dir, IOOptions(),
|
|
&checkpoint_directory, nullptr);
|
|
if (s.ok() && checkpoint_directory != nullptr) {
|
|
s = checkpoint_directory->FsyncWithDirOptions(
|
|
IOOptions(), nullptr,
|
|
DirFsyncOptions(DirFsyncOptions::FsyncReason::kDirRenamed));
|
|
}
|
|
}
|
|
|
|
if (s.ok()) {
|
|
if (sequence_number_ptr != nullptr) {
|
|
*sequence_number_ptr = sequence_number;
|
|
}
|
|
// 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 {
|
|
ROCKS_LOG_INFO(db_options.info_log, "Snapshot failed -- %s",
|
|
s.ToString().c_str());
|
|
// clean all the files and directory we might have created
|
|
Status del_s =
|
|
CleanStagingDirectory(full_private_path, db_options.info_log.get());
|
|
ROCKS_LOG_INFO(db_options.info_log,
|
|
"Clean files or directory we might have created %s: %s",
|
|
full_private_path.c_str(), del_s.ToString().c_str());
|
|
del_s.PermitUncheckedError();
|
|
}
|
|
return s;
|
|
}
|
|
|
|
Status CheckpointImpl::CreateCustomCheckpoint(
|
|
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,
|
|
const std::string& checksum_func_name,
|
|
const std::string& checksum_val, const Temperature temperature)>
|
|
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,
|
|
bool get_live_table_checksum) {
|
|
*sequence_number = db_->GetLatestSequenceNumber();
|
|
|
|
LiveFilesStorageInfoOptions opts;
|
|
opts.include_checksum_info = get_live_table_checksum;
|
|
opts.wal_size_for_flush = log_size_for_flush;
|
|
|
|
std::vector<LiveFileStorageInfo> infos;
|
|
{
|
|
Status s = db_->GetLiveFilesStorageInfo(opts, &infos);
|
|
if (!s.ok()) {
|
|
return s;
|
|
}
|
|
}
|
|
|
|
// Verify that everything except WAL files are in same directory
|
|
// (db_paths / cf_paths not supported)
|
|
std::unordered_set<std::string> dirs;
|
|
for (auto& info : infos) {
|
|
if (info.file_type != kWalFile) {
|
|
dirs.insert(info.directory);
|
|
}
|
|
}
|
|
if (dirs.size() > 1) {
|
|
return Status::NotSupported(
|
|
"db_paths / cf_paths not supported for Checkpoint nor BackupEngine");
|
|
}
|
|
|
|
bool same_fs = true;
|
|
|
|
for (auto& info : infos) {
|
|
Status s;
|
|
if (!info.replacement_contents.empty()) {
|
|
// Currently should only be used for CURRENT file.
|
|
assert(info.file_type == kCurrentFile);
|
|
|
|
if (info.size != info.replacement_contents.size()) {
|
|
s = Status::Corruption("Inconsistent size metadata for " +
|
|
info.relative_filename);
|
|
} else {
|
|
s = create_file_cb(info.relative_filename, info.replacement_contents,
|
|
info.file_type);
|
|
}
|
|
} else {
|
|
if (same_fs && !info.trim_to_size) {
|
|
s = link_file_cb(info.directory, info.relative_filename,
|
|
info.file_type);
|
|
if (s.IsNotSupported()) {
|
|
same_fs = false;
|
|
s = Status::OK();
|
|
}
|
|
s.MustCheck();
|
|
}
|
|
if (!same_fs || info.trim_to_size) {
|
|
assert(info.file_checksum_func_name.empty() ==
|
|
!opts.include_checksum_info);
|
|
// no assertion on file_checksum because empty is used for both "not
|
|
// set" and "unknown"
|
|
if (opts.include_checksum_info) {
|
|
s = copy_file_cb(info.directory, info.relative_filename, info.size,
|
|
info.file_type, info.file_checksum_func_name,
|
|
info.file_checksum, info.temperature);
|
|
} else {
|
|
s = copy_file_cb(info.directory, info.relative_filename, info.size,
|
|
info.file_type, kUnknownFileChecksumFuncName,
|
|
kUnknownFileChecksum, info.temperature);
|
|
}
|
|
}
|
|
}
|
|
if (!s.ok()) {
|
|
return s;
|
|
}
|
|
}
|
|
|
|
return Status::OK();
|
|
}
|
|
|
|
// Exports all live SST files of a specified Column Family onto export_dir,
|
|
// returning SST files information in metadata.
|
|
Status CheckpointImpl::ExportColumnFamily(
|
|
ColumnFamilyHandle* handle, const std::string& export_dir,
|
|
ExportImportFilesMetaData** metadata) {
|
|
auto cfh = static_cast_with_check<ColumnFamilyHandleImpl>(handle);
|
|
const auto cf_name = cfh->GetName();
|
|
const auto db_options = db_->GetDBOptions();
|
|
|
|
assert(metadata != nullptr);
|
|
assert(*metadata == nullptr);
|
|
auto s = db_->GetEnv()->FileExists(export_dir);
|
|
if (s.ok()) {
|
|
return Status::InvalidArgument("Specified export_dir exists");
|
|
} else if (!s.IsNotFound()) {
|
|
assert(s.IsIOError());
|
|
return s;
|
|
}
|
|
|
|
const auto final_nonslash_idx = export_dir.find_last_not_of('/');
|
|
if (final_nonslash_idx == std::string::npos) {
|
|
return Status::InvalidArgument("Specified export_dir invalid");
|
|
}
|
|
ROCKS_LOG_INFO(db_options.info_log,
|
|
"[%s] export column family onto export directory %s",
|
|
cf_name.c_str(), export_dir.c_str());
|
|
|
|
// Create a temporary export directory.
|
|
const auto tmp_export_dir =
|
|
export_dir.substr(0, final_nonslash_idx + 1) + ".tmp";
|
|
s = db_->GetEnv()->CreateDir(tmp_export_dir);
|
|
|
|
if (s.ok()) {
|
|
s = db_->Flush(ROCKSDB_NAMESPACE::FlushOptions(), handle);
|
|
}
|
|
|
|
ColumnFamilyMetaData db_metadata;
|
|
if (s.ok()) {
|
|
// Export live sst files with file deletions disabled.
|
|
s = db_->DisableFileDeletions();
|
|
if (s.ok()) {
|
|
db_->GetColumnFamilyMetaData(handle, &db_metadata);
|
|
|
|
s = ExportFilesInMetaData(
|
|
db_options, db_metadata,
|
|
[&](const std::string& src_dirname, const std::string& fname) {
|
|
ROCKS_LOG_INFO(db_options.info_log, "[%s] HardLinking %s",
|
|
cf_name.c_str(), fname.c_str());
|
|
return db_->GetEnv()->LinkFile(src_dirname + fname,
|
|
tmp_export_dir + fname);
|
|
} /*link_file_cb*/,
|
|
[&](const std::string& src_dirname, const std::string& fname) {
|
|
ROCKS_LOG_INFO(db_options.info_log, "[%s] Copying %s",
|
|
cf_name.c_str(), fname.c_str());
|
|
// FIXME: temperature handling
|
|
return CopyFile(db_->GetFileSystem(), src_dirname + fname,
|
|
Temperature::kUnknown, tmp_export_dir + fname,
|
|
Temperature::kUnknown, 0, db_options.use_fsync,
|
|
nullptr);
|
|
} /*copy_file_cb*/);
|
|
|
|
const auto enable_status = db_->EnableFileDeletions();
|
|
if (s.ok()) {
|
|
s = enable_status;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto moved_to_user_specified_dir = false;
|
|
if (s.ok()) {
|
|
// Move temporary export directory to the actual export directory.
|
|
s = db_->GetEnv()->RenameFile(tmp_export_dir, export_dir);
|
|
}
|
|
|
|
if (s.ok()) {
|
|
// Fsync export directory.
|
|
moved_to_user_specified_dir = true;
|
|
std::unique_ptr<FSDirectory> dir_ptr;
|
|
s = db_->GetFileSystem()->NewDirectory(export_dir, IOOptions(), &dir_ptr,
|
|
nullptr);
|
|
if (s.ok()) {
|
|
assert(dir_ptr != nullptr);
|
|
s = dir_ptr->FsyncWithDirOptions(
|
|
IOOptions(), nullptr,
|
|
DirFsyncOptions(DirFsyncOptions::FsyncReason::kDirRenamed));
|
|
}
|
|
}
|
|
|
|
if (s.ok()) {
|
|
// Export of files succeeded. Fill in the metadata information.
|
|
auto result_metadata = new ExportImportFilesMetaData();
|
|
result_metadata->db_comparator_name = handle->GetComparator()->Name();
|
|
for (const auto& level_metadata : db_metadata.levels) {
|
|
for (const auto& file_metadata : level_metadata.files) {
|
|
LiveFileMetaData live_file_metadata;
|
|
live_file_metadata.size = file_metadata.size;
|
|
live_file_metadata.name = file_metadata.name;
|
|
live_file_metadata.file_number = file_metadata.file_number;
|
|
live_file_metadata.db_path = export_dir;
|
|
live_file_metadata.smallest_seqno = file_metadata.smallest_seqno;
|
|
live_file_metadata.largest_seqno = file_metadata.largest_seqno;
|
|
live_file_metadata.smallestkey = file_metadata.smallestkey;
|
|
live_file_metadata.largestkey = file_metadata.largestkey;
|
|
live_file_metadata.oldest_blob_file_number =
|
|
file_metadata.oldest_blob_file_number;
|
|
live_file_metadata.epoch_number = file_metadata.epoch_number;
|
|
live_file_metadata.level = level_metadata.level;
|
|
live_file_metadata.smallest = file_metadata.smallest;
|
|
live_file_metadata.largest = file_metadata.largest;
|
|
result_metadata->files.push_back(live_file_metadata);
|
|
}
|
|
*metadata = result_metadata;
|
|
}
|
|
ROCKS_LOG_INFO(db_options.info_log, "[%s] Export succeeded.",
|
|
cf_name.c_str());
|
|
} else {
|
|
// Failure: Clean up all the files/directories created.
|
|
ROCKS_LOG_INFO(db_options.info_log, "[%s] Export failed. %s",
|
|
cf_name.c_str(), s.ToString().c_str());
|
|
std::vector<std::string> subchildren;
|
|
const auto cleanup_dir =
|
|
moved_to_user_specified_dir ? export_dir : tmp_export_dir;
|
|
db_->GetEnv()->GetChildren(cleanup_dir, &subchildren);
|
|
for (const auto& subchild : subchildren) {
|
|
const auto subchild_path = cleanup_dir + "/" + subchild;
|
|
const auto status = db_->GetEnv()->DeleteFile(subchild_path);
|
|
if (!status.ok()) {
|
|
ROCKS_LOG_WARN(db_options.info_log, "Failed to cleanup file %s: %s",
|
|
subchild_path.c_str(), status.ToString().c_str());
|
|
}
|
|
}
|
|
const auto status = db_->GetEnv()->DeleteDir(cleanup_dir);
|
|
if (!status.ok()) {
|
|
ROCKS_LOG_WARN(db_options.info_log, "Failed to cleanup dir %s: %s",
|
|
cleanup_dir.c_str(), status.ToString().c_str());
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
Status CheckpointImpl::ExportFilesInMetaData(
|
|
const DBOptions& db_options, const ColumnFamilyMetaData& metadata,
|
|
std::function<Status(const std::string& src_dirname,
|
|
const std::string& src_fname)>
|
|
link_file_cb,
|
|
std::function<Status(const std::string& src_dirname,
|
|
const std::string& src_fname)>
|
|
copy_file_cb) {
|
|
Status s;
|
|
auto hardlink_file = true;
|
|
|
|
// Copy/hard link files in metadata.
|
|
size_t num_files = 0;
|
|
for (const auto& level_metadata : metadata.levels) {
|
|
for (const auto& file_metadata : level_metadata.files) {
|
|
uint64_t number;
|
|
FileType type;
|
|
const auto ok = ParseFileName(file_metadata.name, &number, &type);
|
|
if (!ok) {
|
|
s = Status::Corruption("Could not parse file name");
|
|
break;
|
|
}
|
|
|
|
// We should only get sst files here.
|
|
assert(type == kTableFile);
|
|
assert(file_metadata.size > 0 && file_metadata.name[0] == '/');
|
|
const auto src_fname = file_metadata.name;
|
|
++num_files;
|
|
|
|
if (hardlink_file) {
|
|
s = link_file_cb(db_->GetName(), src_fname);
|
|
if (num_files == 1 && s.IsNotSupported()) {
|
|
// Fallback to copy if link failed due to cross-device directories.
|
|
hardlink_file = false;
|
|
s = Status::OK();
|
|
}
|
|
}
|
|
if (!hardlink_file) {
|
|
s = copy_file_cb(db_->GetName(), src_fname);
|
|
}
|
|
if (!s.ok()) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
ROCKS_LOG_INFO(db_options.info_log, "Number of table files %" ROCKSDB_PRIszt,
|
|
num_files);
|
|
|
|
return s;
|
|
}
|
|
} // namespace ROCKSDB_NAMESPACE
|