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).
|
2014-10-30 00:43:37 +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.
|
|
|
|
|
|
|
|
#include "db/wal_manager.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2019-09-20 19:00:55 +00:00
|
|
|
#include <cinttypes>
|
2014-10-30 00:43:37 +00:00
|
|
|
#include <memory>
|
2019-09-20 19:00:55 +00:00
|
|
|
#include <vector>
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
#include "db/log_reader.h"
|
|
|
|
#include "db/log_writer.h"
|
2017-04-04 01:27:24 +00:00
|
|
|
#include "db/transaction_log_impl.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
#include "db/write_batch_internal.h"
|
2019-05-30 03:44:08 +00:00
|
|
|
#include "file/file_util.h"
|
|
|
|
#include "file/filename.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/sequence_file_reader.h"
|
2019-06-01 00:19:43 +00:00
|
|
|
#include "logging/logging.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/write_batch.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/sync_point.h"
|
2017-07-28 23:23:50 +00:00
|
|
|
#include "util/cast_util.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/mutexlock.h"
|
2014-11-25 04:44:49 +00:00
|
|
|
#include "util/string_util.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
|
2018-01-12 02:48:19 +00:00
|
|
|
Status WalManager::DeleteFile(const std::string& fname, uint64_t number) {
|
2021-07-30 19:15:04 +00:00
|
|
|
auto s = env_->DeleteFile(wal_dir_ + "/" + fname);
|
2018-01-12 02:48:19 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
read_first_record_cache_.erase(number);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2024-07-02 06:29:02 +00:00
|
|
|
Status WalManager::GetSortedWalFiles(VectorWalPtr& files, bool need_seqnos,
|
|
|
|
bool include_archived) {
|
2014-10-30 00:43:37 +00:00
|
|
|
// First get sorted files in db dir, then get sorted files from archived
|
|
|
|
// dir, to avoid a race condition where a log file is moved to archived
|
|
|
|
// dir in between.
|
|
|
|
Status s;
|
|
|
|
// list wal files in main db dir.
|
2024-05-28 16:24:49 +00:00
|
|
|
VectorWalPtr logs;
|
2024-07-02 06:29:02 +00:00
|
|
|
s = GetSortedWalsOfType(wal_dir_, logs, kAliveLogFile, need_seqnos);
|
|
|
|
|
|
|
|
if (!include_archived || !s.ok()) {
|
2014-10-30 00:43:37 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reproduce the race condition where a log file is moved
|
|
|
|
// to archived dir, between these two sync points, used in
|
|
|
|
// (DBTest,TransactionLogIteratorRace)
|
|
|
|
TEST_SYNC_POINT("WalManager::GetSortedWalFiles:1");
|
|
|
|
TEST_SYNC_POINT("WalManager::GetSortedWalFiles:2");
|
|
|
|
|
|
|
|
files.clear();
|
|
|
|
// list wal files in archive dir.
|
2021-07-30 19:15:04 +00:00
|
|
|
std::string archivedir = ArchivalDirectory(wal_dir_);
|
2015-07-21 00:20:40 +00:00
|
|
|
Status exists = env_->FileExists(archivedir);
|
|
|
|
if (exists.ok()) {
|
2024-07-02 06:29:02 +00:00
|
|
|
s = GetSortedWalsOfType(archivedir, files, kArchivedLogFile, need_seqnos);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2015-07-21 00:20:40 +00:00
|
|
|
} else if (!exists.IsNotFound()) {
|
Inject more errors to more files in stress test (#12713)
Summary:
**Context:**
We currently have partial error injection:
- DB operation: all read, SST write
- DB open: all read, SST write, all metadata write.
This PR completes the error injection (with some limitations below):
- DB operation & open: all read, all write, all metadata write, all metadata read
**Summary:**
- Inject retryable metadata read, metadata write error concerning directory (e.g, dir sync, ) or file metadata (e.g, name, size, file creation/deletion...)
- Inject retryable errors to all major file types: random access file, sequential file, writable file
- Allow db stress test operations to handle above injected errors gracefully without crashing
- Change all error injection to thread-local implementation for easier disabling and enabling in the same thread. For example, we can control error handling thread to have no error injection. It's also cleaner in code.
- Limitation: compared to before, we now don't have write fault injection for backup/restore CopyOrCreateFiles work threads since they use anonymous background threads as well as read injection for db open bg thread
- Add a new flag to test error recovery without error injection so we can test the path where error recovery actually succeeds
- Some Refactory & fix to db stress test framework (see PR review comments)
- Fix some minor bugs surfaced (see PR review comments)
- Limitation: had to disable backup restore with metadata read/write injection since it surfaces too many testing issues. Will add it back later to focus on surfacing actual code/internal bugs first.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12713
Test Plan:
- Existing UT
- CI with no trivial error failure
Reviewed By: pdillinger
Differential Revision: D58326608
Pulled By: hx235
fbshipit-source-id: 011b5195aaeb6011641ae0a9194f7f2a0e325ad7
2024-06-19 15:42:00 +00:00
|
|
|
assert(s.ok());
|
|
|
|
return exists;
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t latest_archived_log_number = 0;
|
|
|
|
if (!files.empty()) {
|
|
|
|
latest_archived_log_number = files.back()->LogNumber();
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_INFO(db_options_.info_log, "Latest Archived log: %" PRIu64,
|
|
|
|
latest_archived_log_number);
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
files.reserve(files.size() + logs.size());
|
|
|
|
for (auto& log : logs) {
|
|
|
|
if (log->LogNumber() > latest_archived_log_number) {
|
|
|
|
files.push_back(std::move(log));
|
|
|
|
} else {
|
|
|
|
// When the race condition happens, we could see the
|
|
|
|
// same log in both db dir and archived dir. Simply
|
|
|
|
// ignore the one in db dir. Note that, if we read
|
|
|
|
// archived dir first, we would have missed the log file.
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log, "%s already moved to archive",
|
|
|
|
log->PathName().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WalManager::GetUpdatesSince(
|
|
|
|
SequenceNumber seq, std::unique_ptr<TransactionLogIterator>* iter,
|
|
|
|
const TransactionLogIterator::ReadOptions& read_options,
|
|
|
|
VersionSet* version_set) {
|
2022-04-15 00:12:16 +00:00
|
|
|
if (seq_per_batch_) {
|
|
|
|
return Status::NotSupported();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!seq_per_batch_);
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
// Get all sorted Wal Files.
|
|
|
|
// Do binary search and open files and find the seq number.
|
|
|
|
|
2024-05-28 16:24:49 +00:00
|
|
|
std::unique_ptr<VectorWalPtr> wal_files(new VectorWalPtr);
|
2014-10-30 00:43:37 +00:00
|
|
|
Status s = GetSortedWalFiles(*wal_files);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = RetainProbableWalFiles(*wal_files, seq);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
iter->reset(new TransactionLogIteratorImpl(
|
2021-07-30 19:15:04 +00:00
|
|
|
wal_dir_, &db_options_, read_options, file_options_, seq,
|
2020-08-13 00:28:10 +00:00
|
|
|
std::move(wal_files), version_set, seq_per_batch_, io_tracer_));
|
2014-10-30 00:43:37 +00:00
|
|
|
return (*iter)->status();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Go through all archived files and
|
|
|
|
// a. if ttl is enabled, delete outdated files
|
|
|
|
// b. if archive size limit is enabled, delete empty files,
|
|
|
|
// compute file number and size.
|
|
|
|
// 2. If size limit is enabled:
|
|
|
|
// a. compute how many files should be deleted
|
|
|
|
// b. get sorted non-empty archived logs
|
|
|
|
// c. delete what should be deleted
|
|
|
|
void WalManager::PurgeObsoleteWALFiles() {
|
2021-04-23 03:42:50 +00:00
|
|
|
bool const ttl_enabled = db_options_.WAL_ttl_seconds > 0;
|
|
|
|
bool const size_limit_enabled = db_options_.WAL_size_limit_MB > 0;
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!ttl_enabled && !size_limit_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
int64_t current_time = 0;
|
|
|
|
Status s = db_options_.clock->GetCurrentTime(¤t_time);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(db_options_.info_log, "Can't get current time: %s",
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint64_t const now_seconds = static_cast<uint64_t>(current_time);
|
2023-11-15 23:42:28 +00:00
|
|
|
uint64_t const time_to_check =
|
|
|
|
ttl_enabled
|
|
|
|
? std::min(kDefaultIntervalToDeleteObsoleteWAL,
|
|
|
|
std::max(uint64_t{1}, db_options_.WAL_ttl_seconds / 2))
|
|
|
|
: kDefaultIntervalToDeleteObsoleteWAL;
|
2024-03-15 04:24:06 +00:00
|
|
|
uint64_t old_last_run_time = purge_wal_files_last_run_.LoadRelaxed();
|
|
|
|
do {
|
|
|
|
if (old_last_run_time + time_to_check > now_seconds) {
|
|
|
|
// last run is recent enough, no need to purge
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (!purge_wal_files_last_run_.CasWeakRelaxed(
|
|
|
|
/*expected=*/old_last_run_time, /*desired=*/now_seconds));
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2021-07-30 19:15:04 +00:00
|
|
|
std::string archival_dir = ArchivalDirectory(wal_dir_);
|
2014-10-30 00:43:37 +00:00
|
|
|
std::vector<std::string> files;
|
|
|
|
s = env_->GetChildren(archival_dir, &files);
|
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(db_options_.info_log, "Can't get archive files: %s",
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t log_files_num = 0;
|
|
|
|
uint64_t log_file_size = 0;
|
|
|
|
for (auto& f : files) {
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
2020-10-23 00:04:39 +00:00
|
|
|
if (ParseFileName(f, &number, &type) && type == kWalFile) {
|
2014-10-30 00:43:37 +00:00
|
|
|
std::string const file_path = archival_dir + "/" + f;
|
|
|
|
if (ttl_enabled) {
|
|
|
|
uint64_t file_m_time;
|
2014-10-31 18:59:54 +00:00
|
|
|
s = env_->GetFileModificationTime(file_path, &file_m_time);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"Can't get file mod time: %s: %s", file_path.c_str(),
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-23 03:42:50 +00:00
|
|
|
if (now_seconds - file_m_time > db_options_.WAL_ttl_seconds) {
|
2019-07-07 04:04:22 +00:00
|
|
|
s = DeleteDBFile(&db_options_, file_path, archival_dir, false,
|
|
|
|
/*force_fg=*/!wal_in_db_path_);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log, "Can't delete file: %s: %s",
|
|
|
|
file_path.c_str(), s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
read_first_record_cache_.erase(number);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_limit_enabled) {
|
|
|
|
uint64_t file_size;
|
2014-10-31 18:59:54 +00:00
|
|
|
s = env_->GetFileSize(file_path, &file_size);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(db_options_.info_log,
|
|
|
|
"Unable to get file size: %s: %s", file_path.c_str(),
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (file_size > 0) {
|
|
|
|
log_file_size = std::max(log_file_size, file_size);
|
|
|
|
++log_files_num;
|
|
|
|
} else {
|
2019-07-07 04:04:22 +00:00
|
|
|
s = DeleteDBFile(&db_options_, file_path, archival_dir, false,
|
|
|
|
/*force_fg=*/!wal_in_db_path_);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"Unable to delete file: %s: %s", file_path.c_str(),
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
read_first_record_cache_.erase(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == log_files_num || !size_limit_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-23 03:42:50 +00:00
|
|
|
size_t const files_keep_num = static_cast<size_t>(
|
|
|
|
db_options_.WAL_size_limit_MB * 1024 * 1024 / log_file_size);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (log_files_num <= files_keep_num) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t files_del_num = log_files_num - files_keep_num;
|
2024-05-28 16:24:49 +00:00
|
|
|
VectorWalPtr archived_logs;
|
2024-07-02 06:29:02 +00:00
|
|
|
s = GetSortedWalsOfType(archival_dir, archived_logs, kArchivedLogFile,
|
|
|
|
/*need_seqno=*/false);
|
2020-12-23 07:44:44 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"Unable to get archived WALs from: %s: %s",
|
|
|
|
archival_dir.c_str(), s.ToString().c_str());
|
|
|
|
files_del_num = 0;
|
|
|
|
} else if (files_del_num > archived_logs.size()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log,
|
|
|
|
"Trying to delete more archived log files than "
|
|
|
|
"exist. Deleting all");
|
2014-10-30 00:43:37 +00:00
|
|
|
files_del_num = archived_logs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < files_del_num; ++i) {
|
|
|
|
std::string const file_path = archived_logs[i]->PathName();
|
2021-07-30 19:15:04 +00:00
|
|
|
s = DeleteDBFile(&db_options_, wal_dir_ + "/" + file_path, wal_dir_, false,
|
2019-07-07 04:04:22 +00:00
|
|
|
/*force_fg=*/!wal_in_db_path_);
|
2014-10-30 00:43:37 +00:00
|
|
|
if (!s.ok()) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(db_options_.info_log, "Unable to delete file: %s: %s",
|
|
|
|
file_path.c_str(), s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
read_first_record_cache_.erase(archived_logs[i]->LogNumber());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalManager::ArchiveWALFile(const std::string& fname, uint64_t number) {
|
2021-07-30 19:15:04 +00:00
|
|
|
auto archived_log_name = ArchivedLogFileName(wal_dir_, number);
|
2014-10-30 00:43:37 +00:00
|
|
|
// The sync point below is used in (DBTest,TransactionLogIteratorRace)
|
|
|
|
TEST_SYNC_POINT("WalManager::PurgeObsoleteFiles:1");
|
|
|
|
Status s = env_->RenameFile(fname, archived_log_name);
|
|
|
|
// The sync point below is used in (DBTest,TransactionLogIteratorRace)
|
|
|
|
TEST_SYNC_POINT("WalManager::PurgeObsoleteFiles:2");
|
2024-06-28 18:56:26 +00:00
|
|
|
// The sync point below is used in
|
|
|
|
// (CheckPointTest, CheckpointWithArchievedLog)
|
|
|
|
TEST_SYNC_POINT("WalManager::ArchiveWALFile");
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_INFO(db_options_.info_log, "Move log file %s to %s -- %s\n",
|
|
|
|
fname.c_str(), archived_log_name.c_str(),
|
|
|
|
s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status WalManager::GetSortedWalsOfType(const std::string& path,
|
2024-05-28 16:24:49 +00:00
|
|
|
VectorWalPtr& log_files,
|
2024-07-02 06:29:02 +00:00
|
|
|
WalFileType log_type, bool need_seqnos) {
|
2014-10-30 00:43:37 +00:00
|
|
|
std::vector<std::string> all_files;
|
|
|
|
const Status status = env_->GetChildren(path, &all_files);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
log_files.reserve(all_files.size());
|
|
|
|
for (const auto& f : all_files) {
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
2020-10-23 00:04:39 +00:00
|
|
|
if (ParseFileName(f, &number, &type) && type == kWalFile) {
|
2014-10-30 00:43:37 +00:00
|
|
|
SequenceNumber sequence;
|
2024-07-02 06:29:02 +00:00
|
|
|
if (need_seqnos) {
|
|
|
|
Status s = ReadFirstRecord(log_type, number, &sequence);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (sequence == 0) {
|
|
|
|
// empty file
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sequence = 0;
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reproduce the race condition where a log file is moved
|
|
|
|
// to archived dir, between these two sync points, used in
|
|
|
|
// (DBTest,TransactionLogIteratorRace)
|
|
|
|
TEST_SYNC_POINT("WalManager::GetSortedWalsOfType:1");
|
|
|
|
TEST_SYNC_POINT("WalManager::GetSortedWalsOfType:2");
|
|
|
|
|
|
|
|
uint64_t size_bytes;
|
2024-07-02 06:29:02 +00:00
|
|
|
Status s = env_->GetFileSize(LogFileName(path, number), &size_bytes);
|
2014-10-30 00:43:37 +00:00
|
|
|
// re-try in case the alive log file has been moved to archive.
|
2019-09-10 16:40:21 +00:00
|
|
|
if (!s.ok() && log_type == kAliveLogFile) {
|
|
|
|
std::string archived_file = ArchivedLogFileName(path, number);
|
|
|
|
if (env_->FileExists(archived_file).ok()) {
|
|
|
|
s = env_->GetFileSize(archived_file, &size_bytes);
|
|
|
|
if (!s.ok() && env_->FileExists(archived_file).IsNotFound()) {
|
|
|
|
// oops, the file just got deleted from archived dir! move on
|
|
|
|
s = Status::OK();
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-24 01:35:12 +00:00
|
|
|
}
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:40:33 +00:00
|
|
|
log_files.emplace_back(
|
2024-05-28 16:24:49 +00:00
|
|
|
new WalFileImpl(number, log_type, sequence, size_bytes));
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 18:36:47 +00:00
|
|
|
std::sort(
|
|
|
|
log_files.begin(), log_files.end(),
|
2024-05-28 16:24:49 +00:00
|
|
|
[](const std::unique_ptr<WalFile>& a, const std::unique_ptr<WalFile>& b) {
|
|
|
|
WalFileImpl* a_impl = static_cast_with_check<WalFileImpl>(a.get());
|
|
|
|
WalFileImpl* b_impl = static_cast_with_check<WalFileImpl>(b.get());
|
2019-09-04 18:36:47 +00:00
|
|
|
return *a_impl < *b_impl;
|
|
|
|
});
|
2014-10-30 00:43:37 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:24:49 +00:00
|
|
|
Status WalManager::RetainProbableWalFiles(VectorWalPtr& all_logs,
|
2014-10-30 00:43:37 +00:00
|
|
|
const SequenceNumber target) {
|
|
|
|
int64_t start = 0; // signed to avoid overflow when target is < first file.
|
|
|
|
int64_t end = static_cast<int64_t>(all_logs.size()) - 1;
|
|
|
|
// Binary Search. avoid opening all files.
|
|
|
|
while (end >= start) {
|
|
|
|
int64_t mid = start + (end - start) / 2; // Avoid overflow.
|
2022-11-02 21:34:24 +00:00
|
|
|
SequenceNumber current_seq_num =
|
|
|
|
all_logs.at(static_cast<size_t>(mid))->StartSequence();
|
2014-10-30 00:43:37 +00:00
|
|
|
if (current_seq_num == target) {
|
|
|
|
end = mid;
|
|
|
|
break;
|
|
|
|
} else if (current_seq_num < target) {
|
|
|
|
start = mid + 1;
|
|
|
|
} else {
|
|
|
|
end = mid - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// end could be -ve.
|
2022-11-02 21:34:24 +00:00
|
|
|
size_t start_index =
|
|
|
|
static_cast<size_t>(std::max(static_cast<int64_t>(0), end));
|
2014-10-30 00:43:37 +00:00
|
|
|
// The last wal file is always included
|
|
|
|
all_logs.erase(all_logs.begin(), all_logs.begin() + start_index);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WalManager::ReadFirstRecord(const WalFileType type,
|
|
|
|
const uint64_t number,
|
|
|
|
SequenceNumber* sequence) {
|
2015-01-24 01:35:12 +00:00
|
|
|
*sequence = 0;
|
2014-10-30 00:43:37 +00:00
|
|
|
if (type != kAliveLogFile && type != kArchivedLogFile) {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_ERROR(db_options_.info_log, "[WalManger] Unknown file type %s",
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(type).c_str());
|
|
|
|
return Status::NotSupported("File Type Not Known " + std::to_string(type));
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
auto itr = read_first_record_cache_.find(number);
|
|
|
|
if (itr != read_first_record_cache_.end()) {
|
|
|
|
*sequence = itr->second;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Status s;
|
|
|
|
if (type == kAliveLogFile) {
|
2021-07-30 19:15:04 +00:00
|
|
|
std::string fname = LogFileName(wal_dir_, number);
|
2016-09-15 16:55:02 +00:00
|
|
|
s = ReadFirstLine(fname, number, sequence);
|
2019-09-10 16:40:21 +00:00
|
|
|
if (!s.ok() && env_->FileExists(fname).ok()) {
|
2014-10-30 00:43:37 +00:00
|
|
|
// return any error that is not caused by non-existing file
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == kArchivedLogFile || !s.ok()) {
|
|
|
|
// check if the file got moved to archive.
|
2021-07-30 19:15:04 +00:00
|
|
|
std::string archived_file = ArchivedLogFileName(wal_dir_, number);
|
2016-09-15 16:55:02 +00:00
|
|
|
s = ReadFirstLine(archived_file, number, sequence);
|
2015-01-24 01:35:12 +00:00
|
|
|
// maybe the file was deleted from archive dir. If that's the case, return
|
|
|
|
// Status::OK(). The caller with identify this as empty file because
|
|
|
|
// *sequence == 0
|
2015-07-21 00:20:40 +00:00
|
|
|
if (!s.ok() && env_->FileExists(archived_file).IsNotFound()) {
|
2015-01-24 01:35:12 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok() && *sequence != 0) {
|
|
|
|
MutexLock l(&read_first_record_cache_mutex_);
|
|
|
|
read_first_record_cache_.insert({number, *sequence});
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:00:55 +00:00
|
|
|
Status WalManager::GetLiveWalFile(uint64_t number,
|
2024-05-28 16:24:49 +00:00
|
|
|
std::unique_ptr<WalFile>* log_file) {
|
2019-09-04 19:08:56 +00:00
|
|
|
if (!log_file) {
|
|
|
|
return Status::InvalidArgument("log_file not preallocated.");
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:00:55 +00:00
|
|
|
if (!number) {
|
2019-09-04 19:08:56 +00:00
|
|
|
return Status::PathNotFound("log file not available");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status s;
|
|
|
|
|
|
|
|
uint64_t size_bytes;
|
2021-07-30 19:15:04 +00:00
|
|
|
s = env_->GetFileSize(LogFileName(wal_dir_, number), &size_bytes);
|
2019-09-04 19:08:56 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:24:49 +00:00
|
|
|
log_file->reset(new WalFileImpl(number, kAliveLogFile,
|
2019-09-20 19:00:55 +00:00
|
|
|
0, // SequenceNumber
|
|
|
|
size_bytes));
|
2019-09-04 19:08:56 +00:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:43:37 +00:00
|
|
|
// the function returns status.ok() and sequence == 0 if the file exists, but is
|
|
|
|
// empty
|
|
|
|
Status WalManager::ReadFirstLine(const std::string& fname,
|
2016-09-15 16:55:02 +00:00
|
|
|
const uint64_t number,
|
2014-10-30 00:43:37 +00:00
|
|
|
SequenceNumber* sequence) {
|
|
|
|
struct LogReporter : public log::Reader::Reporter {
|
|
|
|
Env* env;
|
|
|
|
Logger* info_log;
|
|
|
|
const char* fname;
|
|
|
|
|
|
|
|
Status* status;
|
|
|
|
bool ignore_error; // true if db_options_.paranoid_checks==false
|
2019-02-19 21:36:04 +00:00
|
|
|
void Corruption(size_t bytes, const Status& s) override {
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_WARN(info_log, "[WalManager] %s%s: dropping %d bytes; %s",
|
|
|
|
(this->ignore_error ? "(ignoring error) " : ""), fname,
|
|
|
|
static_cast<int>(bytes), s.ToString().c_str());
|
2014-10-30 00:43:37 +00:00
|
|
|
if (this->status->ok()) {
|
|
|
|
// only keep the first error
|
|
|
|
*this->status = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 22:47:08 +00:00
|
|
|
std::unique_ptr<FSSequentialFile> file;
|
2022-11-02 21:34:24 +00:00
|
|
|
Status status = fs_->NewSequentialFile(
|
|
|
|
fname, fs_->OptimizeForLogRead(file_options_), &file, nullptr);
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFileReader> file_reader(
|
2020-08-18 23:19:22 +00:00
|
|
|
new SequentialFileReader(std::move(file), fname, io_tracer_));
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogReporter reporter;
|
|
|
|
reporter.env = env_;
|
|
|
|
reporter.info_log = db_options_.info_log.get();
|
|
|
|
reporter.fname = fname.c_str();
|
|
|
|
reporter.status = &status;
|
|
|
|
reporter.ignore_error = !db_options_.paranoid_checks;
|
2015-10-08 17:06:16 +00:00
|
|
|
log::Reader reader(db_options_.info_log, std::move(file_reader), &reporter,
|
2019-03-26 23:41:31 +00:00
|
|
|
true /*checksum*/, number);
|
2014-10-30 00:43:37 +00:00
|
|
|
std::string scratch;
|
|
|
|
Slice record;
|
|
|
|
|
|
|
|
if (reader.ReadRecord(&record, &scratch) &&
|
|
|
|
(status.ok() || !db_options_.paranoid_checks)) {
|
2016-03-30 17:35:22 +00:00
|
|
|
if (record.size() < WriteBatchInternal::kHeader) {
|
2014-10-30 00:43:37 +00:00
|
|
|
reporter.Corruption(record.size(),
|
|
|
|
Status::Corruption("log record too small"));
|
|
|
|
// TODO read record's till the first no corrupt entry?
|
|
|
|
} else {
|
|
|
|
WriteBatch batch;
|
2020-09-29 23:28:42 +00:00
|
|
|
// We can overwrite an existing non-OK Status since it'd only reach here
|
|
|
|
// with `paranoid_checks == false`.
|
|
|
|
status = WriteBatchInternal::SetContents(&batch, record);
|
|
|
|
if (status.ok()) {
|
|
|
|
*sequence = WriteBatchInternal::Sequence(&batch);
|
|
|
|
return status;
|
|
|
|
}
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:16:43 +00:00
|
|
|
if (status.ok() && reader.IsCompressedAndEmptyFile()) {
|
|
|
|
// In case of wal_compression, it writes a `kSetCompressionType` record
|
|
|
|
// which is not associated with any sequence number. As result for an empty
|
|
|
|
// file, GetSortedWalsOfType() will skip these WALs causing the operations
|
|
|
|
// to fail.
|
|
|
|
// Therefore, in order to avoid that failure, it sets sequence_number to 1
|
|
|
|
// indicating those WALs should be included.
|
|
|
|
*sequence = 1;
|
|
|
|
} else {
|
|
|
|
// ReadRecord might have returned false on EOF, which means that the log
|
|
|
|
// file is empty. Or, a failure may have occurred while processing the first
|
|
|
|
// entry. In any case, return status and set sequence number to 0.
|
|
|
|
*sequence = 0;
|
|
|
|
}
|
2014-10-30 00:43:37 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|