2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02: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 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01: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-15 01:44:35 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-04-04 03:27:24 +02:00
|
|
|
#include <inttypes.h>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-04-04 03:27:24 +02:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/log_format.h"
|
|
|
|
#include "db/version_set.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/cache.h"
|
|
|
|
#include "rocksdb/env.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/write_batch.h"
|
2017-04-04 03:27:24 +02:00
|
|
|
#include "util/filename.h"
|
2017-04-06 23:49:13 +02:00
|
|
|
#include "util/string_util.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
static const int kValueSize = 1000;
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class CorruptionTest : public testing::Test {
|
2011-03-18 23:37:00 +01:00
|
|
|
public:
|
|
|
|
test::ErrorEnv env_;
|
|
|
|
std::string dbname_;
|
2013-01-20 11:07:13 +01:00
|
|
|
shared_ptr<Cache> tiny_cache_;
|
2011-03-18 23:37:00 +01:00
|
|
|
Options options_;
|
|
|
|
DB* db_;
|
|
|
|
|
2011-06-22 04:36:45 +02:00
|
|
|
CorruptionTest() {
|
2017-01-27 15:35:41 +01: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 20:15:02 +01:00
|
|
|
tiny_cache_ = NewLRUCache(100, 4);
|
2016-04-08 02:40:42 +02:00
|
|
|
options_.wal_recovery_mode = WALRecoveryMode::kTolerateCorruptedTailRecords;
|
2011-03-18 23:37:00 +01:00
|
|
|
options_.env = &env_;
|
2014-04-24 20:13:28 +02:00
|
|
|
dbname_ = test::TmpDir() + "/corruption_test";
|
2011-03-18 23:37:00 +01:00
|
|
|
DestroyDB(dbname_, options_);
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
db_ = nullptr;
|
2011-03-18 23:37:00 +01:00
|
|
|
options_.create_if_missing = true;
|
2014-08-25 23:22:05 +02: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 23:37:00 +01:00
|
|
|
Reopen();
|
|
|
|
options_.create_if_missing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CorruptionTest() {
|
|
|
|
delete db_;
|
|
|
|
DestroyDB(dbname_, Options());
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:13:49 +02:00
|
|
|
void CloseDb() {
|
|
|
|
delete db_;
|
|
|
|
db_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
Status TryReopen(Options* options = nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
delete db_;
|
2013-03-01 03:04:58 +01:00
|
|
|
db_ = nullptr;
|
2011-03-18 23:37:00 +01:00
|
|
|
Options opt = (options ? *options : options_);
|
|
|
|
opt.env = &env_;
|
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 21:42:23 +02:00
|
|
|
opt.arena_block_size = 4096;
|
2014-08-25 23:22:05 +02: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 23:37:00 +01:00
|
|
|
return DB::Open(opt, dbname_, &db_);
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
void Reopen(Options* options = nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_OK(TryReopen(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RepairDB() {
|
|
|
|
delete db_;
|
2013-03-01 03:04:58 +01:00
|
|
|
db_ = nullptr;
|
2013-10-04 06:49:15 +02:00
|
|
|
ASSERT_OK(::rocksdb::RepairDB(dbname_, options_));
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2015-10-05 23:46:28 +02:00
|
|
|
void Build(int n, int flush_every = 0) {
|
2011-03-18 23:37:00 +01:00
|
|
|
std::string key_space, value_space;
|
|
|
|
WriteBatch batch;
|
|
|
|
for (int i = 0; i < n; i++) {
|
2015-10-05 23:46:28 +02:00
|
|
|
if (flush_every != 0 && i != 0 && i % flush_every == 0) {
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
dbi->TEST_FlushMemTable();
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
//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));
|
|
|
|
ASSERT_OK(db_->Write(WriteOptions(), &batch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check(int min_expected, int max_expected) {
|
2015-11-19 20:47:12 +01:00
|
|
|
uint64_t next_expected = 0;
|
|
|
|
uint64_t missed = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
int bad_keys = 0;
|
|
|
|
int bad_values = 0;
|
|
|
|
int correct = 0;
|
|
|
|
std::string value_space;
|
2014-02-01 02:16:30 +01: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 11:14:27 +02:00
|
|
|
// occurred.
|
2014-02-01 02:16:30 +01:00
|
|
|
Iterator* iter = db_->NewIterator(ReadOptions(false, true));
|
2011-03-18 23:37:00 +01:00
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
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 20:47:12 +01:00
|
|
|
next_expected = key + 1;
|
2014-11-11 22:47:22 +01:00
|
|
|
if (iter->value() != Value(static_cast<int>(key), &value_space)) {
|
2011-03-18 23:37:00 +01:00
|
|
|
bad_values++;
|
|
|
|
} else {
|
|
|
|
correct++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2015-11-19 20:47:12 +01: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 23:37:00 +01:00
|
|
|
ASSERT_LE(min_expected, correct);
|
|
|
|
ASSERT_GE(max_expected, correct);
|
|
|
|
}
|
|
|
|
|
2014-09-26 17:43:47 +02:00
|
|
|
void CorruptFile(const std::string& fname, int offset, int bytes_to_corrupt) {
|
2011-03-18 23:37:00 +01:00
|
|
|
struct stat sbuf;
|
2011-03-21 20:40:57 +01: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 {
|
2014-11-11 22:47:22 +01:00
|
|
|
offset = static_cast<int>(sbuf.st_size + offset);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset > sbuf.st_size) {
|
2014-11-11 22:47:22 +01:00
|
|
|
offset = static_cast<int>(sbuf.st_size);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
if (offset + bytes_to_corrupt > sbuf.st_size) {
|
2014-11-11 22:47:22 +01:00
|
|
|
bytes_to_corrupt = static_cast<int>(sbuf.st_size - offset);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2014-04-24 20:13:28 +02:00
|
|
|
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;
|
|
|
|
std::string fname;
|
|
|
|
int picked_number = -1;
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
2014-04-24 20:13:28 +02: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 22:47:22 +01:00
|
|
|
picked_number = static_cast<int>(number);
|
2014-04-24 20:13:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!fname.empty()) << filetype;
|
|
|
|
|
|
|
|
CorruptFile(fname, offset, bytes_to_corrupt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
CorruptFile(dbname_ + "/" + m.name, offset, bytes_to_corrupt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(false) << "no file found at level";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 21:38:58 +02: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 23:37:00 +01: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 17:07:08 +01: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, ' ');
|
|
|
|
return Slice(*storage);
|
|
|
|
} else {
|
|
|
|
Random r(k);
|
|
|
|
return test::RandomString(&r, kValueSize, storage);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, Recovery) {
|
2011-03-21 20:40:57 +01:00
|
|
|
Build(100);
|
|
|
|
Check(100, 100);
|
2015-07-02 01:13:49 +02: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
|
2011-03-18 23:37:00 +01:00
|
|
|
Corrupt(kLogFile, 19, 1); // WriteBatch tag for first record
|
2011-03-21 20:40:57 +01:00
|
|
|
Corrupt(kLogFile, log::kBlockSize + 1000, 1); // Somewhere in second block
|
2015-01-05 19:26:34 +01:00
|
|
|
ASSERT_TRUE(!TryReopen().ok());
|
|
|
|
options_.paranoid_checks = false;
|
|
|
|
Reopen(&options_);
|
2011-03-21 20:40:57 +01:00
|
|
|
|
|
|
|
// The 64 records in the first two log blocks are completely lost.
|
|
|
|
Check(36, 36);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, RecoverWriteError) {
|
2011-03-18 23:37:00 +01:00
|
|
|
env_.writable_file_error_ = true;
|
|
|
|
Status s = TryReopen();
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, NewFileErrorDuringWrite) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// Do enough writing to force minor compaction
|
|
|
|
env_.writable_file_error_ = true;
|
2014-11-11 22:47:22 +01:00
|
|
|
const int num =
|
|
|
|
static_cast<int>(3 + (Options().write_buffer_size / kValueSize));
|
2011-03-18 23:37:00 +01:00
|
|
|
std::string value_storage;
|
|
|
|
Status s;
|
2013-10-28 20:36:02 +01:00
|
|
|
bool failed = false;
|
|
|
|
for (int i = 0; i < num; i++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
WriteBatch batch;
|
|
|
|
batch.Put("a", Value(100, &value_storage));
|
|
|
|
s = db_->Write(WriteOptions(), &batch);
|
2013-10-28 20:36:02 +01:00
|
|
|
if (!s.ok()) {
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!failed || !s.ok());
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(!s.ok());
|
|
|
|
ASSERT_GE(env_.num_writable_file_errors_, 1);
|
|
|
|
env_.writable_file_error_ = false;
|
|
|
|
Reopen();
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, TableFile) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Build(100);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2013-03-01 03:04:58 +01:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
|
|
|
dbi->TEST_CompactRange(1, nullptr, nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
Check(99, 99);
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, TableFileIndexData) {
|
2015-10-05 23:46:28 +02: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);
|
2011-03-18 23:37:00 +01:00
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-10-05 23:46:28 +02:00
|
|
|
// corrupt an index block of an entire file
|
2011-04-12 21:38:58 +02:00
|
|
|
Corrupt(kTableFile, -2000, 500);
|
2011-03-18 23:37:00 +01:00
|
|
|
Reopen();
|
2015-10-05 23:46:28 +02:00
|
|
|
// one full file should be readable, since only one was corrupted
|
|
|
|
// the other file should be fully non-readable, since index was corrupted
|
|
|
|
Check(5000, 5000);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, MissingDescriptor) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Build(1000);
|
|
|
|
RepairDB();
|
|
|
|
Reopen();
|
|
|
|
Check(1000, 1000);
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, SequenceNumberRecovery) {
|
2011-03-18 23:37:00 +01: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 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, CorruptedDescriptor) {
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2013-03-01 03:04:58 +01:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
2011-03-18 23:37:00 +01: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 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, CompactionInputError) {
|
2014-09-18 22:32:44 +02:00
|
|
|
Options options;
|
|
|
|
Reopen(&options);
|
2011-03-18 23:37:00 +01:00
|
|
|
Build(10);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2015-07-17 21:02:52 +02:00
|
|
|
dbi->TEST_CompactRange(0, nullptr, nullptr);
|
|
|
|
dbi->TEST_CompactRange(1, nullptr, nullptr);
|
|
|
|
ASSERT_EQ(1, Property("rocksdb.num-files-at-level2"));
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
Corrupt(kTableFile, 100, 1);
|
|
|
|
Check(9, 9);
|
|
|
|
|
|
|
|
// Force compactions by writing lots of values
|
|
|
|
Build(10000);
|
|
|
|
Check(10000, 10000);
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Options options;
|
|
|
|
options.paranoid_checks = true;
|
2014-04-11 21:48:38 +02:00
|
|
|
options.write_buffer_size = 131072;
|
|
|
|
options.max_write_buffer_number = 2;
|
2011-03-18 23:37:00 +01:00
|
|
|
Reopen(&options);
|
2011-06-22 04:36:45 +02:00
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
|
2015-07-17 21:02:52 +02:00
|
|
|
// Fill levels >= 1
|
2012-06-23 04:30:03 +02:00
|
|
|
for (int level = 1; level < dbi->NumberLevels(); level++) {
|
2011-06-22 04:36:45 +02:00
|
|
|
dbi->Put(WriteOptions(), "", "begin");
|
|
|
|
dbi->Put(WriteOptions(), "~", "end");
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2015-07-17 21:02:52 +02:00
|
|
|
for (int comp_level = 0; comp_level < dbi->NumberLevels() - level;
|
|
|
|
++comp_level) {
|
|
|
|
dbi->TEST_CompactRange(comp_level, nullptr, nullptr);
|
|
|
|
}
|
2011-06-22 04:36:45 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2014-04-24 20:13:28 +02:00
|
|
|
Reopen(&options);
|
|
|
|
|
2014-04-24 20:56:37 +02:00
|
|
|
dbi = reinterpret_cast<DBImpl*>(db_);
|
2011-03-18 23:37:00 +01:00
|
|
|
Build(10);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2013-10-16 22:32:53 +02:00
|
|
|
dbi->TEST_WaitForCompact();
|
2013-10-05 07:32:05 +02:00
|
|
|
ASSERT_EQ(1, Property("rocksdb.num-files-at-level0"));
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2014-04-24 20:13:28 +02:00
|
|
|
CorruptTableFileAtLevel(0, 100, 1);
|
2011-03-18 23:37:00 +01:00
|
|
|
Check(9, 9);
|
|
|
|
|
|
|
|
// Write must eventually fail because of corrupted table
|
|
|
|
Status s;
|
|
|
|
std::string tmp1, tmp2;
|
2013-10-28 20:36:02 +01:00
|
|
|
bool failed = false;
|
2014-04-11 21:48:38 +02:00
|
|
|
for (int i = 0; i < 10000; i++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
s = db_->Put(WriteOptions(), Key(i, &tmp1), Value(i, &tmp2));
|
2013-10-28 20:36:02 +01: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 23:37:00 +01:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, UnrelatedKeys) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Build(10);
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2011-03-18 23:37:00 +01:00
|
|
|
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);
|
2013-10-15 00:12:15 +02:00
|
|
|
dbi->TEST_FlushMemTable();
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
|
|
|
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(CorruptionTest, FileSystemStateCorrupted) {
|
2014-03-20 22:18:29 +01:00
|
|
|
for (int iter = 0; iter < 2; ++iter) {
|
|
|
|
Options options;
|
|
|
|
options.paranoid_checks = true;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
Reopen(&options);
|
|
|
|
Build(10);
|
|
|
|
ASSERT_OK(db_->Flush(FlushOptions()));
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
std::vector<LiveFileMetaData> metadata;
|
|
|
|
dbi->GetLiveFilesMetaData(&metadata);
|
2014-03-27 01:53:39 +01:00
|
|
|
ASSERT_GT(metadata.size(), size_t(0));
|
2014-03-20 22:18:29 +01:00
|
|
|
std::string filename = dbname_ + metadata[0].name;
|
|
|
|
|
|
|
|
delete db_;
|
2014-03-20 22:37:30 +01:00
|
|
|
db_ = nullptr;
|
2014-03-20 22:18:29 +01:00
|
|
|
|
|
|
|
if (iter == 0) { // corrupt file size
|
|
|
|
unique_ptr<WritableFile> file;
|
|
|
|
env_.NewWritableFile(filename, &file, EnvOptions());
|
|
|
|
file->Append(Slice("corrupted sst"));
|
|
|
|
file.reset();
|
|
|
|
} else { // delete the file
|
|
|
|
env_.DeleteFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status x = TryReopen(&options);
|
|
|
|
ASSERT_TRUE(x.IsCorruption());
|
|
|
|
DestroyDB(dbname_, options_);
|
|
|
|
Reopen(&options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2015-09-15 01:44:35 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
fprintf(stderr, "SKIPPED as RepairDB() is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|