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.
222 lines
7.0 KiB
222 lines
7.0 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// 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).
|
||
9 years ago
|
//
|
||
|
// 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 Env to keep track of the state of a filesystem as of
|
||
|
// the last "sync". It then checks for data loss errors by purposely dropping
|
||
|
// file data (or entire files) not protected by a "sync".
|
||
|
|
||
6 years ago
|
#pragma once
|
||
9 years ago
|
|
||
|
#include <map>
|
||
|
#include <set>
|
||
|
#include <string>
|
||
|
|
||
6 years ago
|
#include "file/filename.h"
|
||
9 years ago
|
#include "rocksdb/env.h"
|
||
|
#include "util/mutexlock.h"
|
||
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
5 years ago
|
class Random;
|
||
9 years ago
|
class TestWritableFile;
|
||
|
class FaultInjectionTestEnv;
|
||
|
|
||
|
struct FileState {
|
||
|
std::string filename_;
|
||
|
ssize_t pos_;
|
||
|
ssize_t pos_at_last_sync_;
|
||
|
ssize_t pos_at_last_flush_;
|
||
|
|
||
|
explicit FileState(const std::string& filename)
|
||
|
: filename_(filename),
|
||
|
pos_(-1),
|
||
|
pos_at_last_sync_(-1),
|
||
|
pos_at_last_flush_(-1) {}
|
||
|
|
||
|
FileState() : pos_(-1), pos_at_last_sync_(-1), pos_at_last_flush_(-1) {}
|
||
|
|
||
|
bool IsFullySynced() const { return pos_ <= 0 || pos_ == pos_at_last_sync_; }
|
||
|
|
||
|
Status DropUnsyncedData(Env* env) const;
|
||
|
|
||
|
Status DropRandomUnsyncedData(Env* env, Random* rand) const;
|
||
|
};
|
||
|
|
||
|
// A wrapper around WritableFileWriter* file
|
||
|
// is written to or sync'ed.
|
||
|
class TestWritableFile : public WritableFile {
|
||
|
public:
|
||
|
explicit TestWritableFile(const std::string& fname,
|
||
6 years ago
|
std::unique_ptr<WritableFile>&& f,
|
||
9 years ago
|
FaultInjectionTestEnv* env);
|
||
|
virtual ~TestWritableFile();
|
||
|
virtual Status Append(const Slice& data) override;
|
||
|
virtual Status Truncate(uint64_t size) override {
|
||
|
return target_->Truncate(size);
|
||
|
}
|
||
|
virtual Status Close() override;
|
||
|
virtual Status Flush() override;
|
||
|
virtual Status Sync() override;
|
||
|
virtual bool IsSyncThreadSafe() const override { return true; }
|
||
7 years ago
|
virtual Status PositionedAppend(const Slice& data,
|
||
|
uint64_t offset) override {
|
||
|
return target_->PositionedAppend(data, offset);
|
||
|
}
|
||
|
virtual bool use_direct_io() const override {
|
||
|
return target_->use_direct_io();
|
||
|
};
|
||
9 years ago
|
|
||
|
private:
|
||
|
FileState state_;
|
||
6 years ago
|
std::unique_ptr<WritableFile> target_;
|
||
9 years ago
|
bool writable_file_opened_;
|
||
|
FaultInjectionTestEnv* env_;
|
||
|
};
|
||
|
|
||
6 years ago
|
// A wrapper around WritableFileWriter* file
|
||
|
// is written to or sync'ed.
|
||
|
class TestRandomRWFile : public RandomRWFile {
|
||
|
public:
|
||
|
explicit TestRandomRWFile(const std::string& fname,
|
||
|
std::unique_ptr<RandomRWFile>&& f,
|
||
|
FaultInjectionTestEnv* env);
|
||
|
virtual ~TestRandomRWFile();
|
||
|
Status Write(uint64_t offset, const Slice& data) override;
|
||
|
Status Read(uint64_t offset, size_t n, Slice* result,
|
||
|
char* scratch) const override;
|
||
|
Status Close() override;
|
||
|
Status Flush() override;
|
||
|
Status Sync() override;
|
||
|
size_t GetRequiredBufferAlignment() const override {
|
||
|
return target_->GetRequiredBufferAlignment();
|
||
|
}
|
||
|
bool use_direct_io() const override { return target_->use_direct_io(); };
|
||
|
|
||
|
private:
|
||
|
std::unique_ptr<RandomRWFile> target_;
|
||
|
bool file_opened_;
|
||
|
FaultInjectionTestEnv* env_;
|
||
|
};
|
||
|
|
||
9 years ago
|
class TestDirectory : public Directory {
|
||
|
public:
|
||
|
explicit TestDirectory(FaultInjectionTestEnv* env, std::string dirname,
|
||
|
Directory* dir)
|
||
|
: env_(env), dirname_(dirname), dir_(dir) {}
|
||
|
~TestDirectory() {}
|
||
|
|
||
|
virtual Status Fsync() override;
|
||
|
|
||
|
private:
|
||
|
FaultInjectionTestEnv* env_;
|
||
|
std::string dirname_;
|
||
6 years ago
|
std::unique_ptr<Directory> dir_;
|
||
9 years ago
|
};
|
||
|
|
||
|
class FaultInjectionTestEnv : public EnvWrapper {
|
||
|
public:
|
||
|
explicit FaultInjectionTestEnv(Env* base)
|
||
|
: EnvWrapper(base), filesystem_active_(true) {}
|
||
|
virtual ~FaultInjectionTestEnv() {}
|
||
|
|
||
|
Status NewDirectory(const std::string& name,
|
||
6 years ago
|
std::unique_ptr<Directory>* result) override;
|
||
9 years ago
|
|
||
|
Status NewWritableFile(const std::string& fname,
|
||
6 years ago
|
std::unique_ptr<WritableFile>* result,
|
||
9 years ago
|
const EnvOptions& soptions) override;
|
||
|
|
||
6 years ago
|
Status ReopenWritableFile(const std::string& fname,
|
||
6 years ago
|
std::unique_ptr<WritableFile>* result,
|
||
6 years ago
|
const EnvOptions& soptions) override;
|
||
|
|
||
6 years ago
|
Status NewRandomRWFile(const std::string& fname,
|
||
|
std::unique_ptr<RandomRWFile>* result,
|
||
|
const EnvOptions& soptions) override;
|
||
|
|
||
6 years ago
|
Status NewRandomAccessFile(const std::string& fname,
|
||
|
std::unique_ptr<RandomAccessFile>* result,
|
||
|
const EnvOptions& soptions) override;
|
||
|
|
||
9 years ago
|
virtual Status DeleteFile(const std::string& f) override;
|
||
|
|
||
|
virtual Status RenameFile(const std::string& s,
|
||
|
const std::string& t) override;
|
||
|
|
||
6 years ago
|
// Undef to eliminate clash on Windows
|
||
|
#undef GetFreeSpace
|
||
6 years ago
|
virtual Status GetFreeSpace(const std::string& path,
|
||
|
uint64_t* disk_free) override {
|
||
|
if (!IsFilesystemActive() && error_ == Status::NoSpace()) {
|
||
|
*disk_free = 0;
|
||
|
return Status::OK();
|
||
|
} else {
|
||
|
return target()->GetFreeSpace(path, disk_free);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void WritableFileClosed(const FileState& state);
|
||
|
|
||
6 years ago
|
void WritableFileSynced(const FileState& state);
|
||
|
|
||
|
void WritableFileAppended(const FileState& state);
|
||
|
|
||
9 years ago
|
// For every file that is not fully synced, make a call to `func` with
|
||
|
// FileState of the file as the parameter.
|
||
|
Status DropFileData(std::function<Status(Env*, FileState)> func);
|
||
|
|
||
|
Status DropUnsyncedFileData();
|
||
|
|
||
|
Status DropRandomUnsyncedFileData(Random* rnd);
|
||
|
|
||
|
Status DeleteFilesCreatedAfterLastDirSync();
|
||
|
|
||
|
void ResetState();
|
||
|
|
||
|
void UntrackFile(const std::string& f);
|
||
|
|
||
|
void SyncDir(const std::string& dirname) {
|
||
|
MutexLock l(&mutex_);
|
||
|
dir_to_new_files_since_last_sync_.erase(dirname);
|
||
|
}
|
||
|
|
||
|
// Setting the filesystem to inactive is the test equivalent to simulating a
|
||
|
// system reset. Setting to inactive will freeze our saved filesystem state so
|
||
|
// that it will stop being recorded. It can then be reset back to the state at
|
||
|
// the time of the reset.
|
||
|
bool IsFilesystemActive() {
|
||
|
MutexLock l(&mutex_);
|
||
|
return filesystem_active_;
|
||
|
}
|
||
7 years ago
|
void SetFilesystemActiveNoLock(bool active,
|
||
|
Status error = Status::Corruption("Not active")) {
|
||
|
filesystem_active_ = active;
|
||
|
if (!active) {
|
||
|
error_ = error;
|
||
|
}
|
||
|
}
|
||
|
void SetFilesystemActive(bool active,
|
||
|
Status error = Status::Corruption("Not active")) {
|
||
9 years ago
|
MutexLock l(&mutex_);
|
||
7 years ago
|
SetFilesystemActiveNoLock(active, error);
|
||
9 years ago
|
}
|
||
|
void AssertNoOpenFile() { assert(open_files_.empty()); }
|
||
7 years ago
|
Status GetError() { return error_; }
|
||
9 years ago
|
|
||
|
private:
|
||
|
port::Mutex mutex_;
|
||
|
std::map<std::string, FileState> db_file_state_;
|
||
|
std::set<std::string> open_files_;
|
||
|
std::unordered_map<std::string, std::set<std::string>>
|
||
|
dir_to_new_files_since_last_sync_;
|
||
|
bool filesystem_active_; // Record flushes, syncs, writes
|
||
7 years ago
|
Status error_;
|
||
9 years ago
|
};
|
||
|
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|