Fix some bugs in MockEnv

Summary:
Fixing some bugs in MockEnv so it be actually used.
Closes https://github.com/facebook/rocksdb/pull/1914

Differential Revision: D4609923

Pulled By: maysamyabandeh

fbshipit-source-id: ca25735
This commit is contained in:
Maysam Yabandeh 2017-03-13 09:41:30 -07:00 committed by Facebook Github Bot
parent 900c62be61
commit e6725e8c8d
14 changed files with 102 additions and 7 deletions

View file

@ -606,6 +606,7 @@ class BloomStatsTestWithParam
PlainTableOptions table_options; PlainTableOptions table_options;
options_.table_factory.reset(NewPlainTableFactory(table_options)); options_.table_factory.reset(NewPlainTableFactory(table_options));
} }
options_.env = env_;
perf_context.Reset(); perf_context.Reset();
DestroyAndReopen(options_); DestroyAndReopen(options_);

View file

@ -75,6 +75,7 @@ TEST_F(DBTestDynamicLevel, DynamicLevelMaxBytesBase) {
options.compression_per_level[0] = kNoCompression; options.compression_per_level[0] = kNoCompression;
options.compression_per_level[1] = kLZ4Compression; options.compression_per_level[1] = kLZ4Compression;
options.compression_per_level[2] = kSnappyCompression; options.compression_per_level[2] = kSnappyCompression;
options.env = env_;
DestroyAndReopen(options); DestroyAndReopen(options);

View file

@ -25,6 +25,7 @@ TEST_F(DBFlushTest, FlushWhileWritingManifest) {
Options options; Options options;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.max_background_flushes = 2; options.max_background_flushes = 2;
options.env = env_;
Reopen(options); Reopen(options);
FlushOptions no_wait; FlushOptions no_wait;
no_wait.wait = false; no_wait.wait = false;
@ -50,7 +51,7 @@ TEST_F(DBFlushTest, FlushWhileWritingManifest) {
TEST_F(DBFlushTest, SyncFail) { TEST_F(DBFlushTest, SyncFail) {
std::unique_ptr<FaultInjectionTestEnv> fault_injection_env( std::unique_ptr<FaultInjectionTestEnv> fault_injection_env(
new FaultInjectionTestEnv(Env::Default())); new FaultInjectionTestEnv(env_));
Options options; Options options;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.env = fault_injection_env.get(); options.env = fault_injection_env.get();

View file

@ -981,6 +981,7 @@ TEST_F(DBIteratorTest, PrevAfterAndNextAfterMerge) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.merge_operator = MergeOperators::CreatePutOperator(); options.merge_operator = MergeOperators::CreatePutOperator();
options.env = env_;
DestroyAndReopen(options); DestroyAndReopen(options);
// write three entries with different keys using Merge() // write three entries with different keys using Merge()

View file

@ -123,6 +123,7 @@ TEST_F(DBMemTableTest, InsertWithHint) {
options.memtable_factory.reset(new MockMemTableRepFactory()); options.memtable_factory.reset(new MockMemTableRepFactory());
options.memtable_insert_with_hint_prefix_extractor.reset( options.memtable_insert_with_hint_prefix_extractor.reset(
new TestPrefixExtractor()); new TestPrefixExtractor());
options.env = env_;
Reopen(options); Reopen(options);
MockMemTableRep* rep = MockMemTableRep* rep =
reinterpret_cast<MockMemTableRepFactory*>(options.memtable_factory.get()) reinterpret_cast<MockMemTableRepFactory*>(options.memtable_factory.get())

View file

@ -44,6 +44,7 @@ TEST_F(DBMergeOperatorTest, MergeErrorOnRead) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.merge_operator.reset(new TestPutOperator()); options.merge_operator.reset(new TestPutOperator());
options.env = env_;
Reopen(options); Reopen(options);
ASSERT_OK(Merge("k1", "v1")); ASSERT_OK(Merge("k1", "v1"));
ASSERT_OK(Merge("k1", "corrupted")); ASSERT_OK(Merge("k1", "corrupted"));
@ -57,6 +58,7 @@ TEST_F(DBMergeOperatorTest, MergeErrorOnWrite) {
options.create_if_missing = true; options.create_if_missing = true;
options.merge_operator.reset(new TestPutOperator()); options.merge_operator.reset(new TestPutOperator());
options.max_successive_merges = 3; options.max_successive_merges = 3;
options.env = env_;
Reopen(options); Reopen(options);
ASSERT_OK(Merge("k1", "v1")); ASSERT_OK(Merge("k1", "v1"));
ASSERT_OK(Merge("k1", "v2")); ASSERT_OK(Merge("k1", "v2"));
@ -71,6 +73,7 @@ TEST_F(DBMergeOperatorTest, MergeErrorOnIteration) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.merge_operator.reset(new TestPutOperator()); options.merge_operator.reset(new TestPutOperator());
options.env = env_;
DestroyAndReopen(options); DestroyAndReopen(options);
ASSERT_OK(Merge("k1", "v1")); ASSERT_OK(Merge("k1", "v1"));

View file

@ -62,6 +62,7 @@ class DBOptionsTest : public DBTestBase {
std::unordered_map<std::string, std::string> GetRandomizedMutableCFOptionsMap( std::unordered_map<std::string, std::string> GetRandomizedMutableCFOptionsMap(
Random* rnd) { Random* rnd) {
Options options; Options options;
options.env = env_;
ImmutableDBOptions db_options(options); ImmutableDBOptions db_options(options);
test::RandomInitCFOptions(&options, rnd); test::RandomInitCFOptions(&options, rnd);
auto sanitized_options = SanitizeOptions(db_options, options); auto sanitized_options = SanitizeOptions(db_options, options);
@ -87,6 +88,7 @@ TEST_F(DBOptionsTest, GetLatestDBOptions) {
// GetOptions should be able to get latest option changed by SetOptions. // GetOptions should be able to get latest option changed by SetOptions.
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.env = env_;
Random rnd(228); Random rnd(228);
Reopen(options); Reopen(options);
auto new_options = GetRandomizedMutableDBOptionsMap(&rnd); auto new_options = GetRandomizedMutableDBOptionsMap(&rnd);
@ -98,6 +100,7 @@ TEST_F(DBOptionsTest, GetLatestCFOptions) {
// GetOptions should be able to get latest option changed by SetOptions. // GetOptions should be able to get latest option changed by SetOptions.
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.env = env_;
Random rnd(228); Random rnd(228);
Reopen(options); Reopen(options);
CreateColumnFamilies({"foo"}, options); CreateColumnFamilies({"foo"}, options);
@ -118,6 +121,7 @@ TEST_F(DBOptionsTest, SetOptionsAndReopen) {
ASSERT_OK(dbfull()->SetOptions(rand_opts)); ASSERT_OK(dbfull()->SetOptions(rand_opts));
// Verify if DB can be reopen after setting options. // Verify if DB can be reopen after setting options.
Options options; Options options;
options.env = env_;
ASSERT_OK(TryReopen(options)); ASSERT_OK(TryReopen(options));
} }
@ -137,6 +141,7 @@ TEST_F(DBOptionsTest, EnableAutoCompactionAndTriggerStall) {
std::numeric_limits<uint64_t>::max(); std::numeric_limits<uint64_t>::max();
options.soft_pending_compaction_bytes_limit = options.soft_pending_compaction_bytes_limit =
std::numeric_limits<uint64_t>::max(); std::numeric_limits<uint64_t>::max();
options.env = env_;
DestroyAndReopen(options); DestroyAndReopen(options);
int i = 0; int i = 0;
@ -228,6 +233,7 @@ TEST_F(DBOptionsTest, SetOptionsMayTriggerCompaction) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.level0_file_num_compaction_trigger = 1000; options.level0_file_num_compaction_trigger = 1000;
options.env = env_;
Reopen(options); Reopen(options);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
// Need to insert two keys to avoid trivial move. // Need to insert two keys to avoid trivial move.
@ -247,6 +253,7 @@ TEST_F(DBOptionsTest, SetBackgroundCompactionThreads) {
options.create_if_missing = true; options.create_if_missing = true;
options.base_background_compactions = 1; // default value options.base_background_compactions = 1; // default value
options.max_background_compactions = 1; // default value options.max_background_compactions = 1; // default value
options.env = env_;
Reopen(options); Reopen(options);
ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed()); ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
ASSERT_OK(dbfull()->SetDBOptions({{"base_background_compactions", "2"}, ASSERT_OK(dbfull()->SetDBOptions({{"base_background_compactions", "2"},
@ -260,6 +267,7 @@ TEST_F(DBOptionsTest, AvoidFlushDuringShutdown) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.env = env_;
WriteOptions write_without_wal; WriteOptions write_without_wal;
write_without_wal.disableWAL = true; write_without_wal.disableWAL = true;
@ -282,6 +290,7 @@ TEST_F(DBOptionsTest, SetDelayedWriteRateOption) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.delayed_write_rate = 2 * 1024U * 1024U; options.delayed_write_rate = 2 * 1024U * 1024U;
options.env = env_;
Reopen(options); Reopen(options);
ASSERT_EQ(2 * 1024U * 1024U, dbfull()->TEST_write_controler().max_delayed_write_rate()); ASSERT_EQ(2 * 1024U * 1024U, dbfull()->TEST_write_controler().max_delayed_write_rate());
@ -297,6 +306,7 @@ TEST_F(DBOptionsTest, MaxTotalWalSizeChange) {
Options options; Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.env = env_;
CreateColumnFamilies({"1", "2", "3"}, options); CreateColumnFamilies({"1", "2", "3"}, options);
ReopenWithColumnFamilies({"default", "1", "2", "3"}, options); ReopenWithColumnFamilies({"default", "1", "2", "3"}, options);
@ -326,7 +336,7 @@ static void assert_candidate_files_empty(DBImpl* dbfull, const bool empty) {
} }
TEST_F(DBOptionsTest, DeleteObsoleteFilesPeriodChange) { TEST_F(DBOptionsTest, DeleteObsoleteFilesPeriodChange) {
SpecialEnv env(Env::Default()); SpecialEnv env(env_);
env.time_elapse_only_sleep_ = true; env.time_elapse_only_sleep_ = true;
Options options; Options options;
options.env = &env; options.env = &env;

View file

@ -1156,6 +1156,7 @@ TEST_F(DBPropertiesTest, TablePropertiesNeedCompactTest) {
options.max_bytes_for_level_multiplier = 4; options.max_bytes_for_level_multiplier = 4;
options.soft_pending_compaction_bytes_limit = 1024 * 1024; options.soft_pending_compaction_bytes_limit = 1024 * 1024;
options.num_levels = 8; options.num_levels = 8;
options.env = env_;
std::shared_ptr<TablePropertiesCollectorFactory> collector_factory = std::shared_ptr<TablePropertiesCollectorFactory> collector_factory =
std::make_shared<CountingDeleteTabPropCollectorFactory>(); std::make_shared<CountingDeleteTabPropCollectorFactory>();
@ -1227,6 +1228,7 @@ TEST_F(DBPropertiesTest, NeedCompactHintPersistentTest) {
options.level0_slowdown_writes_trigger = 10; options.level0_slowdown_writes_trigger = 10;
options.level0_stop_writes_trigger = 10; options.level0_stop_writes_trigger = 10;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.env = env_;
std::shared_ptr<TablePropertiesCollectorFactory> collector_factory = std::shared_ptr<TablePropertiesCollectorFactory> collector_factory =
std::make_shared<CountingDeleteTabPropCollectorFactory>(); std::make_shared<CountingDeleteTabPropCollectorFactory>();

View file

@ -147,6 +147,7 @@ TEST_F(DBTablePropertiesTest, GetPropertiesOfTablesInRange) {
options.max_bytes_for_level_multiplier = 4; options.max_bytes_for_level_multiplier = 4;
options.hard_pending_compaction_bytes_limit = 16 * 1024; options.hard_pending_compaction_bytes_limit = 16 * 1024;
options.num_levels = 8; options.num_levels = 8;
options.env = env_;
DestroyAndReopen(options); DestroyAndReopen(options);

View file

@ -126,6 +126,7 @@ TEST_F(DBTestTailingIterator, TailingIteratorTrimSeekToNext) {
options.write_buffer_size = k150KB; options.write_buffer_size = k150KB;
options.max_write_buffer_number = 3; options.max_write_buffer_number = 3;
options.min_write_buffer_number_to_merge = 2; options.min_write_buffer_number_to_merge = 2;
options.env = env_;
CreateAndReopenWithCF({"pikachu"}, options); CreateAndReopenWithCF({"pikachu"}, options);
ReadOptions read_options; ReadOptions read_options;
read_options.tailing = true; read_options.tailing = true;
@ -298,7 +299,6 @@ TEST_F(DBTestTailingIterator, TailingIteratorPrefixSeek) {
read_options.tailing = true; read_options.tailing = true;
Options options = CurrentOptions(); Options options = CurrentOptions();
options.env = env_;
options.create_if_missing = true; options.create_if_missing = true;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.prefix_extractor.reset(NewFixedPrefixTransform(2)); options.prefix_extractor.reset(NewFixedPrefixTransform(2));
@ -616,7 +616,6 @@ TEST_F(DBTestTailingIterator, ManagedTailingIteratorPrefixSeek) {
read_options.managed = true; read_options.managed = true;
Options options = CurrentOptions(); Options options = CurrentOptions();
options.env = env_;
options.create_if_missing = true; options.create_if_missing = true;
options.disable_auto_compactions = true; options.disable_auto_compactions = true;
options.prefix_extractor.reset(NewFixedPrefixTransform(2)); options.prefix_extractor.reset(NewFixedPrefixTransform(2));

View file

@ -52,6 +52,7 @@ DBTestBase::DBTestBase(const std::string path)
alternative_wal_dir_ = dbname_ + "/wal"; alternative_wal_dir_ = dbname_ + "/wal";
alternative_db_log_dir_ = dbname_ + "/db_log_dir"; alternative_db_log_dir_ = dbname_ + "/db_log_dir";
auto options = CurrentOptions(); auto options = CurrentOptions();
options.env = env_;
auto delete_options = options; auto delete_options = options;
delete_options.wal_dir = alternative_wal_dir_; delete_options.wal_dir = alternative_wal_dir_;
EXPECT_OK(DestroyDB(dbname_, delete_options)); EXPECT_OK(DestroyDB(dbname_, delete_options));
@ -72,6 +73,7 @@ DBTestBase::~DBTestBase() {
options.db_paths.emplace_back(dbname_ + "_2", 0); options.db_paths.emplace_back(dbname_ + "_2", 0);
options.db_paths.emplace_back(dbname_ + "_3", 0); options.db_paths.emplace_back(dbname_ + "_3", 0);
options.db_paths.emplace_back(dbname_ + "_4", 0); options.db_paths.emplace_back(dbname_ + "_4", 0);
options.env = env_;
if (getenv("KEEP_DB")) { if (getenv("KEEP_DB")) {
printf("DB is still at %s\n", dbname_.c_str()); printf("DB is still at %s\n", dbname_.c_str());

View file

@ -14,9 +14,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#ifndef OS_WIN
#include <unistd.h>
#endif
#include <algorithm> #include <algorithm>
#include <map> #include <map>

View file

@ -116,6 +116,17 @@ class MemFile {
return Status::OK(); return Status::OK();
} }
Status Write(uint64_t offset, const Slice& data) {
MutexLock lock(&mutex_);
if (offset + data.size() > data_.size()) {
data_.resize(offset + data.size());
}
data_.replace(offset, data.size(), data.data(), data.size());
size_ = data_.size();
modified_time_ = Now();
return Status::OK();
}
Status Append(const Slice& data) { Status Append(const Slice& data) {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
data_.append(data.data(), data.size()); data_.append(data.data(), data.size());
@ -223,6 +234,31 @@ class MockRandomAccessFile : public RandomAccessFile {
MemFile* file_; MemFile* file_;
}; };
class MockRandomRWFile : public RandomRWFile {
public:
explicit MockRandomRWFile(MemFile* file) : file_(file) { file_->Ref(); }
~MockRandomRWFile() { file_->Unref(); }
virtual Status Write(uint64_t offset, const Slice& data) override {
return file_->Write(offset, data);
}
virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const override {
return file_->Read(offset, n, result, scratch);
}
virtual Status Close() override { return file_->Fsync(); }
virtual Status Flush() override { return Status::OK(); }
virtual Status Sync() override { return file_->Fsync(); }
private:
MemFile* file_;
};
class MockWritableFile : public WritableFile { class MockWritableFile : public WritableFile {
public: public:
MockWritableFile(MemFile* file, RateLimiter* rate_limiter) MockWritableFile(MemFile* file, RateLimiter* rate_limiter)
@ -248,6 +284,7 @@ class MockWritableFile : public WritableFile {
return Status::OK(); return Status::OK();
} }
virtual Status Truncate(uint64_t size) override { virtual Status Truncate(uint64_t size) override {
file_->Truncate(size);
return Status::OK(); return Status::OK();
} }
virtual Status Close() override { return file_->Fsync(); } virtual Status Close() override { return file_->Fsync(); }
@ -439,6 +476,35 @@ Status MockEnv::NewRandomAccessFile(const std::string& fname,
return Status::OK(); return Status::OK();
} }
Status MockEnv::NewRandomRWFile(const std::string& fname,
unique_ptr<RandomRWFile>* result,
const EnvOptions& soptions) {
auto fn = NormalizePath(fname);
MutexLock lock(&mutex_);
if (file_map_.find(fn) == file_map_.end()) {
*result = NULL;
return Status::IOError(fn, "File not found");
}
auto* f = file_map_[fn];
if (f->is_lock_file()) {
return Status::InvalidArgument(fn, "Cannot open a lock file.");
}
result->reset(new MockRandomRWFile(f));
return Status::OK();
}
Status MockEnv::ReuseWritableFile(const std::string& fname,
const std::string& old_fname,
unique_ptr<WritableFile>* result,
const EnvOptions& options) {
auto s = RenameFile(old_fname, fname);
if (!s.ok()) {
return s;
}
result->reset();
return NewWritableFile(fname, result, options);
}
Status MockEnv::NewWritableFile(const std::string& fname, Status MockEnv::NewWritableFile(const std::string& fname,
unique_ptr<WritableFile>* result, unique_ptr<WritableFile>* result,
const EnvOptions& env_options) { const EnvOptions& env_options) {
@ -598,6 +664,7 @@ Status MockEnv::LinkFile(const std::string& src, const std::string& dest) {
DeleteFileInternal(t); DeleteFileInternal(t);
file_map_[t] = file_map_[s]; file_map_[t] = file_map_[s];
file_map_[t]->Ref(); // Otherwise it might get deleted when noone uses s
return Status::OK(); return Status::OK();
} }

View file

@ -35,6 +35,15 @@ class MockEnv : public EnvWrapper {
unique_ptr<RandomAccessFile>* result, unique_ptr<RandomAccessFile>* result,
const EnvOptions& soptions) override; const EnvOptions& soptions) override;
virtual Status NewRandomRWFile(const std::string& fname,
unique_ptr<RandomRWFile>* result,
const EnvOptions& options) override;
virtual Status ReuseWritableFile(const std::string& fname,
const std::string& old_fname,
unique_ptr<WritableFile>* result,
const EnvOptions& options) override;
virtual Status NewWritableFile(const std::string& fname, virtual Status NewWritableFile(const std::string& fname,
unique_ptr<WritableFile>* result, unique_ptr<WritableFile>* result,
const EnvOptions& env_options) override; const EnvOptions& env_options) override;