summaryrefslogtreecommitdiffstats
path: root/src/test/rgw/test_log_backing.cc
blob: e4109d535d10cc7693b5dafd288b78c39ea750a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "rgw_log_backing.h"

#include <cerrno>
#include <iostream>
#include <string_view>

#include <fmt/format.h>

#include "include/types.h"
#include "include/rados/librados.hpp"

#include "test/librados/test_cxx.h"
#include "global/global_context.h"

#include "cls/log/cls_log_client.h"

#include "rgw_tools.h"
#include "cls_fifo_legacy.h"

#include "gtest/gtest.h"

namespace lr = librados;
namespace cb = ceph::buffer;
namespace fifo = rados::cls::fifo;
namespace RCf = rgw::cls::fifo;

auto cct = new CephContext(CEPH_ENTITY_TYPE_CLIENT);
const DoutPrefix dp(cct, 1, "test log backing: ");

class LogBacking : public testing::Test {
protected:
  static constexpr int SHARDS = 3;
  const std::string pool_name = get_temp_pool_name();
  lr::Rados rados;
  lr::IoCtx ioctx;
  lr::Rados rados2;
  lr::IoCtx ioctx2;

  void SetUp() override {
    ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
    ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
    connect_cluster_pp(rados2);
    ASSERT_EQ(0, rados2.ioctx_create(pool_name.c_str(), ioctx2));
  }
  void TearDown() override {
    destroy_one_pool_pp(pool_name, rados);
  }

  std::string get_oid(uint64_t gen_id, int i) const {
    return (gen_id > 0 ?
	    fmt::format("shard@G{}.{}", gen_id, i) :
	    fmt::format("shard.{}", i));
  }

  void make_omap() {
    for (int i = 0; i < SHARDS; ++i) {
      using ceph::encode;
      lr::ObjectWriteOperation op;
      cb::list bl;
      encode(i, bl);
      cls_log_add(op, ceph_clock_now(), {}, "meow", bl);
      auto r = rgw_rados_operate(&dp, ioctx, get_oid(0, i), &op, null_yield);
      ASSERT_GE(r, 0);
    }
  }

  void add_omap(int i) {
    using ceph::encode;
    lr::ObjectWriteOperation op;
    cb::list bl;
    encode(i, bl);
    cls_log_add(op, ceph_clock_now(), {}, "meow", bl);
    auto r = rgw_rados_operate(&dp, ioctx, get_oid(0, i), &op, null_yield);
    ASSERT_GE(r, 0);
  }

  void empty_omap() {
    for (int i = 0; i < SHARDS; ++i) {
      auto oid = get_oid(0, i);
      std::string to_marker;
      {
	lr::ObjectReadOperation op;
	std::list<cls_log_entry> entries;
	bool truncated = false;
	cls_log_list(op, {}, {}, {}, 1, entries, &to_marker, &truncated);
	auto r = rgw_rados_operate(&dp, ioctx, oid, &op, nullptr, null_yield);
	ASSERT_GE(r, 0);
	ASSERT_FALSE(entries.empty());
      }
      {
	lr::ObjectWriteOperation op;
	cls_log_trim(op, {}, {}, {}, to_marker);
	auto r = rgw_rados_operate(&dp, ioctx, oid, &op, null_yield);
	ASSERT_GE(r, 0);
      }
      {
	lr::ObjectReadOperation op;
	std::list<cls_log_entry> entries;
	bool truncated = false;
	cls_log_list(op, {}, {}, {}, 1, entries, &to_marker, &truncated);
	auto r = rgw_rados_operate(&dp, ioctx, oid, &op, nullptr, null_yield);
	ASSERT_GE(r, 0);
	ASSERT_TRUE(entries.empty());
      }
    }
  }

  void make_fifo()
    {
      for (int i = 0; i < SHARDS; ++i) {
	std::unique_ptr<RCf::FIFO> fifo;
	auto r = RCf::FIFO::create(&dp, ioctx, get_oid(0, i), &fifo, null_yield);
	ASSERT_EQ(0, r);
	ASSERT_TRUE(fifo);
      }
    }

  void add_fifo(int i)
    {
      using ceph::encode;
      std::unique_ptr<RCf::FIFO> fifo;
      auto r = RCf::FIFO::open(&dp, ioctx, get_oid(0, i), &fifo, null_yield);
      ASSERT_GE(0, r);
      ASSERT_TRUE(fifo);
      cb::list bl;
      encode(i, bl);
      r = fifo->push(&dp, bl, null_yield);
      ASSERT_GE(0, r);
    }

  void assert_empty() {
    std::vector<lr::ObjectItem> result;
    lr::ObjectCursor next;
    auto r = ioctx.object_list(ioctx.object_list_begin(), ioctx.object_list_end(),
			       100, {}, &result, &next);
    ASSERT_GE(r, 0);
    ASSERT_TRUE(result.empty());
  }
};

TEST_F(LogBacking, TestOmap)
{
  make_omap();
  auto stat = log_backing_type(&dp, ioctx, log_type::fifo, SHARDS,
			       [this](int shard){ return get_oid(0, shard); },
			       null_yield);
  ASSERT_EQ(log_type::omap, *stat);
}

TEST_F(LogBacking, TestOmapEmpty)
{
  auto stat = log_backing_type(&dp, ioctx, log_type::omap, SHARDS,
			       [this](int shard){ return get_oid(0, shard); },
			       null_yield);
  ASSERT_EQ(log_type::omap, *stat);
}

TEST_F(LogBacking, TestFIFO)
{
  make_fifo();
  auto stat = log_backing_type(&dp, ioctx, log_type::fifo, SHARDS,
			       [this](int shard){ return get_oid(0, shard); },
			       null_yield);
  ASSERT_EQ(log_type::fifo, *stat);
}

TEST_F(LogBacking, TestFIFOEmpty)
{
  auto stat = log_backing_type(&dp, ioctx, log_type::fifo, SHARDS,
			       [this](int shard){ return get_oid(0, shard); },
			       null_yield);
  ASSERT_EQ(log_type::fifo, *stat);
}

TEST(CursorGen, RoundTrip) {
  const std::string_view pcurs = "fded";
  {
    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);
  }
}

class generations final : public logback_generations {
public:

  entries_t got_entries;
  std::optional<uint64_t> tail;

  using logback_generations::logback_generations;

  bs::error_code handle_init(entries_t e) noexcept {
    got_entries = e;
    return {};
  }

  bs::error_code handle_new_gens(entries_t e) noexcept  {
    got_entries = e;
    return {};
  }

  bs::error_code handle_empty_to(uint64_t new_tail) noexcept {
    tail = new_tail;
    return {};
  }
};

TEST_F(LogBacking, GenerationSingle)
{
  auto lgr = logback_generations::init<generations>(
    &dp, ioctx, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);
  ASSERT_TRUE(lgr);

  auto lg = std::move(*lgr);

  ASSERT_EQ(0, lg->got_entries.begin()->first);

  ASSERT_EQ(0, lg->got_entries[0].gen_id);
  ASSERT_EQ(log_type::fifo, lg->got_entries[0].type);
  ASSERT_FALSE(lg->got_entries[0].pruned);

  auto ec = lg->empty_to(&dp, 0, null_yield);
  ASSERT_TRUE(ec);

  lg.reset();

  lg = *logback_generations::init<generations>(
    &dp, ioctx, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);

  ASSERT_EQ(0, lg->got_entries.begin()->first);

  ASSERT_EQ(0, lg->got_entries[0].gen_id);
  ASSERT_EQ(log_type::fifo, lg->got_entries[0].type);
  ASSERT_FALSE(lg->got_entries[0].pruned);

  lg->got_entries.clear();

  ec = lg->new_backing(&dp, log_type::omap, null_yield);
  ASSERT_FALSE(ec);

  ASSERT_EQ(1, lg->got_entries.size());
  ASSERT_EQ(1, lg->got_entries[1].gen_id);
  ASSERT_EQ(log_type::omap, lg->got_entries[1].type);
  ASSERT_FALSE(lg->got_entries[1].pruned);

  lg.reset();

  lg = *logback_generations::init<generations>(
    &dp, ioctx, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);

  ASSERT_EQ(2, lg->got_entries.size());
  ASSERT_EQ(0, lg->got_entries[0].gen_id);
  ASSERT_EQ(log_type::fifo, lg->got_entries[0].type);
  ASSERT_FALSE(lg->got_entries[0].pruned);

  ASSERT_EQ(1, lg->got_entries[1].gen_id);
  ASSERT_EQ(log_type::omap, lg->got_entries[1].type);
  ASSERT_FALSE(lg->got_entries[1].pruned);

  ec = lg->empty_to(&dp, 0, null_yield);
  ASSERT_FALSE(ec);

  ASSERT_EQ(0, *lg->tail);

  lg.reset();

  lg = *logback_generations::init<generations>(
    &dp, ioctx, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);

  ASSERT_EQ(1, lg->got_entries.size());
  ASSERT_EQ(1, lg->got_entries[1].gen_id);
  ASSERT_EQ(log_type::omap, lg->got_entries[1].type);
  ASSERT_FALSE(lg->got_entries[1].pruned);
}

TEST_F(LogBacking, GenerationWN)
{
  auto lg1 = *logback_generations::init<generations>(
    &dp, ioctx, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);

  auto ec = lg1->new_backing(&dp, log_type::omap, null_yield);
  ASSERT_FALSE(ec);

  ASSERT_EQ(1, lg1->got_entries.size());
  ASSERT_EQ(1, lg1->got_entries[1].gen_id);
  ASSERT_EQ(log_type::omap, lg1->got_entries[1].type);
  ASSERT_FALSE(lg1->got_entries[1].pruned);

  lg1->got_entries.clear();

  auto lg2 = *logback_generations::init<generations>(
    &dp, ioctx2, "foobar", [this](uint64_t gen_id, int shard) {
      return get_oid(gen_id, shard);
    }, SHARDS, log_type::fifo, null_yield);

  ASSERT_EQ(2, lg2->got_entries.size());

  ASSERT_EQ(0, lg2->got_entries[0].gen_id);
  ASSERT_EQ(log_type::fifo, lg2->got_entries[0].type);
  ASSERT_FALSE(lg2->got_entries[0].pruned);

  ASSERT_EQ(1, lg2->got_entries[1].gen_id);
  ASSERT_EQ(log_type::omap, lg2->got_entries[1].type);
  ASSERT_FALSE(lg2->got_entries[1].pruned);

  lg2->got_entries.clear();

  ec = lg1->new_backing(&dp, log_type::fifo, null_yield);
  ASSERT_FALSE(ec);

  ASSERT_EQ(1, lg1->got_entries.size());
  ASSERT_EQ(2, lg1->got_entries[2].gen_id);
  ASSERT_EQ(log_type::fifo, lg1->got_entries[2].type);
  ASSERT_FALSE(lg1->got_entries[2].pruned);

  ASSERT_EQ(1, lg2->got_entries.size());
  ASSERT_EQ(2, lg2->got_entries[2].gen_id);
  ASSERT_EQ(log_type::fifo, lg2->got_entries[2].type);
  ASSERT_FALSE(lg2->got_entries[2].pruned);

  lg1->got_entries.clear();
  lg2->got_entries.clear();

  ec = lg2->empty_to(&dp, 1, null_yield);
  ASSERT_FALSE(ec);

  ASSERT_EQ(1, *lg1->tail);
  ASSERT_EQ(1, *lg2->tail);

  lg1->tail.reset();
  lg2->tail.reset();
}