From: Adam C. Emerson Date: Mon, 4 Jan 2021 00:08:09 +0000 (-0500) Subject: rgw: Logback generation data structures X-Git-Tag: v16.2.2~8^2~9^2~18 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=1436be5861c8a19bd4969c219fb2a8848f359a92;p=ceph.git rgw: Logback generation data structures Signed-off-by: Adam C. Emerson (cherry picked from commit b97b207928c60b48fe405ab38be15ba55f927d5c) Signed-off-by: Adam C. Emerson --- diff --git a/src/rgw/rgw_log_backing.h b/src/rgw/rgw_log_backing.h index d769af48b01fe..8546370a3757a 100644 --- a/src/rgw/rgw_log_backing.h +++ b/src/rgw/rgw_log_backing.h @@ -13,11 +13,18 @@ #include +#undef FMT_HEADER_ONLY +#define FMT_HEADER_ONLY 1 +#include + #include "include/rados/librados.hpp" +#include "include/encoding.h" #include "include/expected.hpp" #include "include/function2.hpp" #include "common/async/yield_context.h" +#include "common/Formatter.h" +#include "common/strtol.h" namespace bs = boost::system; @@ -28,6 +35,17 @@ enum class log_type { fifo = 1 }; +inline void encode(const log_type& type, ceph::buffer::list& bl) { + auto t = static_cast(type); + encode(t, bl); +} + +inline void decode(log_type& type, bufferlist::const_iterator& bl) { + uint8_t t; + decode(t, bl); + type = static_cast(type); +} + inline std::optional to_log_type(std::string_view s) { if (strncasecmp(s.data(), "omap", s.length()) == 0) { return log_type::omap; @@ -67,4 +85,48 @@ bs::error_code log_remove(librados::IoCtx& ioctx, optional_yield y); +struct logback_generation { + uint64_t gen_id = 0; + log_type type; + bool empty = false; + + void encode(ceph::buffer::list& bl) const { + ENCODE_START(1, 1, bl); + encode(gen_id, bl); + encode(type, bl); + encode(empty, bl); + ENCODE_FINISH(bl); + } + + void decode(bufferlist::const_iterator& bl) { + DECODE_START(1, bl); + decode(gen_id, bl); + decode(type, bl); + decode(empty, bl); + DECODE_FINISH(bl); + } +}; +WRITE_CLASS_ENCODER(logback_generation) + +inline std::string gencursor(uint64_t gen_id, std::string_view cursor) { + return (gen_id > 0 ? + fmt::format("G{:0>20}@{}", gen_id, cursor) : + std::string(cursor)); +} + +inline std::pair +cursorgen(std::string_view cursor_) { + std::string_view cursor = cursor_; + if (cursor[0] != 'G') { + return { 0, cursor }; + } + cursor.remove_prefix(1); + auto gen_id = ceph::consume(cursor); + if (!gen_id || cursor[0] != '@') { + return { 0, cursor_ }; + } + cursor.remove_prefix(1); + return { *gen_id, cursor }; +} + #endif diff --git a/src/test/rgw/test_log_backing.cc b/src/test/rgw/test_log_backing.cc index 5180d5fc74fe8..848bd6b50c4e5 100644 --- a/src/test/rgw/test_log_backing.cc +++ b/src/test/rgw/test_log_backing.cc @@ -174,3 +174,21 @@ TEST_F(LogBacking, TestFIFOEmpty) get_oid, null_yield); ASSERT_EQ(log_type::fifo, *stat); } + +TEST(CursorGen, RoundTrip) { + const auto pcurs = "fded"sv; + { + auto gc = gencursor(0, pcurs); + ASSERT_EQ(pcurs, gc); + auto [gen, cursor] = cursorgen(gc); + ASSERT_EQ(0, gen); + ASSERT_EQ(pcurs, cursor); + } + { + auto gc = gencursor(53, pcurs); + ASSERT_NE(pcurs, gc); + auto [gen, cursor] = cursorgen(gc); + ASSERT_EQ(53, gen); + ASSERT_EQ(pcurs, cursor); + } +}