From 1490f2c75df7151936d29fcb02e43133cd097fcd Mon Sep 17 00:00:00 2001 From: Samuel Just Date: Thu, 11 Jun 2020 11:53:56 -0700 Subject: [PATCH] src/test/crimson: unify the two TestBlock implemenations cache.cc is going to need a way to generate an appopriately typed extent from a delta or on disk block based on the type tag. Thus, there can only be one TestBlock for now unless it becomes at some point worth making it dynamic. Signed-off-by: Samuel Just --- src/test/crimson/seastore/test_block.h | 70 ++++++++++++++++ .../crimson/seastore/test_seastore_cache.cc | 80 ++++++------------- .../seastore/test_transaction_manager.cc | 55 +------------ 3 files changed, 96 insertions(+), 109 deletions(-) create mode 100644 src/test/crimson/seastore/test_block.h diff --git a/src/test/crimson/seastore/test_block.h b/src/test/crimson/seastore/test_block.h new file mode 100644 index 000000000000..3277511bc47d --- /dev/null +++ b/src/test/crimson/seastore/test_block.h @@ -0,0 +1,70 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#pragma once + +#include "crimson/os/seastore/transaction_manager.h" + +namespace crimson::os::seastore { + +struct test_extent_desc_t { + size_t len = 0; + unsigned checksum = 0; + + bool operator==(const test_extent_desc_t &rhs) const { + return (len == rhs.len && + checksum == rhs.checksum); + } + bool operator!=(const test_extent_desc_t &rhs) const { + return !(*this == rhs); + } +}; + +inline std::ostream &operator<<( + std::ostream &lhs, const test_extent_desc_t &rhs) { + return lhs << "test_extent_desc_t(len=" << rhs.len + << ", checksum=" << rhs.checksum << ")"; +} + +struct TestBlock : crimson::os::seastore::LogicalCachedExtent { + constexpr static segment_off_t SIZE = 4<<10; + using Ref = TCachedExtentRef; + + TestBlock(ceph::bufferptr &&ptr) : LogicalCachedExtent(std::move(ptr)) {} + TestBlock(const TestBlock &other) : LogicalCachedExtent(other) {} + + CachedExtentRef duplicate_for_write() final { + return CachedExtentRef(new TestBlock(*this)); + }; + + static constexpr extent_types_t TYPE = extent_types_t::TEST_BLOCK; + extent_types_t get_type() const final { + return TYPE; + } + + ceph::bufferlist get_delta() final { + return ceph::bufferlist(); + } + + void set_contents(char c) { + ::memset(get_bptr().c_str(), c, get_length()); + } + + int checksum() { + return ceph_crc32c( + 1, + (const unsigned char *)get_bptr().c_str(), + get_length()); + } + + test_extent_desc_t get_desc() { + return { get_length(), get_crc32c(1) }; + } + + void apply_delta(paddr_t delta_base, ceph::bufferlist &bl) final { + ceph_assert(0 == "TODO"); + } +}; +using TestBlockRef = TCachedExtentRef; + +} diff --git a/src/test/crimson/seastore/test_seastore_cache.cc b/src/test/crimson/seastore/test_seastore_cache.cc index 797d080e194f..e5dda0d23237 100644 --- a/src/test/crimson/seastore/test_seastore_cache.cc +++ b/src/test/crimson/seastore/test_seastore_cache.cc @@ -7,6 +7,8 @@ #include "crimson/os/seastore/cache.h" #include "crimson/os/seastore/segment_manager/ephemeral.h" +#include "test/crimson/seastore/test_block.h" + using namespace crimson; using namespace crimson::os; using namespace crimson::os::seastore; @@ -17,42 +19,6 @@ namespace { } } -struct CacheTestBlock : CachedExtent { - constexpr static segment_off_t SIZE = 4<<10; - using Ref = TCachedExtentRef; - - template - CacheTestBlock(T&&... t) : CachedExtent(std::forward(t)...) {} - - CachedExtentRef duplicate_for_write() final { - return CachedExtentRef(new CacheTestBlock(*this)); - }; - - static constexpr extent_types_t TYPE = extent_types_t::TEST_BLOCK; - extent_types_t get_type() const final { - return TYPE; - } - - ceph::bufferlist get_delta() final { - return ceph::bufferlist(); - } - - void apply_delta(paddr_t delta_base, ceph::bufferlist &bl) final { - ceph_assert(0 == "TODO"); - } - - void set_contents(char c) { - ::memset(get_bptr().c_str(), c, get_length()); - } - - int checksum() { - return ceph_crc32c( - 1, - (const unsigned char *)get_bptr().c_str(), - get_length()); - } -}; - struct cache_test_t : public seastar_test_suite_t { segment_manager::EphemeralSegmentManager segment_manager; Cache cache; @@ -135,9 +101,9 @@ TEST_F(cache_test_t, test_addr_fixup) int csum = 0; { auto t = get_transaction(); - auto extent = cache.alloc_new_extent( + auto extent = cache.alloc_new_extent( *t, - CacheTestBlock::SIZE); + TestBlock::SIZE); extent->set_contents('c'); csum = extent->checksum(); auto ret = submit_transaction(std::move(t)).get0(); @@ -146,10 +112,10 @@ TEST_F(cache_test_t, test_addr_fixup) } { auto t = get_transaction(); - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_EQ(extent->get_paddr(), addr); ASSERT_EQ(extent->checksum(), csum); } @@ -165,9 +131,9 @@ TEST_F(cache_test_t, test_dirty_extent) { // write out initial test block auto t = get_transaction(); - auto extent = cache.alloc_new_extent( + auto extent = cache.alloc_new_extent( *t, - CacheTestBlock::SIZE); + TestBlock::SIZE); extent->set_contents('c'); csum = extent->checksum(); auto reladdr = extent->get_paddr(); @@ -175,10 +141,10 @@ TEST_F(cache_test_t, test_dirty_extent) { // test that read with same transaction sees new block though // uncommitted - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, reladdr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_TRUE(extent->is_clean()); ASSERT_TRUE(extent->is_pending()); ASSERT_TRUE(extent->get_paddr().is_relative()); @@ -192,26 +158,26 @@ TEST_F(cache_test_t, test_dirty_extent) { // test that consecutive reads on the same extent get the same ref auto t = get_transaction(); - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); auto t2 = get_transaction(); - auto extent2 = cache.get_extent( + auto extent2 = cache.get_extent( *t2, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_EQ(&*extent, &*extent2); } { // read back test block auto t = get_transaction(); - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); // duplicate and reset contents - extent = cache.duplicate_for_write(*t, extent)->cast(); + extent = cache.duplicate_for_write(*t, extent)->cast(); extent->set_contents('c'); csum2 = extent->checksum(); ASSERT_EQ(extent->get_paddr(), addr); @@ -219,10 +185,10 @@ TEST_F(cache_test_t, test_dirty_extent) // test that concurrent read with fresh transaction sees old // block auto t2 = get_transaction(); - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t2, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_TRUE(extent->is_clean()); ASSERT_FALSE(extent->is_pending()); ASSERT_EQ(addr, extent->get_paddr()); @@ -231,10 +197,10 @@ TEST_F(cache_test_t, test_dirty_extent) } { // test that read with same transaction sees new block - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_TRUE(extent->is_dirty()); ASSERT_TRUE(extent->is_pending()); ASSERT_EQ(addr, extent->get_paddr()); @@ -252,10 +218,10 @@ TEST_F(cache_test_t, test_dirty_extent) { // test that fresh transaction now sees newly dirty block auto t = get_transaction(); - auto extent = cache.get_extent( + auto extent = cache.get_extent( *t, addr, - CacheTestBlock::SIZE).unsafe_get0(); + TestBlock::SIZE).unsafe_get0(); ASSERT_TRUE(extent->is_dirty()); ASSERT_EQ(addr, extent->get_paddr()); ASSERT_EQ(extent->get_version(), 1); diff --git a/src/test/crimson/seastore/test_transaction_manager.cc b/src/test/crimson/seastore/test_transaction_manager.cc index e0d8ef7f694c..7ef6187183f6 100644 --- a/src/test/crimson/seastore/test_transaction_manager.cc +++ b/src/test/crimson/seastore/test_transaction_manager.cc @@ -7,6 +7,8 @@ #include "crimson/os/seastore/transaction_manager.h" #include "crimson/os/seastore/segment_manager.h" +#include "test/crimson/seastore/test_block.h" + using namespace crimson; using namespace crimson::os; using namespace crimson::os::seastore; @@ -17,24 +19,6 @@ namespace { } } -struct test_extent_desc_t { - size_t len = 0; - unsigned checksum = 0; - - bool operator==(const test_extent_desc_t &rhs) const { - return (len == rhs.len && - checksum == rhs.checksum); - } - bool operator!=(const test_extent_desc_t &rhs) const { - return !(*this == rhs); - } -}; - -std::ostream &operator<<(std::ostream &lhs, const test_extent_desc_t &rhs) { - return lhs << "test_extent_desc_t(len=" << rhs.len - << ", checksum=" << rhs.checksum << ")"; -} - struct test_extent_record_t { test_extent_desc_t desc; unsigned refcount = 0; @@ -60,40 +44,6 @@ std::ostream &operator<<(std::ostream &lhs, const test_extent_record_t &rhs) { << ", refcount=" << rhs.refcount << ")"; } - -struct TestBlock : LogicalCachedExtent { - using Ref = TCachedExtentRef; - - TestBlock(ceph::bufferptr &&ptr) : LogicalCachedExtent(std::move(ptr)) {} - TestBlock(const TestBlock &other) : LogicalCachedExtent(other) {} - - CachedExtentRef duplicate_for_write() final { - return CachedExtentRef(new TestBlock(*this)); - }; - - static constexpr extent_types_t TYPE = extent_types_t::TEST_BLOCK; - extent_types_t get_type() const final { - return TYPE; - } - - ceph::bufferlist get_delta() final { - return ceph::bufferlist(); - } - - void set_contents(char c) { - ::memset(get_bptr().c_str(), c, get_length()); - } - - test_extent_desc_t get_desc() { - return { get_length(), get_crc32c(1) }; - } - - void apply_delta(paddr_t delta_base, ceph::bufferlist &bl) final { - ceph_assert(0 == "TODO"); - } -}; -using TestBlockRef = TCachedExtentRef; - struct transaction_manager_test_t : public seastar_test_suite_t { std::unique_ptr segment_manager; Journal journal; @@ -129,6 +79,7 @@ struct transaction_manager_test_t : public seastar_test_suite_t { }) ); } + struct test_extents_t : std::map { private: void check_available(laddr_t addr, extent_len_t len) { -- 2.47.3