db_stress_driver.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. //
  10. #include "db_stress_tool/db_stress_shared_state.h"
  11. #ifdef GFLAGS
  12. #include "db_stress_tool/db_stress_common.h"
  13. #include "utilities/fault_injection_fs.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. void ThreadBody(void* v) {
  16. ThreadStatusUtil::RegisterThread(db_stress_env, ThreadStatus::USER);
  17. ThreadState* thread = static_cast<ThreadState*>(v);
  18. SharedState* shared = thread->shared;
  19. if (!FLAGS_skip_verifydb && shared->ShouldVerifyAtBeginning()) {
  20. thread->shared->GetStressTest()->VerifyDb(thread);
  21. }
  22. {
  23. MutexLock l(shared->GetMutex());
  24. shared->IncInitialized();
  25. if (shared->AllInitialized()) {
  26. shared->GetCondVar()->SignalAll();
  27. }
  28. }
  29. if (!FLAGS_verification_only) {
  30. {
  31. MutexLock l(shared->GetMutex());
  32. while (!shared->Started()) {
  33. shared->GetCondVar()->Wait();
  34. }
  35. }
  36. thread->shared->GetStressTest()->OperateDb(thread);
  37. {
  38. MutexLock l(shared->GetMutex());
  39. shared->IncOperated();
  40. if (shared->AllOperated()) {
  41. shared->GetCondVar()->SignalAll();
  42. }
  43. while (!shared->VerifyStarted()) {
  44. shared->GetCondVar()->Wait();
  45. }
  46. }
  47. if (!FLAGS_skip_verifydb) {
  48. thread->shared->GetStressTest()->VerifyDb(thread);
  49. }
  50. {
  51. MutexLock l(shared->GetMutex());
  52. shared->IncDone();
  53. if (shared->AllDone()) {
  54. shared->GetCondVar()->SignalAll();
  55. }
  56. }
  57. }
  58. ThreadStatusUtil::UnregisterThread();
  59. }
  60. bool RunStressTestImpl(SharedState* shared) {
  61. SystemClock* clock = db_stress_env->GetSystemClock().get();
  62. StressTest* stress = shared->GetStressTest();
  63. if (shared->ShouldVerifyAtBeginning() && FLAGS_preserve_unverified_changes) {
  64. Status s = InitUnverifiedSubdir(FLAGS_db);
  65. if (s.ok() && !FLAGS_expected_values_dir.empty()) {
  66. s = InitUnverifiedSubdir(FLAGS_expected_values_dir);
  67. }
  68. if (!s.ok()) {
  69. fprintf(stderr, "Failed to setup unverified state dir: %s\n",
  70. s.ToString().c_str());
  71. exit(1);
  72. }
  73. }
  74. stress->InitDb(shared);
  75. stress->FinishInitDb(shared);
  76. uint32_t n = FLAGS_threads;
  77. uint64_t now = clock->NowMicros();
  78. fprintf(stdout, "%s Initializing worker threads\n",
  79. clock->TimeToString(now / 1000000).c_str());
  80. shared->SetThreads(n);
  81. if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
  82. shared->IncBgThreads();
  83. }
  84. if (FLAGS_continuous_verification_interval > 0) {
  85. shared->IncBgThreads();
  86. }
  87. if (FLAGS_compressed_secondary_cache_size > 0 ||
  88. FLAGS_compressed_secondary_cache_ratio > 0.0) {
  89. shared->IncBgThreads();
  90. }
  91. uint32_t remote_compaction_worker_thread_count =
  92. FLAGS_remote_compaction_worker_threads;
  93. if (remote_compaction_worker_thread_count > 0) {
  94. for (uint32_t i = 0; i < remote_compaction_worker_thread_count; i++) {
  95. shared->IncBgThreads();
  96. }
  97. }
  98. std::vector<ThreadState*> threads(n);
  99. for (uint32_t i = 0; i < n; i++) {
  100. threads[i] = new ThreadState(i, shared);
  101. db_stress_env->StartThread(ThreadBody, threads[i]);
  102. }
  103. ThreadState bg_thread(0, shared);
  104. if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
  105. db_stress_env->StartThread(PoolSizeChangeThread, &bg_thread);
  106. }
  107. ThreadState continuous_verification_thread(0, shared);
  108. if (FLAGS_continuous_verification_interval > 0) {
  109. db_stress_env->StartThread(DbVerificationThread,
  110. &continuous_verification_thread);
  111. }
  112. ThreadState compressed_cache_set_capacity_thread(0, shared);
  113. if (FLAGS_compressed_secondary_cache_size > 0 ||
  114. FLAGS_compressed_secondary_cache_ratio > 0.0) {
  115. db_stress_env->StartThread(CompressedCacheSetCapacityThread,
  116. &compressed_cache_set_capacity_thread);
  117. }
  118. std::vector<ThreadState*> remote_compaction_worker_threads;
  119. if (remote_compaction_worker_thread_count > 0) {
  120. remote_compaction_worker_threads.reserve(
  121. remote_compaction_worker_thread_count);
  122. for (uint32_t i = 0; i < remote_compaction_worker_thread_count; i++) {
  123. ThreadState* ts = new ThreadState(i, shared);
  124. remote_compaction_worker_threads.push_back(ts);
  125. db_stress_env->StartThread(RemoteCompactionWorkerThread, ts);
  126. }
  127. }
  128. // Each thread goes through the following states:
  129. // initializing -> wait for others to init -> read/populate/depopulate
  130. // wait for others to operate -> verify -> done
  131. {
  132. MutexLock l(shared->GetMutex());
  133. while (!shared->AllInitialized()) {
  134. shared->GetCondVar()->Wait();
  135. }
  136. if (shared->ShouldVerifyAtBeginning()) {
  137. if (shared->HasVerificationFailedYet()) {
  138. fprintf(stderr, "Crash-recovery verification failed :(\n");
  139. } else {
  140. fprintf(stdout, "Crash-recovery verification passed :)\n");
  141. Status s = DestroyUnverifiedSubdir(FLAGS_db);
  142. if (s.ok() && !FLAGS_expected_values_dir.empty()) {
  143. s = DestroyUnverifiedSubdir(FLAGS_expected_values_dir);
  144. }
  145. if (!s.ok()) {
  146. fprintf(stderr, "Failed to cleanup unverified state dir: %s\n",
  147. s.ToString().c_str());
  148. exit(1);
  149. }
  150. }
  151. }
  152. if (!FLAGS_verification_only) {
  153. // This is after the verification step to avoid making all those `Get()`s
  154. // and `MultiGet()`s contend on the DB-wide trace mutex.
  155. if (!FLAGS_expected_values_dir.empty()) {
  156. stress->TrackExpectedState(shared);
  157. }
  158. if (FLAGS_sync_fault_injection || FLAGS_write_fault_one_in > 0) {
  159. fault_fs_guard->SetFilesystemDirectWritable(false);
  160. fault_fs_guard->SetInjectUnsyncedDataLoss(FLAGS_sync_fault_injection);
  161. if (FLAGS_exclude_wal_from_write_fault_injection) {
  162. fault_fs_guard->SetFileTypesExcludedFromWriteFaultInjection(
  163. {FileType::kWalFile});
  164. }
  165. }
  166. if (ShouldDisableAutoCompactionsBeforeVerifyDb()) {
  167. Status s = stress->EnableAutoCompaction();
  168. assert(s.ok());
  169. }
  170. fprintf(stdout, "%s Starting database operations\n",
  171. clock->TimeToString(now / 1000000).c_str());
  172. shared->SetStart();
  173. shared->GetCondVar()->SignalAll();
  174. while (!shared->AllOperated()) {
  175. shared->GetCondVar()->Wait();
  176. }
  177. now = clock->NowMicros();
  178. if (FLAGS_test_batches_snapshots) {
  179. fprintf(stdout, "%s Limited verification already done during gets\n",
  180. clock->TimeToString((uint64_t)now / 1000000).c_str());
  181. } else if (FLAGS_skip_verifydb) {
  182. fprintf(stdout, "%s Verification skipped\n",
  183. clock->TimeToString((uint64_t)now / 1000000).c_str());
  184. } else {
  185. fprintf(stdout, "%s Starting verification\n",
  186. clock->TimeToString((uint64_t)now / 1000000).c_str());
  187. }
  188. shared->SetStartVerify();
  189. shared->GetCondVar()->SignalAll();
  190. while (!shared->AllDone()) {
  191. shared->GetCondVar()->Wait();
  192. }
  193. }
  194. }
  195. // If we are running verification_only
  196. // stats will be empty and trying to report them will
  197. // emit no ops or writes error. To avoid this, merging and reporting stats
  198. // are not executed when running with verification_only
  199. // TODO: We need to create verification stats (e.g. how many keys
  200. // are verified by which method) and report them here instead of operation
  201. // stats.
  202. if (!FLAGS_verification_only) {
  203. for (unsigned int i = 1; i < n; i++) {
  204. threads[0]->stats.Merge(threads[i]->stats);
  205. }
  206. threads[0]->stats.Report("Stress Test");
  207. }
  208. for (unsigned int i = 0; i < n; i++) {
  209. delete threads[i];
  210. threads[i] = nullptr;
  211. }
  212. now = clock->NowMicros();
  213. if (!FLAGS_skip_verifydb && !FLAGS_test_batches_snapshots &&
  214. !shared->HasVerificationFailedYet()) {
  215. fprintf(stdout, "%s Verification successful\n",
  216. clock->TimeToString(now / 1000000).c_str());
  217. }
  218. if (!FLAGS_verification_only) {
  219. stress->PrintStatistics();
  220. }
  221. if (FLAGS_compaction_thread_pool_adjust_interval > 0 ||
  222. FLAGS_continuous_verification_interval > 0 ||
  223. FLAGS_compressed_secondary_cache_size > 0 ||
  224. FLAGS_compressed_secondary_cache_ratio > 0.0 ||
  225. remote_compaction_worker_thread_count > 0) {
  226. MutexLock l(shared->GetMutex());
  227. shared->SetShouldStopBgThread();
  228. while (!shared->BgThreadsFinished()) {
  229. shared->GetCondVar()->Wait();
  230. }
  231. }
  232. assert(remote_compaction_worker_threads.size() ==
  233. remote_compaction_worker_thread_count);
  234. if (remote_compaction_worker_thread_count > 0) {
  235. for (ThreadState* thread_state : remote_compaction_worker_threads) {
  236. delete thread_state;
  237. }
  238. remote_compaction_worker_threads.clear();
  239. }
  240. if (shared->HasVerificationFailedYet()) {
  241. fprintf(stderr, "Verification failed :(\n");
  242. return false;
  243. }
  244. return true;
  245. }
  246. bool RunStressTest(SharedState* shared) {
  247. ThreadStatusUtil::RegisterThread(db_stress_env, ThreadStatus::USER);
  248. bool result = RunStressTestImpl(shared);
  249. ThreadStatusUtil::UnregisterThread();
  250. return result;
  251. }
  252. } // namespace ROCKSDB_NAMESPACE
  253. #endif // GFLAGS