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.
|
|
|
|
|
2017-04-06 20:59:31 +00:00
|
|
|
#include "memtable/skiplist.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <set>
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "memory/arena.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/testharness.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/random.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using Key = uint64_t;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-15 02:37:01 +00:00
|
|
|
struct TestComparator {
|
2011-03-18 22:37:00 +00:00
|
|
|
int operator()(const Key& a, const Key& b) const {
|
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
} else if (a > b) {
|
|
|
|
return +1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class SkipTest : public testing::Test {};
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(SkipTest, Empty) {
|
2014-01-31 01:18:17 +00:00
|
|
|
Arena arena;
|
2013-01-15 02:37:01 +00:00
|
|
|
TestComparator cmp;
|
2014-01-31 01:18:17 +00:00
|
|
|
SkipList<Key, TestComparator> list(cmp, &arena);
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_TRUE(!list.Contains(10));
|
|
|
|
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator>::Iterator iter(&list);
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
iter.SeekToFirst();
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
iter.Seek(100);
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
2016-09-28 01:20:57 +00:00
|
|
|
iter.SeekForPrev(100);
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
2011-03-18 22:37:00 +00:00
|
|
|
iter.SeekToLast();
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(SkipTest, InsertAndLookup) {
|
2011-03-18 22:37:00 +00:00
|
|
|
const int N = 2000;
|
|
|
|
const int R = 5000;
|
|
|
|
Random rnd(1000);
|
|
|
|
std::set<Key> keys;
|
2014-01-31 01:18:17 +00:00
|
|
|
Arena arena;
|
2013-01-15 02:37:01 +00:00
|
|
|
TestComparator cmp;
|
2014-01-31 01:18:17 +00:00
|
|
|
SkipList<Key, TestComparator> list(cmp, &arena);
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
Key key = rnd.Next() % R;
|
|
|
|
if (keys.insert(key).second) {
|
|
|
|
list.Insert(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < R; i++) {
|
|
|
|
if (list.Contains(i)) {
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(keys.count(i), 1U);
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(keys.count(i), 0U);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple iterator tests
|
|
|
|
{
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator>::Iterator iter(&list);
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
|
|
|
|
iter.Seek(0);
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*(keys.begin()), iter.key());
|
|
|
|
|
2016-09-28 01:20:57 +00:00
|
|
|
iter.SeekForPrev(R - 1);
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*(keys.rbegin()), iter.key());
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
iter.SeekToFirst();
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*(keys.begin()), iter.key());
|
|
|
|
|
|
|
|
iter.SeekToLast();
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*(keys.rbegin()), iter.key());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward iteration test
|
|
|
|
for (int i = 0; i < R; i++) {
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator>::Iterator iter(&list);
|
2011-03-18 22:37:00 +00:00
|
|
|
iter.Seek(i);
|
|
|
|
|
|
|
|
// Compare against model iterator
|
|
|
|
std::set<Key>::iterator model_iter = keys.lower_bound(i);
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
if (model_iter == keys.end()) {
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*model_iter, iter.key());
|
|
|
|
++model_iter;
|
|
|
|
iter.Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backward iteration test
|
2016-09-28 01:20:57 +00:00
|
|
|
for (int i = 0; i < R; i++) {
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator>::Iterator iter(&list);
|
2016-09-28 01:20:57 +00:00
|
|
|
iter.SeekForPrev(i);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Compare against model iterator
|
2016-09-28 01:20:57 +00:00
|
|
|
std::set<Key>::iterator model_iter = keys.upper_bound(i);
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
if (model_iter == keys.begin()) {
|
|
|
|
ASSERT_TRUE(!iter.Valid());
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(iter.Valid());
|
|
|
|
ASSERT_EQ(*--model_iter, iter.key());
|
|
|
|
iter.Prev();
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to make sure that with a single writer and multiple
|
|
|
|
// concurrent readers (with no synchronization other than when a
|
|
|
|
// reader's iterator is created), the reader always observes all the
|
|
|
|
// data that was present in the skip list when the iterator was
|
|
|
|
// constructor. Because insertions are happening concurrently, we may
|
|
|
|
// also observe new values that were inserted since the iterator was
|
|
|
|
// constructed, but we should never miss any values that were present
|
|
|
|
// at iterator construction time.
|
|
|
|
//
|
|
|
|
// We generate multi-part keys:
|
|
|
|
// <key,gen,hash>
|
|
|
|
// where:
|
|
|
|
// key is in range [0..K-1]
|
|
|
|
// gen is a generation number for key
|
|
|
|
// hash is hash(key,gen)
|
|
|
|
//
|
|
|
|
// The insertion code picks a random key, sets gen to be 1 + the last
|
|
|
|
// generation number inserted for that key, and sets hash to Hash(key,gen).
|
|
|
|
//
|
|
|
|
// At the beginning of a read, we snapshot the last inserted
|
|
|
|
// generation number for each key. We then iterate, including random
|
|
|
|
// calls to Next() and Seek(). For every key we encounter, we
|
|
|
|
// check that it is either expected given the initial snapshot or has
|
|
|
|
// been concurrently added since the iterator started.
|
|
|
|
class ConcurrentTest {
|
|
|
|
private:
|
|
|
|
static const uint32_t K = 4;
|
|
|
|
|
|
|
|
static uint64_t key(Key key) { return (key >> 40); }
|
|
|
|
static uint64_t gen(Key key) { return (key >> 8) & 0xffffffffu; }
|
|
|
|
static uint64_t hash(Key key) { return key & 0xff; }
|
|
|
|
|
|
|
|
static uint64_t HashNumbers(uint64_t k, uint64_t g) {
|
|
|
|
uint64_t data[2] = { k, g };
|
|
|
|
return Hash(reinterpret_cast<char*>(data), sizeof(data), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Key MakeKey(uint64_t k, uint64_t g) {
|
|
|
|
assert(sizeof(Key) == sizeof(uint64_t));
|
|
|
|
assert(k <= K); // We sometimes pass K to seek to the end of the skiplist
|
|
|
|
assert(g <= 0xffffffffu);
|
|
|
|
return ((k << 40) | (g << 8) | (HashNumbers(k, g) & 0xff));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsValidKey(Key k) {
|
|
|
|
return hash(k) == (HashNumbers(key(k), gen(k)) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Key RandomTarget(Random* rnd) {
|
|
|
|
switch (rnd->Next() % 10) {
|
|
|
|
case 0:
|
|
|
|
// Seek to beginning
|
|
|
|
return MakeKey(0, 0);
|
|
|
|
case 1:
|
|
|
|
// Seek to end
|
|
|
|
return MakeKey(K, 0);
|
|
|
|
default:
|
|
|
|
// Seek to middle
|
|
|
|
return MakeKey(rnd->Next() % K, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Per-key generation
|
|
|
|
struct State {
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<int> generation[K];
|
|
|
|
void Set(int k, int v) {
|
|
|
|
generation[k].store(v, std::memory_order_release);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2014-10-27 21:50:21 +00:00
|
|
|
int Get(int k) { return generation[k].load(std::memory_order_acquire); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
State() {
|
2012-11-06 20:02:18 +00:00
|
|
|
for (unsigned int k = 0; k < K; k++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Set(k, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Current state of the test
|
|
|
|
State current_;
|
|
|
|
|
2014-01-31 01:18:17 +00:00
|
|
|
Arena arena_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// SkipList is not protected by mu_. We just use a single writer
|
|
|
|
// thread to modify it.
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator> list_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
public:
|
2014-01-31 01:18:17 +00:00
|
|
|
ConcurrentTest() : list_(TestComparator(), &arena_) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// REQUIRES: External synchronization
|
|
|
|
void WriteStep(Random* rnd) {
|
|
|
|
const uint32_t k = rnd->Next() % K;
|
2014-10-27 21:50:21 +00:00
|
|
|
const int g = current_.Get(k) + 1;
|
2014-11-06 19:14:28 +00:00
|
|
|
const Key new_key = MakeKey(k, g);
|
|
|
|
list_.Insert(new_key);
|
2011-03-18 22:37:00 +00:00
|
|
|
current_.Set(k, g);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadStep(Random* rnd) {
|
|
|
|
// Remember the initial committed state of the skiplist.
|
|
|
|
State initial_state;
|
2012-11-06 20:02:18 +00:00
|
|
|
for (unsigned int k = 0; k < K; k++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
initial_state.Set(k, current_.Get(k));
|
|
|
|
}
|
|
|
|
|
|
|
|
Key pos = RandomTarget(rnd);
|
2013-01-15 02:37:01 +00:00
|
|
|
SkipList<Key, TestComparator>::Iterator iter(&list_);
|
2011-03-18 22:37:00 +00:00
|
|
|
iter.Seek(pos);
|
|
|
|
while (true) {
|
|
|
|
Key current;
|
|
|
|
if (!iter.Valid()) {
|
|
|
|
current = MakeKey(K, 0);
|
|
|
|
} else {
|
|
|
|
current = iter.key();
|
2011-07-19 23:36:47 +00:00
|
|
|
ASSERT_TRUE(IsValidKey(current)) << current;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
ASSERT_LE(pos, current) << "should not go backwards";
|
|
|
|
|
|
|
|
// Verify that everything in [pos,current) was not present in
|
|
|
|
// initial_state.
|
|
|
|
while (pos < current) {
|
2011-07-19 23:36:47 +00:00
|
|
|
ASSERT_LT(key(pos), K) << pos;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Note that generation 0 is never inserted, so it is ok if
|
|
|
|
// <*,0,*> is missing.
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_TRUE((gen(pos) == 0U) ||
|
2014-11-11 21:47:22 +00:00
|
|
|
(gen(pos) > static_cast<uint64_t>(initial_state.Get(
|
|
|
|
static_cast<int>(key(pos))))))
|
|
|
|
<< "key: " << key(pos) << "; gen: " << gen(pos)
|
|
|
|
<< "; initgen: " << initial_state.Get(static_cast<int>(key(pos)));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Advance to next key in the valid key space
|
|
|
|
if (key(pos) < key(current)) {
|
|
|
|
pos = MakeKey(key(pos) + 1, 0);
|
|
|
|
} else {
|
|
|
|
pos = MakeKey(key(pos), gen(pos) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iter.Valid()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rnd->Next() % 2) {
|
|
|
|
iter.Next();
|
|
|
|
pos = MakeKey(key(pos), gen(pos) + 1);
|
|
|
|
} else {
|
|
|
|
Key new_target = RandomTarget(rnd);
|
|
|
|
if (new_target > pos) {
|
|
|
|
pos = new_target;
|
|
|
|
iter.Seek(new_target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const uint32_t ConcurrentTest::K;
|
|
|
|
|
|
|
|
// Simple test that does single-threaded testing of the ConcurrentTest
|
|
|
|
// scaffolding.
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(SkipTest, ConcurrentWithoutThreads) {
|
2011-03-18 22:37:00 +00:00
|
|
|
ConcurrentTest test;
|
|
|
|
Random rnd(test::RandomSeed());
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
test.ReadStep(&rnd);
|
|
|
|
test.WriteStep(&rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestState {
|
|
|
|
public:
|
|
|
|
ConcurrentTest t_;
|
|
|
|
int seed_;
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<bool> quit_flag_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
enum ReaderState {
|
|
|
|
STARTING,
|
|
|
|
RUNNING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit TestState(int s)
|
2014-10-27 21:50:21 +00:00
|
|
|
: seed_(s), quit_flag_(false), state_(STARTING), state_cv_(&mu_) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
void Wait(ReaderState s) {
|
|
|
|
mu_.Lock();
|
|
|
|
while (state_ != s) {
|
|
|
|
state_cv_.Wait();
|
|
|
|
}
|
|
|
|
mu_.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Change(ReaderState s) {
|
|
|
|
mu_.Lock();
|
|
|
|
state_ = s;
|
|
|
|
state_cv_.Signal();
|
|
|
|
mu_.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
port::Mutex mu_;
|
|
|
|
ReaderState state_;
|
|
|
|
port::CondVar state_cv_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ConcurrentReader(void* arg) {
|
|
|
|
TestState* state = reinterpret_cast<TestState*>(arg);
|
|
|
|
Random rnd(state->seed_);
|
|
|
|
int64_t reads = 0;
|
|
|
|
state->Change(TestState::RUNNING);
|
2014-10-27 21:50:21 +00:00
|
|
|
while (!state->quit_flag_.load(std::memory_order_acquire)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
state->t_.ReadStep(&rnd);
|
|
|
|
++reads;
|
|
|
|
}
|
|
|
|
state->Change(TestState::DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RunConcurrent(int run) {
|
|
|
|
const int seed = test::RandomSeed() + (run * 100);
|
|
|
|
Random rnd(seed);
|
|
|
|
const int N = 1000;
|
|
|
|
const int kSize = 1000;
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
if ((i % 100) == 0) {
|
|
|
|
fprintf(stderr, "Run %d of %d\n", i, N);
|
|
|
|
}
|
|
|
|
TestState state(seed + 1);
|
2017-08-03 22:36:28 +00:00
|
|
|
Env::Default()->SetBackgroundThreads(1);
|
2011-03-18 22:37:00 +00:00
|
|
|
Env::Default()->Schedule(ConcurrentReader, &state);
|
|
|
|
state.Wait(TestState::RUNNING);
|
2014-10-31 18:59:54 +00:00
|
|
|
for (int k = 0; k < kSize; k++) {
|
2011-03-18 22:37:00 +00:00
|
|
|
state.t_.WriteStep(&rnd);
|
|
|
|
}
|
2014-10-27 21:50:21 +00:00
|
|
|
state.quit_flag_.store(true, std::memory_order_release);
|
2011-03-18 22:37:00 +00:00
|
|
|
state.Wait(TestState::DONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(SkipTest, Concurrent1) { RunConcurrent(1); }
|
|
|
|
TEST_F(SkipTest, Concurrent2) { RunConcurrent(2); }
|
|
|
|
TEST_F(SkipTest, Concurrent3) { RunConcurrent(3); }
|
|
|
|
TEST_F(SkipTest, Concurrent4) { RunConcurrent(4); }
|
|
|
|
TEST_F(SkipTest, Concurrent5) { RunConcurrent(5); }
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|