summaryrefslogtreecommitdiffstats
path: root/src/neorados/cls/fifo.cc
blob: fa99275b25f96cbf3350830f35a72504b89070d1 (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
// -*- 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) 2020 Red Hat <contact@redhat.com>
 * Author: Adam C. Emerson
 *
 * 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 <cstdint>
#include <numeric>
#include <optional>
#include <string_view>

#undef FMT_HEADER_ONLY
#define FMT_HEADER_ONLY 1
#include <fmt/format.h>

#include <boost/system/error_code.hpp>

#include "include/neorados/RADOS.hpp"

#include "include/buffer.h"

#include "common/random_string.h"

#include "cls/fifo/cls_fifo_types.h"
#include "cls/fifo/cls_fifo_ops.h"

#include "fifo.h"

namespace neorados::cls::fifo {
namespace bs = boost::system;
namespace cb = ceph::buffer;
namespace fifo = rados::cls::fifo;

void create_meta(WriteOp& op, std::string_view id,
		 std::optional<fifo::objv> objv,
		 std::optional<std::string_view> oid_prefix,
		 bool exclusive,
		 std::uint64_t max_part_size,
		 std::uint64_t max_entry_size)
{
  fifo::op::create_meta cm;

  cm.id = id;
  cm.version = objv;
  cm.oid_prefix = oid_prefix;
  cm.max_part_size = max_part_size;
  cm.max_entry_size = max_entry_size;
  cm.exclusive = exclusive;

  cb::list in;
  encode(cm, in);
  op.exec(fifo::op::CLASS, fifo::op::CREATE_META, in);
}

void get_meta(ReadOp& op, std::optional<fifo::objv> objv,
	      bs::error_code* ec_out, fifo::info* info,
	      std::uint32_t* part_header_size,
	      std::uint32_t* part_entry_overhead)
{
  fifo::op::get_meta gm;
  gm.version = objv;
  cb::list in;
  encode(gm, in);
  op.exec(fifo::op::CLASS, fifo::op::GET_META, in,
	  [ec_out, info, part_header_size,
	   part_entry_overhead](bs::error_code ec, const cb::list& bl) {
	    fifo::op::get_meta_reply reply;
	    if (!ec) try {
		auto iter = bl.cbegin();
		decode(reply, iter);
	      } catch (const cb::error& err) {
		ec = err.code();
	      }
	    if (ec_out) *ec_out = ec;
	    if (info) *info = std::move(reply.info);
	    if (part_header_size) *part_header_size = reply.part_header_size;
	    if (part_entry_overhead)
		*part_entry_overhead = reply.part_entry_overhead;
	  });
};

void update_meta(WriteOp& op, const fifo::objv& objv,
		 const fifo::update& update)
{
  fifo::op::update_meta um;

  um.version = objv;
  um.tail_part_num = update.tail_part_num();
  um.head_part_num = update.head_part_num();
  um.min_push_part_num = update.min_push_part_num();
  um.max_push_part_num = update.max_push_part_num();
  um.journal_entries_add = std::move(update).journal_entries_add();
  um.journal_entries_rm = std::move(update).journal_entries_rm();

  cb::list in;
  encode(um, in);
  op.exec(fifo::op::CLASS, fifo::op::UPDATE_META, in);
}

void part_init(WriteOp& op, std::string_view tag,
	       fifo::data_params params)
{
  fifo::op::init_part ip;

  ip.tag = tag;
  ip.params = params;

  cb::list in;
  encode(ip, in);
  op.exec(fifo::op::CLASS, fifo::op::INIT_PART, in);
}

void push_part(WriteOp& op, std::string_view tag,
	       std::deque<cb::list> data_bufs,
	       fu2::unique_function<void(bs::error_code, int)> f)
{
  fifo::op::push_part pp;

  pp.tag = tag;
  pp.data_bufs = data_bufs;
  pp.total_len = 0;

  for (const auto& bl : data_bufs)
    pp.total_len += bl.length();

  cb::list in;
  encode(pp, in);
  op.exec(fifo::op::CLASS, fifo::op::PUSH_PART, in,
	  [f = std::move(f)](bs::error_code ec, int r, const cb::list&) mutable {
	    std::move(f)(ec, r);
	  });
  op.returnvec();
}

void trim_part(WriteOp& op,
	       std::optional<std::string_view> tag,
	       std::uint64_t ofs, bool exclusive)
{
  fifo::op::trim_part tp;

  tp.tag = tag;
  tp.ofs = ofs;
  tp.exclusive = exclusive;

  bufferlist in;
  encode(tp, in);
  op.exec(fifo::op::CLASS, fifo::op::TRIM_PART, in);
}

void list_part(ReadOp& op,
	       std::optional<string_view> tag,
	       std::uint64_t ofs,
	       std::uint64_t max_entries,
	       bs::error_code* ec_out,
	       std::vector<fifo::part_list_entry>* entries,
	       bool* more,
	       bool* full_part,
	       std::string* ptag)
{
  fifo::op::list_part lp;

  lp.tag = tag;
  lp.ofs = ofs;
  lp.max_entries = max_entries;

  bufferlist in;
  encode(lp, in);
  op.exec(fifo::op::CLASS, fifo::op::LIST_PART, in,
	  [entries, more, full_part, ptag, ec_out](bs::error_code ec,
						   const cb::list& bl) {
	    if (ec) {
	      if (ec_out) *ec_out = ec;
	      return;
	    }

	    fifo::op::list_part_reply reply;
	    auto iter = bl.cbegin();
	    try {
	      decode(reply, iter);
	    } catch (const cb::error& err) {
	      if (ec_out) *ec_out = ec;
	      return;
	    }

	    if (entries) *entries = std::move(reply.entries);
	    if (more) *more = reply.more;
	    if (full_part) *full_part = reply.full_part;
	    if (ptag) *ptag = reply.tag;
	  });
}

void get_part_info(ReadOp& op,
		   bs::error_code* out_ec,
		   fifo::part_header* header)
{
  fifo::op::get_part_info gpi;

  bufferlist in;
  encode(gpi, in);
  op.exec(fifo::op::CLASS, fifo::op::GET_PART_INFO, in,
	  [out_ec, header](bs::error_code ec, const cb::list& bl) {
	    if (ec) {
	      if (out_ec) *out_ec = ec;
	    }
	    fifo::op::get_part_info_reply reply;
	    auto iter = bl.cbegin();
	    try {
	      decode(reply, iter);
	    } catch (const cb::error& err) {
	      if (out_ec) *out_ec = ec;
	      return;
	    }

	    if (header) *header = std::move(reply.header);
	  });
}

std::optional<marker> FIFO::to_marker(std::string_view s) {
  marker m;
  if (s.empty()) {
    m.num = info.tail_part_num;
    m.ofs = 0;
    return m;
  }

  auto pos = s.find(':');
  if (pos == string::npos) {
    return std::nullopt;
  }

  auto num = s.substr(0, pos);
  auto ofs = s.substr(pos + 1);

  auto n = ceph::parse<decltype(m.num)>(num);
  if (!n) {
    return std::nullopt;
  }
  m.num = *n;
  auto o = ceph::parse<decltype(m.ofs)>(ofs);
  if (!o) {
    return std::nullopt;
  }
  m.ofs = *o;
  return m;
}

bs::error_code FIFO::apply_update(fifo::info* info,
				  const fifo::objv& objv,
				  const fifo::update& update) {
  std::unique_lock l(m);
  auto err = info->apply_update(update);
  if (objv != info->version) {
    ldout(r->cct(), 0) << __func__ << "(): Raced locally!" << dendl;
    return errc::raced;
  }
  if (err) {
    ldout(r->cct(), 0) << __func__ << "(): ERROR: " << err << dendl;
    return errc::update_failed;
  }

  ++info->version.ver;

  return {};
}

std::string FIFO::generate_tag() const
{
  static constexpr auto HEADER_TAG_SIZE = 16;
  return gen_rand_alphanumeric_plain(r->cct(), HEADER_TAG_SIZE);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
class error_category : public ceph::converting_category {
public:
  error_category(){}
  const char* name() const noexcept override;
  const char* message(int ev, char*, std::size_t) const noexcept override;
  std::string message(int ev) const override;
  bs::error_condition default_error_condition(int ev) const noexcept
    override;
  bool equivalent(int ev, const bs::error_condition& c) const
    noexcept override;
  using ceph::converting_category::equivalent;
  int from_code(int ev) const noexcept override;
};
#pragma GCC diagnostic pop
#pragma clang diagnostic pop

const char* error_category::name() const noexcept {
  return "FIFO";
}

const char* error_category::message(int ev, char*, std::size_t) const noexcept {
  if (ev == 0)
    return "No error";

  switch (static_cast<errc>(ev)) {
  case errc::raced:
    return "Retry-race count exceeded";

  case errc::inconsistency:
    return "Inconsistent result! New head before old head";

  case errc::entry_too_large:
    return "Pushed entry too large";

  case errc::invalid_marker:
    return "Invalid marker string";

  case errc::update_failed:
    return "Update failed";
  }

  return "Unknown error";
}

std::string error_category::message(int ev) const {
  return message(ev, nullptr, 0);
}

bs::error_condition
error_category::default_error_condition(int ev) const noexcept {
  switch (static_cast<errc>(ev)) {
  case errc::raced:
    return bs::errc::operation_canceled;

  case errc::inconsistency:
    return bs::errc::io_error;

  case errc::entry_too_large:
    return bs::errc::value_too_large;

  case errc::invalid_marker:
    return bs::errc::invalid_argument;

  case errc::update_failed:
    return bs::errc::invalid_argument;
  }

  return { ev, *this };
}

bool error_category::equivalent(int ev, const bs::error_condition& c) const noexcept {
  return default_error_condition(ev) == c;
}

int error_category::from_code(int ev) const noexcept {
  switch (static_cast<errc>(ev)) {
  case errc::raced:
    return -ECANCELED;

  case errc::inconsistency:
    return -EIO;

  case errc::entry_too_large:
    return -E2BIG;

  case errc::invalid_marker:
    return -EINVAL;

  case errc::update_failed:
    return -EINVAL;

  }
  return -EDOM;
}

const bs::error_category& error_category() noexcept {
  static const class error_category c;
  return c;
}

}