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).
|
2015-07-15 01:08:21 +02: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.
|
|
|
|
|
|
|
|
// Introduction of SyncPoint effectively disabled building and running this test
|
|
|
|
// in Release build.
|
|
|
|
// which is a pity, it is a good test
|
2015-10-23 00:15:37 +02:00
|
|
|
#if !defined(ROCKSDB_LITE)
|
2015-07-15 01:08:21 +02:00
|
|
|
|
2015-10-12 22:05:42 +02:00
|
|
|
#include "db/db_test_util.h"
|
2021-01-26 07:07:26 +01:00
|
|
|
#include "env/mock_env.h"
|
2015-07-15 01:08:21 +02:00
|
|
|
#include "port/stack_trace.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
class DBTestXactLogIterator : public DBTestBase {
|
|
|
|
public:
|
2020-08-18 03:41:20 +02:00
|
|
|
DBTestXactLogIterator()
|
2021-07-23 17:37:27 +02:00
|
|
|
: DBTestBase("db_log_iter_test", /*env_do_fsync=*/true) {}
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
std::unique_ptr<TransactionLogIterator> OpenTransactionLogIter(
|
|
|
|
const SequenceNumber seq) {
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<TransactionLogIterator> iter;
|
2015-07-15 01:08:21 +02:00
|
|
|
Status status = dbfull()->GetUpdatesSince(seq, &iter);
|
|
|
|
EXPECT_OK(status);
|
|
|
|
EXPECT_TRUE(iter->Valid());
|
2015-12-23 21:10:00 +01:00
|
|
|
return iter;
|
2015-07-15 01:08:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2020-12-23 08:44:44 +01:00
|
|
|
SequenceNumber ReadRecords(std::unique_ptr<TransactionLogIterator>& iter,
|
|
|
|
int& count, bool expect_ok = true) {
|
2015-07-15 01:08:21 +02:00
|
|
|
count = 0;
|
|
|
|
SequenceNumber lastSequence = 0;
|
|
|
|
BatchResult res;
|
|
|
|
while (iter->Valid()) {
|
|
|
|
res = iter->GetBatch();
|
|
|
|
EXPECT_TRUE(res.sequence > lastSequence);
|
|
|
|
++count;
|
|
|
|
lastSequence = res.sequence;
|
|
|
|
EXPECT_OK(iter->status());
|
|
|
|
iter->Next();
|
|
|
|
}
|
2020-12-23 08:44:44 +01:00
|
|
|
if (expect_ok) {
|
|
|
|
EXPECT_OK(iter->status());
|
|
|
|
} else {
|
|
|
|
EXPECT_NOK(iter->status());
|
|
|
|
}
|
2015-07-15 01:08:21 +02:00
|
|
|
return res.sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectRecords(
|
|
|
|
const int expected_no_records,
|
|
|
|
std::unique_ptr<TransactionLogIterator>& iter) {
|
|
|
|
int num_records;
|
|
|
|
ReadRecords(iter, num_records);
|
|
|
|
ASSERT_EQ(num_records, expected_no_records);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIterator) {
|
|
|
|
do {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put(0, "key1", DummyString(1024)));
|
|
|
|
ASSERT_OK(Put(1, "key2", DummyString(1024)));
|
|
|
|
ASSERT_OK(Put(1, "key2", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
ASSERT_EQ(dbfull()->GetLatestSequenceNumber(), 3U);
|
|
|
|
{
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ExpectRecords(3, iter);
|
|
|
|
}
|
|
|
|
ReopenWithColumnFamilies({"default", "pikachu"}, options);
|
|
|
|
env_->SleepForMicroseconds(2 * 1000 * 1000);
|
|
|
|
{
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put(0, "key4", DummyString(1024)));
|
|
|
|
ASSERT_OK(Put(1, "key5", DummyString(1024)));
|
|
|
|
ASSERT_OK(Put(0, "key6", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ExpectRecords(6, iter);
|
|
|
|
}
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG // sync point is not included with DNDEBUG build
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorRace) {
|
|
|
|
static const int LOG_ITERATOR_RACE_TEST_COUNT = 2;
|
|
|
|
static const char* sync_points[LOG_ITERATOR_RACE_TEST_COUNT][4] = {
|
|
|
|
{"WalManager::GetSortedWalFiles:1", "WalManager::PurgeObsoleteFiles:1",
|
|
|
|
"WalManager::PurgeObsoleteFiles:2", "WalManager::GetSortedWalFiles:2"},
|
|
|
|
{"WalManager::GetSortedWalsOfType:1",
|
|
|
|
"WalManager::PurgeObsoleteFiles:1",
|
|
|
|
"WalManager::PurgeObsoleteFiles:2",
|
|
|
|
"WalManager::GetSortedWalsOfType:2"}};
|
|
|
|
for (int test = 0; test < LOG_ITERATOR_RACE_TEST_COUNT; ++test) {
|
|
|
|
// Setup sync point dependency to reproduce the race condition of
|
|
|
|
// a log file moved to archived dir, in the middle of GetSortedWalFiles
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
|
|
|
{sync_points[test][0], sync_points[test][1]},
|
|
|
|
{sync_points[test][2], sync_points[test][3]},
|
|
|
|
});
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
do {
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-07-15 01:08:21 +02:00
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key1", DummyString(1024)));
|
|
|
|
ASSERT_OK(dbfull()->Flush(FlushOptions()));
|
|
|
|
ASSERT_OK(Put("key2", DummyString(1024)));
|
|
|
|
ASSERT_OK(dbfull()->Flush(FlushOptions()));
|
|
|
|
ASSERT_OK(Put("key3", DummyString(1024)));
|
|
|
|
ASSERT_OK(dbfull()->Flush(FlushOptions()));
|
|
|
|
ASSERT_OK(Put("key4", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
ASSERT_EQ(dbfull()->GetLatestSequenceNumber(), 4U);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(dbfull()->FlushWAL(false));
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ExpectRecords(4, iter);
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-07-15 01:08:21 +02:00
|
|
|
// trigger async flush, and log move. Well, log move will
|
|
|
|
// wait until the GetSortedWalFiles:1 to reproduce the race
|
|
|
|
// condition
|
|
|
|
FlushOptions flush_options;
|
|
|
|
flush_options.wait = false;
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(dbfull()->Flush(flush_options));
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
// "key5" would be written in a new memtable and log
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key5", DummyString(1024)));
|
|
|
|
ASSERT_OK(dbfull()->FlushWAL(false));
|
2015-07-15 01:08:21 +02:00
|
|
|
{
|
|
|
|
// this iter would miss "key4" if not fixed
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ExpectRecords(5, iter);
|
|
|
|
}
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorStallAtLastRecord) {
|
|
|
|
do {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key1", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ASSERT_OK(iter->status());
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
ASSERT_OK(iter->status());
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key2", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
iter->Next();
|
|
|
|
ASSERT_OK(iter->status());
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorCheckAfterRestart) {
|
|
|
|
do {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key1", DummyString(1024)));
|
|
|
|
ASSERT_OK(Put("key2", DummyString(1023)));
|
|
|
|
ASSERT_OK(dbfull()->Flush(FlushOptions()));
|
2015-07-15 01:08:21 +02:00
|
|
|
Reopen(options);
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
|
|
|
ExpectRecords(2, iter);
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorCorruptedLog) {
|
|
|
|
do {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
2021-08-10 20:08:34 +02:00
|
|
|
|
2015-07-15 01:08:21 +02:00
|
|
|
for (int i = 0; i < 1024; i++) {
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key" + ToString(i), DummyString(10)));
|
2015-07-15 01:08:21 +02:00
|
|
|
}
|
2021-08-10 20:08:34 +02:00
|
|
|
|
|
|
|
ASSERT_OK(Flush());
|
|
|
|
ASSERT_OK(db_->FlushWAL(false));
|
|
|
|
|
2015-07-15 01:08:21 +02:00
|
|
|
// Corrupt this log to create a gap
|
2021-08-10 20:08:34 +02:00
|
|
|
ASSERT_OK(db_->DisableFileDeletions());
|
|
|
|
|
|
|
|
VectorLogPtr wal_files;
|
|
|
|
ASSERT_OK(db_->GetSortedWalFiles(wal_files));
|
|
|
|
ASSERT_FALSE(wal_files.empty());
|
|
|
|
|
2015-07-15 01:08:21 +02:00
|
|
|
const auto logfile_path = dbname_ + "/" + wal_files.front()->PathName();
|
2021-08-10 20:08:34 +02:00
|
|
|
ASSERT_OK(test::TruncateFile(env_, logfile_path,
|
|
|
|
wal_files.front()->SizeFileBytes() / 2));
|
|
|
|
|
|
|
|
ASSERT_OK(db_->EnableFileDeletions());
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
// Insert a new entry to a new log file
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put("key1025", DummyString(10)));
|
2021-08-10 20:08:34 +02:00
|
|
|
ASSERT_OK(db_->FlushWAL(false));
|
|
|
|
|
2015-07-15 01:08:21 +02:00
|
|
|
// Try to read from the beginning. Should stop before the gap and read less
|
|
|
|
// than 1025 entries
|
|
|
|
auto iter = OpenTransactionLogIter(0);
|
2021-08-10 20:08:34 +02:00
|
|
|
int count = 0;
|
2020-12-23 08:44:44 +01:00
|
|
|
SequenceNumber last_sequence_read = ReadRecords(iter, count, false);
|
2015-07-15 01:08:21 +02:00
|
|
|
ASSERT_LT(last_sequence_read, 1025U);
|
2021-08-10 20:08:34 +02:00
|
|
|
|
2015-07-15 01:08:21 +02:00
|
|
|
// Try to read past the gap, should be able to seek to key1025
|
|
|
|
auto iter2 = OpenTransactionLogIter(last_sequence_read + 1);
|
|
|
|
ExpectRecords(1, iter2);
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorBatchOperations) {
|
|
|
|
do {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
WriteBatch batch;
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(batch.Put(handles_[1], "key1", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.Put(handles_[0], "key2", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.Put(handles_[1], "key3", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.Delete(handles_[0], "key2"));
|
|
|
|
ASSERT_OK(dbfull()->Write(WriteOptions(), &batch));
|
|
|
|
ASSERT_OK(Flush(1));
|
|
|
|
ASSERT_OK(Flush(0));
|
2015-07-15 01:08:21 +02:00
|
|
|
ReopenWithColumnFamilies({"default", "pikachu"}, options);
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(Put(1, "key4", DummyString(1024)));
|
2015-07-15 01:08:21 +02:00
|
|
|
auto iter = OpenTransactionLogIter(3);
|
|
|
|
ExpectRecords(2, iter);
|
|
|
|
} while (ChangeCompactOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DBTestXactLogIterator, TransactionLogIteratorBlobs) {
|
|
|
|
Options options = OptionsForLogIterTest();
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
{
|
|
|
|
WriteBatch batch;
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(batch.Put(handles_[1], "key1", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.Put(handles_[0], "key2", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.PutLogData(Slice("blob1")));
|
|
|
|
ASSERT_OK(batch.Put(handles_[1], "key3", DummyString(1024)));
|
|
|
|
ASSERT_OK(batch.PutLogData(Slice("blob2")));
|
|
|
|
ASSERT_OK(batch.Delete(handles_[0], "key2"));
|
|
|
|
ASSERT_OK(dbfull()->Write(WriteOptions(), &batch));
|
2015-07-15 01:08:21 +02:00
|
|
|
ReopenWithColumnFamilies({"default", "pikachu"}, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res = OpenTransactionLogIter(0)->GetBatch();
|
|
|
|
struct Handler : public WriteBatch::Handler {
|
|
|
|
std::string seen;
|
2019-02-14 22:52:47 +01:00
|
|
|
Status PutCF(uint32_t cf, const Slice& key, const Slice& value) override {
|
2015-07-15 01:08:21 +02:00
|
|
|
seen += "Put(" + ToString(cf) + ", " + key.ToString() + ", " +
|
|
|
|
ToString(value.size()) + ")";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
Status MergeCF(uint32_t cf, const Slice& key, const Slice& value) override {
|
2015-07-15 01:08:21 +02:00
|
|
|
seen += "Merge(" + ToString(cf) + ", " + key.ToString() + ", " +
|
|
|
|
ToString(value.size()) + ")";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
void LogData(const Slice& blob) override {
|
2015-07-15 01:08:21 +02:00
|
|
|
seen += "LogData(" + blob.ToString() + ")";
|
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
Status DeleteCF(uint32_t cf, const Slice& key) override {
|
2015-07-15 01:08:21 +02:00
|
|
|
seen += "Delete(" + ToString(cf) + ", " + key.ToString() + ")";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
} handler;
|
2020-12-23 08:44:44 +01:00
|
|
|
ASSERT_OK(res.writeBatchPtr->Iterate(&handler));
|
2015-07-15 01:08:21 +02:00
|
|
|
ASSERT_EQ(
|
|
|
|
"Put(1, key1, 1024)"
|
|
|
|
"Put(0, key2, 1024)"
|
|
|
|
"LogData(blob1)"
|
|
|
|
"Put(1, key3, 1024)"
|
|
|
|
"LogData(blob2)"
|
|
|
|
"Delete(0, key2)",
|
|
|
|
handler.seen);
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2015-07-15 01:08:21 +02:00
|
|
|
|
2015-10-23 00:15:37 +02:00
|
|
|
#endif // !defined(ROCKSDB_LITE)
|
2015-07-15 01:08:21 +02:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-10-23 00:15:37 +02:00
|
|
|
#if !defined(ROCKSDB_LITE)
|
2020-02-20 21:07:53 +01:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-07-15 01:08:21 +02:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
#else
|
2018-04-16 02:19:57 +02:00
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
2015-07-15 01:08:21 +02:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|