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).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +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.
|
|
|
|
|
2015-09-14 23:44:35 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-07-09 21:33:42 +00:00
|
|
|
|
2019-08-16 23:40:09 +00:00
|
|
|
#include <cinttypes>
|
2020-07-09 21:33:42 +00:00
|
|
|
|
2019-05-31 18:52:59 +00:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2019-08-16 23:40:09 +00:00
|
|
|
#include "db/db_test_util.h"
|
2017-04-04 01:27:24 +00:00
|
|
|
#include "db/log_format.h"
|
|
|
|
#include "db/version_set.h"
|
2019-05-30 03:44:08 +00:00
|
|
|
#include "file/filename.h"
|
2020-11-21 02:38:47 +00:00
|
|
|
#include "port/stack_trace.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/cache.h"
|
2017-08-09 22:49:40 +00:00
|
|
|
#include "rocksdb/convenience.h"
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "rocksdb/db.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
2013-10-29 00:54:09 +00:00
|
|
|
#include "rocksdb/table.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/write_batch.h"
|
2019-05-30 21:47:29 +00:00
|
|
|
#include "table/block_based/block_based_table_builder.h"
|
2018-12-21 05:57:18 +00:00
|
|
|
#include "table/meta_blocks.h"
|
2020-07-22 18:03:29 +00:00
|
|
|
#include "table/mock_table.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2020-11-11 15:58:15 +00:00
|
|
|
#include "util/cast_util.h"
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "util/random.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "util/string_util.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-11-04 04:33:45 +00:00
|
|
|
static constexpr int kValueSize = 1000;
|
2022-01-05 00:44:54 +00:00
|
|
|
namespace {
|
|
|
|
// A wrapper that allows injection of errors.
|
|
|
|
class ErrorEnv : public EnvWrapper {
|
|
|
|
public:
|
|
|
|
bool writable_file_error_;
|
|
|
|
int num_writable_file_errors_;
|
|
|
|
|
|
|
|
explicit ErrorEnv(Env* _target)
|
|
|
|
: EnvWrapper(_target),
|
|
|
|
writable_file_error_(false),
|
|
|
|
num_writable_file_errors_(0) {}
|
|
|
|
const char* Name() const override { return "ErrorEnv"; }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2022-01-05 00:44:54 +00:00
|
|
|
virtual Status NewWritableFile(const std::string& fname,
|
|
|
|
std::unique_ptr<WritableFile>* result,
|
|
|
|
const EnvOptions& soptions) override {
|
|
|
|
result->reset();
|
|
|
|
if (writable_file_error_) {
|
|
|
|
++num_writable_file_errors_;
|
|
|
|
return Status::IOError(fname, "fake error");
|
|
|
|
}
|
|
|
|
return target()->NewWritableFile(fname, result, soptions);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2015-03-17 21:08:00 +00:00
|
|
|
class CorruptionTest : public testing::Test {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2020-11-21 02:38:47 +00:00
|
|
|
std::shared_ptr<Env> env_guard_;
|
2022-01-05 00:44:54 +00:00
|
|
|
ErrorEnv* env_;
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string dbname_;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::shared_ptr<Cache> tiny_cache_;
|
2011-03-18 22:37:00 +00:00
|
|
|
Options options_;
|
|
|
|
DB* db_;
|
|
|
|
|
2011-06-22 02:36:45 +00:00
|
|
|
CorruptionTest() {
|
2017-01-27 14:35:41 +00:00
|
|
|
// If LRU cache shard bit is smaller than 2 (or -1 which will automatically
|
|
|
|
// set it to 0), test SequenceNumberRecovery will fail, likely because of a
|
|
|
|
// bug in recovery code. Keep it 4 for now to make the test passes.
|
2017-01-27 19:15:02 +00:00
|
|
|
tiny_cache_ = NewLRUCache(100, 4);
|
2020-11-21 02:38:47 +00:00
|
|
|
Env* base_env = Env::Default();
|
2021-06-15 10:42:52 +00:00
|
|
|
EXPECT_OK(
|
|
|
|
test::CreateEnvFromSystem(ConfigOptions(), &base_env, &env_guard_));
|
|
|
|
EXPECT_NE(base_env, nullptr);
|
2022-01-05 00:44:54 +00:00
|
|
|
env_ = new ErrorEnv(base_env);
|
2016-04-08 00:40:42 +00:00
|
|
|
options_.wal_recovery_mode = WALRecoveryMode::kTolerateCorruptedTailRecords;
|
2020-11-21 02:38:47 +00:00
|
|
|
options_.env = env_;
|
|
|
|
dbname_ = test::PerThreadDBPath(env_, "corruption_test");
|
2020-10-03 05:09:28 +00:00
|
|
|
Status s = DestroyDB(dbname_, options_);
|
|
|
|
EXPECT_OK(s);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-03-01 02:04:58 +00:00
|
|
|
db_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
options_.create_if_missing = true;
|
2014-08-25 21:22:05 +00:00
|
|
|
BlockBasedTableOptions table_options;
|
|
|
|
table_options.block_size_deviation = 0; // make unit test pass for now
|
|
|
|
options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
2011-03-18 22:37:00 +00:00
|
|
|
Reopen();
|
|
|
|
options_.create_if_missing = false;
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~CorruptionTest() override {
|
2020-11-04 04:33:45 +00:00
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->LoadDependency({});
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
2019-02-14 21:52:47 +00:00
|
|
|
delete db_;
|
2020-08-13 02:28:28 +00:00
|
|
|
db_ = nullptr;
|
2020-11-04 04:33:45 +00:00
|
|
|
if (getenv("KEEP_DB")) {
|
|
|
|
fprintf(stdout, "db is still at %s\n", dbname_.c_str());
|
|
|
|
} else {
|
2020-11-21 02:38:47 +00:00
|
|
|
Options opts;
|
|
|
|
opts.env = env_->target();
|
|
|
|
EXPECT_OK(DestroyDB(dbname_, opts));
|
2020-11-04 04:33:45 +00:00
|
|
|
}
|
2020-11-21 02:38:47 +00:00
|
|
|
delete env_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
void CloseDb() {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:04:58 +00:00
|
|
|
Status TryReopen(Options* options = nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
delete db_;
|
2013-03-01 02:04:58 +00:00
|
|
|
db_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
Options opt = (options ? *options : options_);
|
2019-08-16 23:40:09 +00:00
|
|
|
if (opt.env == Options().env) {
|
|
|
|
// If env is not overridden, replace it with ErrorEnv.
|
|
|
|
// Otherwise, the test already uses a non-default Env.
|
2020-11-21 02:38:47 +00:00
|
|
|
opt.env = env_;
|
2019-08-16 23:40:09 +00:00
|
|
|
}
|
Make arena block size configurable
Summary:
Add an option for arena block size, default value 4096 bytes. Arena will allocate blocks with such size.
I am not sure about passing parameter to skiplist in the new virtualized framework, though I talked to Jim a bit. So add Jim as reviewer.
Test Plan:
new unit test, I am running db_test.
For passing paramter from configured option to Arena, I tried tests like:
TEST(DBTest, Arena_Option) {
std::string dbname = test::TmpDir() + "/db_arena_option_test";
DestroyDB(dbname, Options());
DB* db = nullptr;
Options opts;
opts.create_if_missing = true;
opts.arena_block_size = 1000000; // tested 99, 999999
Status s = DB::Open(opts, dbname, &db);
db->Put(WriteOptions(), "a", "123");
}
and printed some debug info. The results look good. Any suggestion for such a unit-test?
Reviewers: haobo, dhruba, emayanke, jpaton
Reviewed By: dhruba
CC: leveldb, zshao
Differential Revision: https://reviews.facebook.net/D11799
2013-07-31 19:42:23 +00:00
|
|
|
opt.arena_block_size = 4096;
|
2014-08-25 21:22:05 +00:00
|
|
|
BlockBasedTableOptions table_options;
|
|
|
|
table_options.block_cache = tiny_cache_;
|
|
|
|
table_options.block_size_deviation = 0;
|
|
|
|
opt.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
2011-03-18 22:37:00 +00:00
|
|
|
return DB::Open(opt, dbname_, &db_);
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:04:58 +00:00
|
|
|
void Reopen(Options* options = nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_OK(TryReopen(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RepairDB() {
|
|
|
|
delete db_;
|
2013-03-01 02:04:58 +00:00
|
|
|
db_ = nullptr;
|
2020-02-20 20:07:53 +00:00
|
|
|
ASSERT_OK(::ROCKSDB_NAMESPACE::RepairDB(dbname_, options_));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 17:19:07 +00:00
|
|
|
void Build(int n, int start, int flush_every) {
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string key_space, value_space;
|
|
|
|
WriteBatch batch;
|
|
|
|
for (int i = 0; i < n; i++) {
|
2015-10-05 21:46:28 +00:00
|
|
|
if (flush_every != 0 && i != 0 && i % flush_every == 0) {
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
2015-10-05 21:46:28 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
//if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
|
2020-10-13 17:19:07 +00:00
|
|
|
Slice key = Key(i + start, &key_space);
|
2011-03-18 22:37:00 +00:00
|
|
|
batch.Clear();
|
2020-10-13 17:19:07 +00:00
|
|
|
ASSERT_OK(batch.Put(key, Value(i + start, &value_space)));
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_OK(db_->Write(WriteOptions(), &batch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:19:07 +00:00
|
|
|
void Build(int n, int flush_every = 0) { Build(n, 0, flush_every); }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
void Check(int min_expected, int max_expected) {
|
2015-11-19 19:47:12 +00:00
|
|
|
uint64_t next_expected = 0;
|
|
|
|
uint64_t missed = 0;
|
2011-03-18 22:37:00 +00:00
|
|
|
int bad_keys = 0;
|
|
|
|
int bad_values = 0;
|
|
|
|
int correct = 0;
|
|
|
|
std::string value_space;
|
2014-02-01 01:16:30 +00:00
|
|
|
// Do not verify checksums. If we verify checksums then the
|
|
|
|
// db itself will raise errors because data is corrupted.
|
|
|
|
// Instead, we want the reads to be successful and this test
|
|
|
|
// will detect whether the appropriate corruptions have
|
2015-04-25 09:14:27 +00:00
|
|
|
// occurred.
|
2014-02-01 01:16:30 +00:00
|
|
|
Iterator* iter = db_->NewIterator(ReadOptions(false, true));
|
2011-03-18 22:37:00 +00:00
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(iter->status());
|
2011-03-18 22:37:00 +00:00
|
|
|
uint64_t key;
|
|
|
|
Slice in(iter->key());
|
|
|
|
if (!ConsumeDecimalNumber(&in, &key) ||
|
|
|
|
!in.empty() ||
|
|
|
|
key < next_expected) {
|
|
|
|
bad_keys++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
missed += (key - next_expected);
|
2015-11-19 19:47:12 +00:00
|
|
|
next_expected = key + 1;
|
2014-11-11 21:47:22 +00:00
|
|
|
if (iter->value() != Value(static_cast<int>(key), &value_space)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
bad_values++;
|
|
|
|
} else {
|
|
|
|
correct++;
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 05:09:28 +00:00
|
|
|
iter->status().PermitUncheckedError();
|
2011-03-18 22:37:00 +00:00
|
|
|
delete iter;
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2015-11-19 19:47:12 +00:00
|
|
|
"expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%llu\n",
|
|
|
|
min_expected, max_expected, correct, bad_keys, bad_values,
|
|
|
|
static_cast<unsigned long long>(missed));
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_LE(min_expected, correct);
|
|
|
|
ASSERT_GE(max_expected, correct);
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:13:28 +00:00
|
|
|
void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
|
|
|
|
// Pick file to corrupt
|
|
|
|
std::vector<std::string> filenames;
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_OK(env_->GetChildren(dbname_, &filenames));
|
2014-04-24 18:13:28 +00:00
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
std::string fname;
|
|
|
|
int picked_number = -1;
|
2015-12-15 23:26:20 +00:00
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
2014-04-24 18:13:28 +00:00
|
|
|
if (ParseFileName(filenames[i], &number, &type) &&
|
|
|
|
type == filetype &&
|
|
|
|
static_cast<int>(number) > picked_number) { // Pick latest file
|
|
|
|
fname = dbname_ + "/" + filenames[i];
|
2014-11-11 21:47:22 +00:00
|
|
|
picked_number = static_cast<int>(number);
|
2014-04-24 18:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!fname.empty()) << filetype;
|
|
|
|
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_OK(test::CorruptFile(env_, fname, offset, bytes_to_corrupt));
|
2014-04-24 18:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// corrupts exactly one file at level `level`. if no file found at level,
|
|
|
|
// asserts
|
|
|
|
void CorruptTableFileAtLevel(int level, int offset, int bytes_to_corrupt) {
|
|
|
|
std::vector<LiveFileMetaData> metadata;
|
|
|
|
db_->GetLiveFilesMetaData(&metadata);
|
|
|
|
for (const auto& m : metadata) {
|
|
|
|
if (m.level == level) {
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_OK(test::CorruptFile(env_, dbname_ + "/" + m.name, offset,
|
Fix many tests to run with MEM_ENV and ENCRYPTED_ENV; Introduce a MemoryFileSystem class (#7566)
Summary:
This PR does a few things:
1. The MockFileSystem class was split out from the MockEnv. This change would theoretically allow a MockFileSystem to be used by other Environments as well (if we created a means of constructing one). The MockFileSystem implements a FileSystem in its entirety and does not rely on any Wrapper implementation.
2. Make the RocksDB test suite work when MOCK_ENV=1 and ENCRYPTED_ENV=1 are set. To accomplish this, a few things were needed:
- The tests that tried to use the "wrong" environment (Env::Default() instead of env_) were updated
- The MockFileSystem was changed to support the features it was missing or mishandled (such as recursively deleting files in a directory or supporting renaming of a directory).
3. Updated the test framework to have a ROCKSDB_GTEST_SKIP macro. This can be used to flag tests that are skipped. Currently, this defaults to doing nothing (marks the test as SUCCESS) but will mark the tests as SKIPPED when RocksDB is upgraded to a version of gtest that supports this (gtest-1.10).
I have run a full "make check" with MEM_ENV, ENCRYPTED_ENV, both, and neither under both MacOS and RedHat. A few tests were disabled/skipped for the MEM/ENCRYPTED cases. The error_handler_fs_test fails/hangs for MEM_ENV (presumably a timing problem) and I will introduce another PR/issue to track that problem. (I will also push a change to disable those tests soon). There is one more test in DBTest2 that also fails which I need to investigate or skip before this PR is merged.
Theoretically, this PR should also allow the test suite to run against an Env loaded from the registry, though I do not have one to try it with currently.
Finally, once this is accepted, it would be nice if there was a CircleCI job to run these tests on a checkin so this effort does not become stale. I do not know how to do that, so if someone could write that job, it would be appreciated :)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7566
Reviewed By: zhichao-cao
Differential Revision: D24408980
Pulled By: jay-zhuang
fbshipit-source-id: 911b1554a4d0da06fd51feca0c090a4abdcb4a5f
2020-10-27 17:31:34 +00:00
|
|
|
bytes_to_corrupt));
|
2014-04-24 18:13:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 04:23:33 +00:00
|
|
|
FAIL() << "no file found at level";
|
2014-04-24 18:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 19:38:58 +00:00
|
|
|
int Property(const std::string& name) {
|
|
|
|
std::string property;
|
|
|
|
int result;
|
|
|
|
if (db_->GetProperty(name, &property) &&
|
|
|
|
sscanf(property.c_str(), "%d", &result) == 1) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return -1;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the ith key
|
|
|
|
Slice Key(int i, std::string* storage) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "%016d", i);
|
|
|
|
storage->assign(buf, strlen(buf));
|
|
|
|
return Slice(*storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the value to associate with the specified key
|
|
|
|
Slice Value(int k, std::string* storage) {
|
2015-11-06 16:07:08 +00:00
|
|
|
if (k == 0) {
|
|
|
|
// Ugh. Random seed of 0 used to produce no entropy. This code
|
|
|
|
// preserves the implementation that was in place when all of the
|
|
|
|
// magic values in this file were picked.
|
|
|
|
*storage = std::string(kValueSize, ' ');
|
|
|
|
} else {
|
|
|
|
Random r(k);
|
2020-07-09 21:33:42 +00:00
|
|
|
*storage = r.RandomString(kValueSize);
|
2015-11-06 16:07:08 +00:00
|
|
|
}
|
2020-07-09 21:33:42 +00:00
|
|
|
return Slice(*storage);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, Recovery) {
|
2011-03-21 19:40:57 +00:00
|
|
|
Build(100);
|
|
|
|
Check(100, 100);
|
2015-07-01 23:13:49 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
// On Wndows OS Disk cache does not behave properly
|
|
|
|
// We do not call FlushBuffers on every Flush. If we do not close
|
|
|
|
// the log file prior to the corruption we end up with the first
|
|
|
|
// block not corrupted but only the second. However, under the debugger
|
|
|
|
// things work just fine but never pass when running normally
|
|
|
|
// For that reason people may want to run with unbuffered I/O. That option
|
|
|
|
// is not available for WAL though.
|
|
|
|
CloseDb();
|
|
|
|
#endif
|
2020-10-23 00:04:39 +00:00
|
|
|
Corrupt(kWalFile, 19, 1); // WriteBatch tag for first record
|
|
|
|
Corrupt(kWalFile, log::kBlockSize + 1000, 1); // Somewhere in second block
|
2015-01-05 18:26:34 +00:00
|
|
|
ASSERT_TRUE(!TryReopen().ok());
|
|
|
|
options_.paranoid_checks = false;
|
|
|
|
Reopen(&options_);
|
2011-03-21 19:40:57 +00:00
|
|
|
|
|
|
|
// The 64 records in the first two log blocks are completely lost.
|
|
|
|
Check(36, 36);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, RecoverWriteError) {
|
2020-11-21 02:38:47 +00:00
|
|
|
env_->writable_file_error_ = true;
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s = TryReopen();
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, NewFileErrorDuringWrite) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Do enough writing to force minor compaction
|
2020-11-21 02:38:47 +00:00
|
|
|
env_->writable_file_error_ = true;
|
2014-11-11 21:47:22 +00:00
|
|
|
const int num =
|
|
|
|
static_cast<int>(3 + (Options().write_buffer_size / kValueSize));
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string value_storage;
|
|
|
|
Status s;
|
2013-10-28 19:36:02 +00:00
|
|
|
bool failed = false;
|
|
|
|
for (int i = 0; i < num; i++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
WriteBatch batch;
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(batch.Put("a", Value(100, &value_storage)));
|
2011-03-18 22:37:00 +00:00
|
|
|
s = db_->Write(WriteOptions(), &batch);
|
2013-10-28 19:36:02 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!failed || !s.ok());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(!s.ok());
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_GE(env_->num_writable_file_errors_, 1);
|
|
|
|
env_->writable_file_error_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
Reopen();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, TableFile) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Build(100);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
Check(99, 99);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 23:40:09 +00:00
|
|
|
TEST_F(CorruptionTest, VerifyChecksumReadahead) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
SpecialEnv senv(env_->target());
|
2019-08-16 23:40:09 +00:00
|
|
|
options.env = &senv;
|
|
|
|
// Disable block cache as we are going to check checksum for
|
|
|
|
// the same file twice and measure number of reads.
|
|
|
|
BlockBasedTableOptions table_options_no_bc;
|
|
|
|
table_options_no_bc.no_block_cache = true;
|
|
|
|
options.table_factory.reset(NewBlockBasedTableFactory(table_options_no_bc));
|
|
|
|
|
|
|
|
Reopen(&options);
|
|
|
|
|
|
|
|
Build(10000);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
2019-08-16 23:40:09 +00:00
|
|
|
|
|
|
|
senv.count_random_reads_ = true;
|
|
|
|
senv.random_read_counter_.Reset();
|
|
|
|
ASSERT_OK(dbi->VerifyChecksum());
|
|
|
|
|
|
|
|
// Make sure the counter is enabled.
|
|
|
|
ASSERT_GT(senv.random_read_counter_.Read(), 0);
|
|
|
|
|
|
|
|
// The SST file is about 10MB. Default readahead size is 256KB.
|
|
|
|
// Give a conservative 20 reads for metadata blocks, The number
|
|
|
|
// of random reads should be within 10 MB / 256KB + 20 = 60.
|
|
|
|
ASSERT_LT(senv.random_read_counter_.Read(), 60);
|
|
|
|
|
|
|
|
senv.random_read_bytes_counter_ = 0;
|
|
|
|
ReadOptions ro;
|
|
|
|
ro.readahead_size = size_t{32 * 1024};
|
|
|
|
ASSERT_OK(dbi->VerifyChecksum(ro));
|
|
|
|
// The SST file is about 10MB. We set readahead size to 32KB.
|
|
|
|
// Give 0 to 20 reads for metadata blocks, and allow real read
|
|
|
|
// to range from 24KB to 48KB. The lower bound would be:
|
|
|
|
// 10MB / 48KB + 0 = 213
|
|
|
|
// The higher bound is
|
|
|
|
// 10MB / 24KB + 20 = 447.
|
|
|
|
ASSERT_GE(senv.random_read_counter_.Read(), 213);
|
|
|
|
ASSERT_LE(senv.random_read_counter_.Read(), 447);
|
|
|
|
|
2019-10-21 18:37:09 +00:00
|
|
|
// Test readahead shouldn't break mmap mode (where it should be
|
|
|
|
// disabled).
|
|
|
|
options.allow_mmap_reads = true;
|
|
|
|
Reopen(&options);
|
|
|
|
dbi = static_cast<DBImpl*>(db_);
|
|
|
|
ASSERT_OK(dbi->VerifyChecksum(ro));
|
|
|
|
|
2019-08-16 23:40:09 +00:00
|
|
|
CloseDb();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, TableFileIndexData) {
|
2015-10-05 21:46:28 +00:00
|
|
|
Options options;
|
|
|
|
// very big, we'll trigger flushes manually
|
|
|
|
options.write_buffer_size = 100 * 1024 * 1024;
|
|
|
|
Reopen(&options);
|
|
|
|
// build 2 tables, flush at 5000
|
|
|
|
Build(10000, 5000);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2015-10-05 21:46:28 +00:00
|
|
|
// corrupt an index block of an entire file
|
2011-04-12 19:38:58 +00:00
|
|
|
Corrupt(kTableFile, -2000, 500);
|
2020-02-14 16:15:24 +00:00
|
|
|
options.paranoid_checks = false;
|
|
|
|
Reopen(&options);
|
2020-07-03 02:24:25 +00:00
|
|
|
dbi = static_cast_with_check<DBImpl>(db_);
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
// one full file may be readable, since only one was corrupted
|
2015-10-05 21:46:28 +00:00
|
|
|
// the other file should be fully non-readable, since index was corrupted
|
Change and clarify the relationship between Valid(), status() and Seek*() for all iterators. Also fix some bugs
Summary:
Before this PR, Iterator/InternalIterator may simultaneously have non-ok status() and Valid() = true. That state means that the last operation failed, but the iterator is nevertheless positioned on some unspecified record. Likely intended uses of that are:
* If some sst files are corrupted, a normal iterator can be used to read the data from files that are not corrupted.
* When using read_tier = kBlockCacheTier, read the data that's in block cache, skipping over the data that is not.
However, this behavior wasn't documented well (and until recently the wiki on github had misleading incorrect information). In the code there's a lot of confusion about the relationship between status() and Valid(), and about whether Seek()/SeekToLast()/etc reset the status or not. There were a number of bugs caused by this confusion, both inside rocksdb and in the code that uses rocksdb (including ours).
This PR changes the convention to:
* If status() is not ok, Valid() always returns false.
* Any seek operation resets status. (Before the PR, it depended on iterator type and on particular error.)
This does sacrifice the two use cases listed above, but siying said it's ok.
Overview of the changes:
* A commit that adds missing status checks in MergingIterator. This fixes a bug that actually affects us, and we need it fixed. `DBIteratorTest.NonBlockingIterationBugRepro` explains the scenario.
* Changes to lots of iterator types to make all of them conform to the new convention. Some bug fixes along the way. By far the biggest changes are in DBIter, which is a big messy piece of code; I tried to make it less big and messy but mostly failed.
* A stress-test for DBIter, to gain some confidence that I didn't break it. It does a few million random operations on the iterator, while occasionally modifying the underlying data (like ForwardIterator does) and occasionally returning non-ok status from internal iterator.
To find the iterator types that needed changes I searched for "public .*Iterator" in the code. Here's an overview of all 27 iterator types:
Iterators that didn't need changes:
* status() is always ok(), or Valid() is always false: MemTableIterator, ModelIter, TestIterator, KVIter (2 classes with this name anonymous namespaces), LoggingForwardVectorIterator, VectorIterator, MockTableIterator, EmptyIterator, EmptyInternalIterator.
* Thin wrappers that always pass through Valid() and status(): ArenaWrappedDBIter, TtlIterator, InternalIteratorFromIterator.
Iterators with changes (see inline comments for details):
* DBIter - an overhaul:
- It used to silently skip corrupted keys (`FindParseableKey()`), which seems dangerous. This PR makes it just stop immediately after encountering a corrupted key, just like it would for other kinds of corruption. Let me know if there was actually some deeper meaning in this behavior and I should put it back.
- It had a few code paths silently discarding subiterator's status. The stress test caught a few.
- The backwards iteration code path was expecting the internal iterator's set of keys to be immutable. It's probably always true in practice at the moment, since ForwardIterator doesn't support backwards iteration, but this PR fixes it anyway. See added DBIteratorTest.ReverseToForwardBug for an example.
- Some parts of backwards iteration code path even did things like `assert(iter_->Valid())` after a seek, which is never a safe assumption.
- It used to not reset status on seek for some types of errors.
- Some simplifications and better comments.
- Some things got more complicated from the added error handling. I'm open to ideas for how to make it nicer.
* MergingIterator - check status after every operation on every subiterator, and in some places assert that valid subiterators have ok status.
* ForwardIterator - changed to the new convention, also slightly simplified.
* ForwardLevelIterator - fixed some bugs and simplified.
* LevelIterator - simplified.
* TwoLevelIterator - changed to the new convention. Also fixed a bug that would make SeekForPrev() sometimes silently ignore errors from first_level_iter_.
* BlockBasedTableIterator - minor changes.
* BlockIter - replaced `SetStatus()` with `Invalidate()` to make sure non-ok BlockIter is always invalid.
* PlainTableIterator - some seeks used to not reset status.
* CuckooTableIterator - tiny code cleanup.
* ManagedIterator - fixed some bugs.
* BaseDeltaIterator - changed to the new convention and fixed a bug.
* BlobDBIterator - seeks used to not reset status.
* KeyConvertingIterator - some small change.
Closes https://github.com/facebook/rocksdb/pull/3810
Differential Revision: D7888019
Pulled By: al13n321
fbshipit-source-id: 4aaf6d3421c545d16722a815b2fa2e7912bc851d
2018-05-17 09:44:14 +00:00
|
|
|
Check(0, 5000);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2020-02-14 16:15:24 +00:00
|
|
|
|
|
|
|
// In paranoid mode, the db cannot be opened due to the corrupted file.
|
|
|
|
ASSERT_TRUE(TryReopen().IsCorruption());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, MissingDescriptor) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Build(1000);
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
Check(1000, 1000);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, SequenceNumberRecovery) {
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v3"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v4"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v5"));
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
std::string v;
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
|
|
|
|
ASSERT_EQ("v5", v);
|
|
|
|
// Write something. If sequence number was not recovered properly,
|
|
|
|
// it will be hidden by an earlier write.
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "v6"));
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
|
|
|
|
ASSERT_EQ("v6", v);
|
|
|
|
Reopen();
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
|
|
|
|
ASSERT_EQ("v6", v);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, CorruptedDescriptor) {
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
Corrupt(kDescriptorFile, 0, 1000);
|
|
|
|
Status s = TryReopen();
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
std::string v;
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
|
|
|
|
ASSERT_EQ("hello", v);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, CompactionInputError) {
|
2014-09-18 20:32:44 +00:00
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2014-09-18 20:32:44 +00:00
|
|
|
Reopen(&options);
|
2011-03-18 22:37:00 +00:00
|
|
|
Build(10);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
2015-07-17 19:02:52 +00:00
|
|
|
ASSERT_EQ(1, Property("rocksdb.num-files-at-level2"));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
Check(9, 9);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Force compactions by writing lots of values
|
|
|
|
Build(10000);
|
|
|
|
Check(10000, 10000);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2011-03-18 22:37:00 +00:00
|
|
|
options.paranoid_checks = true;
|
2014-04-11 19:48:38 +00:00
|
|
|
options.write_buffer_size = 131072;
|
|
|
|
options.max_write_buffer_number = 2;
|
2011-03-18 22:37:00 +00:00
|
|
|
Reopen(&options);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2011-06-22 02:36:45 +00:00
|
|
|
|
2015-07-17 19:02:52 +00:00
|
|
|
// Fill levels >= 1
|
2012-06-23 02:30:03 +00:00
|
|
|
for (int level = 1; level < dbi->NumberLevels(); level++) {
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->Put(WriteOptions(), "", "begin"));
|
|
|
|
ASSERT_OK(dbi->Put(WriteOptions(), "~", "end"));
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
2015-07-17 19:02:52 +00:00
|
|
|
for (int comp_level = 0; comp_level < dbi->NumberLevels() - level;
|
|
|
|
++comp_level) {
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(comp_level, nullptr, nullptr));
|
2015-07-17 19:02:52 +00:00
|
|
|
}
|
2011-06-22 02:36:45 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-04-24 18:13:28 +00:00
|
|
|
Reopen(&options);
|
|
|
|
|
2020-07-03 02:24:25 +00:00
|
|
|
dbi = static_cast_with_check<DBImpl>(db_);
|
2011-03-18 22:37:00 +00:00
|
|
|
Build(10);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_WaitForCompact());
|
2013-10-05 05:32:05 +00:00
|
|
|
ASSERT_EQ(1, Property("rocksdb.num-files-at-level0"));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-04-24 18:13:28 +00:00
|
|
|
CorruptTableFileAtLevel(0, 100, 1);
|
2011-03-18 22:37:00 +00:00
|
|
|
Check(9, 9);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Write must eventually fail because of corrupted table
|
|
|
|
Status s;
|
|
|
|
std::string tmp1, tmp2;
|
2013-10-28 19:36:02 +00:00
|
|
|
bool failed = false;
|
2014-04-11 19:48:38 +00:00
|
|
|
for (int i = 0; i < 10000; i++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
s = db_->Put(WriteOptions(), Key(i, &tmp1), Value(i, &tmp2));
|
2013-10-28 19:36:02 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
// if one write failed, every subsequent write must fail, too
|
|
|
|
ASSERT_TRUE(!failed || !s.ok()) << "write did not fail in a corrupted db";
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, UnrelatedKeys) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Build(10);
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
2011-03-18 22:37:00 +00:00
|
|
|
Corrupt(kTableFile, 100, 1);
|
2017-08-09 22:49:40 +00:00
|
|
|
ASSERT_NOK(dbi->VerifyChecksum());
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
std::string tmp1, tmp2;
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
|
|
|
|
std::string v;
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
|
|
|
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
|
|
|
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
|
|
|
}
|
|
|
|
|
2018-12-21 05:57:18 +00:00
|
|
|
TEST_F(CorruptionTest, RangeDeletionCorrupted) {
|
|
|
|
ASSERT_OK(
|
|
|
|
db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(), "a", "b"));
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
std::vector<LiveFileMetaData> metadata;
|
|
|
|
db_->GetLiveFilesMetaData(&metadata);
|
|
|
|
ASSERT_EQ(static_cast<size_t>(1), metadata.size());
|
|
|
|
std::string filename = dbname_ + metadata[0].name;
|
|
|
|
|
2021-01-29 06:08:46 +00:00
|
|
|
FileOptions file_opts;
|
|
|
|
const auto& fs = options_.env->GetFileSystem();
|
|
|
|
std::unique_ptr<RandomAccessFileReader> file_reader;
|
|
|
|
ASSERT_OK(RandomAccessFileReader::Create(fs, filename, file_opts,
|
|
|
|
&file_reader, nullptr));
|
2018-12-21 05:57:18 +00:00
|
|
|
|
|
|
|
uint64_t file_size;
|
2021-01-29 06:08:46 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
fs->GetFileSize(filename, file_opts.io_options, &file_size, nullptr));
|
2018-12-21 05:57:18 +00:00
|
|
|
|
|
|
|
BlockHandle range_del_handle;
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 19:42:12 +00:00
|
|
|
ASSERT_OK(FindMetaBlockInFile(
|
2018-12-21 05:57:18 +00:00
|
|
|
file_reader.get(), file_size, kBlockBasedTableMagicNumber,
|
2021-12-10 16:12:09 +00:00
|
|
|
ImmutableOptions(options_), kRangeDelBlockName, &range_del_handle));
|
2018-12-21 05:57:18 +00:00
|
|
|
|
|
|
|
ASSERT_OK(TryReopen());
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_OK(test::CorruptFile(env_, filename,
|
Fix many tests to run with MEM_ENV and ENCRYPTED_ENV; Introduce a MemoryFileSystem class (#7566)
Summary:
This PR does a few things:
1. The MockFileSystem class was split out from the MockEnv. This change would theoretically allow a MockFileSystem to be used by other Environments as well (if we created a means of constructing one). The MockFileSystem implements a FileSystem in its entirety and does not rely on any Wrapper implementation.
2. Make the RocksDB test suite work when MOCK_ENV=1 and ENCRYPTED_ENV=1 are set. To accomplish this, a few things were needed:
- The tests that tried to use the "wrong" environment (Env::Default() instead of env_) were updated
- The MockFileSystem was changed to support the features it was missing or mishandled (such as recursively deleting files in a directory or supporting renaming of a directory).
3. Updated the test framework to have a ROCKSDB_GTEST_SKIP macro. This can be used to flag tests that are skipped. Currently, this defaults to doing nothing (marks the test as SUCCESS) but will mark the tests as SKIPPED when RocksDB is upgraded to a version of gtest that supports this (gtest-1.10).
I have run a full "make check" with MEM_ENV, ENCRYPTED_ENV, both, and neither under both MacOS and RedHat. A few tests were disabled/skipped for the MEM/ENCRYPTED cases. The error_handler_fs_test fails/hangs for MEM_ENV (presumably a timing problem) and I will introduce another PR/issue to track that problem. (I will also push a change to disable those tests soon). There is one more test in DBTest2 that also fails which I need to investigate or skip before this PR is merged.
Theoretically, this PR should also allow the test suite to run against an Env loaded from the registry, though I do not have one to try it with currently.
Finally, once this is accepted, it would be nice if there was a CircleCI job to run these tests on a checkin so this effort does not become stale. I do not know how to do that, so if someone could write that job, it would be appreciated :)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7566
Reviewed By: zhichao-cao
Differential Revision: D24408980
Pulled By: jay-zhuang
fbshipit-source-id: 911b1554a4d0da06fd51feca0c090a4abdcb4a5f
2020-10-27 17:31:34 +00:00
|
|
|
static_cast<int>(range_del_handle.offset()), 1));
|
2020-02-14 16:15:24 +00:00
|
|
|
ASSERT_TRUE(TryReopen().IsCorruption());
|
2018-12-21 05:57:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(CorruptionTest, FileSystemStateCorrupted) {
|
2014-03-20 21:18:29 +00:00
|
|
|
for (int iter = 0; iter < 2; ++iter) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2014-03-20 21:18:29 +00:00
|
|
|
options.paranoid_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
Reopen(&options);
|
|
|
|
Build(10);
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
2020-07-03 02:24:25 +00:00
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
2014-03-20 21:18:29 +00:00
|
|
|
std::vector<LiveFileMetaData> metadata;
|
|
|
|
dbi->GetLiveFilesMetaData(&metadata);
|
2020-11-11 15:58:15 +00:00
|
|
|
ASSERT_GT(metadata.size(), 0);
|
2014-03-20 21:18:29 +00:00
|
|
|
std::string filename = dbname_ + metadata[0].name;
|
|
|
|
|
|
|
|
delete db_;
|
2014-03-20 21:37:30 +00:00
|
|
|
db_ = nullptr;
|
2014-03-20 21:18:29 +00:00
|
|
|
|
|
|
|
if (iter == 0) { // corrupt file size
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> file;
|
2020-12-24 00:54:05 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(filename, &file, EnvOptions()));
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(file->Append(Slice("corrupted sst")));
|
2014-03-20 21:18:29 +00:00
|
|
|
file.reset();
|
2020-02-14 16:15:24 +00:00
|
|
|
Status x = TryReopen(&options);
|
|
|
|
ASSERT_TRUE(x.IsCorruption());
|
2014-03-20 21:18:29 +00:00
|
|
|
} else { // delete the file
|
2020-11-21 02:38:47 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(filename));
|
2020-02-14 16:15:24 +00:00
|
|
|
Status x = TryReopen(&options);
|
2020-11-11 15:58:15 +00:00
|
|
|
ASSERT_TRUE(x.IsCorruption());
|
2014-03-20 21:18:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname_, options_));
|
2014-03-20 21:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:08:52 +00:00
|
|
|
static const auto& corruption_modes = {
|
|
|
|
mock::MockTableFactory::kCorruptNone, mock::MockTableFactory::kCorruptKey,
|
|
|
|
mock::MockTableFactory::kCorruptValue,
|
|
|
|
mock::MockTableFactory::kCorruptReorderKey};
|
2020-07-22 18:03:29 +00:00
|
|
|
|
2020-08-06 21:24:24 +00:00
|
|
|
TEST_F(CorruptionTest, ParanoidFileChecksOnFlush) {
|
2020-07-22 18:03:29 +00:00
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-01 17:08:52 +00:00
|
|
|
options.check_flush_compaction_key_order = false;
|
2020-07-22 18:03:29 +00:00
|
|
|
options.paranoid_file_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
Status s;
|
|
|
|
for (const auto& mode : corruption_modes) {
|
|
|
|
delete db_;
|
2020-08-13 02:28:28 +00:00
|
|
|
db_ = nullptr;
|
2020-07-22 18:03:29 +00:00
|
|
|
s = DestroyDB(dbname_, options);
|
2020-10-03 05:09:28 +00:00
|
|
|
ASSERT_OK(s);
|
2020-07-22 18:03:29 +00:00
|
|
|
std::shared_ptr<mock::MockTableFactory> mock =
|
|
|
|
std::make_shared<mock::MockTableFactory>();
|
|
|
|
options.table_factory = mock;
|
|
|
|
mock->SetCorruptionMode(mode);
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-07-22 18:03:29 +00:00
|
|
|
Build(10);
|
|
|
|
s = db_->Flush(FlushOptions());
|
|
|
|
if (mode == mock::MockTableFactory::kCorruptNone) {
|
|
|
|
ASSERT_OK(s);
|
|
|
|
} else {
|
|
|
|
ASSERT_NOK(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 21:24:24 +00:00
|
|
|
TEST_F(CorruptionTest, ParanoidFileChecksOnCompact) {
|
2020-07-22 18:03:29 +00:00
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-07-22 18:03:29 +00:00
|
|
|
options.paranoid_file_checks = true;
|
|
|
|
options.create_if_missing = true;
|
2020-10-01 17:08:52 +00:00
|
|
|
options.check_flush_compaction_key_order = false;
|
2020-07-22 18:03:29 +00:00
|
|
|
Status s;
|
|
|
|
for (const auto& mode : corruption_modes) {
|
|
|
|
delete db_;
|
2020-08-13 02:28:28 +00:00
|
|
|
db_ = nullptr;
|
2020-07-22 18:03:29 +00:00
|
|
|
s = DestroyDB(dbname_, options);
|
|
|
|
std::shared_ptr<mock::MockTableFactory> mock =
|
|
|
|
std::make_shared<mock::MockTableFactory>();
|
|
|
|
options.table_factory = mock;
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-07-22 18:03:29 +00:00
|
|
|
Build(100, 2);
|
|
|
|
// ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
mock->SetCorruptionMode(mode);
|
|
|
|
s = dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true);
|
|
|
|
if (mode == mock::MockTableFactory::kCorruptNone) {
|
|
|
|
ASSERT_OK(s);
|
|
|
|
} else {
|
|
|
|
ASSERT_NOK(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:19:07 +00:00
|
|
|
TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeFirst) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-13 17:19:07 +00:00
|
|
|
options.check_flush_compaction_key_order = false;
|
|
|
|
options.paranoid_file_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
for (bool do_flush : {true, false}) {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
|
|
|
std::string start, end;
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-10-13 17:19:07 +00:00
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(3, &start), Key(7, &end)));
|
|
|
|
auto snap = db_->GetSnapshot();
|
|
|
|
ASSERT_NE(snap, nullptr);
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(8, &start), Key(9, &end)));
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(2, &start), Key(5, &end)));
|
|
|
|
Build(10);
|
|
|
|
if (do_flush) {
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
} else {
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true));
|
|
|
|
}
|
|
|
|
db_->ReleaseSnapshot(snap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRange) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-13 17:19:07 +00:00
|
|
|
options.check_flush_compaction_key_order = false;
|
|
|
|
options.paranoid_file_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
for (bool do_flush : {true, false}) {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-10-13 17:19:07 +00:00
|
|
|
Build(10, 0, 0);
|
|
|
|
std::string start, end;
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(5, &start), Key(15, &end)));
|
|
|
|
auto snap = db_->GetSnapshot();
|
|
|
|
ASSERT_NE(snap, nullptr);
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(8, &start), Key(9, &end)));
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(12, &start), Key(17, &end)));
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(2, &start), Key(4, &end)));
|
|
|
|
Build(10, 10, 0);
|
|
|
|
if (do_flush) {
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
} else {
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true));
|
|
|
|
}
|
|
|
|
db_->ReleaseSnapshot(snap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeLast) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-13 17:19:07 +00:00
|
|
|
options.check_flush_compaction_key_order = false;
|
|
|
|
options.paranoid_file_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
for (bool do_flush : {true, false}) {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-10-13 17:19:07 +00:00
|
|
|
std::string start, end;
|
|
|
|
Build(10);
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(3, &start), Key(7, &end)));
|
|
|
|
auto snap = db_->GetSnapshot();
|
|
|
|
ASSERT_NE(snap, nullptr);
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(6, &start), Key(8, &end)));
|
|
|
|
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
|
|
|
Key(2, &start), Key(5, &end)));
|
|
|
|
if (do_flush) {
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
} else {
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true));
|
|
|
|
}
|
|
|
|
db_->ReleaseSnapshot(snap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:16:12 +00:00
|
|
|
TEST_F(CorruptionTest, LogCorruptionErrorsInCompactionIterator) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-09-30 06:16:12 +00:00
|
|
|
options.create_if_missing = true;
|
|
|
|
options.allow_data_in_errors = true;
|
|
|
|
auto mode = mock::MockTableFactory::kCorruptKey;
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
|
|
|
|
std::shared_ptr<mock::MockTableFactory> mock =
|
|
|
|
std::make_shared<mock::MockTableFactory>();
|
|
|
|
mock->SetCorruptionMode(mode);
|
|
|
|
options.table_factory = mock;
|
|
|
|
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-09-30 06:16:12 +00:00
|
|
|
Build(100, 2);
|
|
|
|
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
Status s = dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true);
|
|
|
|
ASSERT_NOK(s);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:08:52 +00:00
|
|
|
TEST_F(CorruptionTest, CompactionKeyOrderCheck) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-01 17:08:52 +00:00
|
|
|
options.paranoid_file_checks = false;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.check_flush_compaction_key_order = false;
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
std::shared_ptr<mock::MockTableFactory> mock =
|
|
|
|
std::make_shared<mock::MockTableFactory>();
|
|
|
|
options.table_factory = mock;
|
|
|
|
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
2020-12-24 00:54:05 +00:00
|
|
|
assert(db_ != nullptr); // suppress false clang-analyze report
|
2020-10-01 17:08:52 +00:00
|
|
|
mock->SetCorruptionMode(mock::MockTableFactory::kCorruptReorderKey);
|
|
|
|
Build(100, 2);
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
|
|
|
|
mock->SetCorruptionMode(mock::MockTableFactory::kCorruptNone);
|
|
|
|
ASSERT_OK(db_->SetOptions({{"check_flush_compaction_key_order", "true"}}));
|
|
|
|
ASSERT_NOK(dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CorruptionTest, FlushKeyOrderCheck) {
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-10-01 17:08:52 +00:00
|
|
|
options.paranoid_file_checks = false;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
ASSERT_OK(db_->SetOptions({{"check_flush_compaction_key_order", "true"}}));
|
|
|
|
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo1", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo2", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo3", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo4", "v1"));
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
// Generate some out of order keys from the memtable
|
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"MemTableIterator::Next:0", [&](void* arg) {
|
|
|
|
MemTableRep::Iterator* mem_iter =
|
|
|
|
static_cast<MemTableRep::Iterator*>(arg);
|
|
|
|
if (++cnt == 3) {
|
|
|
|
mem_iter->Prev();
|
|
|
|
mem_iter->Prev();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
Status s = static_cast_with_check<DBImpl>(db_)->TEST_FlushMemTable();
|
|
|
|
ASSERT_NOK(s);
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CorruptionTest, DisableKeyOrderCheck) {
|
|
|
|
ASSERT_OK(db_->SetOptions({{"check_flush_compaction_key_order", "false"}}));
|
|
|
|
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
|
|
|
|
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"OutputValidator::Add:order_check",
|
|
|
|
[&](void* /*arg*/) { ASSERT_TRUE(false); });
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo1", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo3", "v1"));
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo2", "v1"));
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo4", "v1"));
|
|
|
|
ASSERT_OK(dbi->TEST_FlushMemTable());
|
|
|
|
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr, nullptr, true));
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
}
|
|
|
|
|
2020-11-04 04:33:45 +00:00
|
|
|
TEST_F(CorruptionTest, VerifyWholeTableChecksum) {
|
|
|
|
CloseDb();
|
|
|
|
Options options;
|
2020-11-21 02:38:47 +00:00
|
|
|
options.env = env_;
|
2020-11-04 04:33:45 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname_, options));
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.file_checksum_gen_factory =
|
|
|
|
ROCKSDB_NAMESPACE::GetFileChecksumGenCrc32cFactory();
|
|
|
|
Reopen(&options);
|
|
|
|
|
|
|
|
Build(10, 5);
|
|
|
|
|
|
|
|
ASSERT_OK(db_->VerifyFileChecksums(ReadOptions()));
|
|
|
|
CloseDb();
|
|
|
|
|
|
|
|
// Corrupt the first byte of each table file, this must be data block.
|
|
|
|
Corrupt(kTableFile, 0, 1);
|
|
|
|
|
|
|
|
ASSERT_OK(TryReopen(&options));
|
|
|
|
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
int count{0};
|
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
2021-02-23 06:07:59 +00:00
|
|
|
"DBImpl::VerifyFullFileChecksum:mismatch", [&](void* arg) {
|
2020-11-04 04:33:45 +00:00
|
|
|
auto* s = reinterpret_cast<Status*>(arg);
|
2020-12-24 00:54:05 +00:00
|
|
|
ASSERT_NE(s, nullptr);
|
2020-11-04 04:33:45 +00:00
|
|
|
++count;
|
|
|
|
ASSERT_NOK(*s);
|
|
|
|
});
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
ASSERT_TRUE(db_->VerifyFileChecksums(ReadOptions()).IsCorruption());
|
|
|
|
ASSERT_EQ(1, count);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2020-11-21 02:38:47 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
2020-11-21 02:38:47 +00:00
|
|
|
RegisterCustomObjects(argc, argv);
|
2015-03-17 21:08:00 +00:00
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2015-09-14 23:44:35 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 00:19:57 +00:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2015-09-14 23:44:35 +00:00
|
|
|
fprintf(stderr, "SKIPPED as RepairDB() is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|