From f8a0432b71dc3d61af019d0f1ab01c6444f9a914 Mon Sep 17 00:00:00 2001 From: Andrew Chang Date: Mon, 6 Jan 2025 11:09:48 -0800 Subject: [PATCH] Create new FileSnapshotExpectedState to use instead of AnonExpectedState --- db_stress_tool/expected_state.cc | 74 +++++++++++++++++-------- db_stress_tool/expected_state.h | 13 +++++ db_stress_tool/no_batched_ops_stress.cc | 11 ++-- tools/db_crashtest.py | 2 +- 4 files changed, 70 insertions(+), 30 deletions(-) diff --git a/db_stress_tool/expected_state.cc b/db_stress_tool/expected_state.cc index 18715125526..b228a3ff642 100644 --- a/db_stress_tool/expected_state.cc +++ b/db_stress_tool/expected_state.cc @@ -198,6 +198,33 @@ Status FileExpectedState::Open(bool create) { return status; } +FileSnapshotExpectedState::FileSnapshotExpectedState( + const std::string& expected_state_file_path, size_t max_key, + size_t num_column_families) + : ExpectedState(max_key, num_column_families), + expected_state_file_path_(expected_state_file_path) {} + +Status FileSnapshotExpectedState::Open(bool create) { + if (create) { + return Status::NotSupported(); + } + size_t expected_values_size = GetValuesLen(); + + Env* default_env = Env::Default(); + Status status = default_env->NewMemoryMappedFileBuffer( + expected_state_file_path_, &expected_state_mmap_buffer_); + if (!status.ok()) { + return status; + } + + assert(expected_state_mmap_buffer_->GetLen() == expected_values_size); + + values_ = static_cast*>( + expected_state_mmap_buffer_->GetBase()); + assert(values_ != nullptr); + return status; +} + AnonExpectedState::AnonExpectedState(size_t max_key, size_t num_column_families) : ExpectedState(max_key, num_column_families) {} @@ -687,31 +714,28 @@ class ExpectedStateTraceRecordHandler : public TraceRecord::Handler, Status FileExpectedStateManager::GetExpectedState( DB* db, std::unique_ptr& state) { std::cout << "Enter FileExpectedStateManager::GetExpectedState" << std::endl; - if (!HasHistory()) { - fprintf(stderr, "No history to restore from\n"); - exit(123); - } - std::cout << "History exists to restore from" << std::endl; + assert(HasHistory()); SequenceNumber seqno = db->GetLatestSequenceNumber(); - std::cout << "saved_seqno_ = " << saved_seqno_ << std::endl; - std::cout << "seqno = " << seqno << std::endl; if (seqno < saved_seqno_) { + std::cout << "seqno " << seqno << " is less than saved_seq_no_ " + << saved_seqno_ << std::endl; return Status::Corruption("DB is older than any restorable expected state"); } - std::cout << "Above last sequence number" << std::endl; - std::string trace_filename = std::to_string(saved_seqno_) + kTraceFilenameSuffix; std::string trace_file_path = GetPathForFilename(trace_filename); std::cout << "trace_file_path = " << trace_file_path << std::endl; Status exists_status = Env::Default()->FileExists(trace_file_path); - if (exists_status.ok()) { - std::cout << "Trace file exists" << std::endl; - } else if (exists_status.IsNotFound()) { - std::cout << "Cannot find trace file" << std::endl; + if (!exists_status.ok()) { + if (exists_status.IsNotFound()) { + std::cout << "Cannot find trace file" << std::endl; + } else { + std::cout << "Encountered error checking for trace file existence" + << std::endl; + } + return exists_status; } - std::unique_ptr trace_reader; Status s = NewFileTraceReader(Env::Default(), EnvOptions(), trace_file_path, &trace_reader); @@ -720,22 +744,26 @@ Status FileExpectedStateManager::GetExpectedState( return s; } - std::cout << "Attempting to define the AnonExpectedState" << std::endl; - std::unique_ptr replay_state( - new AnonExpectedState(max_key_, num_column_families_)); - std::cout << "Attempting to open the AnonExpectedState" << std::endl; - s = replay_state->Open(true /* create */); + std::string state_filename = + std::to_string(saved_seqno_) + kStateFilenameSuffix; + std::string state_file_path = GetPathForFilename(state_filename); + std::unique_ptr replay_state( + new FileSnapshotExpectedState(state_file_path, max_key_, + num_column_families_)); + s = replay_state->Open(false /* create */); if (!s.ok()) { + std::cout << "Error opening FileSnapshotExpectedState" << std::endl; return s; } - std::cout << "Set up replay state" << std::endl; s = ReplayTrace(db, std::move(trace_reader), seqno - saved_seqno_, replay_state.get()); - if (s.ok()) { - state = std::move(replay_state); + if (!s.ok()) { + std::cout << "Error replaying trace" << std::endl; + return s; } - std::cout << "Expected state should be all done" << std::endl; + state = std::move(replay_state); + std::cout << "Successful exit from GetExpectedState" << std::endl; return s; } diff --git a/db_stress_tool/expected_state.h b/db_stress_tool/expected_state.h index 62a093c83bd..6c05d6115ec 100644 --- a/db_stress_tool/expected_state.h +++ b/db_stress_tool/expected_state.h @@ -181,6 +181,19 @@ class FileExpectedState : public ExpectedState { std::unique_ptr expected_persisted_seqno_mmap_buffer_; }; +class FileSnapshotExpectedState : public ExpectedState { + public: + explicit FileSnapshotExpectedState( + const std::string& expected_state_file_path, size_t max_key, + size_t num_column_families); + + Status Open(bool create) override; + + private: + const std::string expected_state_file_path_; + std::unique_ptr expected_state_mmap_buffer_; +}; + // An `AnonExpectedState` implements `ExpectedState` backed by a memory // allocation. class AnonExpectedState : public ExpectedState { diff --git a/db_stress_tool/no_batched_ops_stress.cc b/db_stress_tool/no_batched_ops_stress.cc index 3faa836461a..4fe958bbd5f 100644 --- a/db_stress_tool/no_batched_ops_stress.cc +++ b/db_stress_tool/no_batched_ops_stress.cc @@ -338,18 +338,16 @@ class NonBatchedOpsStressTest : public StressTest { if (!cmp_db_) { return; } - std::cout << "NonBatchedOpsStressTest::ContinuouslyVerifyDb" << std::endl; if (thread->shared->HasHistory()) { std::unique_ptr state; Status getExpectedStateStatus = thread->shared->GetExpectedState(db_, state); if (!getExpectedStateStatus.ok()) { - fprintf(stdout, - "[NonBatchedOpsStressTest::ContinuouslyVerifyDb]: Failed to " - "set expected state\n"); + std::cout << "[NonBatchedOpsStressTest::ContinuouslyVerifyDb]: Failed " + "to get expected state" + << std::endl; assert(false); - exit(1); } } @@ -2740,7 +2738,8 @@ class NonBatchedOpsStressTest : public StressTest { std::unique_ptr state; Status getExpectedStateStatus = shared->GetExpectedState(db_, state); if (!getExpectedStateStatus.ok()) { - fprintf(stdout, "[VerifyOrSyncValue]: Failed to get expected state\n"); + std::cout << "[VerifyOrSyncValue]: Failed to get expected state" + << std::endl; return false; } } diff --git a/tools/db_crashtest.py b/tools/db_crashtest.py index f87e595fdce..16759b0f04d 100644 --- a/tools/db_crashtest.py +++ b/tools/db_crashtest.py @@ -344,7 +344,7 @@ "paranoid_memory_checks": lambda: random.choice([0] * 7 + [1]), "allow_unprepared_value": lambda: random.choice([0, 1]), "track_and_verify_wals": lambda: random.choice([0, 1]), - "test_secondary": lambda: random.choice([0, 1]), + "test_secondary": 1, } _TEST_DIR_ENV_VAR = "TEST_TMPDIR" # If TEST_TMPDIR_EXPECTED is not specified, default value will be TEST_TMPDIR