summaryrefslogtreecommitdiffstats
path: root/src/test/cls_log/test_cls_log.cc
blob: 868afcaf65d8f54ee7c7de8a14845742999b794d (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "include/types.h"
#include "cls/log/cls_log_types.h"
#include "cls/log/cls_log_client.h"

#include "include/utime.h"
#include "common/Clock.h"
#include "global/global_context.h"

#include "gtest/gtest.h"
#include "test/librados/test_cxx.h"

#include <errno.h>
#include <string>
#include <vector>

/// creates a temporary pool and initializes an IoCtx for each test
class cls_log : public ::testing::Test {
  librados::Rados rados;
  std::string pool_name;
 protected:
  librados::IoCtx ioctx;

  void SetUp() {
    pool_name = get_temp_pool_name();
    /* create pool */
    ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
    ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
  }
  void TearDown() {
    /* remove pool */
    ioctx.close();
    ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados));
  }
};

static int read_bl(bufferlist& bl, int *i)
{
  auto iter = bl.cbegin();

  try {
    decode(*i, iter);
  } catch (buffer::error& err) {
    std::cout << "failed to decode buffer" << std::endl;
    return -EIO;
  }

  return 0;
}

void add_log(librados::ObjectWriteOperation *op, utime_t& timestamp, string& section, string&name, int i)
{
  bufferlist bl;
  encode(i, bl);

  cls_log_add(*op, timestamp, section, name, bl);
}


string get_name(int i)
{
  string name_prefix = "data-source";

  char buf[16];
  snprintf(buf, sizeof(buf), "%d", i);
  return name_prefix + buf;
}

void generate_log(librados::IoCtx& ioctx, string& oid, int max, utime_t& start_time, bool modify_time)
{
  string section = "global";

  librados::ObjectWriteOperation op;

  int i;

  for (i = 0; i < max; i++) {
    uint32_t secs = start_time.sec();
    if (modify_time)
      secs += i;

    utime_t ts(secs, start_time.nsec());
    string name = get_name(i);

    add_log(&op, ts, section, name, i);
  }

  ASSERT_EQ(0, ioctx.operate(oid, &op));
}

utime_t get_time(utime_t& start_time, int i, bool modify_time)
{
  uint32_t secs = start_time.sec();
  if (modify_time)
    secs += i;
  return utime_t(secs, start_time.nsec());
}

void check_entry(cls_log_entry& entry, utime_t& start_time, int i, bool modified_time)
{
  string section = "global";
  string name = get_name(i);
  utime_t ts = get_time(start_time, i, modified_time);

  ASSERT_EQ(section, entry.section);
  ASSERT_EQ(name, entry.name);
  ASSERT_EQ(ts, entry.timestamp);
}

static int log_list(librados::IoCtx& ioctx, const std::string& oid,
                    utime_t& from, utime_t& to,
                    const string& in_marker, int max_entries,
                    list<cls_log_entry>& entries,
                    string *out_marker, bool *truncated)
{
  librados::ObjectReadOperation rop;
  cls_log_list(rop, from, to, in_marker, max_entries,
               entries, out_marker, truncated);
  bufferlist obl;
  return ioctx.operate(oid, &rop, &obl);
}

static int log_list(librados::IoCtx& ioctx, const std::string& oid,
                    utime_t& from, utime_t& to, int max_entries,
                    list<cls_log_entry>& entries, bool *truncated)
{
  std::string marker;
  return log_list(ioctx, oid, from, to, marker, max_entries,
                  entries, &marker, truncated);
}

static int log_list(librados::IoCtx& ioctx, const std::string& oid,
                    list<cls_log_entry>& entries)
{
  utime_t from, to;
  bool truncated{false};
  return log_list(ioctx, oid, from, to, 0, entries, &truncated);
}

TEST_F(cls_log, test_log_add_same_time)
{
  /* add chains */
  string oid = "obj";

  /* create object */
  ASSERT_EQ(0, ioctx.create(oid, true));

  /* generate log */
  utime_t start_time = ceph_clock_now();
  utime_t to_time = get_time(start_time, 1, true);
  generate_log(ioctx, oid, 10, start_time, false);

  list<cls_log_entry> entries;
  bool truncated;

  /* check list */
  {
    ASSERT_EQ(0, log_list(ioctx, oid, start_time, to_time, 0,
                          entries, &truncated));
    ASSERT_EQ(10, (int)entries.size());
    ASSERT_EQ(0, (int)truncated);
  }
  list<cls_log_entry>::iterator iter;

  /* need to sort returned entries, all were using the same time as key */
  map<int, cls_log_entry> check_ents;

  for (iter = entries.begin(); iter != entries.end(); ++iter) {
    cls_log_entry& entry = *iter;

    int num;
    ASSERT_EQ(0, read_bl(entry.data, &num));

    check_ents[num] = entry;
  }

  ASSERT_EQ(10, (int)check_ents.size());

  map<int, cls_log_entry>::iterator ei;

  /* verify entries are as expected */

  int i;

  for (i = 0, ei = check_ents.begin(); i < 10; i++, ++ei) {
    cls_log_entry& entry = ei->second;

    ASSERT_EQ(i, ei->first);
    check_entry(entry, start_time, i, false);
  }

  /* check list again, now want to be truncated*/
  {
    ASSERT_EQ(0, log_list(ioctx, oid, start_time, to_time, 1,
                          entries, &truncated));
    ASSERT_EQ(1, (int)entries.size());
    ASSERT_EQ(1, (int)truncated);
  }
}

TEST_F(cls_log, test_log_add_different_time)
{
  /* add chains */
  string oid = "obj";

  /* create object */
  ASSERT_EQ(0, ioctx.create(oid, true));

  /* generate log */
  utime_t start_time = ceph_clock_now();
  generate_log(ioctx, oid, 10, start_time, true);

  list<cls_log_entry> entries;
  bool truncated;

  utime_t to_time = utime_t(start_time.sec() + 10, start_time.nsec());

  {
    /* check list */
    ASSERT_EQ(0, log_list(ioctx, oid, start_time, to_time, 0,
                          entries, &truncated));
    ASSERT_EQ(10, (int)entries.size());
    ASSERT_EQ(0, (int)truncated);
  }

  list<cls_log_entry>::iterator iter;

  /* returned entries should be sorted by time */
  map<int, cls_log_entry> check_ents;

  int i;

  for (i = 0, iter = entries.begin(); iter != entries.end(); ++iter, ++i) {
    cls_log_entry& entry = *iter;

    int num;

    ASSERT_EQ(0, read_bl(entry.data, &num));

    ASSERT_EQ(i, num);

    check_entry(entry, start_time, i, true);
  }

  /* check list again with shifted time */
  {
    utime_t next_time = get_time(start_time, 1, true);
    ASSERT_EQ(0, log_list(ioctx, oid, next_time, to_time, 0,
                          entries, &truncated));
    ASSERT_EQ(9u, entries.size());
    ASSERT_FALSE(truncated);
  }

  string marker;
  i = 0;
  do {
    string old_marker = std::move(marker);
    ASSERT_EQ(0, log_list(ioctx, oid, start_time, to_time, old_marker, 1,
                          entries, &marker, &truncated));
    ASSERT_NE(old_marker, marker);
    ASSERT_EQ(1, (int)entries.size());

    ++i;
    ASSERT_GE(10, i);
  } while (truncated);

  ASSERT_EQ(10, i);
}

int do_log_trim(librados::IoCtx& ioctx, const std::string& oid,
                const std::string& from_marker, const std::string& to_marker)
{
  librados::ObjectWriteOperation op;
  cls_log_trim(op, {}, {}, from_marker, to_marker);
  return ioctx.operate(oid, &op);
}

int do_log_trim(librados::IoCtx& ioctx, const std::string& oid,
                const utime_t& from_time, const utime_t& to_time)
{
  librados::ObjectWriteOperation op;
  cls_log_trim(op, from_time, to_time, "", "");
  return ioctx.operate(oid, &op);
}

TEST_F(cls_log, trim_by_time)
{
  /* add chains */
  string oid = "obj";

  /* create object */
  ASSERT_EQ(0, ioctx.create(oid, true));

  /* generate log */
  utime_t start_time = ceph_clock_now();
  generate_log(ioctx, oid, 10, start_time, true);

  list<cls_log_entry> entries;
  bool truncated;

  /* check list */

  /* trim */
  utime_t to_time = get_time(start_time, 10, true);

  for (int i = 0; i < 10; i++) {
    utime_t trim_time = get_time(start_time, i, true);

    utime_t zero_time;

    ASSERT_EQ(0, do_log_trim(ioctx, oid, zero_time, trim_time));
    ASSERT_EQ(-ENODATA, do_log_trim(ioctx, oid, zero_time, trim_time));

    ASSERT_EQ(0, log_list(ioctx, oid, start_time, to_time, 0,
                          entries, &truncated));
    ASSERT_EQ(9u - i, entries.size());
    ASSERT_FALSE(truncated);
  }
}

TEST_F(cls_log, trim_by_marker)
{
  string oid = "obj";
  ASSERT_EQ(0, ioctx.create(oid, true));

  utime_t start_time = ceph_clock_now();
  generate_log(ioctx, oid, 10, start_time, true);

  utime_t zero_time;
  std::vector<cls_log_entry> log1;
  {
    list<cls_log_entry> entries;
    ASSERT_EQ(0, log_list(ioctx, oid, entries));
    ASSERT_EQ(10u, entries.size());

    log1.assign(std::make_move_iterator(entries.begin()),
                std::make_move_iterator(entries.end()));
  }
  // trim front of log
  {
    const std::string from = "";
    const std::string to = log1[0].id;
    ASSERT_EQ(0, do_log_trim(ioctx, oid, from, to));
    list<cls_log_entry> entries;
    ASSERT_EQ(0, log_list(ioctx, oid, entries));
    ASSERT_EQ(9u, entries.size());
    EXPECT_EQ(log1[1].id, entries.begin()->id);
    ASSERT_EQ(-ENODATA, do_log_trim(ioctx, oid, from, to));
  }
  // trim back of log
  {
    const std::string from = log1[8].id;
    const std::string to = "9";
    ASSERT_EQ(0, do_log_trim(ioctx, oid, from, to));
    list<cls_log_entry> entries;
    ASSERT_EQ(0, log_list(ioctx, oid, entries));
    ASSERT_EQ(8u, entries.size());
    EXPECT_EQ(log1[8].id, entries.rbegin()->id);
    ASSERT_EQ(-ENODATA, do_log_trim(ioctx, oid, from, to));
  }
  // trim a key from the middle
  {
    const std::string from = log1[3].id;
    const std::string to = log1[4].id;
    ASSERT_EQ(0, do_log_trim(ioctx, oid, from, to));
    list<cls_log_entry> entries;
    ASSERT_EQ(0, log_list(ioctx, oid, entries));
    ASSERT_EQ(7u, entries.size());
    ASSERT_EQ(-ENODATA, do_log_trim(ioctx, oid, from, to));
  }
  // trim full log
  {
    const std::string from = "";
    const std::string to = "9";
    ASSERT_EQ(0, do_log_trim(ioctx, oid, from, to));
    list<cls_log_entry> entries;
    ASSERT_EQ(0, log_list(ioctx, oid, entries));
    ASSERT_EQ(0u, entries.size());
    ASSERT_EQ(-ENODATA, do_log_trim(ioctx, oid, from, to));
  }
}