summaryrefslogtreecommitdiffstats
path: root/src/test/librados/snapshots_stats_cxx.cc
blob: f6be3a915df2d445efaab8bdfb140a1f4c0071f3 (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
#include <algorithm>
#include <errno.h>
#include <string>
#include <vector>

#include "gtest/gtest.h"

#include "include/rados.h"
#include "include/rados/librados.hpp"
#include "json_spirit/json_spirit.h"
#include "test/librados/test_cxx.h"
#include "test/librados/testcase_cxx.h"

using namespace librados;

using std::string;

class LibRadosSnapshotStatsSelfManagedPP : public RadosTestPP {
public:
  LibRadosSnapshotStatsSelfManagedPP() {};
  ~LibRadosSnapshotStatsSelfManagedPP() override {};
protected:
  void SetUp() override {
    // disable pg autoscaler for the tests
    string cmd =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"off\""
      "}";
    std::cout << "Setting pg_autoscaler to 'off'" << std::endl;
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    // disable scrubs for the test
    cmd = "{\"prefix\": \"osd set\",\"key\":\"noscrub\"}";
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));
    cmd = "{\"prefix\": \"osd set\",\"key\":\"nodeep-scrub\"}";
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    RadosTestPP::SetUp();
  }

  void TearDown() override {
    // re-enable pg autoscaler
    string cmd =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"on\""
      "}";
    std::cout << "Setting pg_autoscaler to 'on'" << std::endl;
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    // re-enable scrubs
    cmd = "{\"prefix\": \"osd unset\",\"key\":\"noscrub\"}";
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));
    cmd = string("{\"prefix\": \"osd unset\",\"key\":\"nodeep-scrub\"}");
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    RadosTestPP::TearDown();
  }
};

class LibRadosSnapshotStatsSelfManagedECPP : public RadosTestECPP {
public:
  LibRadosSnapshotStatsSelfManagedECPP() {};
  ~LibRadosSnapshotStatsSelfManagedECPP() override {};
protected:
  void SetUp() override {
    // disable pg autoscaler for the tests
    string cmd =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"off\""
      "}";
    std::cout << "Setting pg_autoscaler to 'off'" << std::endl;
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    // disable scrubs for the test
    cmd = string("{\"prefix\": \"osd set\",\"key\":\"noscrub\"}");
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));
    cmd = string("{\"prefix\": \"osd set\",\"key\":\"nodeep-scrub\"}");
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    RadosTestECPP::SetUp();
  }

  void TearDown() override {
    // re-enable pg autoscaler
    string cmd =
      "{"
        "\"prefix\": \"config set\", "
        "\"who\": \"global\", "
        "\"name\": \"osd_pool_default_pg_autoscale_mode\", "
        "\"value\": \"on\""
      "}";
    std::cout << "Setting pg_autoscaler to 'on'" << std::endl;
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    // re-enable scrubs
    cmd = string("{\"prefix\": \"osd unset\",\"key\":\"noscrub\"}");
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));
    cmd = string("{\"prefix\": \"osd unset\",\"key\":\"nodeep-scrub\"}");
    ASSERT_EQ(0, s_cluster.mon_command(cmd, inbl, &outbl, NULL));

    RadosTestECPP::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(LibRadosSnapshotStatsSelfManagedPP, SnaptrimStatsPP) {
  int num_objs = 10;

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

  std::vector<uint64_t> my_snaps;
  char buf2[sizeof(buf)];
  memset(buf2, 0xdd, sizeof(buf2));
  bufferlist bl2;
  bl2.append(buf2, sizeof(buf2));
  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, ioctx.selfmanaged_snap_create(&my_snaps[0]));
    ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0], my_snaps));
    for (int i = 0; i < num_objs; ++i) {
      string obj = string("foo") + std::to_string(i);
      ASSERT_EQ(0, ioctx.write(obj, bl2, sizeof(buf2), 0));
    }
  }

  // wait for maps to settle
  cluster.wait_for_latest_osdmap();

  // remove snaps - should trigger snaptrim
  for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
    ioctx.selfmanaged_snap_remove(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 {
    string cmd = string("{\"prefix\": \"pg dump\",\"format\":\"json\"}");
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, cluster.mon_command(cmd, inbl, &outbl, NULL));
    string outstr(outbl.c_str(), outbl.length());
    json_spirit::Value v;
    ASSERT_NE(0, json_spirit::read(outstr, v)) << "unable to parse json." << '\n' << outstr;

    // pg_map
    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
  ioctx.snap_set_read(librados::SNAP_HEAD);
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, ioctx.remove(obj));
  }
}

// EC testing
TEST_F(LibRadosSnapshotStatsSelfManagedECPP, SnaptrimStatsECPP) {
  int num_objs = 10;
  int bsize = alignment;

  // create objects
  char *buf = (char *)new char[bsize];
  memset(buf, 0xcc, bsize);
  bufferlist bl;
  bl.append(buf, bsize);
  for (int i = 0; i < num_objs; ++i) {
   string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, ioctx.write(obj, bl, bsize, 0));
  }

  std::vector<uint64_t> my_snaps;
  char *buf2 = (char *)new char[bsize];
  memset(buf2, 0xdd, bsize);
  bufferlist bl2;
  bl2.append(buf2, bsize);
  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, ioctx.selfmanaged_snap_create(&my_snaps[0]));
    ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0], my_snaps));
    for (int i = 0; i < num_objs; ++i) {
      string obj = string("foo") + std::to_string(i);
      ASSERT_EQ(0, ioctx.write(obj, bl2, bsize, bsize));
    }
  }

  // wait for maps to settle
  cluster.wait_for_latest_osdmap();

  // remove snaps - should trigger snaptrim
  for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
    ioctx.selfmanaged_snap_remove(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 {
    string cmd = string("{\"prefix\": \"pg dump\",\"format\":\"json\"}");
    bufferlist inbl;
    bufferlist outbl;
    ASSERT_EQ(0, cluster.mon_command(cmd, inbl, &outbl, NULL));
    string outstr(outbl.c_str(), outbl.length());
    json_spirit::Value v;
    ASSERT_NE(0, json_spirit::read(outstr, v)) << "unable to parse json." << '\n' << outstr;

    // pg_map
    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
  ioctx.snap_set_read(LIBRADOS_SNAP_HEAD);
  for (int i = 0; i < num_objs; ++i) {
    string obj = string("foo") + std::to_string(i);
    ASSERT_EQ(0, ioctx.remove(obj));
  }

  delete[] buf;
  delete[] buf2;
}