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
17 class RGWCompletionManager::WaitContext
: public Context
{
18 RGWCompletionManager
*manager
;
21 WaitContext(RGWCompletionManager
*_cm
, void *_opaque
) : manager(_cm
), opaque(_opaque
) {}
22 void finish(int r
) override
{
23 manager
->_wakeup(opaque
);
27 RGWCompletionManager::RGWCompletionManager(CephContext
*_cct
) : cct(_cct
),
33 RGWCompletionManager::~RGWCompletionManager()
35 std::lock_guard l
{lock
};
36 timer
.cancel_all_events();
40 void RGWCompletionManager::complete(RGWAioCompletionNotifier
*cn
, const rgw_io_id
& io_id
, void *user_info
)
42 std::lock_guard l
{lock
};
43 _complete(cn
, io_id
, user_info
);
46 void RGWCompletionManager::register_completion_notifier(RGWAioCompletionNotifier
*cn
)
48 std::lock_guard l
{lock
};
54 void RGWCompletionManager::unregister_completion_notifier(RGWAioCompletionNotifier
*cn
)
56 std::lock_guard l
{lock
};
62 void RGWCompletionManager::_complete(RGWAioCompletionNotifier
*cn
, const rgw_io_id
& io_id
, void *user_info
)
68 if (complete_reqs_set
.find(io_id
) != complete_reqs_set
.end()) {
69 /* already have completion for this io_id, don't allow multiple completions for it */
72 complete_reqs
.push_back(io_completion
{io_id
, user_info
});
76 int RGWCompletionManager::get_next(io_completion
*io
)
78 std::unique_lock l
{lock
};
79 while (complete_reqs
.empty()) {
85 *io
= complete_reqs
.front();
86 complete_reqs_set
.erase(io
->io_id
);
87 complete_reqs
.pop_front();
91 bool RGWCompletionManager::try_get_next(io_completion
*io
)
93 std::lock_guard l
{lock
};
94 if (complete_reqs
.empty()) {
97 *io
= complete_reqs
.front();
98 complete_reqs_set
.erase(io
->io_id
);
99 complete_reqs
.pop_front();
103 void RGWCompletionManager::go_down()
105 std::lock_guard l
{lock
};
106 for (auto cn
: cns
) {
113 void RGWCompletionManager::wait_interval(void *opaque
, const utime_t
& interval
, void *user_info
)
115 std::lock_guard l
{lock
};
116 ceph_assert(waiters
.find(opaque
) == waiters
.end());
117 waiters
[opaque
] = user_info
;
118 timer
.add_event_after(interval
, new WaitContext(this, opaque
));
121 void RGWCompletionManager::wakeup(void *opaque
)
123 std::lock_guard l
{lock
};
127 void RGWCompletionManager::_wakeup(void *opaque
)
129 map
<void *, void *>::iterator iter
= waiters
.find(opaque
);
130 if (iter
!= waiters
.end()) {
131 void *user_id
= iter
->second
;
133 _complete(NULL
, rgw_io_id
{0, -1} /* no IO id */, user_id
);
137 RGWCoroutine::~RGWCoroutine() {
138 for (auto stack
: spawned
.entries
) {
143 void RGWCoroutine::init_new_io(RGWIOProvider
*io_provider
)
145 ceph_assert(stack
); // if there's no stack, io_provider won't be uninitialized
146 stack
->init_new_io(io_provider
);
149 void RGWCoroutine::set_io_blocked(bool flag
) {
151 stack
->set_io_blocked(flag
);
155 void RGWCoroutine::set_sleeping(bool flag
) {
157 stack
->set_sleeping(flag
);
161 int RGWCoroutine::io_block(int ret
, int64_t io_id
) {
162 return io_block(ret
, rgw_io_id
{io_id
, -1});
165 int RGWCoroutine::io_block(int ret
, const rgw_io_id
& io_id
) {
169 if (stack
->consume_io_finish(io_id
)) {
172 set_io_blocked(true);
173 stack
->set_io_blocked_id(io_id
);
177 void RGWCoroutine::io_complete(const rgw_io_id
& io_id
) {
179 stack
->io_complete(io_id
);
183 void RGWCoroutine::StatusItem::dump(Formatter
*f
) const {
184 ::encode_json("timestamp", timestamp
, f
);
185 ::encode_json("status", status
, f
);
188 stringstream
& RGWCoroutine::Status::set_status()
190 std::unique_lock l
{lock
};
191 string s
= status
.str();
192 status
.str(string());
193 if (!timestamp
.is_zero()) {
194 history
.push_back(StatusItem(timestamp
, s
));
196 if (history
.size() > (size_t)max_history
) {
199 timestamp
= ceph_clock_now();
204 int64_t RGWCoroutinesManager::get_next_io_id()
206 return (int64_t)++max_io_id
;
209 RGWCoroutinesStack::RGWCoroutinesStack(CephContext
*_cct
, RGWCoroutinesManager
*_ops_mgr
, RGWCoroutine
*start
) : cct(_cct
), ops_mgr(_ops_mgr
),
210 done_flag(false), error_flag(false), blocked_flag(false),
211 sleep_flag(false), interval_wait_flag(false), is_scheduled(false), is_waiting_for_child(false),
212 retcode(0), run_count(0),
213 env(NULL
), parent(NULL
)
216 ops
.push_back(start
);
221 RGWCoroutinesStack::~RGWCoroutinesStack()
223 for (auto op
: ops
) {
227 for (auto stack
: spawned
.entries
) {
232 int RGWCoroutinesStack::operate(RGWCoroutinesEnv
*_env
)
235 RGWCoroutine
*op
= *pos
;
237 ldout(cct
, 20) << *op
<< ": operate()" << dendl
;
238 int r
= op
->operate_wrapper();
240 ldout(cct
, 20) << *op
<< ": operate() returned r=" << r
<< dendl
;
243 error_flag
= op
->is_error();
247 r
= unwind(op_retcode
);
249 done_flag
= (pos
== ops
.end());
250 blocked_flag
&= !done_flag
;
252 retcode
= op_retcode
;
257 /* should r ever be negative at this point? */
263 string
RGWCoroutinesStack::error_str()
265 if (pos
!= ops
.end()) {
266 return (*pos
)->error_str();
271 void RGWCoroutinesStack::call(RGWCoroutine
*next_op
) {
275 ops
.push_back(next_op
);
276 if (pos
!= ops
.end()) {
283 void RGWCoroutinesStack::schedule()
285 env
->manager
->schedule(env
, this);
288 void RGWCoroutinesStack::_schedule()
290 env
->manager
->_schedule(env
, this);
293 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*source_op
, RGWCoroutine
*op
, bool wait
)
299 rgw_spawned_stacks
*s
= (source_op
? &source_op
->spawned
: &spawned
);
301 RGWCoroutinesStack
*stack
= env
->manager
->allocate_stack();
302 s
->add_pending(stack
);
303 stack
->parent
= this;
305 stack
->get(); /* we'll need to collect the stack */
308 env
->manager
->schedule(env
, stack
);
311 set_blocked_by(stack
);
317 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*op
, bool wait
)
319 return spawn(NULL
, op
, wait
);
322 int RGWCoroutinesStack::wait(const utime_t
& interval
)
324 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
325 completion_mgr
->wait_interval((void *)this, interval
, (void *)this);
326 set_io_blocked(true);
327 set_interval_wait(true);
331 void RGWCoroutinesStack::wakeup()
333 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
334 completion_mgr
->wakeup((void *)this);
337 void RGWCoroutinesStack::io_complete(const rgw_io_id
& io_id
)
339 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
340 completion_mgr
->complete(nullptr, io_id
, (void *)this);
343 int RGWCoroutinesStack::unwind(int retcode
)
345 rgw_spawned_stacks
*src_spawned
= &(*pos
)->spawned
;
347 if (pos
== ops
.begin()) {
348 ldout(cct
, 15) << "stack " << (void *)this << " end" << dendl
;
349 spawned
.inherit(src_spawned
);
357 RGWCoroutine
*op
= *pos
;
358 op
->set_retcode(retcode
);
359 op
->spawned
.inherit(src_spawned
);
363 void RGWCoroutinesStack::cancel()
365 while (!ops
.empty()) {
366 RGWCoroutine
*op
= *pos
;
373 bool RGWCoroutinesStack::collect(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
375 bool need_retry
= false;
376 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
378 vector
<RGWCoroutinesStack
*> new_list
;
380 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
381 RGWCoroutinesStack
*stack
= *iter
;
382 if (stack
== skip_stack
|| !stack
->is_done()) {
383 new_list
.push_back(stack
);
384 if (!stack
->is_done()) {
385 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is still running" << dendl
;
386 } else if (stack
== skip_stack
) {
387 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " explicitly skipping stack" << dendl
;
391 int r
= stack
->get_ret_status();
395 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " encountered error (r=" << r
<< "), skipping next stacks" << dendl
;
396 new_list
.insert(new_list
.end(), ++iter
, s
->entries
.end());
397 need_retry
= (iter
!= s
->entries
.end());
401 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is complete" << dendl
;
404 s
->entries
.swap(new_list
);
408 bool RGWCoroutinesStack::collect_next(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
410 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
413 if (collected_stack
) {
414 *collected_stack
= NULL
;
417 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
418 RGWCoroutinesStack
*stack
= *iter
;
419 if (!stack
->is_done()) {
422 int r
= stack
->get_ret_status();
427 if (collected_stack
) {
428 *collected_stack
= stack
;
432 s
->entries
.erase(iter
);
439 bool RGWCoroutinesStack::collect(int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
441 return collect(NULL
, ret
, skip_stack
);
444 static void _aio_completion_notifier_cb(librados::completion_t cb
, void *arg
)
446 (static_cast<RGWAioCompletionNotifier
*>(arg
))->cb();
449 RGWAioCompletionNotifier::RGWAioCompletionNotifier(RGWCompletionManager
*_mgr
, const rgw_io_id
& _io_id
, void *_user_data
) : completion_mgr(_mgr
),
451 user_data(_user_data
), registered(true) {
452 c
= librados::Rados::aio_create_completion(this, _aio_completion_notifier_cb
);
455 RGWAioCompletionNotifier
*RGWCoroutinesStack::create_completion_notifier()
457 return ops_mgr
->create_completion_notifier(this);
460 RGWCompletionManager
*RGWCoroutinesStack::get_completion_mgr()
462 return ops_mgr
->get_completion_mgr();
465 bool RGWCoroutinesStack::unblock_stack(RGWCoroutinesStack
**s
)
467 if (blocking_stacks
.empty()) {
471 set
<RGWCoroutinesStack
*>::iterator iter
= blocking_stacks
.begin();
473 blocking_stacks
.erase(iter
);
474 (*s
)->blocked_by_stack
.erase(this);
479 void RGWCoroutinesManager::report_error(RGWCoroutinesStack
*op
)
484 string err
= op
->error_str();
488 lderr(cct
) << "ERROR: failed operation: " << op
->error_str() << dendl
;
491 void RGWCoroutinesStack::dump(Formatter
*f
) const {
494 ::encode_json("stack", ss
.str(), f
);
495 ::encode_json("run_count", run_count
, f
);
496 f
->open_array_section("ops");
497 for (auto& i
: ops
) {
498 encode_json("op", *i
, f
);
503 void RGWCoroutinesStack::init_new_io(RGWIOProvider
*io_provider
)
505 io_provider
->set_io_user_info((void *)this);
506 io_provider
->assign_io(env
->manager
->get_io_id_provider());
509 bool RGWCoroutinesStack::try_io_unblock(const rgw_io_id
& io_id
)
511 if (!can_io_unblock(io_id
)) {
512 auto p
= io_finish_ids
.emplace(io_id
.id
, io_id
);
513 auto& iter
= p
.first
;
514 bool inserted
= p
.second
;
515 if (!inserted
) { /* could not insert, entry already existed, add channel to completion mask */
516 iter
->second
.channels
|= io_id
.channels
;
524 bool RGWCoroutinesStack::consume_io_finish(const rgw_io_id
& io_id
)
526 auto iter
= io_finish_ids
.find(io_id
.id
);
527 if (iter
== io_finish_ids
.end()) {
530 int finish_mask
= iter
->second
.channels
;
531 bool found
= (finish_mask
& io_id
.channels
) != 0;
533 finish_mask
&= ~(finish_mask
& io_id
.channels
);
535 if (finish_mask
== 0) {
536 io_finish_ids
.erase(iter
);
542 void RGWCoroutinesManager::handle_unblocked_stack(set
<RGWCoroutinesStack
*>& context_stacks
, list
<RGWCoroutinesStack
*>& scheduled_stacks
,
543 RGWCompletionManager::io_completion
& io
, int *blocked_count
)
545 ceph_assert(ceph_mutex_is_wlocked(lock
));
546 RGWCoroutinesStack
*stack
= static_cast<RGWCoroutinesStack
*>(io
.user_info
);
547 if (context_stacks
.find(stack
) == context_stacks
.end()) {
550 if (!stack
->try_io_unblock(io
.io_id
)) {
553 if (stack
->is_io_blocked()) {
555 stack
->set_io_blocked(false);
557 stack
->set_interval_wait(false);
558 if (!stack
->is_done()) {
559 if (!stack
->is_scheduled
) {
560 scheduled_stacks
.push_back(stack
);
561 stack
->set_is_scheduled(true);
564 context_stacks
.erase(stack
);
569 void RGWCoroutinesManager::schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
571 std::unique_lock wl
{lock
};
572 _schedule(env
, stack
);
575 void RGWCoroutinesManager::_schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
577 ceph_assert(ceph_mutex_is_wlocked(lock
));
578 if (!stack
->is_scheduled
) {
579 env
->scheduled_stacks
->push_back(stack
);
580 stack
->set_is_scheduled(true);
582 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[env
->run_context
];
583 context_stacks
.insert(stack
);
586 void RGWCoroutinesManager::set_sleeping(RGWCoroutine
*cr
, bool flag
)
588 cr
->set_sleeping(flag
);
591 void RGWCoroutinesManager::io_complete(RGWCoroutine
*cr
, const rgw_io_id
& io_id
)
593 cr
->io_complete(io_id
);
596 int RGWCoroutinesManager::run(list
<RGWCoroutinesStack
*>& stacks
)
599 int blocked_count
= 0;
600 int interval_wait_count
= 0;
601 bool canceled
= false; // set on going_down
602 RGWCoroutinesEnv env
;
605 uint64_t run_context
= ++run_context_count
;
608 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[run_context
];
609 list
<RGWCoroutinesStack
*> scheduled_stacks
;
610 for (auto& st
: stacks
) {
611 context_stacks
.insert(st
);
612 scheduled_stacks
.push_back(st
);
613 st
->set_is_scheduled(true);
615 env
.run_context
= run_context
;
617 env
.scheduled_stacks
= &scheduled_stacks
;
619 for (list
<RGWCoroutinesStack
*>::iterator iter
= scheduled_stacks
.begin(); iter
!= scheduled_stacks
.end() && !going_down
;) {
620 RGWCompletionManager::io_completion io
;
621 RGWCoroutinesStack
*stack
= *iter
;
623 scheduled_stacks
.pop_front();
625 if (context_stacks
.find(stack
) == context_stacks
.end()) {
626 /* stack was probably schedule more than once due to IO, but was since complete */
633 ret
= stack
->operate(&env
);
637 stack
->set_is_scheduled(false);
639 ldout(cct
, 20) << "stack->operate() returned ret=" << ret
<< dendl
;
642 if (stack
->is_error()) {
646 op_not_blocked
= false;
648 if (stack
->is_io_blocked()) {
649 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is io blocked" << dendl
;
650 if (stack
->is_interval_waiting()) {
651 interval_wait_count
++;
654 } else if (stack
->is_blocked()) {
655 /* do nothing, we'll re-add the stack when the blocking stack is done,
656 * or when we're awaken
658 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is_blocked_by_stack()=" << stack
->is_blocked_by_stack()
659 << " is_sleeping=" << stack
->is_sleeping() << " waiting_for_child()=" << stack
->waiting_for_child() << dendl
;
660 } else if (stack
->is_done()) {
661 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is done" << dendl
;
662 RGWCoroutinesStack
*s
;
663 while (stack
->unblock_stack(&s
)) {
664 if (!s
->is_blocked_by_stack() && !s
->is_done()) {
665 if (s
->is_io_blocked()) {
666 if (stack
->is_interval_waiting()) {
667 interval_wait_count
++;
675 if (stack
->parent
&& stack
->parent
->waiting_for_child()) {
676 stack
->parent
->set_wait_for_child(false);
677 stack
->parent
->_schedule();
679 context_stacks
.erase(stack
);
683 op_not_blocked
= true;
688 if (!op_not_blocked
&& stack
) {
689 stack
->run_count
= 0;
692 while (completion_mgr
->try_get_next(&io
)) {
693 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
697 * only account blocked operations that are not in interval_wait, these are stacks that
698 * were put on a wait without any real IO operations. While we mark these as io_blocked,
699 * these aren't really waiting for IOs
701 while (blocked_count
- interval_wait_count
>= ops_window
) {
703 ret
= completion_mgr
->get_next(&io
);
706 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
708 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
712 while (scheduled_stacks
.empty() && blocked_count
> 0) {
714 ret
= completion_mgr
->get_next(&io
);
717 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
720 ldout(cct
, 5) << __func__
<< "(): was stopped, exiting" << dendl
;
725 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
726 iter
= scheduled_stacks
.begin();
732 if (iter
== scheduled_stacks
.end()) {
733 iter
= scheduled_stacks
.begin();
737 if (!context_stacks
.empty() && !going_down
) {
738 JSONFormatter
formatter(true);
739 formatter
.open_array_section("context_stacks");
740 for (auto& s
: context_stacks
) {
741 ::encode_json("entry", *s
, &formatter
);
743 formatter
.close_section();
744 lderr(cct
) << __func__
<< "(): ERROR: deadlock detected, dumping remaining coroutines:\n";
745 formatter
.flush(*_dout
);
747 ceph_assert(context_stacks
.empty() || going_down
); // assert on deadlock
750 for (auto stack
: context_stacks
) {
751 ldout(cct
, 20) << "clearing stack on run() exit: stack=" << (void *)stack
<< " nref=" << stack
->get_nref() << dendl
;
754 run_contexts
.erase(run_context
);
760 int RGWCoroutinesManager::run(RGWCoroutine
*op
)
765 list
<RGWCoroutinesStack
*> stacks
;
766 RGWCoroutinesStack
*stack
= allocate_stack();
770 stacks
.push_back(stack
);
774 ldout(cct
, 20) << "run(stacks) returned r=" << r
<< dendl
;
776 r
= op
->get_ret_status();
783 RGWAioCompletionNotifier
*RGWCoroutinesManager::create_completion_notifier(RGWCoroutinesStack
*stack
)
785 rgw_io_id io_id
{get_next_io_id(), -1};
786 RGWAioCompletionNotifier
*cn
= new RGWAioCompletionNotifier(completion_mgr
, io_id
, (void *)stack
);
787 completion_mgr
->register_completion_notifier(cn
);
791 void RGWCoroutinesManager::dump(Formatter
*f
) const {
792 std::shared_lock rl
{lock
};
794 f
->open_array_section("run_contexts");
795 for (auto& i
: run_contexts
) {
796 f
->open_object_section("context");
797 ::encode_json("id", i
.first
, f
);
798 f
->open_array_section("entries");
799 for (auto& s
: i
.second
) {
800 ::encode_json("entry", *s
, f
);
808 RGWCoroutinesStack
*RGWCoroutinesManager::allocate_stack() {
809 return new RGWCoroutinesStack(cct
, this);
812 string
RGWCoroutinesManager::get_id()
822 void RGWCoroutinesManagerRegistry::add(RGWCoroutinesManager
*mgr
)
824 std::unique_lock wl
{lock
};
825 if (managers
.find(mgr
) == managers
.end()) {
826 managers
.insert(mgr
);
831 void RGWCoroutinesManagerRegistry::remove(RGWCoroutinesManager
*mgr
)
833 std::unique_lock wl
{lock
};
834 if (managers
.find(mgr
) != managers
.end()) {
840 RGWCoroutinesManagerRegistry::~RGWCoroutinesManagerRegistry()
842 AdminSocket
*admin_socket
= cct
->get_admin_socket();
843 if (!admin_command
.empty()) {
844 admin_socket
->unregister_commands(this);
848 int RGWCoroutinesManagerRegistry::hook_to_admin_command(const string
& command
)
850 AdminSocket
*admin_socket
= cct
->get_admin_socket();
851 if (!admin_command
.empty()) {
852 admin_socket
->unregister_commands(this);
854 admin_command
= command
;
855 int r
= admin_socket
->register_command(admin_command
, this,
856 "dump current coroutines stack state");
858 lderr(cct
) << "ERROR: fail to register admin socket command (r=" << r
<< ")" << dendl
;
864 int RGWCoroutinesManagerRegistry::call(std::string_view command
,
865 const cmdmap_t
& cmdmap
,
869 std::shared_lock rl
{lock
};
870 ::encode_json("cr_managers", *this, f
);
874 void RGWCoroutinesManagerRegistry::dump(Formatter
*f
) const {
875 f
->open_array_section("coroutine_managers");
876 for (auto m
: managers
) {
877 ::encode_json("entry", *m
, f
);
882 void RGWCoroutine::call(RGWCoroutine
*op
)
887 // the call()er expects this to set a retcode
892 RGWCoroutinesStack
*RGWCoroutine::spawn(RGWCoroutine
*op
, bool wait
)
894 return stack
->spawn(this, op
, wait
);
897 bool RGWCoroutine::collect(int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
899 return stack
->collect(this, ret
, skip_stack
);
902 bool RGWCoroutine::collect_next(int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
904 return stack
->collect_next(this, ret
, collected_stack
);
907 int RGWCoroutine::wait(const utime_t
& interval
)
909 return stack
->wait(interval
);
912 void RGWCoroutine::wait_for_child()
914 /* should only wait for child if there is a child that is not done yet, and no complete children */
915 if (spawned
.entries
.empty()) {
918 for (vector
<RGWCoroutinesStack
*>::iterator iter
= spawned
.entries
.begin(); iter
!= spawned
.entries
.end(); ++iter
) {
919 if ((*iter
)->is_done()) {
923 stack
->set_wait_for_child(true);
926 string
RGWCoroutine::to_str() const
928 return typeid(*this).name();
931 ostream
& operator<<(ostream
& out
, const RGWCoroutine
& cr
)
933 out
<< "cr:s=" << (void *)cr
.get_stack() << ":op=" << (void *)&cr
<< ":" << typeid(cr
).name();
937 bool RGWCoroutine::drain_children(int num_cr_left
, RGWCoroutinesStack
*skip_stack
)
940 ceph_assert(num_cr_left
>= 0);
941 if (num_cr_left
== 0 && skip_stack
) {
945 while (num_spawned() > (size_t)num_cr_left
) {
946 yield
wait_for_child();
948 while (collect(&ret
, skip_stack
)) {
950 ldout(cct
, 10) << "collect() returned ret=" << ret
<< dendl
;
951 /* we should have reported this error */
952 log_error() << "ERROR: collect() returned error (ret=" << ret
<< ")";
961 void RGWCoroutine::wakeup()
966 RGWCoroutinesEnv
*RGWCoroutine::get_env() const
968 return stack
->get_env();
971 void RGWCoroutine::dump(Formatter
*f
) const {
972 if (!description
.str().empty()) {
973 encode_json("description", description
.str(), f
);
975 encode_json("type", to_str(), f
);
976 if (!spawned
.entries
.empty()) {
977 f
->open_array_section("spawned");
978 for (auto& i
: spawned
.entries
) {
980 snprintf(buf
, sizeof(buf
), "%p", (void *)i
);
981 encode_json("stack", string(buf
), f
);
985 if (!status
.history
.empty()) {
986 encode_json("history", status
.history
, f
);
989 if (!status
.status
.str().empty()) {
990 f
->open_object_section("status");
991 encode_json("status", status
.status
.str(), f
);
992 encode_json("timestamp", status
.timestamp
, f
);
997 RGWSimpleCoroutine::~RGWSimpleCoroutine()
999 if (!called_cleanup
) {
1004 void RGWSimpleCoroutine::call_cleanup()
1006 called_cleanup
= true;
1010 int RGWSimpleCoroutine::operate()
1014 yield
return state_init();
1015 yield
return state_send_request();
1016 yield
return state_request_complete();
1017 yield
return state_all_complete();
1020 return set_state(RGWCoroutine_Done
, ret
);
1025 int RGWSimpleCoroutine::state_init()
1030 return set_state(RGWCoroutine_Error
, ret
);
1035 int RGWSimpleCoroutine::state_send_request()
1037 int ret
= send_request();
1040 return set_state(RGWCoroutine_Error
, ret
);
1045 int RGWSimpleCoroutine::state_request_complete()
1047 int ret
= request_complete();
1050 return set_state(RGWCoroutine_Error
, ret
);
1055 int RGWSimpleCoroutine::state_all_complete()
1060 return set_state(RGWCoroutine_Error
, ret
);