1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #include "include/Context.h"
5 #include "common/ceph_json.h"
6 #include "rgw_coroutine.h"
8 // re-include our assert to clobber the system one; fix dout:
9 #include "include/ceph_assert.h"
11 #include <boost/asio/yield.hpp>
13 #define dout_subsys ceph_subsys_rgw
14 #define dout_context g_ceph_context
18 class RGWCompletionManager::WaitContext
: public Context
{
19 RGWCompletionManager
*manager
;
22 WaitContext(RGWCompletionManager
*_cm
, void *_opaque
) : manager(_cm
), opaque(_opaque
) {}
23 void finish(int r
) override
{
24 manager
->_wakeup(opaque
);
28 RGWCompletionManager::RGWCompletionManager(CephContext
*_cct
) : cct(_cct
),
34 RGWCompletionManager::~RGWCompletionManager()
36 std::lock_guard l
{lock
};
37 timer
.cancel_all_events();
41 void RGWCompletionManager::complete(RGWAioCompletionNotifier
*cn
, const rgw_io_id
& io_id
, void *user_info
)
43 std::lock_guard l
{lock
};
44 _complete(cn
, io_id
, user_info
);
47 void RGWCompletionManager::register_completion_notifier(RGWAioCompletionNotifier
*cn
)
49 std::lock_guard l
{lock
};
55 void RGWCompletionManager::unregister_completion_notifier(RGWAioCompletionNotifier
*cn
)
57 std::lock_guard l
{lock
};
63 void RGWCompletionManager::_complete(RGWAioCompletionNotifier
*cn
, const rgw_io_id
& io_id
, void *user_info
)
69 if (complete_reqs_set
.find(io_id
) != complete_reqs_set
.end()) {
70 /* already have completion for this io_id, don't allow multiple completions for it */
73 complete_reqs
.push_back(io_completion
{io_id
, user_info
});
77 int RGWCompletionManager::get_next(io_completion
*io
)
79 std::unique_lock l
{lock
};
80 while (complete_reqs
.empty()) {
86 *io
= complete_reqs
.front();
87 complete_reqs_set
.erase(io
->io_id
);
88 complete_reqs
.pop_front();
92 bool RGWCompletionManager::try_get_next(io_completion
*io
)
94 std::lock_guard l
{lock
};
95 if (complete_reqs
.empty()) {
98 *io
= complete_reqs
.front();
99 complete_reqs_set
.erase(io
->io_id
);
100 complete_reqs
.pop_front();
104 void RGWCompletionManager::go_down()
106 std::lock_guard l
{lock
};
107 for (auto cn
: cns
) {
114 void RGWCompletionManager::wait_interval(void *opaque
, const utime_t
& interval
, void *user_info
)
116 std::lock_guard l
{lock
};
117 ceph_assert(waiters
.find(opaque
) == waiters
.end());
118 waiters
[opaque
] = user_info
;
119 timer
.add_event_after(interval
, new WaitContext(this, opaque
));
122 void RGWCompletionManager::wakeup(void *opaque
)
124 std::lock_guard l
{lock
};
128 void RGWCompletionManager::_wakeup(void *opaque
)
130 map
<void *, void *>::iterator iter
= waiters
.find(opaque
);
131 if (iter
!= waiters
.end()) {
132 void *user_id
= iter
->second
;
134 _complete(NULL
, rgw_io_id
{0, -1} /* no IO id */, user_id
);
138 RGWCoroutine::~RGWCoroutine() {
139 for (auto stack
: spawned
.entries
) {
144 void RGWCoroutine::init_new_io(RGWIOProvider
*io_provider
)
146 stack
->init_new_io(io_provider
);
149 void RGWCoroutine::set_io_blocked(bool flag
) {
150 stack
->set_io_blocked(flag
);
153 void RGWCoroutine::set_sleeping(bool flag
) {
154 stack
->set_sleeping(flag
);
157 int RGWCoroutine::io_block(int ret
, int64_t io_id
) {
158 return io_block(ret
, rgw_io_id
{io_id
, -1});
161 int RGWCoroutine::io_block(int ret
, const rgw_io_id
& io_id
) {
162 if (stack
->consume_io_finish(io_id
)) {
165 set_io_blocked(true);
166 stack
->set_io_blocked_id(io_id
);
170 void RGWCoroutine::io_complete(const rgw_io_id
& io_id
) {
171 stack
->io_complete(io_id
);
174 void RGWCoroutine::StatusItem::dump(Formatter
*f
) const {
175 ::encode_json("timestamp", timestamp
, f
);
176 ::encode_json("status", status
, f
);
179 stringstream
& RGWCoroutine::Status::set_status()
181 std::unique_lock l
{lock
};
182 string s
= status
.str();
183 status
.str(string());
184 if (!timestamp
.is_zero()) {
185 history
.push_back(StatusItem(timestamp
, s
));
187 if (history
.size() > (size_t)max_history
) {
190 timestamp
= ceph_clock_now();
195 RGWCoroutinesManager::~RGWCoroutinesManager() {
197 completion_mgr
->put();
199 cr_registry
->remove(this);
203 int64_t RGWCoroutinesManager::get_next_io_id()
205 return (int64_t)++max_io_id
;
208 uint64_t RGWCoroutinesManager::get_next_stack_id() {
209 return (uint64_t)++max_stack_id
;
212 RGWCoroutinesStack::RGWCoroutinesStack(CephContext
*_cct
, RGWCoroutinesManager
*_ops_mgr
, RGWCoroutine
*start
) : cct(_cct
), ops_mgr(_ops_mgr
),
213 done_flag(false), error_flag(false), blocked_flag(false),
214 sleep_flag(false), interval_wait_flag(false), is_scheduled(false), is_waiting_for_child(false),
215 retcode(0), run_count(0),
216 env(NULL
), parent(NULL
)
218 id
= ops_mgr
->get_next_stack_id();
220 ops
.push_back(start
);
225 RGWCoroutinesStack::~RGWCoroutinesStack()
227 for (auto op
: ops
) {
231 for (auto stack
: spawned
.entries
) {
235 if (preallocated_stack
) {
236 preallocated_stack
->put();
240 int RGWCoroutinesStack::operate(const DoutPrefixProvider
*dpp
, RGWCoroutinesEnv
*_env
)
243 RGWCoroutine
*op
= *pos
;
245 ldpp_dout(dpp
, 20) << *op
<< ": operate()" << dendl
;
246 int r
= op
->operate_wrapper(dpp
);
248 ldpp_dout(dpp
, 20) << *op
<< ": operate() returned r=" << r
<< dendl
;
251 error_flag
= op
->is_error();
255 r
= unwind(op_retcode
);
257 done_flag
= (pos
== ops
.end());
258 blocked_flag
&= !done_flag
;
260 retcode
= op_retcode
;
265 /* should r ever be negative at this point? */
271 string
RGWCoroutinesStack::error_str()
273 if (pos
!= ops
.end()) {
274 return (*pos
)->error_str();
279 void RGWCoroutinesStack::call(RGWCoroutine
*next_op
) {
283 ops
.push_back(next_op
);
284 if (pos
!= ops
.end()) {
291 void RGWCoroutinesStack::schedule()
293 env
->manager
->schedule(env
, this);
296 void RGWCoroutinesStack::_schedule()
298 env
->manager
->_schedule(env
, this);
301 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*source_op
, RGWCoroutine
*op
, bool wait
)
307 rgw_spawned_stacks
*s
= (source_op
? &source_op
->spawned
: &spawned
);
309 RGWCoroutinesStack
*stack
= preallocated_stack
;
311 stack
= env
->manager
->allocate_stack();
313 preallocated_stack
= nullptr;
315 s
->add_pending(stack
);
316 stack
->parent
= this;
318 stack
->get(); /* we'll need to collect the stack */
321 env
->manager
->schedule(env
, stack
);
324 set_blocked_by(stack
);
330 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*op
, bool wait
)
332 return spawn(NULL
, op
, wait
);
335 RGWCoroutinesStack
*RGWCoroutinesStack::prealloc_stack()
337 if (!preallocated_stack
) {
338 preallocated_stack
= env
->manager
->allocate_stack();
340 return preallocated_stack
;
343 int RGWCoroutinesStack::wait(const utime_t
& interval
)
345 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
346 completion_mgr
->wait_interval((void *)this, interval
, (void *)this);
347 set_io_blocked(true);
348 set_interval_wait(true);
352 void RGWCoroutinesStack::wakeup()
354 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
355 completion_mgr
->wakeup((void *)this);
358 void RGWCoroutinesStack::io_complete(const rgw_io_id
& io_id
)
360 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
361 completion_mgr
->complete(nullptr, io_id
, (void *)this);
364 int RGWCoroutinesStack::unwind(int retcode
)
366 rgw_spawned_stacks
*src_spawned
= &(*pos
)->spawned
;
368 if (pos
== ops
.begin()) {
369 ldout(cct
, 15) << "stack " << (void *)this << " end" << dendl
;
370 spawned
.inherit(src_spawned
);
378 RGWCoroutine
*op
= *pos
;
379 op
->set_retcode(retcode
);
380 op
->spawned
.inherit(src_spawned
);
384 void RGWCoroutinesStack::cancel()
386 while (!ops
.empty()) {
387 RGWCoroutine
*op
= *pos
;
394 bool RGWCoroutinesStack::collect(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
*skip_stack
, uint64_t *stack_id
) /* returns true if needs to be called again */
396 bool need_retry
= false;
397 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
399 vector
<RGWCoroutinesStack
*> new_list
;
401 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
402 RGWCoroutinesStack
*stack
= *iter
;
403 if (stack
== skip_stack
|| !stack
->is_done()) {
404 new_list
.push_back(stack
);
405 if (!stack
->is_done()) {
406 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is still running" << dendl
;
407 } else if (stack
== skip_stack
) {
408 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " explicitly skipping stack" << dendl
;
413 *stack_id
= stack
->get_id();
415 int r
= stack
->get_ret_status();
419 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " encountered error (r=" << r
<< "), skipping next stacks" << dendl
;
420 new_list
.insert(new_list
.end(), ++iter
, s
->entries
.end());
421 need_retry
= (iter
!= s
->entries
.end());
425 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is complete" << dendl
;
428 s
->entries
.swap(new_list
);
432 bool RGWCoroutinesStack::collect_next(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
434 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
437 if (collected_stack
) {
438 *collected_stack
= NULL
;
441 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
442 RGWCoroutinesStack
*stack
= *iter
;
443 if (!stack
->is_done()) {
446 int r
= stack
->get_ret_status();
451 if (collected_stack
) {
452 *collected_stack
= stack
;
456 s
->entries
.erase(iter
);
463 bool RGWCoroutinesStack::collect(int *ret
, RGWCoroutinesStack
*skip_stack
, uint64_t *stack_id
) /* returns true if needs to be called again */
465 return collect(NULL
, ret
, skip_stack
, stack_id
);
468 static void _aio_completion_notifier_cb(librados::completion_t cb
, void *arg
)
470 (static_cast<RGWAioCompletionNotifier
*>(arg
))->cb();
473 RGWAioCompletionNotifier::RGWAioCompletionNotifier(RGWCompletionManager
*_mgr
, const rgw_io_id
& _io_id
, void *_user_data
) : completion_mgr(_mgr
),
475 user_data(_user_data
), registered(true) {
476 c
= librados::Rados::aio_create_completion(this, _aio_completion_notifier_cb
);
479 RGWAioCompletionNotifier
*RGWCoroutinesStack::create_completion_notifier()
481 return ops_mgr
->create_completion_notifier(this);
484 RGWCompletionManager
*RGWCoroutinesStack::get_completion_mgr()
486 return ops_mgr
->get_completion_mgr();
489 bool RGWCoroutinesStack::unblock_stack(RGWCoroutinesStack
**s
)
491 if (blocking_stacks
.empty()) {
495 set
<RGWCoroutinesStack
*>::iterator iter
= blocking_stacks
.begin();
497 blocking_stacks
.erase(iter
);
498 (*s
)->blocked_by_stack
.erase(this);
503 void RGWCoroutinesManager::report_error(RGWCoroutinesStack
*op
)
508 string err
= op
->error_str();
512 lderr(cct
) << "ERROR: failed operation: " << op
->error_str() << dendl
;
515 void RGWCoroutinesStack::dump(Formatter
*f
) const {
518 ::encode_json("stack", ss
.str(), f
);
519 ::encode_json("run_count", run_count
, f
);
520 f
->open_array_section("ops");
521 for (auto& i
: ops
) {
522 encode_json("op", *i
, f
);
527 void RGWCoroutinesStack::init_new_io(RGWIOProvider
*io_provider
)
529 io_provider
->set_io_user_info((void *)this);
530 io_provider
->assign_io(env
->manager
->get_io_id_provider());
533 bool RGWCoroutinesStack::try_io_unblock(const rgw_io_id
& io_id
)
535 if (!can_io_unblock(io_id
)) {
536 auto p
= io_finish_ids
.emplace(io_id
.id
, io_id
);
537 auto& iter
= p
.first
;
538 bool inserted
= p
.second
;
539 if (!inserted
) { /* could not insert, entry already existed, add channel to completion mask */
540 iter
->second
.channels
|= io_id
.channels
;
548 bool RGWCoroutinesStack::consume_io_finish(const rgw_io_id
& io_id
)
550 auto iter
= io_finish_ids
.find(io_id
.id
);
551 if (iter
== io_finish_ids
.end()) {
554 int finish_mask
= iter
->second
.channels
;
555 bool found
= (finish_mask
& io_id
.channels
) != 0;
557 finish_mask
&= ~(finish_mask
& io_id
.channels
);
559 if (finish_mask
== 0) {
560 io_finish_ids
.erase(iter
);
566 void RGWCoroutinesManager::handle_unblocked_stack(set
<RGWCoroutinesStack
*>& context_stacks
, list
<RGWCoroutinesStack
*>& scheduled_stacks
,
567 RGWCompletionManager::io_completion
& io
, int *blocked_count
)
569 ceph_assert(ceph_mutex_is_wlocked(lock
));
570 RGWCoroutinesStack
*stack
= static_cast<RGWCoroutinesStack
*>(io
.user_info
);
571 if (context_stacks
.find(stack
) == context_stacks
.end()) {
574 if (!stack
->try_io_unblock(io
.io_id
)) {
577 if (stack
->is_io_blocked()) {
579 stack
->set_io_blocked(false);
581 stack
->set_interval_wait(false);
582 if (!stack
->is_done()) {
583 if (!stack
->is_scheduled
) {
584 scheduled_stacks
.push_back(stack
);
585 stack
->set_is_scheduled(true);
588 context_stacks
.erase(stack
);
593 void RGWCoroutinesManager::schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
595 std::unique_lock wl
{lock
};
596 _schedule(env
, stack
);
599 void RGWCoroutinesManager::_schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
601 ceph_assert(ceph_mutex_is_wlocked(lock
));
602 if (!stack
->is_scheduled
) {
603 env
->scheduled_stacks
->push_back(stack
);
604 stack
->set_is_scheduled(true);
606 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[env
->run_context
];
607 context_stacks
.insert(stack
);
610 void RGWCoroutinesManager::set_sleeping(RGWCoroutine
*cr
, bool flag
)
612 cr
->set_sleeping(flag
);
615 void RGWCoroutinesManager::io_complete(RGWCoroutine
*cr
, const rgw_io_id
& io_id
)
617 cr
->io_complete(io_id
);
620 int RGWCoroutinesManager::run(const DoutPrefixProvider
*dpp
, list
<RGWCoroutinesStack
*>& stacks
)
623 int blocked_count
= 0;
624 int interval_wait_count
= 0;
625 bool canceled
= false; // set on going_down
626 RGWCoroutinesEnv env
;
629 uint64_t run_context
= ++run_context_count
;
632 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[run_context
];
633 list
<RGWCoroutinesStack
*> scheduled_stacks
;
634 for (auto& st
: stacks
) {
635 context_stacks
.insert(st
);
636 scheduled_stacks
.push_back(st
);
637 st
->set_is_scheduled(true);
639 env
.run_context
= run_context
;
641 env
.scheduled_stacks
= &scheduled_stacks
;
643 for (list
<RGWCoroutinesStack
*>::iterator iter
= scheduled_stacks
.begin(); iter
!= scheduled_stacks
.end() && !going_down
;) {
644 RGWCompletionManager::io_completion io
;
645 RGWCoroutinesStack
*stack
= *iter
;
647 scheduled_stacks
.pop_front();
649 if (context_stacks
.find(stack
) == context_stacks
.end()) {
650 /* stack was probably schedule more than once due to IO, but was since complete */
657 ret
= stack
->operate(dpp
, &env
);
661 stack
->set_is_scheduled(false);
663 ldpp_dout(dpp
, 20) << "stack->operate() returned ret=" << ret
<< dendl
;
666 if (stack
->is_error()) {
670 op_not_blocked
= false;
672 if (stack
->is_io_blocked()) {
673 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is io blocked" << dendl
;
674 if (stack
->is_interval_waiting()) {
675 interval_wait_count
++;
678 } else if (stack
->is_blocked()) {
679 /* do nothing, we'll re-add the stack when the blocking stack is done,
680 * or when we're awaken
682 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is_blocked_by_stack()=" << stack
->is_blocked_by_stack()
683 << " is_sleeping=" << stack
->is_sleeping() << " waiting_for_child()=" << stack
->waiting_for_child() << dendl
;
684 } else if (stack
->is_done()) {
685 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is done" << dendl
;
686 RGWCoroutinesStack
*s
;
687 while (stack
->unblock_stack(&s
)) {
688 if (!s
->is_blocked_by_stack() && !s
->is_done()) {
689 if (s
->is_io_blocked()) {
690 if (stack
->is_interval_waiting()) {
691 interval_wait_count
++;
699 if (stack
->parent
&& stack
->parent
->waiting_for_child()) {
700 stack
->parent
->set_wait_for_child(false);
701 stack
->parent
->_schedule();
703 context_stacks
.erase(stack
);
707 op_not_blocked
= true;
712 if (!op_not_blocked
&& stack
) {
713 stack
->run_count
= 0;
716 while (completion_mgr
->try_get_next(&io
)) {
717 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
721 * only account blocked operations that are not in interval_wait, these are stacks that
722 * were put on a wait without any real IO operations. While we mark these as io_blocked,
723 * these aren't really waiting for IOs
725 while (blocked_count
- interval_wait_count
>= ops_window
) {
727 ret
= completion_mgr
->get_next(&io
);
730 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
732 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
736 while (scheduled_stacks
.empty() && blocked_count
> 0) {
738 ret
= completion_mgr
->get_next(&io
);
741 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
744 ldout(cct
, 5) << __func__
<< "(): was stopped, exiting" << dendl
;
749 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
750 iter
= scheduled_stacks
.begin();
756 if (iter
== scheduled_stacks
.end()) {
757 iter
= scheduled_stacks
.begin();
761 if (!context_stacks
.empty() && !going_down
) {
762 JSONFormatter
formatter(true);
763 formatter
.open_array_section("context_stacks");
764 for (auto& s
: context_stacks
) {
765 ::encode_json("entry", *s
, &formatter
);
767 formatter
.close_section();
768 lderr(cct
) << __func__
<< "(): ERROR: deadlock detected, dumping remaining coroutines:\n";
769 formatter
.flush(*_dout
);
771 ceph_assert(context_stacks
.empty() || going_down
); // assert on deadlock
774 for (auto stack
: context_stacks
) {
775 ldout(cct
, 20) << "clearing stack on run() exit: stack=" << (void *)stack
<< " nref=" << stack
->get_nref() << dendl
;
778 run_contexts
.erase(run_context
);
784 int RGWCoroutinesManager::run(const DoutPrefixProvider
*dpp
, RGWCoroutine
*op
)
789 list
<RGWCoroutinesStack
*> stacks
;
790 RGWCoroutinesStack
*stack
= allocate_stack();
794 stacks
.push_back(stack
);
796 int r
= run(dpp
, stacks
);
798 ldpp_dout(dpp
, 20) << "run(stacks) returned r=" << r
<< dendl
;
800 r
= op
->get_ret_status();
807 RGWAioCompletionNotifier
*RGWCoroutinesManager::create_completion_notifier(RGWCoroutinesStack
*stack
)
809 rgw_io_id io_id
{get_next_io_id(), -1};
810 RGWAioCompletionNotifier
*cn
= new RGWAioCompletionNotifier(completion_mgr
, io_id
, (void *)stack
);
811 completion_mgr
->register_completion_notifier(cn
);
815 void RGWCoroutinesManager::dump(Formatter
*f
) const {
816 std::shared_lock rl
{lock
};
818 f
->open_array_section("run_contexts");
819 for (auto& i
: run_contexts
) {
820 f
->open_object_section("context");
821 ::encode_json("id", i
.first
, f
);
822 f
->open_array_section("entries");
823 for (auto& s
: i
.second
) {
824 ::encode_json("entry", *s
, f
);
832 RGWCoroutinesStack
*RGWCoroutinesManager::allocate_stack() {
833 return new RGWCoroutinesStack(cct
, this);
836 string
RGWCoroutinesManager::get_id()
846 void RGWCoroutinesManagerRegistry::add(RGWCoroutinesManager
*mgr
)
848 std::unique_lock wl
{lock
};
849 if (managers
.find(mgr
) == managers
.end()) {
850 managers
.insert(mgr
);
855 void RGWCoroutinesManagerRegistry::remove(RGWCoroutinesManager
*mgr
)
857 std::unique_lock wl
{lock
};
858 if (managers
.find(mgr
) != managers
.end()) {
864 RGWCoroutinesManagerRegistry::~RGWCoroutinesManagerRegistry()
866 AdminSocket
*admin_socket
= cct
->get_admin_socket();
867 if (!admin_command
.empty()) {
868 admin_socket
->unregister_commands(this);
872 int RGWCoroutinesManagerRegistry::hook_to_admin_command(const string
& command
)
874 AdminSocket
*admin_socket
= cct
->get_admin_socket();
875 if (!admin_command
.empty()) {
876 admin_socket
->unregister_commands(this);
878 admin_command
= command
;
879 int r
= admin_socket
->register_command(admin_command
, this,
880 "dump current coroutines stack state");
882 lderr(cct
) << "ERROR: fail to register admin socket command (r=" << r
<< ")" << dendl
;
888 int RGWCoroutinesManagerRegistry::call(std::string_view command
,
889 const cmdmap_t
& cmdmap
,
893 std::shared_lock rl
{lock
};
894 ::encode_json("cr_managers", *this, f
);
898 void RGWCoroutinesManagerRegistry::dump(Formatter
*f
) const {
899 f
->open_array_section("coroutine_managers");
900 for (auto m
: managers
) {
901 ::encode_json("entry", *m
, f
);
906 void RGWCoroutine::call(RGWCoroutine
*op
)
911 // the call()er expects this to set a retcode
916 RGWCoroutinesStack
*RGWCoroutine::spawn(RGWCoroutine
*op
, bool wait
)
918 return stack
->spawn(this, op
, wait
);
921 RGWCoroutinesStack
*RGWCoroutine::prealloc_stack()
923 return stack
->prealloc_stack();
926 uint64_t RGWCoroutine::prealloc_stack_id()
928 return prealloc_stack()->get_id();
931 bool RGWCoroutine::collect(int *ret
, RGWCoroutinesStack
*skip_stack
, uint64_t *stack_id
) /* returns true if needs to be called again */
933 return stack
->collect(this, ret
, skip_stack
, stack_id
);
936 bool RGWCoroutine::collect_next(int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
938 return stack
->collect_next(this, ret
, collected_stack
);
941 int RGWCoroutine::wait(const utime_t
& interval
)
943 return stack
->wait(interval
);
946 void RGWCoroutine::wait_for_child()
948 /* should only wait for child if there is a child that is not done yet, and no complete children */
949 if (spawned
.entries
.empty()) {
952 for (vector
<RGWCoroutinesStack
*>::iterator iter
= spawned
.entries
.begin(); iter
!= spawned
.entries
.end(); ++iter
) {
953 if ((*iter
)->is_done()) {
957 stack
->set_wait_for_child(true);
960 string
RGWCoroutine::to_str() const
962 return typeid(*this).name();
965 ostream
& operator<<(ostream
& out
, const RGWCoroutine
& cr
)
967 out
<< "cr:s=" << (void *)cr
.get_stack() << ":op=" << (void *)&cr
<< ":" << typeid(cr
).name();
971 bool RGWCoroutine::drain_children(int num_cr_left
,
972 RGWCoroutinesStack
*skip_stack
,
973 std::optional
<std::function
<void(uint64_t stack_id
, int ret
)> > cb
)
976 ceph_assert(num_cr_left
>= 0);
977 if (num_cr_left
== 0 && skip_stack
) {
980 reenter(&drain_status
.cr
) {
981 while (num_spawned() > (size_t)num_cr_left
) {
982 yield
wait_for_child();
987 again
= collect(&ret
, skip_stack
, &stack_id
);
989 ldout(cct
, 10) << "collect() returned ret=" << ret
<< dendl
;
990 /* we should have reported this error */
991 log_error() << "ERROR: collect() returned error (ret=" << ret
<< ")";
994 (*cb
)(stack_id
, ret
);
1003 bool RGWCoroutine::drain_children(int num_cr_left
,
1004 std::optional
<std::function
<int(uint64_t stack_id
, int ret
)> > cb
)
1007 ceph_assert(num_cr_left
>= 0);
1009 reenter(&drain_status
.cr
) {
1010 while (num_spawned() > (size_t)num_cr_left
) {
1011 yield
wait_for_child();
1016 again
= collect(&ret
, nullptr, &stack_id
);
1018 ldout(cct
, 10) << "collect() returned ret=" << ret
<< dendl
;
1019 /* we should have reported this error */
1020 log_error() << "ERROR: collect() returned error (ret=" << ret
<< ")";
1022 if (cb
&& !drain_status
.should_exit
) {
1023 int r
= (*cb
)(stack_id
, ret
);
1025 drain_status
.ret
= r
;
1026 drain_status
.should_exit
= true;
1027 num_cr_left
= 0; /* need to drain all */
1037 void RGWCoroutine::wakeup()
1042 RGWCoroutinesEnv
*RGWCoroutine::get_env() const
1044 return stack
->get_env();
1047 void RGWCoroutine::dump(Formatter
*f
) const {
1048 if (!description
.str().empty()) {
1049 encode_json("description", description
.str(), f
);
1051 encode_json("type", to_str(), f
);
1052 if (!spawned
.entries
.empty()) {
1053 f
->open_array_section("spawned");
1054 for (auto& i
: spawned
.entries
) {
1056 snprintf(buf
, sizeof(buf
), "%p", (void *)i
);
1057 encode_json("stack", string(buf
), f
);
1061 if (!status
.history
.empty()) {
1062 encode_json("history", status
.history
, f
);
1065 if (!status
.status
.str().empty()) {
1066 f
->open_object_section("status");
1067 encode_json("status", status
.status
.str(), f
);
1068 encode_json("timestamp", status
.timestamp
, f
);
1073 RGWSimpleCoroutine::~RGWSimpleCoroutine()
1075 if (!called_cleanup
) {
1080 void RGWSimpleCoroutine::call_cleanup()
1082 called_cleanup
= true;
1086 int RGWSimpleCoroutine::operate(const DoutPrefixProvider
*dpp
)
1090 yield
return state_init();
1091 yield
return state_send_request(dpp
);
1092 yield
return state_request_complete();
1093 yield
return state_all_complete();
1096 return set_state(RGWCoroutine_Done
, ret
);
1101 int RGWSimpleCoroutine::state_init()
1106 return set_state(RGWCoroutine_Error
, ret
);
1111 int RGWSimpleCoroutine::state_send_request(const DoutPrefixProvider
*dpp
)
1113 int ret
= send_request(dpp
);
1116 return set_state(RGWCoroutine_Error
, ret
);
1121 int RGWSimpleCoroutine::state_request_complete()
1123 int ret
= request_complete();
1126 return set_state(RGWCoroutine_Error
, ret
);
1131 int RGWSimpleCoroutine::state_all_complete()
1136 return set_state(RGWCoroutine_Error
, ret
);