summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_coroutine.h
blob: eb3216640c6410255946650ffc7d799e015da7ee (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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#pragma once

#ifdef _ASSERT_H
#define NEED_ASSERT_H
#pragma push_macro("_ASSERT_H")
#endif

#include <boost/asio.hpp>
#include <boost/intrusive_ptr.hpp>

#ifdef NEED_ASSERT_H
#pragma pop_macro("_ASSERT_H")
#endif

#include "include/utime.h"
#include "common/RefCountedObj.h"
#include "common/debug.h"
#include "common/Timer.h"
#include "common/admin_socket.h"

#include "rgw_common.h"
#include "rgw_http_client_types.h"

#include <boost/asio/coroutine.hpp>

#include <atomic>

#define RGW_ASYNC_OPS_MGR_WINDOW 100

class RGWCoroutinesStack;
class RGWCoroutinesManager;
class RGWAioCompletionNotifier;

class RGWCompletionManager : public RefCountedObject {
  friend class RGWCoroutinesManager;

  CephContext *cct;

  struct io_completion {
    rgw_io_id io_id;
    void *user_info;
  };
  std::list<io_completion> complete_reqs;
  std::set<rgw_io_id> complete_reqs_set;
  using NotifierRef = boost::intrusive_ptr<RGWAioCompletionNotifier>;
  std::set<NotifierRef> cns;

  ceph::mutex lock = ceph::make_mutex("RGWCompletionManager::lock");
  ceph::condition_variable cond;

  SafeTimer timer;

  std::atomic<bool> going_down = { false };

  std::map<void *, void *> waiters;

  class WaitContext;

protected:
  void _wakeup(void *opaque);
  void _complete(RGWAioCompletionNotifier *cn, const rgw_io_id& io_id, void *user_info);
public:
  explicit RGWCompletionManager(CephContext *_cct);
  virtual ~RGWCompletionManager() override;

  void complete(RGWAioCompletionNotifier *cn, const rgw_io_id& io_id, void *user_info);
  int get_next(io_completion *io);
  bool try_get_next(io_completion *io);

  void go_down();

  /*
   * wait for interval length to complete user_info
   */
  void wait_interval(void *opaque, const utime_t& interval, void *user_info);
  void wakeup(void *opaque);

  void register_completion_notifier(RGWAioCompletionNotifier *cn);
  void unregister_completion_notifier(RGWAioCompletionNotifier *cn);
};

/* a single use librados aio completion notifier that hooks into the RGWCompletionManager */
class RGWAioCompletionNotifier : public RefCountedObject {
  librados::AioCompletion *c;
  RGWCompletionManager *completion_mgr;
  rgw_io_id io_id;
  void *user_data;
  ceph::mutex lock = ceph::make_mutex("RGWAioCompletionNotifier");
  bool registered;

public:
  RGWAioCompletionNotifier(RGWCompletionManager *_mgr, const rgw_io_id& _io_id, void *_user_data);
  virtual ~RGWAioCompletionNotifier() override {
    c->release();
    lock.lock();
    bool need_unregister = registered;
    if (registered) {
      completion_mgr->get();
    }
    registered = false;
    lock.unlock();
    if (need_unregister) {
      completion_mgr->unregister_completion_notifier(this);
      completion_mgr->put();
    }
  }

  librados::AioCompletion *completion() {
    return c;
  }

  void unregister() {
    std::lock_guard l{lock};
    if (!registered) {
      return;
    }
    registered = false;
  }

  void cb() {
    lock.lock();
    if (!registered) {
      lock.unlock();
      put();
      return;
    }
    completion_mgr->get();
    registered = false;
    lock.unlock();
    completion_mgr->complete(this, io_id, user_data);
    completion_mgr->put();
    put();
  }
};

// completion notifier with opaque payload (ie a reference-counted pointer)
template <typename T>
class RGWAioCompletionNotifierWith : public RGWAioCompletionNotifier {
  T value;
public:
  RGWAioCompletionNotifierWith(RGWCompletionManager *mgr,
                               const rgw_io_id& io_id, void *user_data,
                               T value)
    : RGWAioCompletionNotifier(mgr, io_id, user_data), value(std::move(value))
  {}
};

struct RGWCoroutinesEnv {
  uint64_t run_context;
  RGWCoroutinesManager *manager;
  std::list<RGWCoroutinesStack *> *scheduled_stacks;
  RGWCoroutinesStack *stack;

  RGWCoroutinesEnv() : run_context(0), manager(NULL), scheduled_stacks(NULL), stack(NULL) {}
};

enum RGWCoroutineState {
  RGWCoroutine_Error = -2,
  RGWCoroutine_Done  = -1,
  RGWCoroutine_Run   =  0,
};

struct rgw_spawned_stacks {
  std::vector<RGWCoroutinesStack *> entries;

  rgw_spawned_stacks() {}

  void add_pending(RGWCoroutinesStack *s) {
    entries.push_back(s);
  }

  void inherit(rgw_spawned_stacks *source) {
    for (auto* entry : source->entries) {
      add_pending(entry);
    }
    source->entries.clear();
  }
};



class RGWCoroutine : public RefCountedObject, public boost::asio::coroutine {
  friend class RGWCoroutinesStack;

  struct StatusItem {
    utime_t timestamp;
    std::string status;

    StatusItem(utime_t& t, const std::string& s) : timestamp(t), status(s) {}

    void dump(Formatter *f) const;
  };

#define MAX_COROUTINE_HISTORY 10

  struct Status {
    CephContext *cct;
    ceph::shared_mutex lock =
      ceph::make_shared_mutex("RGWCoroutine::Status::lock");
    int max_history;

    utime_t timestamp;
    std::stringstream status;

    explicit Status(CephContext *_cct) : cct(_cct), max_history(MAX_COROUTINE_HISTORY) {}

    std::deque<StatusItem> history;

    std::stringstream& set_status();
  } status;

  std::stringstream description;

protected:
  bool _yield_ret;

  struct {
    boost::asio::coroutine cr;
    bool should_exit{false};
    int ret{0};

    void init() {
      cr = boost::asio::coroutine();
      should_exit = false;
      ret = 0;
    }
  } drain_status;

  CephContext *cct;

  RGWCoroutinesStack *stack;
  int retcode;
  int state;

  rgw_spawned_stacks spawned;

  std::stringstream error_stream;

  int set_state(int s, int ret = 0) {
    retcode = ret;
    state = s;
    return ret;
  }
  int set_cr_error(int ret) {
    return set_state(RGWCoroutine_Error, ret);
  }
  int set_cr_done() {
    return set_state(RGWCoroutine_Done, 0);
  }
  void set_io_blocked(bool flag);

  void reset_description() {
    description.str(std::string());
  }

  std::stringstream& set_description() {
    return description;
  }
  std::stringstream& set_status() {
    return status.set_status();
  }

  std::stringstream& set_status(const std::string& s) {
    std::stringstream& status = set_status();
    status << s;
    return status;
  }

  virtual int operate_wrapper(const DoutPrefixProvider *dpp) {
    return operate(dpp);
  }
public:
  RGWCoroutine(CephContext *_cct) : status(_cct), _yield_ret(false), cct(_cct), stack(NULL), retcode(0), state(RGWCoroutine_Run) {}
  virtual ~RGWCoroutine() override;

  virtual int operate(const DoutPrefixProvider *dpp) = 0;

  bool is_done() { return (state == RGWCoroutine_Done || state == RGWCoroutine_Error); }
  bool is_error() { return (state == RGWCoroutine_Error); }

  std::stringstream& log_error() { return error_stream; }
  std::string error_str() {
    return error_stream.str();
  }

  void set_retcode(int r) {
    retcode = r;
  }

  int get_ret_status() {
    return retcode;
  }

  void call(RGWCoroutine *op); /* call at the same stack we're in */
  RGWCoroutinesStack *spawn(RGWCoroutine *op, bool wait); /* execute on a different stack */
  bool collect(int *ret, RGWCoroutinesStack *skip_stack, uint64_t *stack_id = nullptr); /* returns true if needs to be called again */
  bool collect_next(int *ret, RGWCoroutinesStack **collected_stack = NULL); /* returns true if found a stack to collect */

  int wait(const utime_t& interval);
  bool drain_children(int num_cr_left,
                      RGWCoroutinesStack *skip_stack = nullptr,
                      std::optional<std::function<void(uint64_t stack_id, int ret)> > cb = std::nullopt); /* returns true if needed to be called again,
                                                                                                             cb will be called on completion of every
                                                                                                             completion. */
  bool drain_children(int num_cr_left,
                      std::optional<std::function<int(uint64_t stack_id, int ret)> > cb); /* returns true if needed to be called again,
                                                                                             cb will be called on every completion, can filter errors.
                                                                                             A negative return value from cb means that current cr
                                                                                             will need to exit */
  void wakeup();
  void set_sleeping(bool flag); /* put in sleep, or wakeup from sleep */

  size_t num_spawned() {
    return spawned.entries.size();
  }

  void wait_for_child();

  virtual std::string to_str() const;

  RGWCoroutinesStack *get_stack() const {
    return stack;
  }

  RGWCoroutinesEnv *get_env() const;

  void dump(Formatter *f) const;

  void init_new_io(RGWIOProvider *io_provider); /* only links the default io id */

  int io_block(int ret = 0) {
    return io_block(ret, -1);
  }
  int io_block(int ret, int64_t io_id);
  int io_block(int ret, const rgw_io_id& io_id);
  void io_complete() {
    io_complete(rgw_io_id{});
  }
  void io_complete(const rgw_io_id& io_id);
};

std::ostream& operator<<(std::ostream& out, const RGWCoroutine& cr);

#define yield_until_true(x)     \
do {                            \
  do {                          \
    yield _yield_ret = x;       \
  } while (!_yield_ret);        \
  _yield_ret = false;           \
} while (0)

#define drain_all() \
  drain_status.init(); \
  yield_until_true(drain_children(0))

#define drain_all_but(n) \
  drain_status.init(); \
  yield_until_true(drain_children(n))

#define drain_all_but_stack(stack) \
  drain_status.init(); \
  yield_until_true(drain_children(1, stack))

#define drain_all_but_stack_cb(stack, cb) \
  drain_status.init(); \
  yield_until_true(drain_children(1, stack, cb))

#define drain_with_cb(n, cb) \
  drain_status.init(); \
  yield_until_true(drain_children(n, cb)); \
  if (drain_status.should_exit) { \
    return set_cr_error(drain_status.ret); \
  }

#define drain_all_cb(cb) \
  drain_with_cb(0, cb)

#define yield_spawn_window(cr, n, cb) \
  do { \
    spawn(cr, false); \
    drain_with_cb(n, cb); /* this is guaranteed to yield */ \
  } while (0)



template <class T>
class RGWConsumerCR : public RGWCoroutine {
  std::list<T> product;

public:
  explicit RGWConsumerCR(CephContext *_cct) : RGWCoroutine(_cct) {}

  bool has_product() {
    return !product.empty();
  }

  void wait_for_product() {
    if (!has_product()) {
      set_sleeping(true);
    }
  }

  bool consume(T *p) {
    if (product.empty()) {
      return false;
    }
    *p = product.front();
    product.pop_front();
    return true;
  }

  void receive(const T& p, bool wakeup = true);
  void receive(std::list<T>& l, bool wakeup = true);
};

class RGWCoroutinesStack : public RefCountedObject {
  friend class RGWCoroutine;
  friend class RGWCoroutinesManager;

  CephContext *cct;

  int64_t id{-1};

  RGWCoroutinesManager *ops_mgr;

  std::list<RGWCoroutine *> ops;
  std::list<RGWCoroutine *>::iterator pos;

  rgw_spawned_stacks spawned;

  std::set<RGWCoroutinesStack *> blocked_by_stack;
  std::set<RGWCoroutinesStack *> blocking_stacks;

  std::map<int64_t, rgw_io_id> io_finish_ids;
  rgw_io_id io_blocked_id;

  bool done_flag;
  bool error_flag;
  bool blocked_flag;
  bool sleep_flag;
  bool interval_wait_flag;

  bool is_scheduled;

  bool is_waiting_for_child;

  int retcode;

  uint64_t run_count;

protected:
  RGWCoroutinesEnv *env;
  RGWCoroutinesStack *parent;

  RGWCoroutinesStack *spawn(RGWCoroutine *source_op, RGWCoroutine *next_op, bool wait);
  bool collect(RGWCoroutine *op, int *ret, RGWCoroutinesStack *skip_stack, uint64_t *stack_id); /* returns true if needs to be called again */
  bool collect_next(RGWCoroutine *op, int *ret, RGWCoroutinesStack **collected_stack); /* returns true if found a stack to collect */
public:
  RGWCoroutinesStack(CephContext *_cct, RGWCoroutinesManager *_ops_mgr, RGWCoroutine *start = NULL);
  virtual ~RGWCoroutinesStack() override;

  int64_t get_id() const {
    return id;
  }

  int operate(const DoutPrefixProvider *dpp, RGWCoroutinesEnv *env);

  bool is_done() {
    return done_flag;
  }
  bool is_error() {
    return error_flag;
  }
  bool is_blocked_by_stack() {
    return !blocked_by_stack.empty();
  }
  void set_io_blocked(bool flag) {
    blocked_flag = flag;
  }
  void set_io_blocked_id(const rgw_io_id& io_id) {
    io_blocked_id = io_id;
  }
  bool is_io_blocked() {
    return blocked_flag && !done_flag;
  }
  bool can_io_unblock(const rgw_io_id& io_id) {
    return ((io_blocked_id.id < 0) ||
            io_blocked_id.intersects(io_id));
  }
  bool try_io_unblock(const rgw_io_id& io_id);
  bool consume_io_finish(const rgw_io_id& io_id);
  void set_interval_wait(bool flag) {
    interval_wait_flag = flag;
  }
  bool is_interval_waiting() {
    return interval_wait_flag;
  }
  void set_sleeping(bool flag) {
    bool wakeup = sleep_flag & !flag;
    sleep_flag = flag;
    if (wakeup) {
      schedule();
    }
  }
  bool is_sleeping() {
    return sleep_flag;
  }
  void set_is_scheduled(bool flag) {
    is_scheduled = flag;
  }

  bool is_blocked() {
    return is_blocked_by_stack() || is_sleeping() ||
          is_io_blocked() || waiting_for_child() ;
  }

  void schedule();
  void _schedule();

  int get_ret_status() {
    return retcode;
  }

  std::string error_str();

  void call(RGWCoroutine *next_op);
  RGWCoroutinesStack *spawn(RGWCoroutine *next_op, bool wait);
  int unwind(int retcode);

  int wait(const utime_t& interval);
  void wakeup();
  void io_complete() {
    io_complete(rgw_io_id{});
  }
  void io_complete(const rgw_io_id& io_id);

  bool collect(int *ret, RGWCoroutinesStack *skip_stack, uint64_t *stack_id); /* returns true if needs to be called again */

  void cancel();

  RGWAioCompletionNotifier *create_completion_notifier();
  template <typename T>
  RGWAioCompletionNotifier *create_completion_notifier(T value);
  RGWCompletionManager *get_completion_mgr();

  void set_blocked_by(RGWCoroutinesStack *s) {
    blocked_by_stack.insert(s);
    s->blocking_stacks.insert(this);
  }

  void set_wait_for_child(bool flag) {
    is_waiting_for_child = flag;
  }

  bool waiting_for_child() {
    return is_waiting_for_child;
  }

  bool unblock_stack(RGWCoroutinesStack **s);

  RGWCoroutinesEnv *get_env() const { return env; }

  void dump(Formatter *f) const;

  void init_new_io(RGWIOProvider *io_provider);
};

template <class T>
void RGWConsumerCR<T>::receive(std::list<T>& l, bool wakeup)
{
  product.splice(product.end(), l);
  if (wakeup) {
    set_sleeping(false);
  }
}


template <class T>
void RGWConsumerCR<T>::receive(const T& p, bool wakeup)
{
  product.push_back(p);
  if (wakeup) {
    set_sleeping(false);
  }
}

class RGWCoroutinesManagerRegistry : public RefCountedObject, public AdminSocketHook {
  CephContext *cct;

  std::set<RGWCoroutinesManager *> managers;
  ceph::shared_mutex lock =
    ceph::make_shared_mutex("RGWCoroutinesRegistry::lock");

  std::string admin_command;

public:
  explicit RGWCoroutinesManagerRegistry(CephContext *_cct) : cct(_cct) {}
  virtual ~RGWCoroutinesManagerRegistry() override;

  void add(RGWCoroutinesManager *mgr);
  void remove(RGWCoroutinesManager *mgr);

  int hook_to_admin_command(const std::string& command);
  int call(std::string_view command, const cmdmap_t& cmdmap,
	   const bufferlist&,
	   Formatter *f,
	   std::ostream& ss,
	   bufferlist& out) override;

  void dump(Formatter *f) const;
};

class RGWCoroutinesManager {
  CephContext *cct;
  std::atomic<bool> going_down = { false };

  std::atomic<int64_t> run_context_count = { 0 };
  std::map<uint64_t, std::set<RGWCoroutinesStack *> > run_contexts;

  std::atomic<int64_t> max_io_id = { 0 };
  std::atomic<uint64_t> max_stack_id = { 0 };

  mutable ceph::shared_mutex lock =
    ceph::make_shared_mutex("RGWCoroutinesManager::lock");

  RGWIOIDProvider io_id_provider;

  void handle_unblocked_stack(std::set<RGWCoroutinesStack *>& context_stacks, std::list<RGWCoroutinesStack *>& scheduled_stacks,
                              RGWCompletionManager::io_completion& io, int *waiting_count, int *interval_wait_count);
protected:
  RGWCompletionManager *completion_mgr;
  RGWCoroutinesManagerRegistry *cr_registry;

  int ops_window;

  std::string id;

  void put_completion_notifier(RGWAioCompletionNotifier *cn);
public:
  RGWCoroutinesManager(CephContext *_cct, RGWCoroutinesManagerRegistry *_cr_registry) : cct(_cct),
                                                                                        cr_registry(_cr_registry), ops_window(RGW_ASYNC_OPS_MGR_WINDOW) {
    completion_mgr = new RGWCompletionManager(cct);
    if (cr_registry) {
      cr_registry->add(this);
    }
  }
  virtual ~RGWCoroutinesManager();

  int run(const DoutPrefixProvider *dpp, std::list<RGWCoroutinesStack *>& ops);
  int run(const DoutPrefixProvider *dpp, RGWCoroutine *op);
  void stop() {
    bool expected = false;
    if (going_down.compare_exchange_strong(expected, true)) {
      completion_mgr->go_down();
    }
  }

  virtual void report_error(RGWCoroutinesStack *op);

  RGWAioCompletionNotifier *create_completion_notifier(RGWCoroutinesStack *stack);
  template <typename T>
  RGWAioCompletionNotifier *create_completion_notifier(RGWCoroutinesStack *stack, T value);
  RGWCompletionManager *get_completion_mgr() { return completion_mgr; }

  void schedule(RGWCoroutinesEnv *env, RGWCoroutinesStack *stack);
  void _schedule(RGWCoroutinesEnv *env, RGWCoroutinesStack *stack);
  RGWCoroutinesStack *allocate_stack();

  int64_t get_next_io_id();
  uint64_t get_next_stack_id();

  void set_sleeping(RGWCoroutine *cr, bool flag);
  void io_complete(RGWCoroutine *cr, const rgw_io_id& io_id);

  virtual std::string get_id();
  void dump(Formatter *f) const;

  RGWIOIDProvider& get_io_id_provider() {
    return io_id_provider;
  }
};

template <typename T>
RGWAioCompletionNotifier *RGWCoroutinesManager::create_completion_notifier(RGWCoroutinesStack *stack, T value)
{
  rgw_io_id io_id{get_next_io_id(), -1};
  RGWAioCompletionNotifier *cn = new RGWAioCompletionNotifierWith<T>(completion_mgr, io_id, (void *)stack, std::move(value));
  completion_mgr->register_completion_notifier(cn);
  return cn;
}

template <typename T>
RGWAioCompletionNotifier *RGWCoroutinesStack::create_completion_notifier(T value)
{
  return ops_mgr->create_completion_notifier(this, std::move(value));
}

class RGWSimpleCoroutine : public RGWCoroutine {
  bool called_cleanup;

  int operate(const DoutPrefixProvider *dpp) override;

  int state_init();
  int state_send_request(const DoutPrefixProvider *dpp);
  int state_request_complete();
  int state_all_complete();

  void call_cleanup();

public:
  RGWSimpleCoroutine(CephContext *_cct) : RGWCoroutine(_cct), called_cleanup(false) {}
  virtual ~RGWSimpleCoroutine() override;

  virtual int init() { return 0; }
  virtual int send_request(const DoutPrefixProvider *dpp) = 0;
  virtual int request_complete() = 0;
  virtual int finish() { return 0; }
  virtual void request_cleanup() {}
};