summaryrefslogtreecommitdiffstats
path: root/src/cls/journal/cls_journal_client.cc
blob: c22a32cf3a32dba37fe0a8ca6e549a9ef8103f7f (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "cls/journal/cls_journal_client.h"
#include "include/rados/librados.hpp"
#include "include/buffer.h"
#include "include/Context.h"
#include "common/Cond.h"
#include <errno.h>

namespace cls {
namespace journal {
namespace client {
using ceph::encode;
using ceph::decode;

namespace {

struct C_AioExec : public Context {
  librados::IoCtx &ioctx;
  std::string oid;

  C_AioExec(librados::IoCtx &_ioctx, const std::string &_oid)
    : ioctx(_ioctx), oid(_oid) {
  }

  static void rados_callback(rados_completion_t c, void *arg) {
    Context *ctx = reinterpret_cast<Context *>(arg);
    ctx->complete(rados_aio_get_return_value(c));
  }
};

struct C_ClientList : public C_AioExec {
  std::set<cls::journal::Client> *clients;
  Context *on_finish;
  bufferlist outbl;

  C_ClientList(librados::IoCtx &_ioctx, const std::string &_oid,
               std::set<cls::journal::Client> *_clients,
               Context *_on_finish)
    : C_AioExec(_ioctx, _oid), clients(_clients), on_finish(_on_finish) {}

  void send(const std::string &start_after) {
    bufferlist inbl;
    encode(start_after, inbl);
    encode(JOURNAL_MAX_RETURN, inbl);

    librados::ObjectReadOperation op;
    op.exec("journal", "client_list", inbl);

    outbl.clear();
    librados::AioCompletion *rados_completion =
       librados::Rados::aio_create_completion(this, rados_callback, NULL);
    int r = ioctx.aio_operate(oid, rados_completion, &op, &outbl);
    ceph_assert(r == 0);
    rados_completion->release();
  }

  void complete(int r) override {
    if (r < 0) {
      finish(r);
      return;
    }

    try {
      auto iter = outbl.cbegin();
      std::set<cls::journal::Client> partial_clients;
      decode(partial_clients, iter);

      std::string start_after;
      if (!partial_clients.empty()) {
        start_after = partial_clients.rbegin()->id;
        clients->insert(partial_clients.begin(), partial_clients.end());
      }

      if (partial_clients.size() < JOURNAL_MAX_RETURN) {
        finish(0);
      } else {
        send(start_after);
      }
    } catch (const buffer::error &err) {
      finish(-EBADMSG);
    }
  }

  void finish(int r) override {
    on_finish->complete(r);
    delete this;
  }
};

struct C_ImmutableMetadata : public C_AioExec {
  uint8_t *order;
  uint8_t *splay_width;
  int64_t *pool_id;
  Context *on_finish;
  bufferlist outbl;

  C_ImmutableMetadata(librados::IoCtx &_ioctx, const std::string &_oid,
                      uint8_t *_order, uint8_t *_splay_width,
		      int64_t *_pool_id, Context *_on_finish)
    : C_AioExec(_ioctx, _oid), order(_order), splay_width(_splay_width),
      pool_id(_pool_id), on_finish(_on_finish) {
  }

  void send() {
    librados::ObjectReadOperation op;
    bufferlist inbl;
    op.exec("journal", "get_order", inbl);
    op.exec("journal", "get_splay_width", inbl);
    op.exec("journal", "get_pool_id", inbl);

    librados::AioCompletion *rados_completion =
      librados::Rados::aio_create_completion(this, rados_callback, NULL);
    int r = ioctx.aio_operate(oid, rados_completion, &op, &outbl);
    ceph_assert(r == 0);
    rados_completion->release();
  }

  void finish(int r) override {
    if (r == 0) {
      try {
        auto iter = outbl.cbegin();
        decode(*order, iter);
        decode(*splay_width, iter);
        decode(*pool_id, iter);
      } catch (const buffer::error &err) {
        r = -EBADMSG;
      }
    }
    on_finish->complete(r);
  }
};

struct C_MutableMetadata : public C_AioExec {
  uint64_t *minimum_set;
  uint64_t *active_set;
  C_ClientList *client_list;
  bufferlist outbl;

  C_MutableMetadata(librados::IoCtx &_ioctx, const std::string &_oid,
                    uint64_t *_minimum_set, uint64_t *_active_set,
                    C_ClientList *_client_list)
    : C_AioExec(_ioctx, _oid), minimum_set(_minimum_set),
      active_set(_active_set), client_list(_client_list) {}

  void send() {
    librados::ObjectReadOperation op;
    bufferlist inbl;
    op.exec("journal", "get_minimum_set", inbl);
    op.exec("journal", "get_active_set", inbl);

    librados::AioCompletion *rados_completion =
      librados::Rados::aio_create_completion(this, rados_callback, NULL);
    int r = ioctx.aio_operate(oid, rados_completion, &op, &outbl);
    ceph_assert(r == 0);
    rados_completion->release();
  }

  void finish(int r) override {
    if (r == 0) {
      try {
        auto iter = outbl.cbegin();
        decode(*minimum_set, iter);
        decode(*active_set, iter);
        client_list->send("");
      } catch (const buffer::error &err) {
        r = -EBADMSG;
      }
    }
    if (r < 0) {
      client_list->complete(r);
    }
  }
};


} // anonymous namespace

void create(librados::ObjectWriteOperation *op,
            uint8_t order, uint8_t splay, int64_t pool_id) {
  bufferlist bl;
  encode(order, bl);
  encode(splay, bl);
  encode(pool_id, bl);

  op->exec("journal", "create", bl);
}

int create(librados::IoCtx &ioctx, const std::string &oid, uint8_t order,
           uint8_t splay, int64_t pool_id) {
  librados::ObjectWriteOperation op;
  create(&op, order, splay, pool_id);

  int r = ioctx.operate(oid, &op);
  if (r < 0) {
    return r;
  }
  return 0;
}

void get_immutable_metadata(librados::IoCtx &ioctx, const std::string &oid,
                            uint8_t *order, uint8_t *splay_width,
                            int64_t *pool_id, Context *on_finish) {
  C_ImmutableMetadata *metadata = new C_ImmutableMetadata(ioctx, oid, order,
                                                          splay_width, pool_id,
                                                          on_finish);
  metadata->send();
}

void get_mutable_metadata(librados::IoCtx &ioctx, const std::string &oid,
                          uint64_t *minimum_set, uint64_t *active_set,
                          std::set<cls::journal::Client> *clients,
                          Context *on_finish) {
  C_ClientList *client_list = new C_ClientList(ioctx, oid, clients, on_finish);
  C_MutableMetadata *metadata = new C_MutableMetadata(
    ioctx, oid, minimum_set, active_set, client_list);
  metadata->send();
}

void set_minimum_set(librados::ObjectWriteOperation *op, uint64_t object_set) {
  bufferlist bl;
  encode(object_set, bl);
  op->exec("journal", "set_minimum_set", bl);
}

void set_active_set(librados::ObjectWriteOperation *op, uint64_t object_set) {
  bufferlist bl;
  encode(object_set, bl);
  op->exec("journal", "set_active_set", bl);
}

int get_client(librados::IoCtx &ioctx, const std::string &oid,
               const std::string &id, cls::journal::Client *client) {
  librados::ObjectReadOperation op;
  get_client_start(&op, id);

  bufferlist out_bl;
  int r = ioctx.operate(oid, &op, &out_bl);
  if (r < 0) {
    return r;
  }

  auto iter = out_bl.cbegin();
  r = get_client_finish(&iter, client);
  if (r < 0) {
    return r;
  }
  return 0;
}

void get_client_start(librados::ObjectReadOperation *op,
                      const std::string &id) {
  bufferlist bl;
  encode(id, bl);
  op->exec("journal", "get_client", bl);
}

int get_client_finish(bufferlist::const_iterator *iter,
                      cls::journal::Client *client) {
  try {
    decode(*client, *iter);
  } catch (const buffer::error &err) {
    return -EBADMSG;
  }
  return 0;
}

int client_register(librados::IoCtx &ioctx, const std::string &oid,
                    const std::string &id, const bufferlist &data) {
  librados::ObjectWriteOperation op;
  client_register(&op, id, data);
  return ioctx.operate(oid, &op);
}

void client_register(librados::ObjectWriteOperation *op,
                     const std::string &id, const bufferlist &data) {
  bufferlist bl;
  encode(id, bl);
  encode(data, bl);
  op->exec("journal", "client_register", bl);
}

int client_update_data(librados::IoCtx &ioctx, const std::string &oid,
                       const std::string &id, const bufferlist &data) {
  librados::ObjectWriteOperation op;
  client_update_data(&op, id, data);
  return ioctx.operate(oid, &op);
}

void client_update_data(librados::ObjectWriteOperation *op,
                        const std::string &id, const bufferlist &data) {
  bufferlist bl;
  encode(id, bl);
  encode(data, bl);
  op->exec("journal", "client_update_data", bl);
}

int client_update_state(librados::IoCtx &ioctx, const std::string &oid,
                        const std::string &id, cls::journal::ClientState state) {
  librados::ObjectWriteOperation op;
  client_update_state(&op, id, state);
  return ioctx.operate(oid, &op);
}

void client_update_state(librados::ObjectWriteOperation *op,
                         const std::string &id,
                         cls::journal::ClientState state) {
  bufferlist bl;
  encode(id, bl);
  encode(static_cast<uint8_t>(state), bl);
  op->exec("journal", "client_update_state", bl);
}

int client_unregister(librados::IoCtx &ioctx, const std::string &oid,
                       const std::string &id) {
  librados::ObjectWriteOperation op;
  client_unregister(&op, id);
  return ioctx.operate(oid, &op);
}

void client_unregister(librados::ObjectWriteOperation *op,
		       const std::string &id) {

  bufferlist bl;
  encode(id, bl);
  op->exec("journal", "client_unregister", bl);
}

void client_commit(librados::ObjectWriteOperation *op, const std::string &id,
                   const cls::journal::ObjectSetPosition &commit_position) {
  bufferlist bl;
  encode(id, bl);
  encode(commit_position, bl);
  op->exec("journal", "client_commit", bl);
}

int client_list(librados::IoCtx &ioctx, const std::string &oid,
                std::set<cls::journal::Client> *clients) {
  C_SaferCond cond;
  client_list(ioctx, oid, clients, &cond);
  return cond.wait();
}

void client_list(librados::IoCtx &ioctx, const std::string &oid,
                 std::set<cls::journal::Client> *clients, Context *on_finish) {
  C_ClientList *client_list = new C_ClientList(ioctx, oid, clients, on_finish);
  client_list->send("");
}

int get_next_tag_tid(librados::IoCtx &ioctx, const std::string &oid,
                     uint64_t *tag_tid) {
  librados::ObjectReadOperation op;
  get_next_tag_tid_start(&op);

  bufferlist out_bl;
  int r = ioctx.operate(oid, &op, &out_bl);
  if (r < 0) {
    return r;
  }

  auto iter = out_bl.cbegin();
  r = get_next_tag_tid_finish(&iter, tag_tid);
  if (r < 0) {
    return r;
  }
  return 0;
}

void get_next_tag_tid_start(librados::ObjectReadOperation *op) {
  bufferlist bl;
  op->exec("journal", "get_next_tag_tid", bl);
}

int get_next_tag_tid_finish(bufferlist::const_iterator *iter,
                            uint64_t *tag_tid) {
  try {
    decode(*tag_tid, *iter);
  } catch (const buffer::error &err) {
    return -EBADMSG;
  }
  return 0;
}

int get_tag(librados::IoCtx &ioctx, const std::string &oid,
            uint64_t tag_tid, cls::journal::Tag *tag) {
  librados::ObjectReadOperation op;
  get_tag_start(&op, tag_tid);

  bufferlist out_bl;
  int r = ioctx.operate(oid, &op, &out_bl);
  if (r < 0) {
    return r;
  }

  auto iter = out_bl.cbegin();
  r = get_tag_finish(&iter, tag);
  if (r < 0) {
    return r;
  }
  return 0;
}

void get_tag_start(librados::ObjectReadOperation *op,
                   uint64_t tag_tid) {
  bufferlist bl;
  encode(tag_tid, bl);
  op->exec("journal", "get_tag", bl);
}

int get_tag_finish(bufferlist::const_iterator *iter, cls::journal::Tag *tag) {
  try {
    decode(*tag, *iter);
  } catch (const buffer::error &err) {
    return -EBADMSG;
  }
  return 0;
}

int tag_create(librados::IoCtx &ioctx, const std::string &oid,
               uint64_t tag_tid, uint64_t tag_class,
               const bufferlist &data) {
  librados::ObjectWriteOperation op;
  tag_create(&op, tag_tid, tag_class, data);
  return ioctx.operate(oid, &op);
}

void tag_create(librados::ObjectWriteOperation *op, uint64_t tag_tid,
                uint64_t tag_class, const bufferlist &data) {
  bufferlist bl;
  encode(tag_tid, bl);
  encode(tag_class, bl);
  encode(data, bl);
  op->exec("journal", "tag_create", bl);
}

int tag_list(librados::IoCtx &ioctx, const std::string &oid,
             const std::string &client_id, boost::optional<uint64_t> tag_class,
             std::set<cls::journal::Tag> *tags) {
  tags->clear();
  uint64_t start_after_tag_tid = 0;
  while (true) {
    librados::ObjectReadOperation op;
    tag_list_start(&op, start_after_tag_tid, JOURNAL_MAX_RETURN, client_id,
                   tag_class);

    bufferlist out_bl;
    int r = ioctx.operate(oid, &op, &out_bl);
    if (r < 0) {
      return r;
    }

    auto iter = out_bl.cbegin();
    std::set<cls::journal::Tag> decode_tags;
    r = tag_list_finish(&iter, &decode_tags);
    if (r < 0) {
      return r;
    }

    tags->insert(decode_tags.begin(), decode_tags.end());
    if (decode_tags.size() < JOURNAL_MAX_RETURN) {
      break;
    }
  }
  return 0;
}

void tag_list_start(librados::ObjectReadOperation *op,
                    uint64_t start_after_tag_tid, uint64_t max_return,
                    const std::string &client_id,
                    boost::optional<uint64_t> tag_class) {
  bufferlist bl;
  encode(start_after_tag_tid, bl);
  encode(max_return, bl);
  encode(client_id, bl);
  encode(tag_class, bl);
  op->exec("journal", "tag_list", bl);
}

int tag_list_finish(bufferlist::const_iterator *iter,
                    std::set<cls::journal::Tag> *tags) {
  try {
    decode(*tags, *iter);
  } catch (const buffer::error &err) {
    return -EBADMSG;
  }
  return 0;
}

void guard_append(librados::ObjectWriteOperation *op, uint64_t soft_max_size) {
  bufferlist bl;
  encode(soft_max_size, bl);
  op->exec("journal", "guard_append", bl);
}

} // namespace client
} // namespace journal
} // namespace cls