From f3006467bf6290c27d2afffd84278ba8c846d3a8 Mon Sep 17 00:00:00 2001 From: Samuel Just Date: Wed, 17 Jun 2020 15:24:25 -0700 Subject: [PATCH] test/crimson/seastore: update test_transaction_manager for replay TestBlock now has deltas and test_transaction_manager validates a simple replay case. Signed-off-by: Samuel Just --- src/crimson/os/seastore/CMakeLists.txt | 1 + src/test/crimson/seastore/CMakeLists.txt | 2 + src/test/crimson/seastore/test_block.cc | 25 +++++++ src/test/crimson/seastore/test_block.h | 67 ++++++++++++++++--- .../seastore/test_transaction_manager.cc | 18 +++-- 5 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 src/test/crimson/seastore/test_block.cc diff --git a/src/crimson/os/seastore/CMakeLists.txt b/src/crimson/os/seastore/CMakeLists.txt index 4acc48f05d4..76ac9b65748 100644 --- a/src/crimson/os/seastore/CMakeLists.txt +++ b/src/crimson/os/seastore/CMakeLists.txt @@ -14,6 +14,7 @@ add_library(crimson-seastore onode_manager/simple-fltree/onode_delta.cc onode_manager/simple-fltree/onode_node.cc seastore.cc + ../../../test/crimson/seastore/test_block.cc ) target_link_libraries(crimson-seastore crimson) diff --git a/src/test/crimson/seastore/CMakeLists.txt b/src/test/crimson/seastore/CMakeLists.txt index 458cc051db9..e84a4d6171a 100644 --- a/src/test/crimson/seastore/CMakeLists.txt +++ b/src/test/crimson/seastore/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable(unittest_transaction_manager + test_block.cc test_transaction_manager.cc ../gtest_seastar.cc) add_ceph_unittest(unittest_transaction_manager) @@ -26,6 +27,7 @@ target_link_libraries( crimson-seastore) add_executable(unittest_seastore_cache + test_block.cc test_seastore_cache.cc) add_ceph_test(unittest_seastore_cache unittest_seastore_cache) diff --git a/src/test/crimson/seastore/test_block.cc b/src/test/crimson/seastore/test_block.cc new file mode 100644 index 00000000000..649e73bf781 --- /dev/null +++ b/src/test/crimson/seastore/test_block.cc @@ -0,0 +1,25 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "test/crimson/seastore/test_block.h" + +namespace crimson::os::seastore { + + +ceph::bufferlist TestBlock::get_delta() { + ceph::bufferlist bl; + ::encode(delta, bl); + return bl; +} + + +void TestBlock::apply_delta(const ceph::bufferlist &bl) { + auto biter = bl.begin(); + decltype(delta) deltas; + ::decode(deltas, biter); + for (auto &&d : deltas) { + set_contents(d.val, d.offset, d.len); + } +} + +} diff --git a/src/test/crimson/seastore/test_block.h b/src/test/crimson/seastore/test_block.h index 4d96c6fe6c3..446e7ec0314 100644 --- a/src/test/crimson/seastore/test_block.h +++ b/src/test/crimson/seastore/test_block.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "crimson/os/seastore/transaction_manager.h" namespace crimson::os::seastore { @@ -20,6 +22,21 @@ struct test_extent_desc_t { } }; +struct test_block_delta_t { + int8_t val = 0; + uint16_t offset = 0; + uint16_t len = 0; + + + DENC(test_block_delta_t, v, p) { + DENC_START(1, 1, p); + denc(v.val, p); + denc(v.offset, p); + denc(v.len, p); + DENC_FINISH(p); + } +}; + inline std::ostream &operator<<( std::ostream &lhs, const test_extent_desc_t &rhs) { return lhs << "test_extent_desc_t(len=" << rhs.len @@ -30,8 +47,12 @@ 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) {} + std::vector delta = {}; + + TestBlock(ceph::bufferptr &&ptr) + : LogicalCachedExtent(std::move(ptr)) {} + TestBlock(const TestBlock &other) + : LogicalCachedExtent(other) {} CachedExtentRef duplicate_for_write() final { return CachedExtentRef(new TestBlock(*this)); @@ -42,12 +63,15 @@ struct TestBlock : crimson::os::seastore::LogicalCachedExtent { return TYPE; } - ceph::bufferlist get_delta() final { - return ceph::bufferlist(); + ceph::bufferlist get_delta() final; + + void set_contents(char c, uint16_t offset, uint16_t len) { + ::memset(get_bptr().c_str() + offset, c, len); + delta.push_back({c, offset, len}); } void set_contents(char c) { - ::memset(get_bptr().c_str(), c, get_length()); + set_contents(c, 0, get_length()); } int checksum() { @@ -61,10 +85,37 @@ struct TestBlock : crimson::os::seastore::LogicalCachedExtent { return { get_length(), get_crc32c(1) }; } - void apply_delta(const ceph::bufferlist &bl) final { - ceph_assert(0 == "TODO"); - } + void apply_delta(const ceph::bufferlist &bl) final; }; using TestBlockRef = TCachedExtentRef; +struct test_block_mutator_t { + std::uniform_int_distribution + contents_distribution = std::uniform_int_distribution( + std::numeric_limits::min(), + std::numeric_limits::max()); + + std::uniform_int_distribution + offset_distribution = std::uniform_int_distribution( + 0, TestBlock::SIZE - 1); + + std::default_random_engine generator = std::default_random_engine(0); + + std::uniform_int_distribution length_distribution(uint16_t offset) { + return std::uniform_int_distribution( + 0, TestBlock::SIZE - offset - 1); + } + + + void mutate(TestBlock &block) { + auto offset = offset_distribution(generator); + block.set_contents( + contents_distribution(generator), + offset, + length_distribution(offset)(generator)); + } +}; + } + +WRITE_CLASS_DENC_BOUNDED(crimson::os::seastore::test_block_delta_t) diff --git a/src/test/crimson/seastore/test_transaction_manager.cc b/src/test/crimson/seastore/test_transaction_manager.cc index 7ef6187183f..5c6cccbebef 100644 --- a/src/test/crimson/seastore/test_transaction_manager.cc +++ b/src/test/crimson/seastore/test_transaction_manager.cc @@ -146,6 +146,11 @@ struct transaction_manager_test_t : public seastar_test_suite_t { return extent; } + void replay() { + tm.close().unsafe_get(); + tm.mount().unsafe_get(); + } + void check_mappings() { auto t = create_transaction(); check_mappings(t); @@ -169,16 +174,16 @@ struct transaction_manager_test_t : public seastar_test_suite_t { return ext; } + test_block_mutator_t mutator; TestBlockRef mutate_extent( test_transaction_t &t, - TestBlockRef ref, - char contents) { + TestBlockRef ref) { ceph_assert(t.mappings.count(ref->get_laddr())); ceph_assert(t.mappings[ref->get_laddr()].desc.len == ref->get_length()); auto ext = tm.get_mutable_extent(*t.t, ref)->cast(); EXPECT_EQ(ext->get_laddr(), ref->get_laddr()); EXPECT_EQ(ext->get_desc(), ref->get_desc()); - ext->set_contents(contents); + mutator.mutate(*ext); t.mappings[ext->get_laddr()].update(ext->get_desc()); return ext; } @@ -255,18 +260,21 @@ TEST_F(transaction_manager_test_t, mutate) submit_transaction(std::move(t)); check_mappings(); } + replay(); { auto t = create_transaction(); auto ext = get_extent( t, ADDR, SIZE); - auto mut = mutate_extent(t, ext, 'c'); + auto mut = mutate_extent(t, ext); check_mappings(t); check_mappings(); submit_transaction(std::move(t)); check_mappings(); } + replay(); + check_mappings(); }); } @@ -288,6 +296,7 @@ TEST_F(transaction_manager_test_t, inc_dec_ref) submit_transaction(std::move(t)); check_mappings(); } + replay(); { auto t = create_transaction(); inc_ref(t, ADDR); @@ -304,6 +313,7 @@ TEST_F(transaction_manager_test_t, inc_dec_ref) submit_transaction(std::move(t)); check_mappings(); } + replay(); { auto t = create_transaction(); dec_ref(t, ADDR); -- 2.39.5