2019-12-09 08:49:32 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef GFLAGS
|
|
|
|
#include "db_stress_tool/db_stress_common.h"
|
2020-07-09 23:33:42 +02:00
|
|
|
#include "utilities/fault_injection_fs.h"
|
2019-12-09 08:49:32 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2019-12-09 08:49:32 +01:00
|
|
|
void ThreadBody(void* v) {
|
|
|
|
ThreadState* thread = reinterpret_cast<ThreadState*>(v);
|
|
|
|
SharedState* shared = thread->shared;
|
|
|
|
|
2020-06-13 04:24:11 +02:00
|
|
|
if (!FLAGS_skip_verifydb && shared->ShouldVerifyAtBeginning()) {
|
2019-12-09 08:49:32 +01:00
|
|
|
thread->shared->GetStressTest()->VerifyDb(thread);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MutexLock l(shared->GetMutex());
|
|
|
|
shared->IncInitialized();
|
|
|
|
if (shared->AllInitialized()) {
|
|
|
|
shared->GetCondVar()->SignalAll();
|
|
|
|
}
|
|
|
|
while (!shared->Started()) {
|
|
|
|
shared->GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread->shared->GetStressTest()->OperateDb(thread);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock l(shared->GetMutex());
|
|
|
|
shared->IncOperated();
|
|
|
|
if (shared->AllOperated()) {
|
|
|
|
shared->GetCondVar()->SignalAll();
|
|
|
|
}
|
|
|
|
while (!shared->VerifyStarted()) {
|
|
|
|
shared->GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 04:24:11 +02:00
|
|
|
if (!FLAGS_skip_verifydb) {
|
|
|
|
thread->shared->GetStressTest()->VerifyDb(thread);
|
|
|
|
}
|
2019-12-09 08:49:32 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock l(shared->GetMutex());
|
|
|
|
shared->IncDone();
|
|
|
|
if (shared->AllDone()) {
|
|
|
|
shared->GetCondVar()->SignalAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunStressTest(StressTest* stress) {
|
2021-03-15 12:32:24 +01:00
|
|
|
SystemClock* clock = db_stress_env->GetSystemClock().get();
|
2019-12-21 01:13:19 +01:00
|
|
|
SharedState shared(db_stress_env, stress);
|
2022-05-02 22:25:45 +02:00
|
|
|
stress->InitDb(&shared);
|
2020-06-18 18:51:14 +02:00
|
|
|
stress->FinishInitDb(&shared);
|
2019-12-09 08:49:32 +01:00
|
|
|
|
2020-04-16 20:10:53 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (FLAGS_sync_fault_injection) {
|
|
|
|
fault_fs_guard->SetFilesystemDirectWritable(false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-18 02:30:45 +01:00
|
|
|
uint32_t n = FLAGS_threads;
|
2021-03-15 12:32:24 +01:00
|
|
|
uint64_t now = clock->NowMicros();
|
2019-12-09 08:49:32 +01:00
|
|
|
fprintf(stdout, "%s Initializing worker threads\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString(now / 1000000).c_str());
|
2021-12-20 22:04:08 +01:00
|
|
|
|
2022-01-29 19:44:22 +01:00
|
|
|
shared.SetThreads(n);
|
|
|
|
|
|
|
|
if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
|
|
|
|
shared.IncBgThreads();
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:55:00 +01:00
|
|
|
if (FLAGS_continuous_verification_interval > 0) {
|
2022-01-29 19:44:22 +01:00
|
|
|
shared.IncBgThreads();
|
|
|
|
}
|
|
|
|
|
2021-12-20 22:04:08 +01:00
|
|
|
std::vector<ThreadState*> threads(n);
|
2022-01-29 19:44:22 +01:00
|
|
|
for (uint32_t i = 0; i < n; i++) {
|
|
|
|
threads[i] = new ThreadState(i, &shared);
|
|
|
|
db_stress_env->StartThread(ThreadBody, threads[i]);
|
|
|
|
}
|
2021-12-20 22:04:08 +01:00
|
|
|
|
2022-01-29 19:44:22 +01:00
|
|
|
ThreadState bg_thread(0, &shared);
|
|
|
|
if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
|
|
|
|
db_stress_env->StartThread(PoolSizeChangeThread, &bg_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState continuous_verification_thread(0, &shared);
|
|
|
|
if (FLAGS_continuous_verification_interval > 0) {
|
|
|
|
db_stress_env->StartThread(DbVerificationThread,
|
|
|
|
&continuous_verification_thread);
|
2019-12-20 17:46:52 +01:00
|
|
|
}
|
2019-12-09 08:49:32 +01:00
|
|
|
|
|
|
|
// Each thread goes through the following states:
|
|
|
|
// initializing -> wait for others to init -> read/populate/depopulate
|
|
|
|
// wait for others to operate -> verify -> done
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock l(shared.GetMutex());
|
|
|
|
while (!shared.AllInitialized()) {
|
|
|
|
shared.GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
if (shared.ShouldVerifyAtBeginning()) {
|
|
|
|
if (shared.HasVerificationFailedYet()) {
|
2019-12-20 17:46:52 +01:00
|
|
|
fprintf(stderr, "Crash-recovery verification failed :(\n");
|
2019-12-09 08:49:32 +01:00
|
|
|
} else {
|
2019-12-20 17:46:52 +01:00
|
|
|
fprintf(stdout, "Crash-recovery verification passed :)\n");
|
2019-12-09 08:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:31:00 +01:00
|
|
|
// This is after the verification step to avoid making all those `Get()`s
|
|
|
|
// and `MultiGet()`s contend on the DB-wide trace mutex.
|
|
|
|
stress->TrackExpectedState(&shared);
|
|
|
|
|
2021-03-15 12:32:24 +01:00
|
|
|
now = clock->NowMicros();
|
2019-12-09 08:49:32 +01:00
|
|
|
fprintf(stdout, "%s Starting database operations\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString(now / 1000000).c_str());
|
2019-12-09 08:49:32 +01:00
|
|
|
|
|
|
|
shared.SetStart();
|
|
|
|
shared.GetCondVar()->SignalAll();
|
|
|
|
while (!shared.AllOperated()) {
|
|
|
|
shared.GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:32:24 +01:00
|
|
|
now = clock->NowMicros();
|
2019-12-09 08:49:32 +01:00
|
|
|
if (FLAGS_test_batches_snapshots) {
|
|
|
|
fprintf(stdout, "%s Limited verification already done during gets\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString((uint64_t)now / 1000000).c_str());
|
2020-06-13 04:24:11 +02:00
|
|
|
} else if (FLAGS_skip_verifydb) {
|
|
|
|
fprintf(stdout, "%s Verification skipped\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString((uint64_t)now / 1000000).c_str());
|
2019-12-09 08:49:32 +01:00
|
|
|
} else {
|
|
|
|
fprintf(stdout, "%s Starting verification\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString((uint64_t)now / 1000000).c_str());
|
2019-12-09 08:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.SetStartVerify();
|
|
|
|
shared.GetCondVar()->SignalAll();
|
|
|
|
while (!shared.AllDone()) {
|
|
|
|
shared.GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i < n; i++) {
|
|
|
|
threads[0]->stats.Merge(threads[i]->stats);
|
|
|
|
}
|
|
|
|
threads[0]->stats.Report("Stress Test");
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < n; i++) {
|
|
|
|
delete threads[i];
|
|
|
|
threads[i] = nullptr;
|
|
|
|
}
|
2021-03-15 12:32:24 +01:00
|
|
|
now = clock->NowMicros();
|
2020-06-13 04:24:11 +02:00
|
|
|
if (!FLAGS_skip_verifydb && !FLAGS_test_batches_snapshots &&
|
|
|
|
!shared.HasVerificationFailedYet()) {
|
2019-12-09 08:49:32 +01:00
|
|
|
fprintf(stdout, "%s Verification successful\n",
|
2021-03-15 12:32:24 +01:00
|
|
|
clock->TimeToString(now / 1000000).c_str());
|
2019-12-09 08:49:32 +01:00
|
|
|
}
|
|
|
|
stress->PrintStatistics();
|
|
|
|
|
2019-12-20 17:46:52 +01:00
|
|
|
if (FLAGS_compaction_thread_pool_adjust_interval > 0 ||
|
|
|
|
FLAGS_continuous_verification_interval > 0) {
|
2019-12-09 08:49:32 +01:00
|
|
|
MutexLock l(shared.GetMutex());
|
|
|
|
shared.SetShouldStopBgThread();
|
2019-12-20 17:46:52 +01:00
|
|
|
while (!shared.BgThreadsFinished()) {
|
2019-12-09 08:49:32 +01:00
|
|
|
shared.GetCondVar()->Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stress->VerifySecondaries()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shared.HasVerificationFailedYet()) {
|
2019-12-20 17:46:52 +01:00
|
|
|
fprintf(stderr, "Verification failed :(\n");
|
2019-12-09 08:49:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2019-12-09 08:49:32 +01:00
|
|
|
#endif // GFLAGS
|