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.
|
|
|
|
|
|
|
|
#include "db/version_edit.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "util/coding.h"
|
2020-02-25 02:38:00 +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
|
|
|
|
|
|
|
static void TestEncodeDecode(const VersionEdit& edit) {
|
|
|
|
std::string encoded, encoded2;
|
|
|
|
edit.EncodeTo(&encoded);
|
2014-01-14 23:32:37 +00:00
|
|
|
VersionEdit parsed;
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s = parsed.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
parsed.EncodeTo(&encoded2);
|
|
|
|
ASSERT_EQ(encoded, encoded2);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class VersionEditTest : public testing::Test {};
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(VersionEditTest, EncodeDecode) {
|
2011-03-18 22:37:00 +00:00
|
|
|
static const uint64_t kBig = 1ull << 50;
|
2014-11-11 21:47:22 +00:00
|
|
|
static const uint32_t kBig32Bit = 1ull << 30;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-01-14 23:32:37 +00:00
|
|
|
VersionEdit edit;
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
TestEncodeDecode(edit);
|
2014-11-11 21:47:22 +00:00
|
|
|
edit.AddFile(3, kBig + 300 + i, kBig32Bit + 400 + i, 0,
|
2011-04-20 22:48:11 +00:00
|
|
|
InternalKey("foo", kBig + 500 + i, kTypeValue),
|
2013-06-14 05:09:08 +00:00
|
|
|
InternalKey("zoo", kBig + 600 + i, kTypeDeletion),
|
2019-11-23 00:01:21 +00:00
|
|
|
kBig + 500 + i, kBig + 600 + i, false, kInvalidBlobFileNumber,
|
2020-02-10 23:42:46 +00:00
|
|
|
888, 678, "234", "crc32c");
|
2011-03-18 22:37:00 +00:00
|
|
|
edit.DeleteFile(4, kBig + 700 + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
edit.SetComparatorName("foo");
|
|
|
|
edit.SetLogNumber(kBig + 100);
|
|
|
|
edit.SetNextFile(kBig + 200);
|
|
|
|
edit.SetLastSequence(kBig + 1000);
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
2015-10-03 00:32:46 +00:00
|
|
|
TEST_F(VersionEditTest, EncodeDecodeNewFile4) {
|
|
|
|
static const uint64_t kBig = 1ull << 50;
|
|
|
|
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500,
|
2019-11-23 00:01:21 +00:00
|
|
|
kBig + 600, true, kInvalidBlobFileNumber,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownOldestAncesterTime, kUnknownFileCreationTime,
|
|
|
|
kUnknownFileChecksum, kUnknownFileChecksumFuncName);
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.AddFile(4, 301, 3, 100, InternalKey("foo", kBig + 501, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 601, kTypeDeletion), kBig + 501,
|
2019-11-23 00:01:21 +00:00
|
|
|
kBig + 601, false, kInvalidBlobFileNumber,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownOldestAncesterTime, kUnknownFileCreationTime,
|
|
|
|
kUnknownFileChecksum, kUnknownFileChecksumFuncName);
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.AddFile(5, 302, 0, 100, InternalKey("foo", kBig + 502, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 602, kTypeDeletion), kBig + 502,
|
2020-02-10 23:42:46 +00:00
|
|
|
kBig + 602, true, kInvalidBlobFileNumber, 666, 888,
|
|
|
|
kUnknownFileChecksum, kUnknownFileChecksumFuncName);
|
2019-10-14 22:19:31 +00:00
|
|
|
edit.AddFile(5, 303, 0, 100, InternalKey("foo", kBig + 503, kTypeBlobIndex),
|
|
|
|
InternalKey("zoo", kBig + 603, kTypeBlobIndex), kBig + 503,
|
2019-11-27 05:38:38 +00:00
|
|
|
kBig + 603, true, 1001, kUnknownOldestAncesterTime,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownFileCreationTime, kUnknownFileChecksum,
|
|
|
|
kUnknownFileChecksumFuncName);
|
2019-11-27 05:38:38 +00:00
|
|
|
;
|
2015-10-03 00:32:46 +00:00
|
|
|
|
|
|
|
edit.DeleteFile(4, 700);
|
|
|
|
|
|
|
|
edit.SetComparatorName("foo");
|
|
|
|
edit.SetLogNumber(kBig + 100);
|
|
|
|
edit.SetNextFile(kBig + 200);
|
|
|
|
edit.SetLastSequence(kBig + 1000);
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
|
|
|
|
std::string encoded, encoded2;
|
|
|
|
edit.EncodeTo(&encoded);
|
|
|
|
VersionEdit parsed;
|
|
|
|
Status s = parsed.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
auto& new_files = parsed.GetNewFiles();
|
|
|
|
ASSERT_TRUE(new_files[0].second.marked_for_compaction);
|
|
|
|
ASSERT_TRUE(!new_files[1].second.marked_for_compaction);
|
|
|
|
ASSERT_TRUE(new_files[2].second.marked_for_compaction);
|
2019-10-14 22:19:31 +00:00
|
|
|
ASSERT_TRUE(new_files[3].second.marked_for_compaction);
|
2019-09-09 18:22:28 +00:00
|
|
|
ASSERT_EQ(3u, new_files[0].second.fd.GetPathId());
|
|
|
|
ASSERT_EQ(3u, new_files[1].second.fd.GetPathId());
|
|
|
|
ASSERT_EQ(0u, new_files[2].second.fd.GetPathId());
|
2019-10-14 22:19:31 +00:00
|
|
|
ASSERT_EQ(0u, new_files[3].second.fd.GetPathId());
|
|
|
|
ASSERT_EQ(kInvalidBlobFileNumber,
|
|
|
|
new_files[0].second.oldest_blob_file_number);
|
|
|
|
ASSERT_EQ(kInvalidBlobFileNumber,
|
|
|
|
new_files[1].second.oldest_blob_file_number);
|
|
|
|
ASSERT_EQ(kInvalidBlobFileNumber,
|
|
|
|
new_files[2].second.oldest_blob_file_number);
|
|
|
|
ASSERT_EQ(1001, new_files[3].second.oldest_blob_file_number);
|
2015-10-03 00:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, ForwardCompatibleNewFile4) {
|
|
|
|
static const uint64_t kBig = 1ull << 50;
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500,
|
2019-11-23 00:01:21 +00:00
|
|
|
kBig + 600, true, kInvalidBlobFileNumber,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownOldestAncesterTime, kUnknownFileCreationTime,
|
|
|
|
kUnknownFileChecksum, kUnknownFileChecksumFuncName);
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.AddFile(4, 301, 3, 100, InternalKey("foo", kBig + 501, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 601, kTypeDeletion), kBig + 501,
|
2020-02-10 23:42:46 +00:00
|
|
|
kBig + 601, false, kInvalidBlobFileNumber, 686, 868, "234",
|
|
|
|
"crc32c");
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.DeleteFile(4, 700);
|
|
|
|
|
|
|
|
edit.SetComparatorName("foo");
|
|
|
|
edit.SetLogNumber(kBig + 100);
|
|
|
|
edit.SetNextFile(kBig + 200);
|
|
|
|
edit.SetLastSequence(kBig + 1000);
|
|
|
|
|
|
|
|
std::string encoded;
|
|
|
|
|
|
|
|
// Call back function to add extra customized builds.
|
|
|
|
bool first = true;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2015-10-03 00:32:46 +00:00
|
|
|
"VersionEdit::EncodeTo:NewFile4:CustomizeFields", [&](void* arg) {
|
|
|
|
std::string* str = reinterpret_cast<std::string*>(arg);
|
|
|
|
PutVarint32(str, 33);
|
|
|
|
const std::string str1 = "random_string";
|
|
|
|
PutLengthPrefixedSlice(str, str1);
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
PutVarint32(str, 22);
|
|
|
|
const std::string str2 = "s";
|
|
|
|
PutLengthPrefixedSlice(str, str2);
|
|
|
|
}
|
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.EncodeTo(&encoded);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-10-03 00:32:46 +00:00
|
|
|
|
|
|
|
VersionEdit parsed;
|
|
|
|
Status s = parsed.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
ASSERT_TRUE(!first);
|
|
|
|
auto& new_files = parsed.GetNewFiles();
|
|
|
|
ASSERT_TRUE(new_files[0].second.marked_for_compaction);
|
|
|
|
ASSERT_TRUE(!new_files[1].second.marked_for_compaction);
|
2019-09-09 18:22:28 +00:00
|
|
|
ASSERT_EQ(3u, new_files[0].second.fd.GetPathId());
|
|
|
|
ASSERT_EQ(3u, new_files[1].second.fd.GetPathId());
|
2015-10-03 00:32:46 +00:00
|
|
|
ASSERT_EQ(1u, parsed.GetDeletedFiles().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, NewFile4NotSupportedField) {
|
|
|
|
static const uint64_t kBig = 1ull << 50;
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue),
|
|
|
|
InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500,
|
2019-11-23 00:01:21 +00:00
|
|
|
kBig + 600, true, kInvalidBlobFileNumber,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownOldestAncesterTime, kUnknownFileCreationTime,
|
|
|
|
kUnknownFileChecksum, kUnknownFileChecksumFuncName);
|
2015-10-03 00:32:46 +00:00
|
|
|
|
|
|
|
edit.SetComparatorName("foo");
|
|
|
|
edit.SetLogNumber(kBig + 100);
|
|
|
|
edit.SetNextFile(kBig + 200);
|
|
|
|
edit.SetLastSequence(kBig + 1000);
|
|
|
|
|
|
|
|
std::string encoded;
|
|
|
|
|
|
|
|
// Call back function to add extra customized builds.
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2015-10-03 00:32:46 +00:00
|
|
|
"VersionEdit::EncodeTo:NewFile4:CustomizeFields", [&](void* arg) {
|
|
|
|
std::string* str = reinterpret_cast<std::string*>(arg);
|
|
|
|
const std::string str1 = "s";
|
|
|
|
PutLengthPrefixedSlice(str, str1);
|
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-10-03 00:32:46 +00:00
|
|
|
edit.EncodeTo(&encoded);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-10-03 00:32:46 +00:00
|
|
|
|
|
|
|
VersionEdit parsed;
|
|
|
|
Status s = parsed.DecodeFrom(encoded);
|
|
|
|
ASSERT_NOK(s);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(VersionEditTest, EncodeEmptyFile) {
|
2014-10-28 21:27:26 +00:00
|
|
|
VersionEdit edit;
|
2019-10-14 22:19:31 +00:00
|
|
|
edit.AddFile(0, 0, 0, 0, InternalKey(), InternalKey(), 0, 0, false,
|
2019-11-27 05:38:38 +00:00
|
|
|
kInvalidBlobFileNumber, kUnknownOldestAncesterTime,
|
2020-02-10 23:42:46 +00:00
|
|
|
kUnknownFileCreationTime, kUnknownFileChecksum,
|
|
|
|
kUnknownFileChecksumFuncName);
|
2014-10-28 21:27:26 +00:00
|
|
|
std::string buffer;
|
|
|
|
ASSERT_TRUE(!edit.EncodeTo(&buffer));
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(VersionEditTest, ColumnFamilyTest) {
|
2014-01-22 01:01:52 +00:00
|
|
|
VersionEdit edit;
|
2013-12-12 01:46:26 +00:00
|
|
|
edit.SetColumnFamily(2);
|
|
|
|
edit.AddColumnFamily("column_family");
|
2014-03-05 20:13:44 +00:00
|
|
|
edit.SetMaxColumnFamily(5);
|
2013-12-12 01:46:26 +00:00
|
|
|
TestEncodeDecode(edit);
|
|
|
|
|
|
|
|
edit.Clear();
|
|
|
|
edit.SetColumnFamily(3);
|
|
|
|
edit.DropColumnFamily();
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-03 22:35:11 +00:00
|
|
|
TEST_F(VersionEditTest, MinLogNumberToKeep) {
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.SetMinLogNumberToKeep(13);
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
|
|
|
|
edit.Clear();
|
|
|
|
edit.SetMinLogNumberToKeep(23);
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
2018-08-20 21:54:03 +00:00
|
|
|
TEST_F(VersionEditTest, AtomicGroupTest) {
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.MarkAtomicGroup(1);
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
2019-02-11 18:58:30 +00:00
|
|
|
TEST_F(VersionEditTest, IgnorableField) {
|
|
|
|
VersionEdit ve;
|
|
|
|
std::string encoded;
|
|
|
|
|
|
|
|
// Size of ignorable field is too large
|
|
|
|
PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66);
|
|
|
|
// This is a customized ignorable tag
|
|
|
|
PutVarint32Varint64(&encoded,
|
|
|
|
0x2710 /* A field with kTagSafeIgnoreMask set */,
|
|
|
|
5 /* fieldlength 5 */);
|
|
|
|
encoded += "abc"; // Only fills 3 bytes,
|
|
|
|
ASSERT_NOK(ve.DecodeFrom(encoded));
|
|
|
|
|
|
|
|
encoded.clear();
|
|
|
|
// Error when seeing unidentified tag that is not ignorable
|
|
|
|
PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66);
|
|
|
|
// This is a customized ignorable tag
|
|
|
|
PutVarint32Varint64(&encoded, 666 /* A field with kTagSafeIgnoreMask unset */,
|
|
|
|
3 /* fieldlength 3 */);
|
|
|
|
encoded += "abc"; // Fill 3 bytes
|
|
|
|
PutVarint32Varint64(&encoded, 3 /* next file number */, 88);
|
|
|
|
ASSERT_NOK(ve.DecodeFrom(encoded));
|
|
|
|
|
|
|
|
// Safely ignore an identified but safely ignorable entry
|
|
|
|
encoded.clear();
|
|
|
|
PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66);
|
|
|
|
// This is a customized ignorable tag
|
|
|
|
PutVarint32Varint64(&encoded,
|
|
|
|
0x2710 /* A field with kTagSafeIgnoreMask set */,
|
|
|
|
3 /* fieldlength 3 */);
|
|
|
|
encoded += "abc"; // Fill 3 bytes
|
|
|
|
PutVarint32Varint64(&encoded, 3 /* kNextFileNumber */, 88);
|
|
|
|
|
|
|
|
ASSERT_OK(ve.DecodeFrom(encoded));
|
|
|
|
|
2020-02-07 21:25:07 +00:00
|
|
|
ASSERT_TRUE(ve.HasLogNumber());
|
|
|
|
ASSERT_TRUE(ve.HasNextFile());
|
|
|
|
ASSERT_EQ(66, ve.GetLogNumber());
|
|
|
|
ASSERT_EQ(88, ve.GetNextFile());
|
2019-02-11 18:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 15:50:47 +00:00
|
|
|
TEST_F(VersionEditTest, DbId) {
|
|
|
|
VersionEdit edit;
|
|
|
|
edit.SetDBId("ab34-cd12-435f-er00");
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
|
|
|
|
edit.Clear();
|
|
|
|
edit.SetDBId("34ba-cd12-435f-er01");
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
2020-03-11 00:24:38 +00:00
|
|
|
TEST_F(VersionEditTest, BlobFileAdditionAndGarbage) {
|
2020-02-25 02:38:00 +00:00
|
|
|
VersionEdit edit;
|
|
|
|
|
|
|
|
const std::string checksum_method_prefix = "Hash";
|
|
|
|
const std::string checksum_value_prefix = "Value";
|
|
|
|
|
|
|
|
for (uint64_t blob_file_number = 1; blob_file_number <= 10;
|
|
|
|
++blob_file_number) {
|
|
|
|
const uint64_t total_blob_count = blob_file_number << 10;
|
|
|
|
const uint64_t total_blob_bytes = blob_file_number << 20;
|
|
|
|
|
|
|
|
std::string checksum_method(checksum_method_prefix);
|
|
|
|
AppendNumberTo(&checksum_method, blob_file_number);
|
|
|
|
|
|
|
|
std::string checksum_value(checksum_value_prefix);
|
|
|
|
AppendNumberTo(&checksum_value, blob_file_number);
|
|
|
|
|
2020-03-11 00:24:38 +00:00
|
|
|
edit.AddBlobFile(blob_file_number, total_blob_count, total_blob_bytes,
|
|
|
|
checksum_method, checksum_value);
|
|
|
|
|
|
|
|
const uint64_t garbage_blob_count = total_blob_count >> 2;
|
|
|
|
const uint64_t garbage_blob_bytes = total_blob_bytes >> 1;
|
|
|
|
|
|
|
|
edit.AddBlobFileGarbage(blob_file_number, garbage_blob_count,
|
|
|
|
garbage_blob_bytes);
|
2020-02-25 02:38:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
Define WAL related classes to be used in VersionEdit and VersionSet (#7164)
Summary:
`WalAddition`, `WalDeletion` are defined in `wal_version.h` and used in `VersionEdit`.
`WalAddition` is used to represent events of creating a new WAL (no size, just log number), or closing a WAL (with size).
`WalDeletion` is used to represent events of deleting or archiving a WAL, it means the WAL is no longer alive (won't be replayed during recovery).
`WalSet` is the set of alive WALs kept in `VersionSet`.
1. Why use `WalDeletion` instead of relying on `MinLogNumber` to identify outdated WALs
On recovery, we can compute `MinLogNumber()` based on the log numbers kept in MANIFEST, any log with number < MinLogNumber can be ignored. So it seems that we don't need to persist `WalDeletion` to MANIFEST, since we can ignore the WALs based on MinLogNumber.
But the `MinLogNumber()` is actually a lower bound, it does not exactly mean that logs starting from MinLogNumber must exist. This is because in a corner case, when a column family is empty and never flushed, its log number is set to the largest log number, but not persisted in MANIFEST. So let's say there are 2 column families, when creating the DB, the first WAL has log number 1, so it's persisted to MANIFEST for both column families. Then CF 0 is empty and never flushed, CF 1 is updated and flushed, so a new WAL with log number 2 is created and persisted to MANIFEST for CF 1. But CF 0's log number in MANIFEST is still 1. So on recovery, MinLogNumber is 1, but since log 1 only contains data for CF 1, and CF 1 is flushed, log 1 might have already been deleted from disk.
We can make `MinLogNumber()` be the exactly minimum log number that must exist, by persisting the most recent log number for empty column families that are not flushed. But if there are N such column families, then every time a new WAL is created, we need to add N records to MANIFEST.
In current design, a record is persisted to MANIFEST only when WAL is created, closed, or deleted/archived, so the number of WAL related records are bounded to 3x number of WALs.
2. Why keep `WalSet` in `VersionSet` instead of applying the `VersionEdit`s to `VersionStorageInfo`
`VersionEdit`s are originally designed to track the addition and deletion of SST files. The SST files are related to column families, each column family has a list of `Version`s, and each `Version` keeps the set of active SST files in `VersionStorageInfo`.
But WALs are a concept of DB, they are not bounded to specific column families. So logically it does not make sense to store WALs in a column family's `Version`s.
Also, `Version`'s purpose is to keep reference to SST / blob files, so that they are not deleted until there is no version referencing them. But a WAL is deleted regardless of version references.
So we keep the WALs in `VersionSet` for the purpose of writing out the DB state's snapshot when creating new MANIFESTs.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7164
Test Plan:
make version_edit_test && ./version_edit_test
make wal_edit_test && ./wal_edit_test
Reviewed By: ltamasi
Differential Revision: D22677936
Pulled By: cheng-chang
fbshipit-source-id: 5a3b6890140e572ffd79eb37e6e4c3c32361a859
2020-08-05 23:32:26 +00:00
|
|
|
TEST_F(VersionEditTest, AddWalEncodeDecode) {
|
|
|
|
VersionEdit edit;
|
|
|
|
for (uint64_t log_number = 1; log_number <= 20; log_number++) {
|
|
|
|
WalMetadata meta(rand() % 100);
|
|
|
|
bool has_size = rand() % 2 == 0;
|
|
|
|
if (has_size) {
|
|
|
|
meta.SetSizeInBytes(rand() % 1000);
|
|
|
|
}
|
|
|
|
edit.AddWal(log_number, meta);
|
|
|
|
}
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, AddWalDecodeBadLogNumber) {
|
|
|
|
std::string encoded;
|
|
|
|
PutVarint32(&encoded, Tag::kWalAddition);
|
|
|
|
|
|
|
|
{
|
|
|
|
// No log number.
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding WAL log number") !=
|
|
|
|
std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// log number should be varint64,
|
|
|
|
// but we only encode 128 which is not a valid representation of varint64.
|
|
|
|
char c = 0;
|
|
|
|
unsigned char* ptr = reinterpret_cast<unsigned char*>(&c);
|
|
|
|
*ptr = 128;
|
|
|
|
encoded.append(1, c);
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding WAL log number") !=
|
|
|
|
std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, AddWalDecodeBadTag) {
|
|
|
|
constexpr WalNumber kLogNumber = 100;
|
|
|
|
constexpr uint64_t kSizeInBytes = 100;
|
|
|
|
|
|
|
|
std::string encoded_without_tag;
|
|
|
|
PutVarint32(&encoded_without_tag, Tag::kWalAddition);
|
|
|
|
PutVarint64(&encoded_without_tag, kLogNumber);
|
|
|
|
|
|
|
|
{
|
|
|
|
// No tag.
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded_without_tag);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding tag") != std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Only has size tag, no terminate tag.
|
|
|
|
std::string encoded_with_size = encoded_without_tag;
|
|
|
|
PutVarint32(&encoded_with_size,
|
|
|
|
static_cast<uint32_t>(WalAdditionTag::kSize));
|
|
|
|
PutVarint64(&encoded_with_size, kSizeInBytes);
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded_with_size);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding tag") != std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Only has terminate tag.
|
|
|
|
std::string encoded_with_terminate = encoded_without_tag;
|
|
|
|
PutVarint32(&encoded_with_terminate,
|
|
|
|
static_cast<uint32_t>(WalAdditionTag::kTerminate));
|
|
|
|
VersionEdit edit;
|
|
|
|
ASSERT_OK(edit.DecodeFrom(encoded_with_terminate));
|
|
|
|
auto& wal_addition = edit.GetWalAdditions()[0];
|
|
|
|
ASSERT_EQ(wal_addition.GetLogNumber(), kLogNumber);
|
|
|
|
ASSERT_FALSE(wal_addition.GetMetadata().HasSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, AddWalDecodeNoSize) {
|
|
|
|
constexpr WalNumber kLogNumber = 100;
|
|
|
|
|
|
|
|
std::string encoded;
|
|
|
|
PutVarint32(&encoded, Tag::kWalAddition);
|
|
|
|
PutVarint64(&encoded, kLogNumber);
|
|
|
|
PutVarint32(&encoded, static_cast<uint32_t>(WalAdditionTag::kSize));
|
|
|
|
// No real size after the size tag.
|
|
|
|
|
|
|
|
{
|
|
|
|
// Without terminate tag.
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding WAL file size") !=
|
|
|
|
std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// With terminate tag.
|
|
|
|
PutVarint32(&encoded, static_cast<uint32_t>(WalAdditionTag::kTerminate));
|
|
|
|
VersionEdit edit;
|
|
|
|
Status s = edit.DecodeFrom(encoded);
|
|
|
|
ASSERT_TRUE(s.IsCorruption());
|
|
|
|
// The terminate tag is misunderstood as the size.
|
|
|
|
ASSERT_TRUE(s.ToString().find("Error decoding tag") != std::string::npos)
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, AddWalDebug) {
|
|
|
|
constexpr int n = 2;
|
|
|
|
constexpr std::array<uint64_t, n> kLogNumbers{{10, 20}};
|
|
|
|
constexpr std::array<uint64_t, n> kSizeInBytes{{100, 200}};
|
|
|
|
|
|
|
|
VersionEdit edit;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
edit.AddWal(kLogNumbers[i], WalMetadata(kSizeInBytes[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
const WalAdditions& wals = edit.GetWalAdditions();
|
|
|
|
|
|
|
|
ASSERT_TRUE(edit.HasWalAddition());
|
|
|
|
ASSERT_EQ(wals.size(), n);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
const WalAddition& wal = wals[i];
|
|
|
|
ASSERT_EQ(wal.GetLogNumber(), kLogNumbers[i]);
|
|
|
|
ASSERT_EQ(wal.GetMetadata().GetSizeInBytes(), kSizeInBytes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string expected_str = "VersionEdit {\n";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " WalAddition: log_number: " << kLogNumbers[i]
|
|
|
|
<< " size_in_bytes: " << kSizeInBytes[i] << "\n";
|
|
|
|
expected_str += ss.str();
|
|
|
|
}
|
|
|
|
expected_str += " ColumnFamily: 0\n}\n";
|
|
|
|
ASSERT_EQ(edit.DebugString(true), expected_str);
|
|
|
|
|
|
|
|
std::string expected_json = "{\"EditNumber\": 4, \"WalAdditions\": [";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "{\"LogNumber\": " << kLogNumbers[i] << ", "
|
|
|
|
<< "\"SizeInBytes\": " << kSizeInBytes[i] << "}";
|
|
|
|
if (i < n - 1) ss << ", ";
|
|
|
|
expected_json += ss.str();
|
|
|
|
}
|
|
|
|
expected_json += "], \"ColumnFamily\": 0}";
|
|
|
|
ASSERT_EQ(edit.DebugJSON(4, true), expected_json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, DeleteWalEncodeDecode) {
|
|
|
|
VersionEdit edit;
|
|
|
|
for (uint64_t log_number = 1; log_number <= 20; log_number++) {
|
|
|
|
edit.DeleteWal(log_number);
|
|
|
|
}
|
|
|
|
TestEncodeDecode(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(VersionEditTest, DeleteWalDebug) {
|
|
|
|
constexpr int n = 2;
|
|
|
|
constexpr std::array<uint64_t, n> kLogNumbers{{10, 20}};
|
|
|
|
|
|
|
|
VersionEdit edit;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
edit.DeleteWal(kLogNumbers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const WalDeletions& wals = edit.GetWalDeletions();
|
|
|
|
|
|
|
|
ASSERT_TRUE(edit.HasWalDeletion());
|
|
|
|
ASSERT_EQ(wals.size(), n);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
const WalDeletion& wal = wals[i];
|
|
|
|
ASSERT_EQ(wal.GetLogNumber(), kLogNumbers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string expected_str = "VersionEdit {\n";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " WalDeletion: log_number: " << kLogNumbers[i] << "\n";
|
|
|
|
expected_str += ss.str();
|
|
|
|
}
|
|
|
|
expected_str += " ColumnFamily: 0\n}\n";
|
|
|
|
ASSERT_EQ(edit.DebugString(true), expected_str);
|
|
|
|
|
|
|
|
std::string expected_json = "{\"EditNumber\": 4, \"WalDeletions\": [";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "{\"LogNumber\": " << kLogNumbers[i] << "}";
|
|
|
|
if (i < n - 1) ss << ", ";
|
|
|
|
expected_json += ss.str();
|
|
|
|
}
|
|
|
|
expected_json += "], \"ColumnFamily\": 0}";
|
|
|
|
ASSERT_EQ(edit.DebugJSON(4, true), expected_json);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|