作者: 谢瑞阳 10225101483 徐翔宇 10225101535
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 line
9.6 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "db/skiplist.h"
  5. #include <atomic>
  6. #include <set>
  7. #include "gtest/gtest.h"
  8. #include "leveldb/env.h"
  9. #include "port/port.h"
  10. #include "port/thread_annotations.h"
  11. #include "util/arena.h"
  12. #include "util/hash.h"
  13. #include "util/random.h"
  14. #include "util/testutil.h"
  15. namespace leveldb {
  16. typedef uint64_t Key;
  17. struct Comparator {
  18. int operator()(const Key& a, const Key& b) const {
  19. if (a < b) {
  20. return -1;
  21. } else if (a > b) {
  22. return +1;
  23. } else {
  24. return 0;
  25. }
  26. }
  27. };
  28. TEST(SkipTest, Empty) {
  29. Arena arena;
  30. Comparator cmp;
  31. SkipList<Key, Comparator> list(cmp, &arena);
  32. ASSERT_TRUE(!list.Contains(10));
  33. SkipList<Key, Comparator>::Iterator iter(&list);
  34. ASSERT_TRUE(!iter.Valid());
  35. iter.SeekToFirst();
  36. ASSERT_TRUE(!iter.Valid());
  37. iter.Seek(100);
  38. ASSERT_TRUE(!iter.Valid());
  39. iter.SeekToLast();
  40. ASSERT_TRUE(!iter.Valid());
  41. }
  42. TEST(SkipTest, InsertAndLookup) {
  43. const int N = 2000;
  44. const int R = 5000;
  45. Random rnd(1000);
  46. std::set<Key> keys;
  47. Arena arena;
  48. Comparator cmp;
  49. SkipList<Key, Comparator> list(cmp, &arena);
  50. for (int i = 0; i < N; i++) {
  51. Key key = rnd.Next() % R;
  52. if (keys.insert(key).second) {
  53. list.Insert(key);
  54. }
  55. }
  56. for (int i = 0; i < R; i++) {
  57. if (list.Contains(i)) {
  58. ASSERT_EQ(keys.count(i), 1);
  59. } else {
  60. ASSERT_EQ(keys.count(i), 0);
  61. }
  62. }
  63. // Simple iterator tests
  64. {
  65. SkipList<Key, Comparator>::Iterator iter(&list);
  66. ASSERT_TRUE(!iter.Valid());
  67. iter.Seek(0);
  68. ASSERT_TRUE(iter.Valid());
  69. ASSERT_EQ(*(keys.begin()), iter.key());
  70. iter.SeekToFirst();
  71. ASSERT_TRUE(iter.Valid());
  72. ASSERT_EQ(*(keys.begin()), iter.key());
  73. iter.SeekToLast();
  74. ASSERT_TRUE(iter.Valid());
  75. ASSERT_EQ(*(keys.rbegin()), iter.key());
  76. }
  77. // Forward iteration test
  78. for (int i = 0; i < R; i++) {
  79. SkipList<Key, Comparator>::Iterator iter(&list);
  80. iter.Seek(i);
  81. // Compare against model iterator
  82. std::set<Key>::iterator model_iter = keys.lower_bound(i);
  83. for (int j = 0; j < 3; j++) {
  84. if (model_iter == keys.end()) {
  85. ASSERT_TRUE(!iter.Valid());
  86. break;
  87. } else {
  88. ASSERT_TRUE(iter.Valid());
  89. ASSERT_EQ(*model_iter, iter.key());
  90. ++model_iter;
  91. iter.Next();
  92. }
  93. }
  94. }
  95. // Backward iteration test
  96. {
  97. SkipList<Key, Comparator>::Iterator iter(&list);
  98. iter.SeekToLast();
  99. // Compare against model iterator
  100. for (std::set<Key>::reverse_iterator model_iter = keys.rbegin();
  101. model_iter != keys.rend(); ++model_iter) {
  102. ASSERT_TRUE(iter.Valid());
  103. ASSERT_EQ(*model_iter, iter.key());
  104. iter.Prev();
  105. }
  106. ASSERT_TRUE(!iter.Valid());
  107. }
  108. }
  109. // We want to make sure that with a single writer and multiple
  110. // concurrent readers (with no synchronization other than when a
  111. // reader's iterator is created), the reader always observes all the
  112. // data that was present in the skip list when the iterator was
  113. // constructed. Because insertions are happening concurrently, we may
  114. // also observe new values that were inserted since the iterator was
  115. // constructed, but we should never miss any values that were present
  116. // at iterator construction time.
  117. //
  118. // We generate multi-part keys:
  119. // <key,gen,hash>
  120. // where:
  121. // key is in range [0..K-1]
  122. // gen is a generation number for key
  123. // hash is hash(key,gen)
  124. //
  125. // The insertion code picks a random key, sets gen to be 1 + the last
  126. // generation number inserted for that key, and sets hash to Hash(key,gen).
  127. //
  128. // At the beginning of a read, we snapshot the last inserted
  129. // generation number for each key. We then iterate, including random
  130. // calls to Next() and Seek(). For every key we encounter, we
  131. // check that it is either expected given the initial snapshot or has
  132. // been concurrently added since the iterator started.
  133. class ConcurrentTest {
  134. private:
  135. static constexpr uint32_t K = 4;
  136. static uint64_t key(Key key) { return (key >> 40); }
  137. static uint64_t gen(Key key) { return (key >> 8) & 0xffffffffu; }
  138. static uint64_t hash(Key key) { return key & 0xff; }
  139. static uint64_t HashNumbers(uint64_t k, uint64_t g) {
  140. uint64_t data[2] = {k, g};
  141. return Hash(reinterpret_cast<char*>(data), sizeof(data), 0);
  142. }
  143. static Key MakeKey(uint64_t k, uint64_t g) {
  144. static_assert(sizeof(Key) == sizeof(uint64_t), "");
  145. assert(k <= K); // We sometimes pass K to seek to the end of the skiplist
  146. assert(g <= 0xffffffffu);
  147. return ((k << 40) | (g << 8) | (HashNumbers(k, g) & 0xff));
  148. }
  149. static bool IsValidKey(Key k) {
  150. return hash(k) == (HashNumbers(key(k), gen(k)) & 0xff);
  151. }
  152. static Key RandomTarget(Random* rnd) {
  153. switch (rnd->Next() % 10) {
  154. case 0:
  155. // Seek to beginning
  156. return MakeKey(0, 0);
  157. case 1:
  158. // Seek to end
  159. return MakeKey(K, 0);
  160. default:
  161. // Seek to middle
  162. return MakeKey(rnd->Next() % K, 0);
  163. }
  164. }
  165. // Per-key generation
  166. struct State {
  167. std::atomic<int> generation[K];
  168. void Set(int k, int v) {
  169. generation[k].store(v, std::memory_order_release);
  170. }
  171. int Get(int k) { return generation[k].load(std::memory_order_acquire); }
  172. State() {
  173. for (int k = 0; k < K; k++) {
  174. Set(k, 0);
  175. }
  176. }
  177. };
  178. // Current state of the test
  179. State current_;
  180. Arena arena_;
  181. // SkipList is not protected by mu_. We just use a single writer
  182. // thread to modify it.
  183. SkipList<Key, Comparator> list_;
  184. public:
  185. ConcurrentTest() : list_(Comparator(), &arena_) {}
  186. // REQUIRES: External synchronization
  187. void WriteStep(Random* rnd) {
  188. const uint32_t k = rnd->Next() % K;
  189. const intptr_t g = current_.Get(k) + 1;
  190. const Key key = MakeKey(k, g);
  191. list_.Insert(key);
  192. current_.Set(k, g);
  193. }
  194. void ReadStep(Random* rnd) {
  195. // Remember the initial committed state of the skiplist.
  196. State initial_state;
  197. for (int k = 0; k < K; k++) {
  198. initial_state.Set(k, current_.Get(k));
  199. }
  200. Key pos = RandomTarget(rnd);
  201. SkipList<Key, Comparator>::Iterator iter(&list_);
  202. iter.Seek(pos);
  203. while (true) {
  204. Key current;
  205. if (!iter.Valid()) {
  206. current = MakeKey(K, 0);
  207. } else {
  208. current = iter.key();
  209. ASSERT_TRUE(IsValidKey(current)) << current;
  210. }
  211. ASSERT_LE(pos, current) << "should not go backwards";
  212. // Verify that everything in [pos,current) was not present in
  213. // initial_state.
  214. while (pos < current) {
  215. ASSERT_LT(key(pos), K) << pos;
  216. // Note that generation 0 is never inserted, so it is ok if
  217. // <*,0,*> is missing.
  218. ASSERT_TRUE((gen(pos) == 0) ||
  219. (gen(pos) > static_cast<Key>(initial_state.Get(key(pos)))))
  220. << "key: " << key(pos) << "; gen: " << gen(pos)
  221. << "; initgen: " << initial_state.Get(key(pos));
  222. // Advance to next key in the valid key space
  223. if (key(pos) < key(current)) {
  224. pos = MakeKey(key(pos) + 1, 0);
  225. } else {
  226. pos = MakeKey(key(pos), gen(pos) + 1);
  227. }
  228. }
  229. if (!iter.Valid()) {
  230. break;
  231. }
  232. if (rnd->Next() % 2) {
  233. iter.Next();
  234. pos = MakeKey(key(pos), gen(pos) + 1);
  235. } else {
  236. Key new_target = RandomTarget(rnd);
  237. if (new_target > pos) {
  238. pos = new_target;
  239. iter.Seek(new_target);
  240. }
  241. }
  242. }
  243. }
  244. };
  245. // Needed when building in C++11 mode.
  246. constexpr uint32_t ConcurrentTest::K;
  247. // Simple test that does single-threaded testing of the ConcurrentTest
  248. // scaffolding.
  249. TEST(SkipTest, ConcurrentWithoutThreads) {
  250. ConcurrentTest test;
  251. Random rnd(test::RandomSeed());
  252. for (int i = 0; i < 10000; i++) {
  253. test.ReadStep(&rnd);
  254. test.WriteStep(&rnd);
  255. }
  256. }
  257. class TestState {
  258. public:
  259. ConcurrentTest t_;
  260. int seed_;
  261. std::atomic<bool> quit_flag_;
  262. enum ReaderState { STARTING, RUNNING, DONE };
  263. explicit TestState(int s)
  264. : seed_(s), quit_flag_(false), state_(STARTING), state_cv_(&mu_) {}
  265. void Wait(ReaderState s) LOCKS_EXCLUDED(mu_) {
  266. mu_.Lock();
  267. while (state_ != s) {
  268. state_cv_.Wait();
  269. }
  270. mu_.Unlock();
  271. }
  272. void Change(ReaderState s) LOCKS_EXCLUDED(mu_) {
  273. mu_.Lock();
  274. state_ = s;
  275. state_cv_.Signal();
  276. mu_.Unlock();
  277. }
  278. private:
  279. port::Mutex mu_;
  280. ReaderState state_ GUARDED_BY(mu_);
  281. port::CondVar state_cv_ GUARDED_BY(mu_);
  282. };
  283. static void ConcurrentReader(void* arg) {
  284. TestState* state = reinterpret_cast<TestState*>(arg);
  285. Random rnd(state->seed_);
  286. int64_t reads = 0;
  287. state->Change(TestState::RUNNING);
  288. while (!state->quit_flag_.load(std::memory_order_acquire)) {
  289. state->t_.ReadStep(&rnd);
  290. ++reads;
  291. }
  292. state->Change(TestState::DONE);
  293. }
  294. static void RunConcurrent(int run) {
  295. const int seed = test::RandomSeed() + (run * 100);
  296. Random rnd(seed);
  297. const int N = 1000;
  298. const int kSize = 1000;
  299. for (int i = 0; i < N; i++) {
  300. if ((i % 100) == 0) {
  301. std::fprintf(stderr, "Run %d of %d\n", i, N);
  302. }
  303. TestState state(seed + 1);
  304. Env::Default()->Schedule(ConcurrentReader, &state);
  305. state.Wait(TestState::RUNNING);
  306. for (int i = 0; i < kSize; i++) {
  307. state.t_.WriteStep(&rnd);
  308. }
  309. state.quit_flag_.store(true, std::memory_order_release);
  310. state.Wait(TestState::DONE);
  311. }
  312. }
  313. TEST(SkipTest, Concurrent1) { RunConcurrent(1); }
  314. TEST(SkipTest, Concurrent2) { RunConcurrent(2); }
  315. TEST(SkipTest, Concurrent3) { RunConcurrent(3); }
  316. TEST(SkipTest, Concurrent4) { RunConcurrent(4); }
  317. TEST(SkipTest, Concurrent5) { RunConcurrent(5); }
  318. } // namespace leveldb