2014-12-11 08:13:18 -08:00
|
|
|
// Copyright (c) 2014 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2019-11-25 09:29:06 -08:00
|
|
|
#include "gtest/gtest.h"
|
2014-12-11 08:13:18 -08:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "db/filename.h"
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/write_batch.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
class RecoveryTest : public testing::Test {
|
2014-12-11 08:13:18 -08:00
|
|
|
public:
|
2018-04-10 16:18:06 -07:00
|
|
|
RecoveryTest() : env_(Env::Default()), db_(nullptr) {
|
2021-08-03 00:25:26 +00:00
|
|
|
dbname_ = testing::TempDir() + "recovery_test";
|
2014-12-11 08:13:18 -08:00
|
|
|
DestroyDB(dbname_, Options());
|
|
|
|
Open();
|
|
|
|
}
|
|
|
|
|
|
|
|
~RecoveryTest() {
|
|
|
|
Close();
|
|
|
|
DestroyDB(dbname_, Options());
|
|
|
|
}
|
|
|
|
|
|
|
|
DBImpl* dbfull() const { return reinterpret_cast<DBImpl*>(db_); }
|
|
|
|
Env* env() const { return env_; }
|
|
|
|
|
|
|
|
bool CanAppend() {
|
|
|
|
WritableFile* tmp;
|
|
|
|
Status s = env_->NewAppendableFile(CurrentFileName(dbname_), &tmp);
|
|
|
|
delete tmp;
|
|
|
|
if (s.IsNotSupportedError()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
|
|
|
delete db_;
|
2018-04-10 16:18:06 -07:00
|
|
|
db_ = nullptr;
|
2014-12-11 08:13:18 -08:00
|
|
|
}
|
|
|
|
|
2018-04-10 16:18:06 -07:00
|
|
|
Status OpenWithStatus(Options* options = nullptr) {
|
2014-12-11 08:13:18 -08:00
|
|
|
Close();
|
|
|
|
Options opts;
|
2018-04-10 16:18:06 -07:00
|
|
|
if (options != nullptr) {
|
2014-12-11 08:13:18 -08:00
|
|
|
opts = *options;
|
|
|
|
} else {
|
|
|
|
opts.reuse_logs = true; // TODO(sanjay): test both ways
|
|
|
|
opts.create_if_missing = true;
|
|
|
|
}
|
2018-04-10 16:18:06 -07:00
|
|
|
if (opts.env == nullptr) {
|
2014-12-11 08:13:18 -08:00
|
|
|
opts.env = env_;
|
|
|
|
}
|
2017-07-10 13:32:58 -07:00
|
|
|
return DB::Open(opts, dbname_, &db_);
|
|
|
|
}
|
|
|
|
|
2018-04-10 16:18:06 -07:00
|
|
|
void Open(Options* options = nullptr) {
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(OpenWithStatus(options));
|
2014-12-11 08:13:18 -08:00
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Put(const std::string& k, const std::string& v) {
|
|
|
|
return db_->Put(WriteOptions(), k, v);
|
|
|
|
}
|
|
|
|
|
2018-04-10 16:18:06 -07:00
|
|
|
std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) {
|
2014-12-11 08:13:18 -08:00
|
|
|
std::string result;
|
|
|
|
Status s = db_->Get(ReadOptions(), k, &result);
|
|
|
|
if (s.IsNotFound()) {
|
|
|
|
result = "NOT_FOUND";
|
|
|
|
} else if (!s.ok()) {
|
|
|
|
result = s.ToString();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ManifestFileName() {
|
|
|
|
std::string current;
|
2019-11-21 13:09:53 -08:00
|
|
|
EXPECT_LEVELDB_OK(
|
|
|
|
ReadFileToString(env_, CurrentFileName(dbname_), ¤t));
|
2014-12-11 08:13:18 -08:00
|
|
|
size_t len = current.size();
|
2019-05-02 11:01:00 -07:00
|
|
|
if (len > 0 && current[len - 1] == '\n') {
|
2014-12-11 08:13:18 -08:00
|
|
|
current.resize(len - 1);
|
|
|
|
}
|
|
|
|
return dbname_ + "/" + current;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
std::string LogName(uint64_t number) { return LogFileName(dbname_, number); }
|
2014-12-11 08:13:18 -08:00
|
|
|
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
size_t RemoveLogFiles() {
|
2019-03-01 13:12:01 -08:00
|
|
|
// Linux allows unlinking open files, but Windows does not.
|
|
|
|
// Closing the db allows for file deletion.
|
|
|
|
Close();
|
2014-12-11 08:13:18 -08:00
|
|
|
std::vector<uint64_t> logs = GetFiles(kLogFile);
|
|
|
|
for (size_t i = 0; i < logs.size(); i++) {
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
EXPECT_LEVELDB_OK(env_->RemoveFile(LogName(logs[i]))) << LogName(logs[i]);
|
2014-12-11 08:13:18 -08:00
|
|
|
}
|
|
|
|
return logs.size();
|
|
|
|
}
|
|
|
|
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
void RemoveManifestFile() {
|
|
|
|
ASSERT_LEVELDB_OK(env_->RemoveFile(ManifestFileName()));
|
2019-11-21 13:09:53 -08:00
|
|
|
}
|
2017-07-10 13:32:58 -07:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
uint64_t FirstLogFile() { return GetFiles(kLogFile)[0]; }
|
2014-12-11 08:13:18 -08:00
|
|
|
|
2016-04-12 23:38:09 +01:00
|
|
|
std::vector<uint64_t> GetFiles(FileType t) {
|
2014-12-11 08:13:18 -08:00
|
|
|
std::vector<std::string> filenames;
|
2019-11-21 13:09:53 -08:00
|
|
|
EXPECT_LEVELDB_OK(env_->GetChildren(dbname_, &filenames));
|
2016-04-12 23:38:09 +01:00
|
|
|
std::vector<uint64_t> result;
|
2014-12-11 08:13:18 -08:00
|
|
|
for (size_t i = 0; i < filenames.size(); i++) {
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
if (ParseFileName(filenames[i], &number, &type) && type == t) {
|
|
|
|
result.push_back(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
int NumLogs() { return GetFiles(kLogFile).size(); }
|
2014-12-11 08:13:18 -08:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
int NumTables() { return GetFiles(kTableFile).size(); }
|
2014-12-11 08:13:18 -08:00
|
|
|
|
|
|
|
uint64_t FileSize(const std::string& fname) {
|
|
|
|
uint64_t result;
|
2019-11-21 13:09:53 -08:00
|
|
|
EXPECT_LEVELDB_OK(env_->GetFileSize(fname, &result)) << fname;
|
2014-12-11 08:13:18 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void CompactMemTable() { dbfull()->TEST_CompactMemTable(); }
|
2014-12-11 08:13:18 -08:00
|
|
|
|
|
|
|
// Directly construct a log file that sets key to val.
|
|
|
|
void MakeLogFile(uint64_t lognum, SequenceNumber seq, Slice key, Slice val) {
|
|
|
|
std::string fname = LogFileName(dbname_, lognum);
|
|
|
|
WritableFile* file;
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile(fname, &file));
|
2014-12-11 08:13:18 -08:00
|
|
|
log::Writer writer(file);
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.Put(key, val);
|
|
|
|
WriteBatchInternal::SetSequence(&batch, seq);
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(writer.AddRecord(WriteBatchInternal::Contents(&batch)));
|
|
|
|
ASSERT_LEVELDB_OK(file->Flush());
|
2014-12-11 08:13:18 -08:00
|
|
|
delete file;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string dbname_;
|
|
|
|
Env* env_;
|
|
|
|
DB* db_;
|
|
|
|
};
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, ManifestReused) {
|
2014-12-11 08:13:18 -08:00
|
|
|
if (!CanAppend()) {
|
2020-04-29 22:31:41 +00:00
|
|
|
std::fprintf(stderr,
|
|
|
|
"skipping test because env does not support appending\n");
|
2014-12-11 08:13:18 -08:00
|
|
|
return;
|
|
|
|
}
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
2014-12-11 08:13:18 -08:00
|
|
|
Close();
|
|
|
|
std::string old_manifest = ManifestFileName();
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ(old_manifest, ManifestFileName());
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ(old_manifest, ManifestFileName());
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, LargeManifestCompacted) {
|
2014-12-11 08:13:18 -08:00
|
|
|
if (!CanAppend()) {
|
2020-04-29 22:31:41 +00:00
|
|
|
std::fprintf(stderr,
|
|
|
|
"skipping test because env does not support appending\n");
|
2014-12-11 08:13:18 -08:00
|
|
|
return;
|
|
|
|
}
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
2014-12-11 08:13:18 -08:00
|
|
|
Close();
|
|
|
|
std::string old_manifest = ManifestFileName();
|
|
|
|
|
|
|
|
// Pad with zeroes to make manifest file very big.
|
|
|
|
{
|
|
|
|
uint64_t len = FileSize(old_manifest);
|
|
|
|
WritableFile* file;
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(env()->NewAppendableFile(old_manifest, &file));
|
2019-05-02 11:01:00 -07:00
|
|
|
std::string zeroes(3 * 1048576 - static_cast<size_t>(len), 0);
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(file->Append(zeroes));
|
|
|
|
ASSERT_LEVELDB_OK(file->Flush());
|
2014-12-11 08:13:18 -08:00
|
|
|
delete file;
|
|
|
|
}
|
|
|
|
|
|
|
|
Open();
|
|
|
|
std::string new_manifest = ManifestFileName();
|
|
|
|
ASSERT_NE(old_manifest, new_manifest);
|
|
|
|
ASSERT_GT(10000, FileSize(new_manifest));
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ(new_manifest, ManifestFileName());
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, NoLogFiles) {
|
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
ASSERT_EQ(1, RemoveLogFiles());
|
2014-12-11 08:13:18 -08:00
|
|
|
Open();
|
|
|
|
ASSERT_EQ("NOT_FOUND", Get("foo"));
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ("NOT_FOUND", Get("foo"));
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, LogFileReuse) {
|
2014-12-11 08:13:18 -08:00
|
|
|
if (!CanAppend()) {
|
2020-04-29 22:31:41 +00:00
|
|
|
std::fprintf(stderr,
|
|
|
|
"skipping test because env does not support appending\n");
|
2014-12-11 08:13:18 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 2; i++) {
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
2014-12-11 08:13:18 -08:00
|
|
|
if (i == 0) {
|
|
|
|
// Compact to ensure current log is empty
|
|
|
|
CompactMemTable();
|
|
|
|
}
|
|
|
|
Close();
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
uint64_t number = FirstLogFile();
|
|
|
|
if (i == 0) {
|
|
|
|
ASSERT_EQ(0, FileSize(LogName(number)));
|
|
|
|
} else {
|
|
|
|
ASSERT_LT(0, FileSize(LogName(number)));
|
|
|
|
}
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
Open();
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
ASSERT_EQ(number, FirstLogFile()) << "did not reuse log file";
|
|
|
|
ASSERT_EQ("bar", Get("foo"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, MultipleMemTables) {
|
2014-12-11 08:13:18 -08:00
|
|
|
// Make a large log.
|
|
|
|
const int kNum = 1000;
|
|
|
|
for (int i = 0; i < kNum; i++) {
|
|
|
|
char buf[100];
|
2020-04-29 22:31:41 +00:00
|
|
|
std::snprintf(buf, sizeof(buf), "%050d", i);
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(Put(buf, buf));
|
2014-12-11 08:13:18 -08:00
|
|
|
}
|
|
|
|
ASSERT_EQ(0, NumTables());
|
|
|
|
Close();
|
|
|
|
ASSERT_EQ(0, NumTables());
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
uint64_t old_log_file = FirstLogFile();
|
|
|
|
|
|
|
|
// Force creation of multiple memtables by reducing the write buffer size.
|
|
|
|
Options opt;
|
|
|
|
opt.reuse_logs = true;
|
2019-05-02 11:01:00 -07:00
|
|
|
opt.write_buffer_size = (kNum * 100) / 2;
|
2014-12-11 08:13:18 -08:00
|
|
|
Open(&opt);
|
|
|
|
ASSERT_LE(2, NumTables());
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
ASSERT_NE(old_log_file, FirstLogFile()) << "must not reuse log";
|
|
|
|
for (int i = 0; i < kNum; i++) {
|
|
|
|
char buf[100];
|
2020-04-29 22:31:41 +00:00
|
|
|
std::snprintf(buf, sizeof(buf), "%050d", i);
|
2014-12-11 08:13:18 -08:00
|
|
|
ASSERT_EQ(buf, Get(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, MultipleLogFiles) {
|
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
2014-12-11 08:13:18 -08:00
|
|
|
Close();
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
|
|
|
|
// Make a bunch of uncompacted log files.
|
|
|
|
uint64_t old_log = FirstLogFile();
|
2019-05-02 11:01:00 -07:00
|
|
|
MakeLogFile(old_log + 1, 1000, "hello", "world");
|
|
|
|
MakeLogFile(old_log + 2, 1001, "hi", "there");
|
|
|
|
MakeLogFile(old_log + 3, 1002, "foo", "bar2");
|
2014-12-11 08:13:18 -08:00
|
|
|
|
|
|
|
// Recover and check that all log files were processed.
|
|
|
|
Open();
|
|
|
|
ASSERT_LE(1, NumTables());
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
uint64_t new_log = FirstLogFile();
|
2019-05-02 11:01:00 -07:00
|
|
|
ASSERT_LE(old_log + 3, new_log);
|
2014-12-11 08:13:18 -08:00
|
|
|
ASSERT_EQ("bar2", Get("foo"));
|
|
|
|
ASSERT_EQ("world", Get("hello"));
|
|
|
|
ASSERT_EQ("there", Get("hi"));
|
|
|
|
|
|
|
|
// Test that previous recovery produced recoverable state.
|
|
|
|
Open();
|
|
|
|
ASSERT_LE(1, NumTables());
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
if (CanAppend()) {
|
|
|
|
ASSERT_EQ(new_log, FirstLogFile());
|
|
|
|
}
|
|
|
|
ASSERT_EQ("bar2", Get("foo"));
|
|
|
|
ASSERT_EQ("world", Get("hello"));
|
|
|
|
ASSERT_EQ("there", Get("hi"));
|
|
|
|
|
|
|
|
// Check that introducing an older log file does not cause it to be re-read.
|
|
|
|
Close();
|
2019-05-02 11:01:00 -07:00
|
|
|
MakeLogFile(old_log + 1, 2000, "hello", "stale write");
|
2014-12-11 08:13:18 -08:00
|
|
|
Open();
|
|
|
|
ASSERT_LE(1, NumTables());
|
|
|
|
ASSERT_EQ(1, NumLogs());
|
|
|
|
if (CanAppend()) {
|
|
|
|
ASSERT_EQ(new_log, FirstLogFile());
|
|
|
|
}
|
|
|
|
ASSERT_EQ("bar2", Get("foo"));
|
|
|
|
ASSERT_EQ("world", Get("hello"));
|
|
|
|
ASSERT_EQ("there", Get("hi"));
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
TEST_F(RecoveryTest, ManifestMissing) {
|
|
|
|
ASSERT_LEVELDB_OK(Put("foo", "bar"));
|
2017-07-10 13:32:58 -07:00
|
|
|
Close();
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
RemoveManifestFile();
|
2017-07-10 13:32:58 -07:00
|
|
|
|
|
|
|
Status status = OpenWithStatus();
|
|
|
|
ASSERT_TRUE(status.IsCorruption());
|
|
|
|
}
|
|
|
|
|
2014-12-11 08:13:18 -08:00
|
|
|
} // namespace leveldb
|