2011-03-19 06:37:00 +08: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.
|
|
|
|
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/db.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2011-04-13 03:38:58 +08:00
|
|
|
#include "leveldb/cache.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/table.h"
|
|
|
|
#include "leveldb/write_batch.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/filename.h"
|
2011-03-22 03:40:57 +08:00
|
|
|
#include "db/log_format.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2019-03-02 05:12:01 +08:00
|
|
|
#if defined(LEVELDB_PLATFORM_WINDOWS)
|
|
|
|
#include "util/env_windows_test_helper.h"
|
|
|
|
#endif // defined(LEVELDB_PLATFORM_WINDOWS)
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
static const int kValueSize = 1000;
|
|
|
|
|
|
|
|
class CorruptionTest {
|
|
|
|
public:
|
|
|
|
test::ErrorEnv env_;
|
|
|
|
std::string dbname_;
|
2011-04-13 03:38:58 +08:00
|
|
|
Cache* tiny_cache_;
|
2011-03-19 06:37:00 +08:00
|
|
|
Options options_;
|
|
|
|
DB* db_;
|
|
|
|
|
2019-03-02 05:12:01 +08:00
|
|
|
#if defined(LEVELDB_PLATFORM_WINDOWS)
|
|
|
|
static void SetFileLimits(int mmap_limit) {
|
|
|
|
EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(cmumford): Modify corruption_test to use MemEnv and remove.
|
|
|
|
static void RelaxFilePermissions() {
|
|
|
|
EnvWindowsTestHelper::RelaxFilePermissions();
|
|
|
|
}
|
|
|
|
#endif // defined(LEVELDB_PLATFORM_WINDOWS)
|
|
|
|
|
2011-06-22 10:36:45 +08:00
|
|
|
CorruptionTest() {
|
2011-04-13 03:38:58 +08:00
|
|
|
tiny_cache_ = NewLRUCache(100);
|
2011-03-19 06:37:00 +08:00
|
|
|
options_.env = &env_;
|
2013-08-22 02:12:47 +08:00
|
|
|
options_.block_cache = tiny_cache_;
|
2014-12-12 00:13:18 +08:00
|
|
|
dbname_ = test::TmpDir() + "/corruption_test";
|
2011-03-19 06:37:00 +08:00
|
|
|
DestroyDB(dbname_, options_);
|
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
db_ = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
options_.create_if_missing = true;
|
|
|
|
Reopen();
|
|
|
|
options_.create_if_missing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CorruptionTest() {
|
|
|
|
delete db_;
|
|
|
|
DestroyDB(dbname_, Options());
|
2011-04-13 03:38:58 +08:00
|
|
|
delete tiny_cache_;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
Status TryReopen() {
|
2011-03-19 06:37:00 +08:00
|
|
|
delete db_;
|
2018-04-11 07:18:06 +08:00
|
|
|
db_ = nullptr;
|
2013-08-22 02:12:47 +08:00
|
|
|
return DB::Open(options_, dbname_, &db_);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
void Reopen() {
|
|
|
|
ASSERT_OK(TryReopen());
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RepairDB() {
|
|
|
|
delete db_;
|
2018-04-11 07:18:06 +08:00
|
|
|
db_ = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
ASSERT_OK(::leveldb::RepairDB(dbname_, options_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Build(int n) {
|
|
|
|
std::string key_space, value_space;
|
|
|
|
WriteBatch batch;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
//if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
|
|
|
|
Slice key = Key(i, &key_space);
|
|
|
|
batch.Clear();
|
|
|
|
batch.Put(key, Value(i, &value_space));
|
2013-12-11 02:36:31 +08:00
|
|
|
WriteOptions options;
|
|
|
|
// Corrupt() doesn't work without this sync on windows; stat reports 0 for
|
|
|
|
// the file size.
|
|
|
|
if (i == n - 1) {
|
|
|
|
options.sync = true;
|
|
|
|
}
|
|
|
|
ASSERT_OK(db_->Write(options, &batch));
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check(int min_expected, int max_expected) {
|
|
|
|
int next_expected = 0;
|
|
|
|
int missed = 0;
|
|
|
|
int bad_keys = 0;
|
|
|
|
int bad_values = 0;
|
|
|
|
int correct = 0;
|
|
|
|
std::string value_space;
|
|
|
|
Iterator* iter = db_->NewIterator(ReadOptions());
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
uint64_t key;
|
|
|
|
Slice in(iter->key());
|
2013-08-22 02:12:47 +08:00
|
|
|
if (in == "" || in == "~") {
|
|
|
|
// Ignore boundary keys.
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
if (!ConsumeDecimalNumber(&in, &key) ||
|
|
|
|
!in.empty() ||
|
|
|
|
key < next_expected) {
|
|
|
|
bad_keys++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
missed += (key - next_expected);
|
|
|
|
next_expected = key + 1;
|
|
|
|
if (iter->value() != Value(key, &value_space)) {
|
|
|
|
bad_values++;
|
|
|
|
} else {
|
|
|
|
correct++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
|
|
|
|
min_expected, max_expected, correct, bad_keys, bad_values, missed);
|
|
|
|
ASSERT_LE(min_expected, correct);
|
|
|
|
ASSERT_GE(max_expected, correct);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
|
|
|
|
// Pick file to corrupt
|
|
|
|
std::vector<std::string> filenames;
|
|
|
|
ASSERT_OK(env_.GetChildren(dbname_, &filenames));
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
2011-06-22 10:36:45 +08:00
|
|
|
std::string fname;
|
|
|
|
int picked_number = -1;
|
2013-12-11 02:36:31 +08:00
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
2011-04-21 06:48:11 +08:00
|
|
|
if (ParseFileName(filenames[i], &number, &type) &&
|
2011-06-22 10:36:45 +08:00
|
|
|
type == filetype &&
|
|
|
|
int(number) > picked_number) { // Pick latest file
|
|
|
|
fname = dbname_ + "/" + filenames[i];
|
|
|
|
picked_number = number;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-22 10:36:45 +08:00
|
|
|
ASSERT_TRUE(!fname.empty()) << filetype;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
struct stat sbuf;
|
2011-03-22 03:40:57 +08:00
|
|
|
if (stat(fname.c_str(), &sbuf) != 0) {
|
|
|
|
const char* msg = strerror(errno);
|
|
|
|
ASSERT_TRUE(false) << fname << ": " << msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < 0) {
|
|
|
|
// Relative to end of file; make it absolute
|
|
|
|
if (-offset > sbuf.st_size) {
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
offset = sbuf.st_size + offset;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset > sbuf.st_size) {
|
|
|
|
offset = sbuf.st_size;
|
|
|
|
}
|
|
|
|
if (offset + bytes_to_corrupt > sbuf.st_size) {
|
|
|
|
bytes_to_corrupt = sbuf.st_size - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do it
|
|
|
|
std::string contents;
|
|
|
|
Status s = ReadFileToString(Env::Default(), fname, &contents);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
for (int i = 0; i < bytes_to_corrupt; i++) {
|
|
|
|
contents[i + offset] ^= 0x80;
|
|
|
|
}
|
|
|
|
s = WriteStringToFile(Env::Default(), contents, fname);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
}
|
|
|
|
|
2011-04-13 03:38:58 +08: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-19 06:37:00 +08: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) {
|
|
|
|
Random r(k);
|
|
|
|
return test::RandomString(&r, kValueSize, storage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(CorruptionTest, Recovery) {
|
2011-03-22 03:40:57 +08:00
|
|
|
Build(100);
|
|
|
|
Check(100, 100);
|
2011-03-19 06:37:00 +08:00
|
|
|
Corrupt(kLogFile, 19, 1); // WriteBatch tag for first record
|
2011-03-22 03:40:57 +08:00
|
|
|
Corrupt(kLogFile, log::kBlockSize + 1000, 1); // Somewhere in second block
|
2011-03-19 06:37:00 +08:00
|
|
|
Reopen();
|
2011-03-22 03:40:57 +08:00
|
|
|
|
|
|
|
// The 64 records in the first two log blocks are completely lost.
|
|
|
|
Check(36, 36);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, RecoverWriteError) {
|
|
|
|
env_.writable_file_error_ = true;
|
|
|
|
Status s = TryReopen();
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, NewFileErrorDuringWrite) {
|
|
|
|
// Do enough writing to force minor compaction
|
|
|
|
env_.writable_file_error_ = true;
|
|
|
|
const int num = 3 + (Options().write_buffer_size / kValueSize);
|
|
|
|
std::string value_storage;
|
|
|
|
Status s;
|
|
|
|
for (int i = 0; s.ok() && i < num; i++) {
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.Put("a", Value(100, &value_storage));
|
|
|
|
s = db_->Write(WriteOptions(), &batch);
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
ASSERT_GE(env_.num_writable_file_errors_, 1);
|
|
|
|
env_.writable_file_error_ = false;
|
|
|
|
Reopen();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, TableFile) {
|
|
|
|
Build(100);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
2018-04-11 07:18:06 +08:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
|
|
|
dbi->TEST_CompactRange(1, nullptr, nullptr);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
2013-08-22 02:12:47 +08:00
|
|
|
Check(90, 99);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 02:36:31 +08:00
|
|
|
TEST(CorruptionTest, TableFileRepair) {
|
|
|
|
options_.block_size = 2 * kValueSize; // Limit scope of corruption
|
|
|
|
options_.paranoid_checks = true;
|
|
|
|
Reopen();
|
|
|
|
Build(100);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
2018-04-11 07:18:06 +08:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
|
|
|
dbi->TEST_CompactRange(1, nullptr, nullptr);
|
2013-12-11 02:36:31 +08:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
Check(95, 99);
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
TEST(CorruptionTest, TableFileIndexData) {
|
|
|
|
Build(10000); // Enough to build multiple Tables
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
|
|
|
|
2011-04-13 03:38:58 +08:00
|
|
|
Corrupt(kTableFile, -2000, 500);
|
2011-03-19 06:37:00 +08:00
|
|
|
Reopen();
|
|
|
|
Check(5000, 9999);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, MissingDescriptor) {
|
|
|
|
Build(1000);
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
Check(1000, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, SequenceNumberRecovery) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, CorruptedDescriptor) {
|
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
2018-04-11 07:18:06 +08:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
2011-03-19 06:37:00 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, CompactionInputError) {
|
|
|
|
Build(10);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
2011-07-15 08:20:57 +08:00
|
|
|
const int last = config::kMaxMemCompactLevel;
|
2011-06-22 10:36:45 +08:00
|
|
|
ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
2013-08-22 02:12:47 +08:00
|
|
|
Check(5, 9);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Force compactions by writing lots of values
|
|
|
|
Build(10000);
|
|
|
|
Check(10000, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, CompactionInputErrorParanoid) {
|
2013-08-22 02:12:47 +08:00
|
|
|
options_.paranoid_checks = true;
|
|
|
|
options_.write_buffer_size = 512 << 10;
|
|
|
|
Reopen();
|
2011-06-22 10:36:45 +08:00
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
// Make multiple inputs so we need to compact.
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
Build(10);
|
2011-06-22 10:36:45 +08:00
|
|
|
dbi->TEST_CompactMemTable();
|
2013-08-22 02:12:47 +08:00
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
env_.SleepForMicroseconds(100000);
|
2011-06-22 10:36:45 +08:00
|
|
|
}
|
2018-04-11 07:18:06 +08:00
|
|
|
dbi->CompactRange(nullptr, nullptr);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
// Write must fail because of corrupted table
|
2011-03-19 06:37:00 +08:00
|
|
|
std::string tmp1, tmp2;
|
2013-08-22 02:12:47 +08:00
|
|
|
Status s = db_->Put(WriteOptions(), Key(5, &tmp1), Value(5, &tmp2));
|
2011-03-19 06:37:00 +08:00
|
|
|
ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CorruptionTest, UnrelatedKeys) {
|
|
|
|
Build(10);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_CompactMemTable();
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
|
|
|
|
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);
|
|
|
|
dbi->TEST_CompactMemTable();
|
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
|
|
|
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2019-03-02 05:12:01 +08:00
|
|
|
#if defined(LEVELDB_PLATFORM_WINDOWS)
|
|
|
|
// When Windows maps the contents of a file into memory, even if read/write,
|
|
|
|
// subsequent attempts to open that file for write access will fail. Forcing
|
|
|
|
// all RandomAccessFile instances to use base file I/O (e.g. ReadFile)
|
|
|
|
// allows these tests to open files in order to corrupt their contents.
|
|
|
|
leveldb::CorruptionTest::SetFileLimits(0);
|
|
|
|
|
|
|
|
// Allow this test to write to (and corrupt) files which are normally
|
|
|
|
// open for exclusive read access.
|
|
|
|
leveldb::CorruptionTest::RelaxFilePermissions();
|
|
|
|
#endif // defined(LEVELDB_PLATFORM_WINDOWS)
|
2011-03-19 06:37:00 +08:00
|
|
|
return leveldb::test::RunAllTests();
|
|
|
|
}
|