summaryrefslogtreecommitdiffstats
path: root/src/test/librados/snapshots_stats.cc
blob: 143299ab1a9b79a85ebd33ff0dace7793a9685fd (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
#include "include/rados.h"
#include "json_spirit/json_spirit.h"
#include "test/librados/test.h"
#include "test/librados/TestCase.h"

#include <algorithm>
#include <errno.h>
#include "gtest/gtest.h"
#include <string>
#include <vector>

using std::string;

class LibRadosSnapshotStatsSelfManaged : public RadosTest {
public:
  LibRadosSnapshotStatsSelfManaged() {};
  ~LibRadosSnapshotStatsSelfManaged() override {};
protected:
  void SetUp() override {
    // disable pg autoscaler for the tests
    string c =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"off\""
      "}";
    char *cmd[1];
    cmd[0] = (char *)c.c_str();
    std::cout << "Setting pg_autoscaler to 'off'" << std::endl;
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL,
      0, NULL, 0));

    // disable scrubs for the test
    c = string("{\"prefix\": \"osd set\",\"key\":\"noscrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
    c = string("{\"prefix\": \"osd set\",\"key\":\"nodeep-scrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));

    RadosTest::SetUp();
  }

  void TearDown() override {
    // re-enable pg autoscaler
    string c =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"on\""
      "}";
    char *cmd[1];
    cmd[0] = (char *)c.c_str();
    std::cout << "Setting pg_autoscaler to 'on'" << std::endl;
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL,
      0, NULL, 0));

    // re-enable scrubs
    c = string("{\"prefix\": \"osd unset\",\"key\":\"noscrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
    c = string("{\"prefix\": \"osd unset\",\"key\":\"nodeep-scrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));

    RadosTest::TearDown();
  }
};

class LibRadosSnapshotStatsSelfManagedEC : public RadosTestEC {
public:
  LibRadosSnapshotStatsSelfManagedEC() {};
  ~LibRadosSnapshotStatsSelfManagedEC() override {};
protected:
  void SetUp() override {
    // disable pg autoscaler for the tests
    string c =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"off\""
      "}";
    char *cmd[1];
    cmd[0] = (char *)c.c_str();
    std::cout << "Setting pg_autoscaler to 'off'" << std::endl;
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL,
      0, NULL, 0));

    // disable scrubs for the test
    c = string("{\"prefix\": \"osd set\",\"key\":\"noscrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
    c = string("{\"prefix\": \"osd set\",\"key\":\"nodeep-scrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));

    RadosTestEC::SetUp();
  }

  void TearDown() override {
    // re-enable pg autoscaler
    string c =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"on\""
      "}";
    char *cmd[1];
    cmd[0] = (char *)c.c_str();
    std::cout << "Setting pg_autoscaler to 'on'" << std::endl;
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL,
      0, NULL, 0));

    // re-enable scrubs
    c = string("{\"prefix\": \"osd unset\",\"key\":\"noscrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
    c = string("{\"prefix\": \"osd unset\",\"key\":\"nodeep-scrub\"}");
    cmd[0] = (char *)c.c_str();
    ASSERT_EQ(0, rados_mon_command(s_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));

    RadosTestEC::TearDown();
  }
};

void get_snaptrim_stats(json_spirit::Object& pg_dump,
                        int *objs_trimmed,
                        double *trim_duration) {
  // pg_map
  json_spirit::Object pgmap;
  for (json_spirit::Object::size_type i = 0; i < pg_dump.size(); ++i) {
    json_spirit::Pair& p = pg_dump[i];
    if (p.name_ == "pg_map") {
      pgmap = p.value_.get_obj();
      break;
    }
  }

  // pg_stats array
  json_spirit::Array pgs;
  for (json_spirit::Object::size_type i = 0; i < pgmap.size(); ++i) {
    json_spirit::Pair& p = pgmap[i];
    if (p.name_ == "pg_stats") {
      pgs = p.value_.get_array();
      break;
    }
  }

  // snaptrim stats
  for (json_spirit::Object::size_type j = 0; j < pgs.size(); ++j) {
    json_spirit::Object& pg_stat = pgs[j].get_obj();
    for(json_spirit::Object::size_type k = 0; k < pg_stat.size(); ++k) {
      json_spirit::Pair& stats = pg_stat[k];
      if (stats.name_ == "objects_trimmed") {
        *objs_trimmed += stats.value_.get_int();
      }
      if (stats.name_ == "snaptrim_duration") {
        *trim_duration += stats.value_.get_real();
      }
    }
  }
}

const int bufsize = 128;

TEST_F(LibRadosSnapshotStatsSelfManaged, SnaptrimStats) {
  int num_objs = 10;

  // create objects
  char buf[bufsize];
  memset(buf, 0xcc, sizeof(buf));
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, rados_write(ioctx, obj.c_str(), buf, sizeof(buf), 0));
  }

  std::vector<uint64_t> my_snaps;
  for (int snap = 0; snap < 1; ++snap) {
    // create a snapshot, clone
    std::vector<uint64_t> ns(1);
    ns.insert(ns.end(), my_snaps.begin(), my_snaps.end());
    my_snaps.swap(ns);
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_create(ioctx, &my_snaps[0]));
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_set_write_ctx(ioctx, my_snaps[0],
                                        &my_snaps[0], my_snaps.size()));
    char buf2[sizeof(buf)];
    memset(buf2, 0xdd, sizeof(buf2));
    for (int i = 0; i < num_objs; ++i) {
      string obj = string("foo") + std::to_string(i);
      ASSERT_EQ(0, rados_write(ioctx, obj.c_str(), buf2, sizeof(buf2), 0));
    }
  }

  // wait for maps to settle
  ASSERT_EQ(0, rados_wait_for_latest_osdmap(cluster));

  // remove snaps - should trigger snaptrim
  rados_ioctx_snap_set_read(ioctx, LIBRADOS_SNAP_HEAD);
  for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_remove(ioctx, my_snaps[snap]));
  }

  // sleep for few secs for the trim stats to populate
  std::cout << "Waiting for snaptrim stats to be generated" << std::endl;
  sleep(30);

  // Dump pg stats and determine if snaptrim stats are getting set
  int objects_trimmed = 0;
  double snaptrim_duration = 0.0;
  int tries = 0;
  do {
    char *buf, *st;
    size_t buflen, stlen;
    string c = string("{\"prefix\": \"pg dump\",\"format\":\"json\"}");
    const char *cmd = c.c_str();
    ASSERT_EQ(0, rados_mon_command(cluster, (const char **)&cmd, 1, "", 0,
      &buf, &buflen, &st, &stlen));
    string outstr(buf, buflen);
    json_spirit::Value v;
    ASSERT_NE(0, json_spirit::read(outstr, v)) << "unable to parse json."
      << '\n' << outstr;

    // pg dump object
    json_spirit::Object& obj = v.get_obj();
    get_snaptrim_stats(obj, &objects_trimmed, &snaptrim_duration);
    if (objects_trimmed < num_objs) {
      tries++;
      objects_trimmed = 0;
      std::cout << "Still waiting for all objects to be trimmed... " <<std::endl;
      sleep(30);
    }
  } while(objects_trimmed < num_objs && tries < 5);

  // final check for objects trimmed
  ASSERT_EQ(objects_trimmed, num_objs);
  std::cout << "Snaptrim duration: " << snaptrim_duration << std::endl;
  ASSERT_GT(snaptrim_duration, 0.0);

  // clean-up remaining objects
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, rados_remove(ioctx, obj.c_str()));
  }
}

// EC testing
TEST_F(LibRadosSnapshotStatsSelfManagedEC, SnaptrimStats) {
  int num_objs = 10;
  int bsize = alignment;
  char *buf = (char *)new char[bsize];
  memset(buf, 0xcc, bsize);
  // create objects
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, rados_write(ioctx, obj.c_str(), buf, bsize, 0));
  }

  std::vector<uint64_t> my_snaps;
  for (int snap = 0; snap < 1; ++snap) {
    // create a snapshot, clone
    std::vector<uint64_t> ns(1);
    ns.insert(ns.end(), my_snaps.begin(), my_snaps.end());
    my_snaps.swap(ns);
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_create(ioctx, &my_snaps[0]));
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_set_write_ctx(ioctx, my_snaps[0],
                                        &my_snaps[0], my_snaps.size()));
    char *buf2 = (char *)new char[bsize];
    memset(buf2, 0xdd, bsize);
    for (int i = 0; i < num_objs; ++i) {
      string obj = string("foo") + std::to_string(i);
      ASSERT_EQ(0, rados_write(ioctx, obj.c_str(), buf2, bsize, bsize));
    }
    delete[] buf2;
  }

  // wait for maps to settle
  ASSERT_EQ(0, rados_wait_for_latest_osdmap(cluster));

  // remove snaps - should trigger snaptrim
  rados_ioctx_snap_set_read(ioctx, LIBRADOS_SNAP_HEAD);
  for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
    ASSERT_EQ(0, rados_ioctx_selfmanaged_snap_remove(ioctx, my_snaps[snap]));
  }

  // sleep for few secs for the trim stats to populate
  std::cout << "Waiting for snaptrim stats to be generated" << std::endl;
  sleep(30);

  // Dump pg stats and determine if snaptrim stats are getting set
  int objects_trimmed = 0;
  double snaptrim_duration = 0.0;
  int tries = 0;
  do {
    char *buf, *st;
    size_t buflen, stlen;
    string c = string("{\"prefix\": \"pg dump\",\"format\":\"json\"}");
    const char *cmd = c.c_str();
    ASSERT_EQ(0, rados_mon_command(cluster, (const char **)&cmd, 1, 0, 0,
      &buf, &buflen, &st, &stlen));
    string outstr(buf, buflen);
    json_spirit::Value v;
    ASSERT_NE(0, json_spirit::read(outstr, v)) << "Unable tp parse json."
      << '\n' << outstr;

    // pg dump object
    json_spirit::Object& obj = v.get_obj();
    get_snaptrim_stats(obj, &objects_trimmed, &snaptrim_duration);
    if (objects_trimmed != num_objs) {
      tries++;
      objects_trimmed = 0;
      std::cout << "Still waiting for all objects to be trimmed... " <<std::endl;
      sleep(30);
    }
  } while (objects_trimmed != num_objs && tries < 5);

  // final check for objects trimmed
  ASSERT_EQ(objects_trimmed, num_objs);
  std::cout << "Snaptrim duration: " << snaptrim_duration << std::endl;
  ASSERT_GT(snaptrim_duration, 0.0);

  // clean-up remaining objects
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, rados_remove(ioctx, obj.c_str()));
  }

  delete[] buf;
}