From ecc52d365c9fabd0c51c87450e75a57c2b016ab1 Mon Sep 17 00:00:00 2001 From: XuPeng-SH Date: Fri, 19 Jun 2020 10:37:41 +0800 Subject: [PATCH] (db/snapshot): fix ut errors (#2605) Signed-off-by: peng.xu --- core/src/db/snapshot/Operations.cpp | 5 +++-- core/src/db/snapshot/Operations.h | 13 +++++++------ core/src/db/snapshot/ResourceOperations.h | 2 +- core/unittest/db/test_snapshot.cpp | 23 ++++++++++++++++------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/src/db/snapshot/Operations.cpp b/core/src/db/snapshot/Operations.cpp index b9589377a..6374973bb 100644 --- a/core/src/db/snapshot/Operations.cpp +++ b/core/src/db/snapshot/Operations.cpp @@ -82,10 +82,11 @@ Operations::operator()(Store& store) { void Operations::SetStatus(const Status& status) { + std::unique_lock lock(finish_mtx_); status_ = status; } -Status +const Status& Operations::WaitToFinish() { std::unique_lock lock(finish_mtx_); finish_cond_.wait(lock, [this] { return done_; }); @@ -182,7 +183,7 @@ Operations::GetSnapshot(ScopedSnapshotT& ss) const { return status; } -Status +const Status& Operations::ApplyToStore(Store& store) { if (GetType() == OperationsType::W_Compound) { /* std::cout << ToString() << std::endl; */ diff --git a/core/src/db/snapshot/Operations.h b/core/src/db/snapshot/Operations.h index 7a6868b6c..a2d872d9a 100644 --- a/core/src/db/snapshot/Operations.h +++ b/core/src/db/snapshot/Operations.h @@ -104,10 +104,10 @@ class Operations : public std::enable_shared_from_this { virtual Status PreCheck(); - virtual Status + virtual const Status& ApplyToStore(Store& store); - Status + const Status& WaitToFinish(); void @@ -116,8 +116,9 @@ class Operations : public std::enable_shared_from_this { void SetStatus(const Status& status); - Status + const Status& GetStatus() const { + std::unique_lock lock(finish_mtx_); return status_; } @@ -241,7 +242,7 @@ class LoadOperation : public Operations { : Operations(OperationContext(), ScopedSnapshotT(), OperationsType::O_Leaf), context_(context) { } - Status + const Status& ApplyToStore(Store& store) override { if (done_) { Done(store); @@ -290,7 +291,7 @@ class HardDeleteOperation : public Operations { : Operations(OperationContext(), ScopedSnapshotT(), OperationsType::W_Leaf), id_(id) { } - Status + const Status& ApplyToStore(Store& store) override { if (done_) return status_; @@ -311,7 +312,7 @@ class HardDeleteOperation : public Operations { : Operations(OperationContext(), ScopedSnapshotT(), OperationsType::W_Leaf), id_(id) { } - Status + const Status& ApplyToStore(Store& store) override { if (done_) { Done(store); diff --git a/core/src/db/snapshot/ResourceOperations.h b/core/src/db/snapshot/ResourceOperations.h index e455513e6..eeefdaefe 100644 --- a/core/src/db/snapshot/ResourceOperations.h +++ b/core/src/db/snapshot/ResourceOperations.h @@ -108,7 +108,7 @@ class LoadOperation : public Operations { : Operations(OperationContext(), ScopedSnapshotT(), OperationsType::O_Leaf), context_(context) { } - Status + const Status& ApplyToStore(Store& store) override { if (done_) { Done(store); diff --git a/core/unittest/db/test_snapshot.cpp b/core/unittest/db/test_snapshot.cpp index aa5b7f15c..2b60937c5 100644 --- a/core/unittest/db/test_snapshot.cpp +++ b/core/unittest/db/test_snapshot.cpp @@ -839,7 +839,7 @@ TEST_F(SnapshotTest, CompoundTest1) { context.lsn = next_lsn(); auto op = std::make_shared(context, latest_ss); SegmentPtr new_seg; - status = op->CommitNewSegment(new_seg); + auto status = op->CommitNewSegment(new_seg); if (!status.ok()) { std::cout << status.ToString() << std::endl; } @@ -1121,8 +1121,12 @@ TEST_F(SnapshotTest, CompoundTest2) { for (auto& id : seg_ids) { auto seg = latest_ss->GetResource(id); if (!seg) { - std::cout << "Error seg=" << id << std::endl; - ASSERT_TRUE(seg); + std::cout << "Stale seg=" << id << std::endl; + std::unique_lock lock(partition_mtx); + std::cout << ((stale_partitions.find(p_id) != stale_partitions.end()) ? " due stale partition" + : " unexpected") << std::endl; + ASSERT_TRUE(stale_partitions.find(p_id) != stale_partitions.end()); + return; } if (!partition) { partition = latest_ss->GetResource(seg->GetPartitionId()); @@ -1212,7 +1216,7 @@ TEST_F(SnapshotTest, CompoundTest2) { context.lsn = next_lsn(); auto op = std::make_shared(context, latest_ss); SegmentPtr new_seg; - status = op->CommitNewSegment(new_seg); + auto status = op->CommitNewSegment(new_seg); if (!status.ok()) { std::cout << status.ToString() << std::endl; std::unique_lock lock(partition_mtx); @@ -1229,8 +1233,13 @@ TEST_F(SnapshotTest, CompoundTest2) { } status = op->Push(); if (!status.ok()) { - std::cout << status.ToString() << std::endl; std::unique_lock lock(partition_mtx); + /* if (stale_partitions.find(partition_id) == stale_partitions.end()) { */ + /* for (auto p : stale_partitions) { */ + /* std::cout << "stale p: " << p << std::endl; */ + /* } */ + /* ASSERT_TRUE(false); */ + /* } */ ASSERT_TRUE(stale_partitions.find(partition_id) != stale_partitions.end()); return; } @@ -1289,10 +1298,10 @@ TEST_F(SnapshotTest, CompoundTest2) { }; for (auto i = 0; i < loop_cnt; ++i) { - if (RandomInt(0, 10) > 6) { + if (RandomInt(0, 10) > 5) { create_partition(); } - if (RandomInt(0, 10) > 8) { + if (RandomInt(0, 10) > 7) { drop_partition(); } create_new_segment(); -- GitLab