diff --git a/db/db_impl/db_impl.h b/db/db_impl/db_impl.h index 0d8ce7624..f46d41ce2 100644 --- a/db/db_impl/db_impl.h +++ b/db/db_impl/db_impl.h @@ -974,6 +974,9 @@ class DBImpl : public DB { Status TEST_AtomicFlushMemTables(const autovector& cfds, const FlushOptions& flush_opts); + // Wait for background threads to complete scheduled work. + Status TEST_WaitForBackgroundWork(); + // Wait for memtable compaction Status TEST_WaitForFlushMemTable(ColumnFamilyHandle* column_family = nullptr); diff --git a/db/db_impl/db_impl_debug.cc b/db/db_impl/db_impl_debug.cc index 2bdc66688..d7e0628f6 100644 --- a/db/db_impl/db_impl_debug.cc +++ b/db/db_impl/db_impl_debug.cc @@ -156,6 +156,12 @@ Status DBImpl::TEST_AtomicFlushMemTables( return AtomicFlushMemTables(cfds, flush_opts, FlushReason::kTest); } +Status DBImpl::TEST_WaitForBackgroundWork() { + InstrumentedMutexLock l(&mutex_); + WaitForBackgroundWork(); + return Status::OK(); +} + Status DBImpl::TEST_WaitForFlushMemTable(ColumnFamilyHandle* column_family) { ColumnFamilyData* cfd; if (column_family == nullptr) { diff --git a/utilities/backupable/backupable_db_test.cc b/utilities/backupable/backupable_db_test.cc index 80111fc1c..1c6a4ba43 100644 --- a/utilities/backupable/backupable_db_test.cc +++ b/utilities/backupable/backupable_db_test.cc @@ -3158,6 +3158,10 @@ TEST_F(BackupEngineTest, ChangeManifestDuringBackupCreation) { FillDB(db_.get(), 0, 100, kAutoFlushOnly); ASSERT_OK(db_chroot_env_->FileExists(prev_manifest_path)); ASSERT_OK(db_->Flush(FlushOptions())); + // Even though manual flush completed above, the background thread may not + // have finished its cleanup work. `TEST_WaitForBackgroundWork()` will wait + // until all the background thread's work has completed, including cleanup. + ASSERT_OK(db_impl->TEST_WaitForBackgroundWork()); ASSERT_TRUE(db_chroot_env_->FileExists(prev_manifest_path).IsNotFound()); CloseDBAndBackupEngine();