db_stress_test_base.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #ifdef GFLAGS
  10. #pragma once
  11. #include "db_stress_tool/db_stress_common.h"
  12. #include "db_stress_tool/db_stress_shared_state.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class Transaction;
  15. class TransactionDB;
  16. class StressTest {
  17. public:
  18. StressTest();
  19. virtual ~StressTest();
  20. std::shared_ptr<Cache> NewCache(size_t capacity);
  21. bool BuildOptionsTable();
  22. void InitDb();
  23. void InitReadonlyDb(SharedState*);
  24. // Return false if verification fails.
  25. bool VerifySecondaries();
  26. void OperateDb(ThreadState* thread);
  27. virtual void VerifyDb(ThreadState* thread) const = 0;
  28. virtual void ContinuouslyVerifyDb(ThreadState* /*thread*/) const {}
  29. void PrintStatistics();
  30. protected:
  31. Status AssertSame(DB* db, ColumnFamilyHandle* cf,
  32. ThreadState::SnapshotState& snap_state);
  33. // Currently PreloadDb has to be single-threaded.
  34. void PreloadDbAndReopenAsReadOnly(int64_t number_of_keys,
  35. SharedState* shared);
  36. Status SetOptions(ThreadState* thread);
  37. #ifndef ROCKSDB_LITE
  38. Status NewTxn(WriteOptions& write_opts, Transaction** txn);
  39. Status CommitTxn(Transaction* txn);
  40. Status RollbackTxn(Transaction* txn);
  41. #endif
  42. virtual void MaybeClearOneColumnFamily(ThreadState* /* thread */) {}
  43. virtual bool ShouldAcquireMutexOnKey() const { return false; }
  44. virtual std::vector<int> GenerateColumnFamilies(
  45. const int /* num_column_families */, int rand_column_family) const {
  46. return {rand_column_family};
  47. }
  48. virtual std::vector<int64_t> GenerateKeys(int64_t rand_key) const {
  49. return {rand_key};
  50. }
  51. virtual Status TestGet(ThreadState* thread, const ReadOptions& read_opts,
  52. const std::vector<int>& rand_column_families,
  53. const std::vector<int64_t>& rand_keys) = 0;
  54. virtual std::vector<Status> TestMultiGet(
  55. ThreadState* thread, const ReadOptions& read_opts,
  56. const std::vector<int>& rand_column_families,
  57. const std::vector<int64_t>& rand_keys) = 0;
  58. virtual Status TestPrefixScan(ThreadState* thread,
  59. const ReadOptions& read_opts,
  60. const std::vector<int>& rand_column_families,
  61. const std::vector<int64_t>& rand_keys) = 0;
  62. virtual Status TestPut(ThreadState* thread, WriteOptions& write_opts,
  63. const ReadOptions& read_opts,
  64. const std::vector<int>& cf_ids,
  65. const std::vector<int64_t>& keys, char (&value)[100],
  66. std::unique_ptr<MutexLock>& lock) = 0;
  67. virtual Status TestDelete(ThreadState* thread, WriteOptions& write_opts,
  68. const std::vector<int>& rand_column_families,
  69. const std::vector<int64_t>& rand_keys,
  70. std::unique_ptr<MutexLock>& lock) = 0;
  71. virtual Status TestDeleteRange(ThreadState* thread, WriteOptions& write_opts,
  72. const std::vector<int>& rand_column_families,
  73. const std::vector<int64_t>& rand_keys,
  74. std::unique_ptr<MutexLock>& lock) = 0;
  75. virtual void TestIngestExternalFile(
  76. ThreadState* thread, const std::vector<int>& rand_column_families,
  77. const std::vector<int64_t>& rand_keys,
  78. std::unique_ptr<MutexLock>& lock) = 0;
  79. // Issue compact range, starting with start_key, whose integer value
  80. // is rand_key.
  81. virtual void TestCompactRange(ThreadState* thread, int64_t rand_key,
  82. const Slice& start_key,
  83. ColumnFamilyHandle* column_family);
  84. // Calculate a hash value for all keys in range [start_key, end_key]
  85. // at a certain snapshot.
  86. uint32_t GetRangeHash(ThreadState* thread, const Snapshot* snapshot,
  87. ColumnFamilyHandle* column_family,
  88. const Slice& start_key, const Slice& end_key);
  89. // Return a column family handle that mirrors what is pointed by
  90. // `column_family_id`, which will be used to validate data to be correct.
  91. // By default, the column family itself will be returned.
  92. virtual ColumnFamilyHandle* GetControlCfh(ThreadState* /* thread*/,
  93. int column_family_id) {
  94. return column_families_[column_family_id];
  95. }
  96. #ifndef ROCKSDB_LITE
  97. // Generated a list of keys that close to boundaries of SST keys.
  98. // If there isn't any SST file in the DB, return empty list.
  99. std::vector<std::string> GetWhiteBoxKeys(ThreadState* thread, DB* db,
  100. ColumnFamilyHandle* cfh,
  101. size_t num_keys);
  102. #else // !ROCKSDB_LITE
  103. std::vector<std::string> GetWhiteBoxKeys(ThreadState*, DB*,
  104. ColumnFamilyHandle*, size_t) {
  105. // Not supported in LITE mode.
  106. return {};
  107. }
  108. #endif // !ROCKSDB_LITE
  109. // Given a key K, this creates an iterator which scans to K and then
  110. // does a random sequence of Next/Prev operations.
  111. virtual Status TestIterate(ThreadState* thread, const ReadOptions& read_opts,
  112. const std::vector<int>& rand_column_families,
  113. const std::vector<int64_t>& rand_keys);
  114. // Enum used by VerifyIterator() to identify the mode to validate.
  115. enum LastIterateOp {
  116. kLastOpSeek,
  117. kLastOpSeekForPrev,
  118. kLastOpNextOrPrev,
  119. kLastOpSeekToFirst,
  120. kLastOpSeekToLast
  121. };
  122. // Compare the two iterator, iter and cmp_iter are in the same position,
  123. // unless iter might be made invalidate or undefined because of
  124. // upper or lower bounds, or prefix extractor.
  125. // Will flag failure if the verification fails.
  126. // diverged = true if the two iterator is already diverged.
  127. // True if verification passed, false if not.
  128. // op_logs is the information to print when validation fails.
  129. void VerifyIterator(ThreadState* thread, ColumnFamilyHandle* cmp_cfh,
  130. const ReadOptions& ro, Iterator* iter, Iterator* cmp_iter,
  131. LastIterateOp op, const Slice& seek_key,
  132. const std::string& op_logs, bool* diverged);
  133. virtual Status TestBackupRestore(ThreadState* thread,
  134. const std::vector<int>& rand_column_families,
  135. const std::vector<int64_t>& rand_keys);
  136. virtual Status TestCheckpoint(ThreadState* thread,
  137. const std::vector<int>& rand_column_families,
  138. const std::vector<int64_t>& rand_keys);
  139. void TestCompactFiles(ThreadState* thread, ColumnFamilyHandle* column_family);
  140. Status TestFlush(const std::vector<int>& rand_column_families);
  141. Status TestPauseBackground(ThreadState* thread);
  142. void TestAcquireSnapshot(ThreadState* thread, int rand_column_family,
  143. const std::string& keystr, uint64_t i);
  144. Status MaybeReleaseSnapshots(ThreadState* thread, uint64_t i);
  145. #ifndef ROCKSDB_LITE
  146. Status VerifyGetLiveAndWalFiles(ThreadState* thread);
  147. virtual Status TestApproximateSize(
  148. ThreadState* thread, uint64_t iteration,
  149. const std::vector<int>& rand_column_families,
  150. const std::vector<int64_t>& rand_keys);
  151. #endif // !ROCKSDB_LITE
  152. void VerificationAbort(SharedState* shared, std::string msg, Status s) const;
  153. void VerificationAbort(SharedState* shared, std::string msg, int cf,
  154. int64_t key) const;
  155. void PrintEnv() const;
  156. void Open();
  157. void Reopen(ThreadState* thread);
  158. std::shared_ptr<Cache> cache_;
  159. std::shared_ptr<Cache> compressed_cache_;
  160. std::shared_ptr<const FilterPolicy> filter_policy_;
  161. DB* db_;
  162. #ifndef ROCKSDB_LITE
  163. TransactionDB* txn_db_;
  164. #endif
  165. Options options_;
  166. std::vector<ColumnFamilyHandle*> column_families_;
  167. std::vector<std::string> column_family_names_;
  168. std::atomic<int> new_column_family_name_;
  169. int num_times_reopened_;
  170. std::unordered_map<std::string, std::vector<std::string>> options_table_;
  171. std::vector<std::string> options_index_;
  172. std::atomic<bool> db_preload_finished_;
  173. // Fields used for stress-testing secondary instance in the same process
  174. std::vector<DB*> secondaries_;
  175. std::vector<std::vector<ColumnFamilyHandle*>> secondary_cfh_lists_;
  176. // Fields used for continuous verification from another thread
  177. DB* cmp_db_;
  178. std::vector<ColumnFamilyHandle*> cmp_cfhs_;
  179. };
  180. } // namespace ROCKSDB_NAMESPACE
  181. #endif // GFLAGS