From c21c459771f65e8e9eae2dc8ee840fc9172996ff Mon Sep 17 00:00:00 2001 From: sdong Date: Tue, 5 May 2020 18:29:50 -0700 Subject: [PATCH] Slightly expand converage to file consistency check failure (#6800) Summary: Current DBCompactionTest.ConsistencyFailTest checks DB fails after L0 inconsitency is found. Add slightly more coverage by introducing DBCompactionTest.ConsistencyFailTest2 which checks non-L0 files too. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6800 Test Plan: Run the new test. Reviewed By: riversand963 Differential Revision: D21384806 fbshipit-source-id: 36db7b657eed42115283fe2f6afa4c3a31a3b510 --- db/db_compaction_test.cc | 45 +++++++++++++++++++++++++++++++++++++++- db/version_builder.cc | 10 ++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/db/db_compaction_test.cc b/db/db_compaction_test.cc index 1eed138af..da5e46600 100644 --- a/db/db_compaction_test.cc +++ b/db/db_compaction_test.cc @@ -5024,10 +5024,11 @@ TEST_P(DBCompactionTestWithParam, FixFileIngestionCompactionDeadlock) { TEST_F(DBCompactionTest, ConsistencyFailTest) { Options options = CurrentOptions(); + options.force_consistency_checks = true; DestroyAndReopen(options); ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack( - "VersionBuilder::CheckConsistency", [&](void* arg) { + "VersionBuilder::CheckConsistency0", [&](void* arg) { auto p = reinterpret_cast*>(arg); // just swap the two FileMetaData so that we hit error @@ -5046,6 +5047,48 @@ TEST_F(DBCompactionTest, ConsistencyFailTest) { ASSERT_NOK(Put("foo", "bar")); ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing(); + SyncPoint::GetInstance()->ClearAllCallBacks(); +} + +TEST_F(DBCompactionTest, ConsistencyFailTest2) { + Options options = CurrentOptions(); + options.force_consistency_checks = true; + options.target_file_size_base = 1000; + options.level0_file_num_compaction_trigger = 2; + BlockBasedTableOptions bbto; + bbto.block_size = 400; // small block size + options.table_factory.reset(new BlockBasedTableFactory(bbto)); + DestroyAndReopen(options); + + ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack( + "VersionBuilder::CheckConsistency1", [&](void* arg) { + auto p = + reinterpret_cast*>(arg); + // just swap the two FileMetaData so that we hit error + // in CheckConsistency funcion + FileMetaData* temp = *(p->first); + *(p->first) = *(p->second); + *(p->second) = temp; + }); + + ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing(); + + Random rnd(301); + std::string value = RandomString(&rnd, 1000); + + ASSERT_OK(Put("foo1", value)); + ASSERT_OK(Put("z", "")); + Flush(); + ASSERT_OK(Put("foo2", value)); + ASSERT_OK(Put("z", "")); + Flush(); + + // This probably returns non-OK, but we rely on the next Put() + // to determine the DB is frozen. + dbfull()->TEST_WaitForCompact(); + ASSERT_NOK(Put("foo", "bar")); + ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing(); + SyncPoint::GetInstance()->ClearAllCallBacks(); } void IngestOneKeyValue(DBImpl* db, const std::string& key, diff --git a/db/version_builder.cc b/db/version_builder.cc index 905b2aaca..6cc4480fa 100644 --- a/db/version_builder.cc +++ b/db/version_builder.cc @@ -236,11 +236,11 @@ class VersionBuilder::Rep { auto f1 = level_files[i - 1]; auto f2 = level_files[i]; + if (level == 0) { #ifndef NDEBUG - auto pair = std::make_pair(&f1, &f2); - TEST_SYNC_POINT_CALLBACK("VersionBuilder::CheckConsistency", &pair); + auto pair = std::make_pair(&f1, &f2); + TEST_SYNC_POINT_CALLBACK("VersionBuilder::CheckConsistency0", &pair); #endif - if (level == 0) { if (!level_zero_cmp_(f1, f2)) { fprintf(stderr, "L0 files are not sorted properly"); return Status::Corruption("L0 files are not sorted properly"); @@ -279,6 +279,10 @@ class VersionBuilder::Rep { NumberToString(f2->fd.GetNumber())); } } else { +#ifndef NDEBUG + auto pair = std::make_pair(&f1, &f2); + TEST_SYNC_POINT_CALLBACK("VersionBuilder::CheckConsistency1", &pair); +#endif if (!level_nonzero_cmp_(f1, f2)) { fprintf(stderr, "L%d files are not sorted properly", level); return Status::Corruption("L" + NumberToString(level) +