2020-03-04 21:30:34 +01:00
|
|
|
// 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 2014 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.
|
|
|
|
|
|
|
|
// This test uses a custom FileSystem to keep track of the state of a file
|
|
|
|
// system the last "Sync". The data being written is cached in a "buffer".
|
2021-01-07 04:27:26 +01:00
|
|
|
// Only when "Sync" is called, the data will be persistent. It can simulate
|
2020-03-04 21:30:34 +01:00
|
|
|
// file data loss (or entire files) not protected by a "Sync". For any of the
|
|
|
|
// FileSystem related operations, by specify the "IOStatus Error", a specific
|
|
|
|
// error can be returned when file system is not activated.
|
|
|
|
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "utilities/fault_injection_fs.h"
|
|
|
|
|
2020-03-04 21:30:34 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <utility>
|
2020-07-09 23:33:42 +02:00
|
|
|
|
|
|
|
#include "env/composite_env_wrapper.h"
|
2020-04-20 22:21:34 +02:00
|
|
|
#include "port/lang.h"
|
2020-04-11 02:18:56 +02:00
|
|
|
#include "port/stack_trace.h"
|
2021-02-11 07:18:33 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/crc32c.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "util/random.h"
|
2021-02-11 07:18:33 +01:00
|
|
|
#include "util/xxhash.h"
|
2020-03-04 21:30:34 +01:00
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
|
|
|
// Assume a filename, and not a directory name like "/foo/bar/"
|
|
|
|
std::string TestFSGetDirName(const std::string filename) {
|
|
|
|
size_t found = filename.find_last_of("/\\");
|
|
|
|
if (found == std::string::npos) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return filename.substr(0, found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim the tailing "/" in the end of `str`
|
|
|
|
std::string TestFSTrimDirname(const std::string& str) {
|
|
|
|
size_t found = str.find_last_not_of("/");
|
|
|
|
if (found == std::string::npos) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return str.substr(0, found + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return pair <parent directory name, file name> of a full path.
|
|
|
|
std::pair<std::string, std::string> TestFSGetDirAndName(
|
|
|
|
const std::string& name) {
|
|
|
|
std::string dirname = TestFSGetDirName(name);
|
|
|
|
std::string fname = name.substr(dirname.size() + 1);
|
|
|
|
return std::make_pair(dirname, fname);
|
|
|
|
}
|
|
|
|
|
2021-02-11 07:18:33 +01:00
|
|
|
// Calculate the checksum of the data with corresponding checksum
|
|
|
|
// type. If name does not match, no checksum is returned.
|
|
|
|
void CalculateTypedChecksum(const ChecksumType& checksum_type, const char* data,
|
|
|
|
size_t size, std::string* checksum) {
|
|
|
|
if (checksum_type == ChecksumType::kCRC32c) {
|
|
|
|
uint32_t v_crc32c = crc32c::Extend(0, data, size);
|
|
|
|
PutFixed32(checksum, v_crc32c);
|
|
|
|
return;
|
|
|
|
} else if (checksum_type == ChecksumType::kxxHash) {
|
|
|
|
uint32_t v = XXH32(data, size, 0);
|
|
|
|
PutFixed32(checksum, v);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus FSFileState::DropUnsyncedData() {
|
|
|
|
buffer_.resize(0);
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FSFileState::DropRandomUnsyncedData(Random* rand) {
|
|
|
|
int range = static_cast<int>(buffer_.size());
|
|
|
|
size_t truncated_size = static_cast<size_t>(rand->Uniform(range));
|
|
|
|
buffer_.resize(truncated_size);
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSDirectory::Fsync(const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = fs_->InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
fs_->SyncDir(dirname_);
|
2021-04-28 19:57:11 +02:00
|
|
|
IOStatus s = dir_->Fsync(options, dbg);
|
|
|
|
{
|
|
|
|
IOStatus in_s = fs_->InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TestFSWritableFile::TestFSWritableFile(const std::string& fname,
|
2021-02-11 07:18:33 +01:00
|
|
|
const FileOptions& file_opts,
|
2020-03-04 21:30:34 +01:00
|
|
|
std::unique_ptr<FSWritableFile>&& f,
|
|
|
|
FaultInjectionTestFS* fs)
|
|
|
|
: state_(fname),
|
2021-02-11 07:18:33 +01:00
|
|
|
file_opts_(file_opts),
|
2020-03-04 21:30:34 +01:00
|
|
|
target_(std::move(f)),
|
|
|
|
writable_file_opened_(true),
|
|
|
|
fs_(fs) {
|
|
|
|
assert(target_ != nullptr);
|
|
|
|
state_.pos_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestFSWritableFile::~TestFSWritableFile() {
|
|
|
|
if (writable_file_opened_) {
|
2020-08-21 04:16:56 +02:00
|
|
|
Close(IOOptions(), nullptr).PermitUncheckedError();
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSWritableFile::Append(const Slice& data, const IOOptions&,
|
|
|
|
IODebugContext*) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
state_.buffer_.append(data.data(), data.size());
|
|
|
|
state_.pos_ += data.size();
|
|
|
|
fs_->WritableFileAppended(state_);
|
2020-12-17 20:51:04 +01:00
|
|
|
IOStatus io_s = fs_->InjectWriteError(state_.filename_);
|
|
|
|
return io_s;
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 07:18:33 +01:00
|
|
|
// By setting the IngestDataCorruptionBeforeWrite(), the data corruption is
|
|
|
|
// simulated.
|
|
|
|
IOStatus TestFSWritableFile::Append(
|
|
|
|
const Slice& data, const IOOptions&,
|
|
|
|
const DataVerificationInfo& verification_info, IODebugContext*) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
if (fs_->ShouldDataCorruptionBeforeWrite()) {
|
|
|
|
return IOStatus::Corruption("Data is corrupted!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the checksum
|
|
|
|
std::string checksum;
|
|
|
|
CalculateTypedChecksum(fs_->GetChecksumHandoffFuncType(), data.data(),
|
|
|
|
data.size(), &checksum);
|
|
|
|
if (fs_->GetChecksumHandoffFuncType() != ChecksumType::kNoChecksum &&
|
|
|
|
checksum != verification_info.checksum.ToString()) {
|
|
|
|
std::string msg = "Data is corrupted! Origin data checksum: " +
|
|
|
|
verification_info.checksum.ToString() +
|
|
|
|
"current data checksum: " + checksum;
|
|
|
|
return IOStatus::Corruption(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
state_.buffer_.append(data.data(), data.size());
|
|
|
|
state_.pos_ += data.size();
|
|
|
|
fs_->WritableFileAppended(state_);
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus TestFSWritableFile::Close(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = fs_->InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
writable_file_opened_ = false;
|
|
|
|
IOStatus io_s;
|
|
|
|
io_s = target_->Append(state_.buffer_, options, dbg);
|
|
|
|
if (io_s.ok()) {
|
|
|
|
state_.buffer_.resize(0);
|
2020-08-21 04:16:56 +02:00
|
|
|
// Ignore sync errors
|
|
|
|
target_->Sync(options, dbg).PermitUncheckedError();
|
2020-03-04 21:30:34 +01:00
|
|
|
io_s = target_->Close(options, dbg);
|
|
|
|
}
|
|
|
|
if (io_s.ok()) {
|
|
|
|
fs_->WritableFileClosed(state_);
|
2021-04-28 19:57:11 +02:00
|
|
|
IOStatus in_s = fs_->InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSWritableFile::Flush(const IOOptions&, IODebugContext*) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
2020-08-21 04:16:56 +02:00
|
|
|
if (fs_->IsFilesystemActive()) {
|
2020-03-04 21:30:34 +01:00
|
|
|
state_.pos_at_last_flush_ = state_.pos_;
|
|
|
|
}
|
2020-08-21 04:16:56 +02:00
|
|
|
return IOStatus::OK();
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSWritableFile::Sync(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
IOStatus io_s = target_->Append(state_.buffer_, options, dbg);
|
|
|
|
state_.buffer_.resize(0);
|
2020-08-21 04:16:56 +02:00
|
|
|
// Ignore sync errors
|
|
|
|
target_->Sync(options, dbg).PermitUncheckedError();
|
2020-03-04 21:30:34 +01:00
|
|
|
state_.pos_at_last_sync_ = state_.pos_;
|
|
|
|
fs_->WritableFileSynced(state_);
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestFSRandomRWFile::TestFSRandomRWFile(const std::string& /*fname*/,
|
|
|
|
std::unique_ptr<FSRandomRWFile>&& f,
|
|
|
|
FaultInjectionTestFS* fs)
|
|
|
|
: target_(std::move(f)), file_opened_(true), fs_(fs) {
|
|
|
|
assert(target_ != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestFSRandomRWFile::~TestFSRandomRWFile() {
|
|
|
|
if (file_opened_) {
|
2020-08-21 04:16:56 +02:00
|
|
|
Close(IOOptions(), nullptr).PermitUncheckedError();
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomRWFile::Write(uint64_t offset, const Slice& data,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
return target_->Write(offset, data, options, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomRWFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options, Slice* result,
|
|
|
|
char* scratch, IODebugContext* dbg) const {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
return target_->Read(offset, n, options, result, scratch, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomRWFile::Close(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
file_opened_ = false;
|
|
|
|
return target_->Close(options, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomRWFile::Flush(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
return target_->Flush(options, dbg);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomRWFile::Sync(const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
return target_->Sync(options, dbg);
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:18:56 +02:00
|
|
|
TestFSRandomAccessFile::TestFSRandomAccessFile(const std::string& /*fname*/,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>&& f,
|
|
|
|
FaultInjectionTestFS* fs)
|
|
|
|
: target_(std::move(f)), fs_(fs) {
|
|
|
|
assert(target_ != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus TestFSRandomAccessFile::Read(uint64_t offset, size_t n,
|
|
|
|
const IOOptions& options, Slice* result,
|
|
|
|
char* scratch, IODebugContext* dbg) const {
|
|
|
|
if (!fs_->IsFilesystemActive()) {
|
|
|
|
return fs_->GetError();
|
|
|
|
}
|
|
|
|
IOStatus s = target_->Read(offset, n, options, result, scratch, dbg);
|
|
|
|
if (s.ok()) {
|
|
|
|
s = fs_->InjectError(FaultInjectionTestFS::ErrorOperation::kRead, result,
|
2020-04-30 04:25:56 +02:00
|
|
|
use_direct_io(), scratch);
|
2020-04-11 02:18:56 +02:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus FaultInjectionTestFS::NewDirectory(
|
|
|
|
const std::string& name, const IOOptions& options,
|
|
|
|
std::unique_ptr<FSDirectory>* result, IODebugContext* dbg) {
|
|
|
|
std::unique_ptr<FSDirectory> r;
|
|
|
|
IOStatus io_s = target()->NewDirectory(name, options, &r, dbg);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
result->reset(
|
|
|
|
new TestFSDirectory(this, TestFSTrimDirname(name), r.release()));
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::NewWritableFile(
|
|
|
|
const std::string& fname, const FileOptions& file_opts,
|
|
|
|
std::unique_ptr<FSWritableFile>* result, IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 02:18:56 +02:00
|
|
|
if (IsFilesystemDirectWritable()) {
|
|
|
|
return target()->NewWritableFile(fname, file_opts, result, dbg);
|
|
|
|
}
|
2020-04-16 20:10:53 +02:00
|
|
|
|
|
|
|
IOStatus io_s = target()->NewWritableFile(fname, file_opts, result, dbg);
|
2020-03-04 21:30:34 +01:00
|
|
|
if (io_s.ok()) {
|
2021-02-11 07:18:33 +01:00
|
|
|
result->reset(
|
|
|
|
new TestFSWritableFile(fname, file_opts, std::move(*result), this));
|
2020-03-04 21:30:34 +01:00
|
|
|
// WritableFileWriter* file is opened
|
|
|
|
// again then it will be truncated - so forget our saved state.
|
|
|
|
UntrackFile(fname);
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
open_files_.insert(fname);
|
|
|
|
auto dir_and_name = TestFSGetDirAndName(fname);
|
|
|
|
auto& list = dir_to_new_files_since_last_sync_[dir_and_name.first];
|
|
|
|
list.insert(dir_and_name.second);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::ReopenWritableFile(
|
|
|
|
const std::string& fname, const FileOptions& file_opts,
|
|
|
|
std::unique_ptr<FSWritableFile>* result, IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2020-04-11 02:18:56 +02:00
|
|
|
if (IsFilesystemDirectWritable()) {
|
|
|
|
return target()->ReopenWritableFile(fname, file_opts, result, dbg);
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus io_s = target()->ReopenWritableFile(fname, file_opts, result, dbg);
|
|
|
|
if (io_s.ok()) {
|
2021-02-11 07:18:33 +01:00
|
|
|
result->reset(
|
|
|
|
new TestFSWritableFile(fname, file_opts, std::move(*result), this));
|
2020-03-04 21:30:34 +01:00
|
|
|
// WritableFileWriter* file is opened
|
|
|
|
// again then it will be truncated - so forget our saved state.
|
|
|
|
UntrackFile(fname);
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
open_files_.insert(fname);
|
|
|
|
auto dir_and_name = TestFSGetDirAndName(fname);
|
|
|
|
auto& list = dir_to_new_files_since_last_sync_[dir_and_name.first];
|
|
|
|
list.insert(dir_and_name.second);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::NewRandomRWFile(
|
|
|
|
const std::string& fname, const FileOptions& file_opts,
|
|
|
|
std::unique_ptr<FSRandomRWFile>* result, IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2020-04-11 02:18:56 +02:00
|
|
|
if (IsFilesystemDirectWritable()) {
|
|
|
|
return target()->NewRandomRWFile(fname, file_opts, result, dbg);
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus io_s = target()->NewRandomRWFile(fname, file_opts, result, dbg);
|
|
|
|
if (io_s.ok()) {
|
|
|
|
result->reset(new TestFSRandomRWFile(fname, std::move(*result), this));
|
|
|
|
// WritableFileWriter* file is opened
|
|
|
|
// again then it will be truncated - so forget our saved state.
|
|
|
|
UntrackFile(fname);
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
open_files_.insert(fname);
|
|
|
|
auto dir_and_name = TestFSGetDirAndName(fname);
|
|
|
|
auto& list = dir_to_new_files_since_last_sync_[dir_and_name.first];
|
|
|
|
list.insert(dir_and_name.second);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::NewRandomAccessFile(
|
|
|
|
const std::string& fname, const FileOptions& file_opts,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>* result, IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2020-04-30 04:25:56 +02:00
|
|
|
IOStatus io_s = InjectError(ErrorOperation::kOpen, nullptr, false, nullptr);
|
2020-04-11 02:18:56 +02:00
|
|
|
if (io_s.ok()) {
|
|
|
|
io_s = target()->NewRandomAccessFile(fname, file_opts, result, dbg);
|
|
|
|
}
|
|
|
|
if (io_s.ok()) {
|
|
|
|
result->reset(new TestFSRandomAccessFile(fname, std::move(*result), this));
|
|
|
|
}
|
|
|
|
return io_s;
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::DeleteFile(const std::string& f,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus io_s = FileSystemWrapper::DeleteFile(f, options, dbg);
|
|
|
|
if (io_s.ok()) {
|
|
|
|
UntrackFile(f);
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::RenameFile(const std::string& s,
|
|
|
|
const std::string& t,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) {
|
|
|
|
if (!IsFilesystemActive()) {
|
|
|
|
return GetError();
|
|
|
|
}
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
IOStatus io_s = FileSystemWrapper::RenameFile(s, t, options, dbg);
|
|
|
|
|
|
|
|
if (io_s.ok()) {
|
2021-04-28 19:57:11 +02:00
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (db_file_state_.find(s) != db_file_state_.end()) {
|
|
|
|
db_file_state_[t] = db_file_state_[s];
|
|
|
|
db_file_state_.erase(s);
|
|
|
|
}
|
2020-03-04 21:30:34 +01:00
|
|
|
|
2021-04-28 19:57:11 +02:00
|
|
|
auto sdn = TestFSGetDirAndName(s);
|
|
|
|
auto tdn = TestFSGetDirAndName(t);
|
|
|
|
if (dir_to_new_files_since_last_sync_[sdn.first].erase(sdn.second) != 0) {
|
|
|
|
auto& tlist = dir_to_new_files_since_last_sync_[tdn.first];
|
|
|
|
assert(tlist.find(tdn.second) == tlist.end());
|
|
|
|
tlist.insert(tdn.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IOStatus in_s = InjectMetadataWriteError();
|
|
|
|
if (!in_s.ok()) {
|
|
|
|
return in_s;
|
2020-03-04 21:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultInjectionTestFS::WritableFileClosed(const FSFileState& state) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (open_files_.find(state.filename_) != open_files_.end()) {
|
|
|
|
db_file_state_[state.filename_] = state;
|
|
|
|
open_files_.erase(state.filename_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultInjectionTestFS::WritableFileSynced(const FSFileState& state) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (open_files_.find(state.filename_) != open_files_.end()) {
|
|
|
|
if (db_file_state_.find(state.filename_) == db_file_state_.end()) {
|
|
|
|
db_file_state_.insert(std::make_pair(state.filename_, state));
|
|
|
|
} else {
|
|
|
|
db_file_state_[state.filename_] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultInjectionTestFS::WritableFileAppended(const FSFileState& state) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (open_files_.find(state.filename_) != open_files_.end()) {
|
|
|
|
if (db_file_state_.find(state.filename_) == db_file_state_.end()) {
|
|
|
|
db_file_state_.insert(std::make_pair(state.filename_, state));
|
|
|
|
} else {
|
|
|
|
db_file_state_[state.filename_] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::DropUnsyncedFileData() {
|
|
|
|
IOStatus io_s;
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
for (std::map<std::string, FSFileState>::iterator it = db_file_state_.begin();
|
|
|
|
io_s.ok() && it != db_file_state_.end(); ++it) {
|
|
|
|
FSFileState& fs_state = it->second;
|
|
|
|
if (!fs_state.IsFullySynced()) {
|
|
|
|
io_s = fs_state.DropUnsyncedData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::DropRandomUnsyncedFileData(Random* rnd) {
|
|
|
|
IOStatus io_s;
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
for (std::map<std::string, FSFileState>::iterator it = db_file_state_.begin();
|
|
|
|
io_s.ok() && it != db_file_state_.end(); ++it) {
|
|
|
|
FSFileState& fs_state = it->second;
|
|
|
|
if (!fs_state.IsFullySynced()) {
|
|
|
|
io_s = fs_state.DropRandomUnsyncedData(rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus FaultInjectionTestFS::DeleteFilesCreatedAfterLastDirSync(
|
|
|
|
const IOOptions& options, IODebugContext* dbg) {
|
|
|
|
// Because DeleteFile access this container make a copy to avoid deadlock
|
|
|
|
std::map<std::string, std::set<std::string>> map_copy;
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
map_copy.insert(dir_to_new_files_since_last_sync_.begin(),
|
|
|
|
dir_to_new_files_since_last_sync_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& pair : map_copy) {
|
|
|
|
for (std::string name : pair.second) {
|
|
|
|
IOStatus io_s = DeleteFile(pair.first + "/" + name, options, dbg);
|
|
|
|
if (!io_s.ok()) {
|
|
|
|
return io_s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultInjectionTestFS::ResetState() {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
db_file_state_.clear();
|
|
|
|
dir_to_new_files_since_last_sync_.clear();
|
|
|
|
SetFilesystemActiveNoLock(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultInjectionTestFS::UntrackFile(const std::string& f) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
auto dir_and_name = TestFSGetDirAndName(f);
|
|
|
|
dir_to_new_files_since_last_sync_[dir_and_name.first].erase(
|
|
|
|
dir_and_name.second);
|
|
|
|
db_file_state_.erase(f);
|
|
|
|
open_files_.erase(f);
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:18:56 +02:00
|
|
|
IOStatus FaultInjectionTestFS::InjectError(ErrorOperation op,
|
|
|
|
Slice* result,
|
2020-04-30 04:25:56 +02:00
|
|
|
bool direct_io,
|
2020-04-11 02:18:56 +02:00
|
|
|
char* scratch) {
|
|
|
|
ErrorContext* ctx =
|
|
|
|
static_cast<ErrorContext*>(thread_local_error_->Get());
|
|
|
|
if (ctx == nullptr || !ctx->enable_error_injection || !ctx->one_in) {
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->rand.OneIn(ctx->one_in)) {
|
|
|
|
ctx->count++;
|
2020-04-14 20:04:39 +02:00
|
|
|
if (ctx->callstack) {
|
|
|
|
free(ctx->callstack);
|
|
|
|
}
|
2020-04-11 02:18:56 +02:00
|
|
|
ctx->callstack = port::SaveStack(&ctx->frames);
|
|
|
|
switch (op) {
|
|
|
|
case kRead:
|
|
|
|
{
|
2020-04-30 04:25:56 +02:00
|
|
|
if (!direct_io) {
|
|
|
|
ctx->type =
|
|
|
|
static_cast<ErrorType>(ctx->rand.Uniform(ErrorType::kErrorTypeMax));
|
|
|
|
} else {
|
|
|
|
// In Direct IO mode, the actual read will read extra data due to
|
|
|
|
// alignment restrictions. So don't inject corruption or
|
|
|
|
// truncated reads as we don't know if it will actually cause a
|
|
|
|
// detectable error
|
|
|
|
ctx->type = ErrorType::kErrorTypeStatus;
|
|
|
|
}
|
|
|
|
switch (ctx->type) {
|
2020-04-11 02:18:56 +02:00
|
|
|
// Inject IO error
|
2020-04-24 22:03:08 +02:00
|
|
|
case ErrorType::kErrorTypeStatus:
|
2020-04-11 02:18:56 +02:00
|
|
|
return IOStatus::IOError();
|
|
|
|
// Inject random corruption
|
2020-04-24 22:03:08 +02:00
|
|
|
case ErrorType::kErrorTypeCorruption:
|
2020-04-11 02:18:56 +02:00
|
|
|
{
|
|
|
|
if (result->data() == scratch) {
|
|
|
|
uint64_t offset = ctx->rand.Uniform((uint32_t)result->size());
|
|
|
|
uint64_t len =
|
|
|
|
std::min<uint64_t>(result->size() - offset, 64UL);
|
|
|
|
assert(offset < result->size());
|
|
|
|
assert(offset + len <= result->size());
|
2020-04-30 04:25:56 +02:00
|
|
|
std::string str;
|
|
|
|
// The randomly generated string could be identical to the
|
|
|
|
// original one, so retry
|
|
|
|
do {
|
2020-07-09 23:33:42 +02:00
|
|
|
str = ctx->rand.RandomString(static_cast<int>(len));
|
2020-04-30 04:25:56 +02:00
|
|
|
} while (str == std::string(scratch + offset, len));
|
2020-04-11 02:18:56 +02:00
|
|
|
memcpy(scratch + offset, str.data(), len);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
FALLTHROUGH_INTENDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Truncate the result
|
2020-04-24 22:03:08 +02:00
|
|
|
case ErrorType::kErrorTypeTruncated:
|
2020-04-11 02:18:56 +02:00
|
|
|
{
|
|
|
|
assert(result->size() > 0);
|
|
|
|
uint64_t offset = ctx->rand.Uniform((uint32_t)result->size());
|
|
|
|
assert(offset < result->size());
|
|
|
|
*result = Slice(result->data(), offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kOpen:
|
|
|
|
return IOStatus::IOError();
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
2020-12-17 20:51:04 +01:00
|
|
|
IOStatus FaultInjectionTestFS::InjectWriteError(const std::string& file_name) {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (!enable_write_error_injection_ || !write_error_one_in_) {
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
bool allowed_type = false;
|
|
|
|
|
|
|
|
uint64_t number;
|
|
|
|
FileType cur_type = kTempFile;
|
|
|
|
std::size_t found = file_name.find_last_of("/");
|
|
|
|
std::string file = file_name.substr(found);
|
|
|
|
bool ret = ParseFileName(file, &number, &cur_type);
|
|
|
|
if (ret) {
|
|
|
|
for (const auto& type : write_error_allowed_types_) {
|
|
|
|
if (cur_type == type) {
|
|
|
|
allowed_type = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allowed_type) {
|
|
|
|
if (write_error_rand_.OneIn(write_error_one_in_)) {
|
|
|
|
return GetError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
2021-04-28 19:57:11 +02:00
|
|
|
IOStatus FaultInjectionTestFS::InjectMetadataWriteError() {
|
|
|
|
MutexLock l(&mutex_);
|
|
|
|
if (!enable_metadata_write_error_injection_ ||
|
|
|
|
!metadata_write_error_one_in_ ||
|
|
|
|
!write_error_rand_.OneIn(metadata_write_error_one_in_)) {
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
return IOStatus::IOError();
|
|
|
|
}
|
|
|
|
|
2020-04-11 02:18:56 +02:00
|
|
|
void FaultInjectionTestFS::PrintFaultBacktrace() {
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
ErrorContext* ctx =
|
|
|
|
static_cast<ErrorContext*>(thread_local_error_->Get());
|
|
|
|
if (ctx == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 22:03:08 +02:00
|
|
|
fprintf(stderr, "Injected error type = %d\n", ctx->type);
|
2020-04-11 02:18:56 +02:00
|
|
|
port::PrintAndFreeStack(ctx->callstack, ctx->frames);
|
2020-04-14 20:04:39 +02:00
|
|
|
ctx->callstack = nullptr;
|
2020-04-11 02:18:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:30:34 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|