]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: Logback generation data structures
authorAdam C. Emerson <aemerson@redhat.com>
Mon, 4 Jan 2021 00:08:09 +0000 (19:08 -0500)
committerAdam C. Emerson <aemerson@redhat.com>
Mon, 5 Apr 2021 17:42:12 +0000 (13:42 -0400)
Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
(cherry picked from commit b97b207928c60b48fe405ab38be15ba55f927d5c)
Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/rgw/rgw_log_backing.h
src/test/rgw/test_log_backing.cc

index d769af48b01fefee2aa2b7063f0ff487772c6efe..8546370a3757a413f3b06c139127f3d5d6f99604 100644 (file)
 
 #include <boost/system/error_code.hpp>
 
+#undef FMT_HEADER_ONLY
+#define FMT_HEADER_ONLY 1
+#include <fmt/format.h>
+
 #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<uint8_t>(type);
+  encode(t, bl);
+}
+
+inline void decode(log_type& type, bufferlist::const_iterator& bl) {
+  uint8_t t;
+  decode(t, bl);
+  type = static_cast<log_type>(type);
+}
+
 inline std::optional<log_type> 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<uint64_t, std::string_view>
+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<uint64_t>(cursor);
+  if (!gen_id || cursor[0] != '@') {
+    return { 0, cursor_ };
+  }
+  cursor.remove_prefix(1);
+  return { *gen_id, cursor };
+}
+
 #endif
index 5180d5fc74fe84b729587741a1f64680f8fd0c2d..848bd6b50c4e55bd72f3336d5f8830ad64f580b1 100644 (file)
@@ -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);
+  }
+}