summaryrefslogtreecommitdiffstats
path: root/src/test/rgw/test_rgw_period_history.cc
blob: a4854ea778f1410cbb884ce34484d360fed30d7e (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
// -*- 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) 2015 Red Hat
 *
 * 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/rgw_period_history.h"
#include "rgw/rgw_rados.h"
#include "rgw/rgw_zone.h"
#include "global/global_init.h"
#include "common/ceph_argparse.h"
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>

namespace {

// construct a period with the given fields
RGWPeriod make_period(const std::string& id, epoch_t realm_epoch,
                      const std::string& predecessor)
{
  RGWPeriod period(id);
  period.set_realm_epoch(realm_epoch);
  period.set_predecessor(predecessor);
  return period;
}

const auto current_period = make_period("5", 5, "4");

// mock puller that throws an exception if it's called
struct ErrorPuller : public RGWPeriodHistory::Puller {
  int pull(const DoutPrefixProvider *dpp, const std::string& id, RGWPeriod& period, optional_yield) override {
    throw std::runtime_error("unexpected call to pull");
  }
};
ErrorPuller puller; // default puller

// mock puller that records the period ids requested and returns an error
using Ids = std::vector<std::string>;
class RecordingPuller : public RGWPeriodHistory::Puller {
  const int error;
 public:
  explicit RecordingPuller(int error) : error(error) {}
  Ids ids;
  int pull(const DoutPrefixProvider *dpp, const std::string& id, RGWPeriod& period, optional_yield) override {
    ids.push_back(id);
    return error;
  }
};

// mock puller that returns a fake period by parsing the period id
struct NumericPuller : public RGWPeriodHistory::Puller {
  int pull(const DoutPrefixProvider *dpp, const std::string& id, RGWPeriod& period, optional_yield) override {
    // relies on numeric period ids to divine the realm_epoch
    auto realm_epoch = boost::lexical_cast<epoch_t>(id);
    auto predecessor = boost::lexical_cast<std::string>(realm_epoch-1);
    period = make_period(id, realm_epoch, predecessor);
    return 0;
  }
};

} // anonymous namespace

// for ASSERT_EQ()
bool operator==(const RGWPeriod& lhs, const RGWPeriod& rhs)
{
  return lhs.get_id() == rhs.get_id()
      && lhs.get_realm_epoch() == rhs.get_realm_epoch();
}

TEST(PeriodHistory, InsertBefore)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  // inserting right before current_period 5 will attach to history
  auto c = history.insert(make_period("4", 4, "3"));
  ASSERT_TRUE(c);
  ASSERT_FALSE(c.has_prev());
  ASSERT_TRUE(c.has_next());

  // cursor can traverse forward to current_period
  c.next();
  ASSERT_EQ(5u, c.get_epoch());
  ASSERT_EQ(current_period, c.get_period());
}

TEST(PeriodHistory, InsertAfter)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  // inserting right after current_period 5 will attach to history
  auto c = history.insert(make_period("6", 6, "5"));
  ASSERT_TRUE(c);
  ASSERT_TRUE(c.has_prev());
  ASSERT_FALSE(c.has_next());

  // cursor can traverse back to current_period
  c.prev();
  ASSERT_EQ(5u, c.get_epoch());
  ASSERT_EQ(current_period, c.get_period());
}

TEST(PeriodHistory, InsertWayBefore)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  // inserting way before current_period 5 will not attach to history
  auto c = history.insert(make_period("1", 1, ""));
  ASSERT_FALSE(c);
  ASSERT_EQ(0, c.get_error());
}

TEST(PeriodHistory, InsertWayAfter)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  // inserting way after current_period 5 will not attach to history
  auto c = history.insert(make_period("9", 9, "8"));
  ASSERT_FALSE(c);
  ASSERT_EQ(0, c.get_error());
}

TEST(PeriodHistory, PullPredecessorsBeforeCurrent)
{
  RecordingPuller puller{-EFAULT};
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);
  const DoutPrefix dp(g_ceph_context, 1, "test rgw period history: ");

  // create a disjoint history at 1 and verify that periods are requested
  // backwards from current_period
  auto c1 = history.attach(&dp, make_period("1", 1, ""), null_yield);
  ASSERT_FALSE(c1);
  ASSERT_EQ(-EFAULT, c1.get_error());
  ASSERT_EQ(Ids{"4"}, puller.ids);

  auto c4 = history.insert(make_period("4", 4, "3"));
  ASSERT_TRUE(c4);

  c1 = history.attach(&dp, make_period("1", 1, ""), null_yield);
  ASSERT_FALSE(c1);
  ASSERT_EQ(-EFAULT, c1.get_error());
  ASSERT_EQ(Ids({"4", "3"}), puller.ids);

  auto c3 = history.insert(make_period("3", 3, "2"));
  ASSERT_TRUE(c3);

  c1 = history.attach(&dp, make_period("1", 1, ""), null_yield);
  ASSERT_FALSE(c1);
  ASSERT_EQ(-EFAULT, c1.get_error());
  ASSERT_EQ(Ids({"4", "3", "2"}), puller.ids);

  auto c2 = history.insert(make_period("2", 2, "1"));
  ASSERT_TRUE(c2);

  c1 = history.attach(&dp, make_period("1", 1, ""), null_yield);
  ASSERT_TRUE(c1);
  ASSERT_EQ(Ids({"4", "3", "2"}), puller.ids);
}

TEST(PeriodHistory, PullPredecessorsAfterCurrent)
{
  RecordingPuller puller{-EFAULT};
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);
  const DoutPrefix dp(g_ceph_context, 1, "test rgw period history: ");

  // create a disjoint history at 9 and verify that periods are requested
  // backwards down to current_period
  auto c9 = history.attach(&dp, make_period("9", 9, "8"), null_yield);
  ASSERT_FALSE(c9);
  ASSERT_EQ(-EFAULT, c9.get_error());
  ASSERT_EQ(Ids{"8"}, puller.ids);

  auto c8 = history.attach(&dp, make_period("8", 8, "7"), null_yield);
  ASSERT_FALSE(c8);
  ASSERT_EQ(-EFAULT, c8.get_error());
  ASSERT_EQ(Ids({"8", "7"}), puller.ids);

  auto c7 = history.attach(&dp, make_period("7", 7, "6"), null_yield);
  ASSERT_FALSE(c7);
  ASSERT_EQ(-EFAULT, c7.get_error());
  ASSERT_EQ(Ids({"8", "7", "6"}), puller.ids);

  auto c6 = history.attach(&dp, make_period("6", 6, "5"), null_yield);
  ASSERT_TRUE(c6);
  ASSERT_EQ(Ids({"8", "7", "6"}), puller.ids);
}

TEST(PeriodHistory, MergeBeforeCurrent)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  auto c = history.get_current();
  ASSERT_FALSE(c.has_prev());

  // create a disjoint history at 3
  auto c3 = history.insert(make_period("3", 3, "2"));
  ASSERT_FALSE(c3);

  // insert the missing period to merge 3 and 5
  auto c4 = history.insert(make_period("4", 4, "3"));
  ASSERT_TRUE(c4);
  ASSERT_TRUE(c4.has_prev());
  ASSERT_TRUE(c4.has_next());

  // verify that the merge didn't destroy the original cursor's history
  ASSERT_EQ(current_period, c.get_period());
  ASSERT_TRUE(c.has_prev());
}

TEST(PeriodHistory, MergeAfterCurrent)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  auto c = history.get_current();
  ASSERT_FALSE(c.has_next());

  // create a disjoint history at 7
  auto c7 = history.insert(make_period("7", 7, "6"));
  ASSERT_FALSE(c7);

  // insert the missing period to merge 5 and 7
  auto c6 = history.insert(make_period("6", 6, "5"));
  ASSERT_TRUE(c6);
  ASSERT_TRUE(c6.has_prev());
  ASSERT_TRUE(c6.has_next());

  // verify that the merge didn't destroy the original cursor's history
  ASSERT_EQ(current_period, c.get_period());
  ASSERT_TRUE(c.has_next());
}

TEST(PeriodHistory, MergeWithoutCurrent)
{
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);

  // create a disjoint history at 7
  auto c7 = history.insert(make_period("7", 7, "6"));
  ASSERT_FALSE(c7);

  // create a disjoint history at 9
  auto c9 = history.insert(make_period("9", 9, "8"));
  ASSERT_FALSE(c9);

  // insert the missing period to merge 7 and 9
  auto c8 = history.insert(make_period("8", 8, "7"));
  ASSERT_FALSE(c8); // not connected to current_period yet

  // insert the missing period to merge 5 and 7-9
  auto c = history.insert(make_period("6", 6, "5"));
  ASSERT_TRUE(c);
  ASSERT_TRUE(c.has_next());

  // verify that we merged all periods from 5-9
  c.next();
  ASSERT_EQ(7u, c.get_epoch());
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(8u, c.get_epoch());
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(9u, c.get_epoch());
  ASSERT_FALSE(c.has_next());
}

TEST(PeriodHistory, AttachBefore)
{
  NumericPuller puller;
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);
  const DoutPrefix dp(g_ceph_context, 1, "test rgw period history: ");

  auto c1 = history.attach(&dp, make_period("1", 1, ""), null_yield);
  ASSERT_TRUE(c1);

  // verify that we pulled and merged all periods from 1-5
  auto c = history.get_current();
  ASSERT_TRUE(c);
  ASSERT_TRUE(c.has_prev());
  c.prev();
  ASSERT_EQ(4u, c.get_epoch());
  ASSERT_TRUE(c.has_prev());
  c.prev();
  ASSERT_EQ(3u, c.get_epoch());
  ASSERT_TRUE(c.has_prev());
  c.prev();
  ASSERT_EQ(2u, c.get_epoch());
  ASSERT_TRUE(c.has_prev());
  c.prev();
  ASSERT_EQ(1u, c.get_epoch());
  ASSERT_FALSE(c.has_prev());
}

TEST(PeriodHistory, AttachAfter)
{
  NumericPuller puller;
  RGWPeriodHistory history(g_ceph_context, &puller, current_period);
  const DoutPrefix dp(g_ceph_context, 1, "test rgw period history: ");

  auto c9 = history.attach(&dp, make_period("9", 9, "8"), null_yield);
  ASSERT_TRUE(c9);

  // verify that we pulled and merged all periods from 5-9
  auto c = history.get_current();
  ASSERT_TRUE(c);
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(6u, c.get_epoch());
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(7u, c.get_epoch());
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(8u, c.get_epoch());
  ASSERT_TRUE(c.has_next());
  c.next();
  ASSERT_EQ(9u, c.get_epoch());
  ASSERT_FALSE(c.has_next());
}

int main(int argc, char** argv)
{
  vector<const char*> args;
  argv_to_vec(argc, (const char **)argv, args);

  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
			 CODE_ENVIRONMENT_UTILITY,
			 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
  common_init_finish(g_ceph_context);

  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}