2016-11-14 03:58:17 +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).
|
2016-11-14 03:58:17 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
|
|
|
#include "db/memtable.h"
|
2018-12-18 02:26:56 +01:00
|
|
|
#include "db/range_del_aggregator.h"
|
2016-11-14 03:58:17 +01:00
|
|
|
#include "port/stack_trace.h"
|
|
|
|
#include "rocksdb/memtablerep.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class DBMemTableTest : public DBTestBase {
|
|
|
|
public:
|
|
|
|
DBMemTableTest() : DBTestBase("/db_memtable_test") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockMemTableRep : public MemTableRep {
|
|
|
|
public:
|
2017-06-02 23:13:59 +02:00
|
|
|
explicit MockMemTableRep(Allocator* allocator, MemTableRep* rep)
|
2016-11-14 03:58:17 +01:00
|
|
|
: MemTableRep(allocator), rep_(rep), num_insert_with_hint_(0) {}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
KeyHandle Allocate(const size_t len, char** buf) override {
|
2016-11-14 03:58:17 +01:00
|
|
|
return rep_->Allocate(len, buf);
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void Insert(KeyHandle handle) override { rep_->Insert(handle); }
|
2016-11-14 03:58:17 +01:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void InsertWithHint(KeyHandle handle, void** hint) override {
|
2016-11-14 03:58:17 +01:00
|
|
|
num_insert_with_hint_++;
|
2018-02-01 03:45:49 +01:00
|
|
|
EXPECT_NE(nullptr, hint);
|
2016-11-14 03:58:17 +01:00
|
|
|
last_hint_in_ = *hint;
|
2018-02-16 02:12:48 +01:00
|
|
|
rep_->InsertWithHint(handle, hint);
|
2016-11-14 03:58:17 +01:00
|
|
|
last_hint_out_ = *hint;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool Contains(const char* key) const override { return rep_->Contains(key); }
|
2016-11-14 03:58:17 +01:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
void Get(const LookupKey& k, void* callback_args,
|
|
|
|
bool (*callback_func)(void* arg, const char* entry)) override {
|
2016-11-14 03:58:17 +01:00
|
|
|
rep_->Get(k, callback_args, callback_func);
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
size_t ApproximateMemoryUsage() override {
|
2016-11-14 03:58:17 +01:00
|
|
|
return rep_->ApproximateMemoryUsage();
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
Iterator* GetIterator(Arena* arena) override {
|
2016-11-14 03:58:17 +01:00
|
|
|
return rep_->GetIterator(arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* last_hint_in() { return last_hint_in_; }
|
|
|
|
void* last_hint_out() { return last_hint_out_; }
|
|
|
|
int num_insert_with_hint() { return num_insert_with_hint_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<MemTableRep> rep_;
|
|
|
|
void* last_hint_in_;
|
|
|
|
void* last_hint_out_;
|
|
|
|
int num_insert_with_hint_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockMemTableRepFactory : public MemTableRepFactory {
|
|
|
|
public:
|
2019-02-14 22:52:47 +01:00
|
|
|
MemTableRep* CreateMemTableRep(const MemTableRep::KeyComparator& cmp,
|
|
|
|
Allocator* allocator,
|
|
|
|
const SliceTransform* transform,
|
|
|
|
Logger* logger) override {
|
2016-11-14 03:58:17 +01:00
|
|
|
SkipListFactory factory;
|
|
|
|
MemTableRep* skiplist_rep =
|
|
|
|
factory.CreateMemTableRep(cmp, allocator, transform, logger);
|
|
|
|
mock_rep_ = new MockMemTableRep(allocator, skiplist_rep);
|
|
|
|
return mock_rep_;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
MemTableRep* CreateMemTableRep(const MemTableRep::KeyComparator& cmp,
|
|
|
|
Allocator* allocator,
|
|
|
|
const SliceTransform* transform,
|
|
|
|
Logger* logger,
|
|
|
|
uint32_t column_family_id) override {
|
2017-06-02 21:08:01 +02:00
|
|
|
last_column_family_id_ = column_family_id;
|
|
|
|
return CreateMemTableRep(cmp, allocator, transform, logger);
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return "MockMemTableRepFactory"; }
|
2016-11-14 03:58:17 +01:00
|
|
|
|
|
|
|
MockMemTableRep* rep() { return mock_rep_; }
|
|
|
|
|
2016-11-16 18:24:52 +01:00
|
|
|
bool IsInsertConcurrentlySupported() const override { return false; }
|
|
|
|
|
2017-06-02 21:08:01 +02:00
|
|
|
uint32_t GetLastColumnFamilyId() { return last_column_family_id_; }
|
|
|
|
|
2016-11-14 03:58:17 +01:00
|
|
|
private:
|
|
|
|
MockMemTableRep* mock_rep_;
|
2017-06-02 21:08:01 +02:00
|
|
|
// workaround since there's no port::kMaxUint32 yet.
|
|
|
|
uint32_t last_column_family_id_ = static_cast<uint32_t>(-1);
|
2016-11-14 03:58:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestPrefixExtractor : public SliceTransform {
|
|
|
|
public:
|
2019-02-14 22:52:47 +01:00
|
|
|
const char* Name() const override { return "TestPrefixExtractor"; }
|
2016-11-14 03:58:17 +01:00
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
Slice Transform(const Slice& key) const override {
|
2016-11-14 03:58:17 +01:00
|
|
|
const char* p = separator(key);
|
|
|
|
if (p == nullptr) {
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
return Slice(key.data(), p - key.data() + 1);
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool InDomain(const Slice& key) const override {
|
2016-11-14 03:58:17 +01:00
|
|
|
return separator(key) != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:52:47 +01:00
|
|
|
bool InRange(const Slice& /*key*/) const override { return false; }
|
2016-11-14 03:58:17 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* separator(const Slice& key) const {
|
|
|
|
return reinterpret_cast<const char*>(memchr(key.data(), '_', key.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-01 03:45:49 +01:00
|
|
|
// Test that ::Add properly returns false when inserting duplicate keys
|
|
|
|
TEST_F(DBMemTableTest, DuplicateSeq) {
|
|
|
|
SequenceNumber seq = 123;
|
|
|
|
std::string value;
|
|
|
|
Status s;
|
|
|
|
MergeContext merge_context;
|
|
|
|
Options options;
|
|
|
|
InternalKeyComparator ikey_cmp(options.comparator);
|
2018-12-18 02:26:56 +01:00
|
|
|
ReadRangeDelAggregator range_del_agg(&ikey_cmp,
|
|
|
|
kMaxSequenceNumber /* upper_bound */);
|
2018-02-01 03:45:49 +01:00
|
|
|
|
|
|
|
// Create a MemTable
|
|
|
|
InternalKeyComparator cmp(BytewiseComparator());
|
|
|
|
auto factory = std::make_shared<SkipListFactory>();
|
|
|
|
options.memtable_factory = factory;
|
|
|
|
ImmutableCFOptions ioptions(options);
|
|
|
|
WriteBufferManager wb(options.db_write_buffer_size);
|
|
|
|
MemTable* mem = new MemTable(cmp, ioptions, MutableCFOptions(options), &wb,
|
|
|
|
kMaxSequenceNumber, 0 /* column_family_id */);
|
|
|
|
|
|
|
|
// Write some keys and make sure it returns false on duplicates
|
|
|
|
bool res;
|
|
|
|
res = mem->Add(seq, kTypeValue, "key", "value2");
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
res = mem->Add(seq, kTypeValue, "key", "value2");
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
// Changing the type should still cause the duplicatae key
|
|
|
|
res = mem->Add(seq, kTypeMerge, "key", "value2");
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
// Changing the seq number will make the key fresh
|
|
|
|
res = mem->Add(seq + 1, kTypeMerge, "key", "value2");
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
// Test with different types for duplicate keys
|
|
|
|
res = mem->Add(seq, kTypeDeletion, "key", "");
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
res = mem->Add(seq, kTypeSingleDeletion, "key", "");
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
|
|
|
|
// Test the duplicate keys under stress
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
bool insert_dup = i % 10 == 1;
|
|
|
|
if (!insert_dup) {
|
|
|
|
seq++;
|
|
|
|
}
|
|
|
|
res = mem->Add(seq, kTypeValue, "foo", "value" + ToString(seq));
|
|
|
|
if (insert_dup) {
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete mem;
|
|
|
|
|
|
|
|
// Test with InsertWithHint
|
|
|
|
options.memtable_insert_with_hint_prefix_extractor.reset(
|
|
|
|
new TestPrefixExtractor()); // which uses _ to extract the prefix
|
|
|
|
ioptions = ImmutableCFOptions(options);
|
|
|
|
mem = new MemTable(cmp, ioptions, MutableCFOptions(options), &wb,
|
|
|
|
kMaxSequenceNumber, 0 /* column_family_id */);
|
|
|
|
// Insert a duplicate key with _ in it
|
|
|
|
res = mem->Add(seq, kTypeValue, "key_1", "value");
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
res = mem->Add(seq, kTypeValue, "key_1", "value");
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
delete mem;
|
|
|
|
|
|
|
|
// Test when InsertConcurrently will be invoked
|
|
|
|
options.allow_concurrent_memtable_write = true;
|
|
|
|
ioptions = ImmutableCFOptions(options);
|
|
|
|
mem = new MemTable(cmp, ioptions, MutableCFOptions(options), &wb,
|
|
|
|
kMaxSequenceNumber, 0 /* column_family_id */);
|
|
|
|
MemTablePostProcessInfo post_process_info;
|
|
|
|
res = mem->Add(seq, kTypeValue, "key", "value", true, &post_process_info);
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
res = mem->Add(seq, kTypeValue, "key", "value", true, &post_process_info);
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
delete mem;
|
|
|
|
}
|
|
|
|
|
2019-05-14 02:43:47 +02:00
|
|
|
// A simple test to verify that the concurrent merge writes is functional
|
|
|
|
TEST_F(DBMemTableTest, ConcurrentMergeWrite) {
|
|
|
|
int num_ops = 1000;
|
|
|
|
std::string value;
|
|
|
|
Status s;
|
|
|
|
MergeContext merge_context;
|
|
|
|
Options options;
|
|
|
|
// A merge operator that is not sensitive to concurrent writes since in this
|
|
|
|
// test we don't order the writes.
|
|
|
|
options.merge_operator = MergeOperators::CreateUInt64AddOperator();
|
|
|
|
|
|
|
|
// Create a MemTable
|
|
|
|
InternalKeyComparator cmp(BytewiseComparator());
|
|
|
|
auto factory = std::make_shared<SkipListFactory>();
|
|
|
|
options.memtable_factory = factory;
|
|
|
|
options.allow_concurrent_memtable_write = true;
|
|
|
|
ImmutableCFOptions ioptions(options);
|
|
|
|
WriteBufferManager wb(options.db_write_buffer_size);
|
|
|
|
MemTable* mem = new MemTable(cmp, ioptions, MutableCFOptions(options), &wb,
|
|
|
|
kMaxSequenceNumber, 0 /* column_family_id */);
|
|
|
|
|
|
|
|
// Put 0 as the base
|
|
|
|
PutFixed64(&value, static_cast<uint64_t>(0));
|
|
|
|
bool res = mem->Add(0, kTypeValue, "key", value);
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
value.clear();
|
|
|
|
|
|
|
|
// Write Merge concurrently
|
|
|
|
rocksdb::port::Thread write_thread1([&]() {
|
2019-05-15 20:18:34 +02:00
|
|
|
MemTablePostProcessInfo post_process_info1;
|
2019-05-14 02:43:47 +02:00
|
|
|
std::string v1;
|
|
|
|
for (int seq = 1; seq < num_ops / 2; seq++) {
|
|
|
|
PutFixed64(&v1, seq);
|
|
|
|
bool res1 =
|
2019-05-15 20:18:34 +02:00
|
|
|
mem->Add(seq, kTypeMerge, "key", v1, true, &post_process_info1);
|
2019-05-14 02:43:47 +02:00
|
|
|
ASSERT_TRUE(res1);
|
|
|
|
v1.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rocksdb::port::Thread write_thread2([&]() {
|
2019-05-15 20:18:34 +02:00
|
|
|
MemTablePostProcessInfo post_process_info2;
|
2019-05-14 02:43:47 +02:00
|
|
|
std::string v2;
|
|
|
|
for (int seq = num_ops / 2; seq < num_ops; seq++) {
|
|
|
|
PutFixed64(&v2, seq);
|
|
|
|
bool res2 =
|
2019-05-15 20:18:34 +02:00
|
|
|
mem->Add(seq, kTypeMerge, "key", v2, true, &post_process_info2);
|
2019-05-14 02:43:47 +02:00
|
|
|
ASSERT_TRUE(res2);
|
|
|
|
v2.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
write_thread1.join();
|
|
|
|
write_thread2.join();
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
ReadOptions roptions;
|
|
|
|
SequenceNumber max_covering_tombstone_seq = 0;
|
|
|
|
LookupKey lkey("key", kMaxSequenceNumber);
|
|
|
|
res = mem->Get(lkey, &value, &status, &merge_context,
|
|
|
|
&max_covering_tombstone_seq, roptions);
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
uint64_t ivalue = DecodeFixed64(Slice(value).data());
|
|
|
|
uint64_t sum = 0;
|
|
|
|
for (int seq = 0; seq < num_ops; seq++) {
|
|
|
|
sum += seq;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ivalue, sum);
|
|
|
|
|
|
|
|
delete mem;
|
|
|
|
}
|
|
|
|
|
2016-11-14 03:58:17 +01:00
|
|
|
TEST_F(DBMemTableTest, InsertWithHint) {
|
|
|
|
Options options;
|
2016-11-16 18:24:52 +01:00
|
|
|
options.allow_concurrent_memtable_write = false;
|
2016-11-14 03:58:17 +01:00
|
|
|
options.create_if_missing = true;
|
|
|
|
options.memtable_factory.reset(new MockMemTableRepFactory());
|
|
|
|
options.memtable_insert_with_hint_prefix_extractor.reset(
|
|
|
|
new TestPrefixExtractor());
|
2017-03-13 17:41:30 +01:00
|
|
|
options.env = env_;
|
2016-11-14 03:58:17 +01:00
|
|
|
Reopen(options);
|
|
|
|
MockMemTableRep* rep =
|
|
|
|
reinterpret_cast<MockMemTableRepFactory*>(options.memtable_factory.get())
|
|
|
|
->rep();
|
|
|
|
ASSERT_OK(Put("foo_k1", "foo_v1"));
|
|
|
|
ASSERT_EQ(nullptr, rep->last_hint_in());
|
|
|
|
void* hint_foo = rep->last_hint_out();
|
|
|
|
ASSERT_OK(Put("foo_k2", "foo_v2"));
|
|
|
|
ASSERT_EQ(hint_foo, rep->last_hint_in());
|
|
|
|
ASSERT_EQ(hint_foo, rep->last_hint_out());
|
|
|
|
ASSERT_OK(Put("foo_k3", "foo_v3"));
|
|
|
|
ASSERT_EQ(hint_foo, rep->last_hint_in());
|
|
|
|
ASSERT_EQ(hint_foo, rep->last_hint_out());
|
|
|
|
ASSERT_OK(Put("bar_k1", "bar_v1"));
|
|
|
|
ASSERT_EQ(nullptr, rep->last_hint_in());
|
|
|
|
void* hint_bar = rep->last_hint_out();
|
|
|
|
ASSERT_NE(hint_foo, hint_bar);
|
|
|
|
ASSERT_OK(Put("bar_k2", "bar_v2"));
|
|
|
|
ASSERT_EQ(hint_bar, rep->last_hint_in());
|
|
|
|
ASSERT_EQ(hint_bar, rep->last_hint_out());
|
|
|
|
ASSERT_EQ(5, rep->num_insert_with_hint());
|
|
|
|
ASSERT_OK(Put("whitelisted", "vvv"));
|
|
|
|
ASSERT_EQ(5, rep->num_insert_with_hint());
|
|
|
|
ASSERT_EQ("foo_v1", Get("foo_k1"));
|
|
|
|
ASSERT_EQ("foo_v2", Get("foo_k2"));
|
|
|
|
ASSERT_EQ("foo_v3", Get("foo_k3"));
|
|
|
|
ASSERT_EQ("bar_v1", Get("bar_k1"));
|
|
|
|
ASSERT_EQ("bar_v2", Get("bar_k2"));
|
|
|
|
ASSERT_EQ("vvv", Get("whitelisted"));
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:08:01 +02:00
|
|
|
TEST_F(DBMemTableTest, ColumnFamilyId) {
|
|
|
|
// Verifies MemTableRepFactory is told the right column family id.
|
|
|
|
Options options;
|
|
|
|
options.allow_concurrent_memtable_write = false;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.memtable_factory.reset(new MockMemTableRepFactory());
|
|
|
|
DestroyAndReopen(options);
|
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
|
2019-09-09 20:22:28 +02:00
|
|
|
for (uint32_t cf = 0; cf < 2; ++cf) {
|
2017-06-02 21:08:01 +02:00
|
|
|
ASSERT_OK(Put(cf, "key", "val"));
|
|
|
|
ASSERT_OK(Flush(cf));
|
|
|
|
ASSERT_EQ(
|
|
|
|
cf, static_cast<MockMemTableRepFactory*>(options.memtable_factory.get())
|
|
|
|
->GetLastColumnFamilyId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 03:58:17 +01:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
rocksdb::port::InstallStackTraceHandler();
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|