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 stack
->init_new_io(io_provider
);
148 void RGWCoroutine::set_io_blocked(bool flag
) {
149 stack
->set_io_blocked(flag
);
152 void RGWCoroutine::set_sleeping(bool flag
) {
153 stack
->set_sleeping(flag
);
156 int RGWCoroutine::io_block(int ret
, int64_t io_id
) {
157 return io_block(ret
, rgw_io_id
{io_id
, -1});
160 int RGWCoroutine::io_block(int ret
, const rgw_io_id
& io_id
) {
161 if (stack
->consume_io_finish(io_id
)) {
164 set_io_blocked(true);
165 stack
->set_io_blocked_id(io_id
);
169 void RGWCoroutine::io_complete(const rgw_io_id
& io_id
) {
170 stack
->io_complete(io_id
);
173 void RGWCoroutine::StatusItem::dump(Formatter
*f
) const {
174 ::encode_json("timestamp", timestamp
, f
);
175 ::encode_json("status", status
, f
);
178 stringstream
& RGWCoroutine::Status::set_status()
180 std::unique_lock l
{lock
};
181 string s
= status
.str();
182 status
.str(string());
183 if (!timestamp
.is_zero()) {
184 history
.push_back(StatusItem(timestamp
, s
));
186 if (history
.size() > (size_t)max_history
) {
189 timestamp
= ceph_clock_now();
194 int64_t RGWCoroutinesManager::get_next_io_id()
196 return (int64_t)++max_io_id
;
199 RGWCoroutinesStack::RGWCoroutinesStack(CephContext
*_cct
, RGWCoroutinesManager
*_ops_mgr
, RGWCoroutine
*start
) : cct(_cct
), ops_mgr(_ops_mgr
),
200 done_flag(false), error_flag(false), blocked_flag(false),
201 sleep_flag(false), interval_wait_flag(false), is_scheduled(false), is_waiting_for_child(false),
202 retcode(0), run_count(0),
203 env(NULL
), parent(NULL
)
206 ops
.push_back(start
);
211 RGWCoroutinesStack::~RGWCoroutinesStack()
213 for (auto op
: ops
) {
217 for (auto stack
: spawned
.entries
) {
222 int RGWCoroutinesStack::operate(RGWCoroutinesEnv
*_env
)
225 RGWCoroutine
*op
= *pos
;
227 ldout(cct
, 20) << *op
<< ": operate()" << dendl
;
228 int r
= op
->operate_wrapper();
230 ldout(cct
, 20) << *op
<< ": operate() returned r=" << r
<< dendl
;
233 error_flag
= op
->is_error();
237 r
= unwind(op_retcode
);
239 done_flag
= (pos
== ops
.end());
240 blocked_flag
&= !done_flag
;
242 retcode
= op_retcode
;
247 /* should r ever be negative at this point? */
253 string
RGWCoroutinesStack::error_str()
255 if (pos
!= ops
.end()) {
256 return (*pos
)->error_str();
261 void RGWCoroutinesStack::call(RGWCoroutine
*next_op
) {
265 ops
.push_back(next_op
);
266 if (pos
!= ops
.end()) {
273 void RGWCoroutinesStack::schedule()
275 env
->manager
->schedule(env
, this);
278 void RGWCoroutinesStack::_schedule()
280 env
->manager
->_schedule(env
, this);
283 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*source_op
, RGWCoroutine
*op
, bool wait
)
289 rgw_spawned_stacks
*s
= (source_op
? &source_op
->spawned
: &spawned
);
291 RGWCoroutinesStack
*stack
= env
->manager
->allocate_stack();
292 s
->add_pending(stack
);
293 stack
->parent
= this;
295 stack
->get(); /* we'll need to collect the stack */
298 env
->manager
->schedule(env
, stack
);
301 set_blocked_by(stack
);
307 RGWCoroutinesStack
*RGWCoroutinesStack::spawn(RGWCoroutine
*op
, bool wait
)
309 return spawn(NULL
, op
, wait
);
312 int RGWCoroutinesStack::wait(const utime_t
& interval
)
314 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
315 completion_mgr
->wait_interval((void *)this, interval
, (void *)this);
316 set_io_blocked(true);
317 set_interval_wait(true);
321 void RGWCoroutinesStack::wakeup()
323 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
324 completion_mgr
->wakeup((void *)this);
327 void RGWCoroutinesStack::io_complete(const rgw_io_id
& io_id
)
329 RGWCompletionManager
*completion_mgr
= env
->manager
->get_completion_mgr();
330 completion_mgr
->complete(nullptr, io_id
, (void *)this);
333 int RGWCoroutinesStack::unwind(int retcode
)
335 rgw_spawned_stacks
*src_spawned
= &(*pos
)->spawned
;
337 if (pos
== ops
.begin()) {
338 ldout(cct
, 15) << "stack " << (void *)this << " end" << dendl
;
339 spawned
.inherit(src_spawned
);
347 RGWCoroutine
*op
= *pos
;
348 op
->set_retcode(retcode
);
349 op
->spawned
.inherit(src_spawned
);
353 void RGWCoroutinesStack::cancel()
355 while (!ops
.empty()) {
356 RGWCoroutine
*op
= *pos
;
363 bool RGWCoroutinesStack::collect(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
365 bool need_retry
= false;
366 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
368 vector
<RGWCoroutinesStack
*> new_list
;
370 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
371 RGWCoroutinesStack
*stack
= *iter
;
372 if (stack
== skip_stack
|| !stack
->is_done()) {
373 new_list
.push_back(stack
);
374 if (!stack
->is_done()) {
375 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is still running" << dendl
;
376 } else if (stack
== skip_stack
) {
377 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " explicitly skipping stack" << dendl
;
381 int r
= stack
->get_ret_status();
385 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " encountered error (r=" << r
<< "), skipping next stacks" << dendl
;
386 new_list
.insert(new_list
.end(), ++iter
, s
->entries
.end());
387 need_retry
= (iter
!= s
->entries
.end());
391 ldout(cct
, 20) << "collect(): s=" << (void *)this << " stack=" << (void *)stack
<< " is complete" << dendl
;
394 s
->entries
.swap(new_list
);
398 bool RGWCoroutinesStack::collect_next(RGWCoroutine
*op
, int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
400 rgw_spawned_stacks
*s
= (op
? &op
->spawned
: &spawned
);
403 if (collected_stack
) {
404 *collected_stack
= NULL
;
407 for (vector
<RGWCoroutinesStack
*>::iterator iter
= s
->entries
.begin(); iter
!= s
->entries
.end(); ++iter
) {
408 RGWCoroutinesStack
*stack
= *iter
;
409 if (!stack
->is_done()) {
412 int r
= stack
->get_ret_status();
417 if (collected_stack
) {
418 *collected_stack
= stack
;
422 s
->entries
.erase(iter
);
429 bool RGWCoroutinesStack::collect(int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
431 return collect(NULL
, ret
, skip_stack
);
434 static void _aio_completion_notifier_cb(librados::completion_t cb
, void *arg
)
436 (static_cast<RGWAioCompletionNotifier
*>(arg
))->cb();
439 RGWAioCompletionNotifier::RGWAioCompletionNotifier(RGWCompletionManager
*_mgr
, const rgw_io_id
& _io_id
, void *_user_data
) : completion_mgr(_mgr
),
441 user_data(_user_data
), registered(true) {
442 c
= librados::Rados::aio_create_completion(this, _aio_completion_notifier_cb
);
445 RGWAioCompletionNotifier
*RGWCoroutinesStack::create_completion_notifier()
447 return ops_mgr
->create_completion_notifier(this);
450 RGWCompletionManager
*RGWCoroutinesStack::get_completion_mgr()
452 return ops_mgr
->get_completion_mgr();
455 bool RGWCoroutinesStack::unblock_stack(RGWCoroutinesStack
**s
)
457 if (blocking_stacks
.empty()) {
461 set
<RGWCoroutinesStack
*>::iterator iter
= blocking_stacks
.begin();
463 blocking_stacks
.erase(iter
);
464 (*s
)->blocked_by_stack
.erase(this);
469 void RGWCoroutinesManager::report_error(RGWCoroutinesStack
*op
)
474 string err
= op
->error_str();
478 lderr(cct
) << "ERROR: failed operation: " << op
->error_str() << dendl
;
481 void RGWCoroutinesStack::dump(Formatter
*f
) const {
484 ::encode_json("stack", ss
.str(), f
);
485 ::encode_json("run_count", run_count
, f
);
486 f
->open_array_section("ops");
487 for (auto& i
: ops
) {
488 encode_json("op", *i
, f
);
493 void RGWCoroutinesStack::init_new_io(RGWIOProvider
*io_provider
)
495 io_provider
->set_io_user_info((void *)this);
496 io_provider
->assign_io(env
->manager
->get_io_id_provider());
499 bool RGWCoroutinesStack::try_io_unblock(const rgw_io_id
& io_id
)
501 if (!can_io_unblock(io_id
)) {
502 auto p
= io_finish_ids
.emplace(io_id
.id
, io_id
);
503 auto& iter
= p
.first
;
504 bool inserted
= p
.second
;
505 if (!inserted
) { /* could not insert, entry already existed, add channel to completion mask */
506 iter
->second
.channels
|= io_id
.channels
;
514 bool RGWCoroutinesStack::consume_io_finish(const rgw_io_id
& io_id
)
516 auto iter
= io_finish_ids
.find(io_id
.id
);
517 if (iter
== io_finish_ids
.end()) {
520 int finish_mask
= iter
->second
.channels
;
521 bool found
= (finish_mask
& io_id
.channels
) != 0;
523 finish_mask
&= ~(finish_mask
& io_id
.channels
);
525 if (finish_mask
== 0) {
526 io_finish_ids
.erase(iter
);
532 void RGWCoroutinesManager::handle_unblocked_stack(set
<RGWCoroutinesStack
*>& context_stacks
, list
<RGWCoroutinesStack
*>& scheduled_stacks
,
533 RGWCompletionManager::io_completion
& io
, int *blocked_count
)
535 ceph_assert(ceph_mutex_is_wlocked(lock
));
536 RGWCoroutinesStack
*stack
= static_cast<RGWCoroutinesStack
*>(io
.user_info
);
537 if (context_stacks
.find(stack
) == context_stacks
.end()) {
540 if (!stack
->try_io_unblock(io
.io_id
)) {
543 if (stack
->is_io_blocked()) {
545 stack
->set_io_blocked(false);
547 stack
->set_interval_wait(false);
548 if (!stack
->is_done()) {
549 if (!stack
->is_scheduled
) {
550 scheduled_stacks
.push_back(stack
);
551 stack
->set_is_scheduled(true);
554 context_stacks
.erase(stack
);
559 void RGWCoroutinesManager::schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
561 std::unique_lock wl
{lock
};
562 _schedule(env
, stack
);
565 void RGWCoroutinesManager::_schedule(RGWCoroutinesEnv
*env
, RGWCoroutinesStack
*stack
)
567 ceph_assert(ceph_mutex_is_wlocked(lock
));
568 if (!stack
->is_scheduled
) {
569 env
->scheduled_stacks
->push_back(stack
);
570 stack
->set_is_scheduled(true);
572 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[env
->run_context
];
573 context_stacks
.insert(stack
);
576 void RGWCoroutinesManager::set_sleeping(RGWCoroutine
*cr
, bool flag
)
578 cr
->set_sleeping(flag
);
581 void RGWCoroutinesManager::io_complete(RGWCoroutine
*cr
, const rgw_io_id
& io_id
)
583 cr
->io_complete(io_id
);
586 int RGWCoroutinesManager::run(list
<RGWCoroutinesStack
*>& stacks
)
589 int blocked_count
= 0;
590 int interval_wait_count
= 0;
591 bool canceled
= false; // set on going_down
592 RGWCoroutinesEnv env
;
595 uint64_t run_context
= ++run_context_count
;
598 set
<RGWCoroutinesStack
*>& context_stacks
= run_contexts
[run_context
];
599 list
<RGWCoroutinesStack
*> scheduled_stacks
;
600 for (auto& st
: stacks
) {
601 context_stacks
.insert(st
);
602 scheduled_stacks
.push_back(st
);
603 st
->set_is_scheduled(true);
605 env
.run_context
= run_context
;
607 env
.scheduled_stacks
= &scheduled_stacks
;
609 for (list
<RGWCoroutinesStack
*>::iterator iter
= scheduled_stacks
.begin(); iter
!= scheduled_stacks
.end() && !going_down
;) {
610 RGWCompletionManager::io_completion io
;
611 RGWCoroutinesStack
*stack
= *iter
;
613 scheduled_stacks
.pop_front();
615 if (context_stacks
.find(stack
) == context_stacks
.end()) {
616 /* stack was probably schedule more than once due to IO, but was since complete */
623 ret
= stack
->operate(&env
);
627 stack
->set_is_scheduled(false);
629 ldout(cct
, 20) << "stack->operate() returned ret=" << ret
<< dendl
;
632 if (stack
->is_error()) {
636 op_not_blocked
= false;
638 if (stack
->is_io_blocked()) {
639 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is io blocked" << dendl
;
640 if (stack
->is_interval_waiting()) {
641 interval_wait_count
++;
644 } else if (stack
->is_blocked()) {
645 /* do nothing, we'll re-add the stack when the blocking stack is done,
646 * or when we're awaken
648 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is_blocked_by_stack()=" << stack
->is_blocked_by_stack()
649 << " is_sleeping=" << stack
->is_sleeping() << " waiting_for_child()=" << stack
->waiting_for_child() << dendl
;
650 } else if (stack
->is_done()) {
651 ldout(cct
, 20) << __func__
<< ":" << " stack=" << (void *)stack
<< " is done" << dendl
;
652 RGWCoroutinesStack
*s
;
653 while (stack
->unblock_stack(&s
)) {
654 if (!s
->is_blocked_by_stack() && !s
->is_done()) {
655 if (s
->is_io_blocked()) {
656 if (stack
->is_interval_waiting()) {
657 interval_wait_count
++;
665 if (stack
->parent
&& stack
->parent
->waiting_for_child()) {
666 stack
->parent
->set_wait_for_child(false);
667 stack
->parent
->_schedule();
669 context_stacks
.erase(stack
);
673 op_not_blocked
= true;
678 if (!op_not_blocked
&& stack
) {
679 stack
->run_count
= 0;
682 while (completion_mgr
->try_get_next(&io
)) {
683 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
687 * only account blocked operations that are not in interval_wait, these are stacks that
688 * were put on a wait without any real IO operations. While we mark these as io_blocked,
689 * these aren't really waiting for IOs
691 while (blocked_count
- interval_wait_count
>= ops_window
) {
693 ret
= completion_mgr
->get_next(&io
);
696 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
698 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
702 while (scheduled_stacks
.empty() && blocked_count
> 0) {
704 ret
= completion_mgr
->get_next(&io
);
707 ldout(cct
, 5) << "completion_mgr.get_next() returned ret=" << ret
<< dendl
;
710 ldout(cct
, 5) << __func__
<< "(): was stopped, exiting" << dendl
;
715 handle_unblocked_stack(context_stacks
, scheduled_stacks
, io
, &blocked_count
);
716 iter
= scheduled_stacks
.begin();
722 if (iter
== scheduled_stacks
.end()) {
723 iter
= scheduled_stacks
.begin();
727 if (!context_stacks
.empty() && !going_down
) {
728 JSONFormatter
formatter(true);
729 formatter
.open_array_section("context_stacks");
730 for (auto& s
: context_stacks
) {
731 ::encode_json("entry", *s
, &formatter
);
733 formatter
.close_section();
734 lderr(cct
) << __func__
<< "(): ERROR: deadlock detected, dumping remaining coroutines:\n";
735 formatter
.flush(*_dout
);
737 ceph_assert(context_stacks
.empty() || going_down
); // assert on deadlock
740 for (auto stack
: context_stacks
) {
741 ldout(cct
, 20) << "clearing stack on run() exit: stack=" << (void *)stack
<< " nref=" << stack
->get_nref() << dendl
;
744 run_contexts
.erase(run_context
);
750 int RGWCoroutinesManager::run(RGWCoroutine
*op
)
755 list
<RGWCoroutinesStack
*> stacks
;
756 RGWCoroutinesStack
*stack
= allocate_stack();
760 stacks
.push_back(stack
);
764 ldout(cct
, 20) << "run(stacks) returned r=" << r
<< dendl
;
766 r
= op
->get_ret_status();
773 RGWAioCompletionNotifier
*RGWCoroutinesManager::create_completion_notifier(RGWCoroutinesStack
*stack
)
775 rgw_io_id io_id
{get_next_io_id(), -1};
776 RGWAioCompletionNotifier
*cn
= new RGWAioCompletionNotifier(completion_mgr
, io_id
, (void *)stack
);
777 completion_mgr
->register_completion_notifier(cn
);
781 void RGWCoroutinesManager::dump(Formatter
*f
) const {
782 std::shared_lock rl
{lock
};
784 f
->open_array_section("run_contexts");
785 for (auto& i
: run_contexts
) {
786 f
->open_object_section("context");
787 ::encode_json("id", i
.first
, f
);
788 f
->open_array_section("entries");
789 for (auto& s
: i
.second
) {
790 ::encode_json("entry", *s
, f
);
798 RGWCoroutinesStack
*RGWCoroutinesManager::allocate_stack() {
799 return new RGWCoroutinesStack(cct
, this);
802 string
RGWCoroutinesManager::get_id()
812 void RGWCoroutinesManagerRegistry::add(RGWCoroutinesManager
*mgr
)
814 std::unique_lock wl
{lock
};
815 if (managers
.find(mgr
) == managers
.end()) {
816 managers
.insert(mgr
);
821 void RGWCoroutinesManagerRegistry::remove(RGWCoroutinesManager
*mgr
)
823 std::unique_lock wl
{lock
};
824 if (managers
.find(mgr
) != managers
.end()) {
830 RGWCoroutinesManagerRegistry::~RGWCoroutinesManagerRegistry()
832 AdminSocket
*admin_socket
= cct
->get_admin_socket();
833 if (!admin_command
.empty()) {
834 admin_socket
->unregister_commands(this);
838 int RGWCoroutinesManagerRegistry::hook_to_admin_command(const string
& command
)
840 AdminSocket
*admin_socket
= cct
->get_admin_socket();
841 if (!admin_command
.empty()) {
842 admin_socket
->unregister_commands(this);
844 admin_command
= command
;
845 int r
= admin_socket
->register_command(admin_command
, this,
846 "dump current coroutines stack state");
848 lderr(cct
) << "ERROR: fail to register admin socket command (r=" << r
<< ")" << dendl
;
854 int RGWCoroutinesManagerRegistry::call(std::string_view command
,
855 const cmdmap_t
& cmdmap
,
859 std::shared_lock rl
{lock
};
860 ::encode_json("cr_managers", *this, f
);
864 void RGWCoroutinesManagerRegistry::dump(Formatter
*f
) const {
865 f
->open_array_section("coroutine_managers");
866 for (auto m
: managers
) {
867 ::encode_json("entry", *m
, f
);
872 void RGWCoroutine::call(RGWCoroutine
*op
)
877 // the call()er expects this to set a retcode
882 RGWCoroutinesStack
*RGWCoroutine::spawn(RGWCoroutine
*op
, bool wait
)
884 return stack
->spawn(this, op
, wait
);
887 bool RGWCoroutine::collect(int *ret
, RGWCoroutinesStack
*skip_stack
) /* returns true if needs to be called again */
889 return stack
->collect(this, ret
, skip_stack
);
892 bool RGWCoroutine::collect_next(int *ret
, RGWCoroutinesStack
**collected_stack
) /* returns true if found a stack to collect */
894 return stack
->collect_next(this, ret
, collected_stack
);
897 int RGWCoroutine::wait(const utime_t
& interval
)
899 return stack
->wait(interval
);
902 void RGWCoroutine::wait_for_child()
904 /* should only wait for child if there is a child that is not done yet, and no complete children */
905 if (spawned
.entries
.empty()) {
908 for (vector
<RGWCoroutinesStack
*>::iterator iter
= spawned
.entries
.begin(); iter
!= spawned
.entries
.end(); ++iter
) {
909 if ((*iter
)->is_done()) {
913 stack
->set_wait_for_child(true);
916 string
RGWCoroutine::to_str() const
918 return typeid(*this).name();
921 ostream
& operator<<(ostream
& out
, const RGWCoroutine
& cr
)
923 out
<< "cr:s=" << (void *)cr
.get_stack() << ":op=" << (void *)&cr
<< ":" << typeid(cr
).name();
927 bool RGWCoroutine::drain_children(int num_cr_left
, RGWCoroutinesStack
*skip_stack
)
930 ceph_assert(num_cr_left
>= 0);
931 if (num_cr_left
== 0 && skip_stack
) {
935 while (num_spawned() > (size_t)num_cr_left
) {
936 yield
wait_for_child();
938 while (collect(&ret
, skip_stack
)) {
940 ldout(cct
, 10) << "collect() returned ret=" << ret
<< dendl
;
941 /* we should have reported this error */
942 log_error() << "ERROR: collect() returned error (ret=" << ret
<< ")";
951 void RGWCoroutine::wakeup()
956 RGWCoroutinesEnv
*RGWCoroutine::get_env() const
958 return stack
->get_env();
961 void RGWCoroutine::dump(Formatter
*f
) const {
962 if (!description
.str().empty()) {
963 encode_json("description", description
.str(), f
);
965 encode_json("type", to_str(), f
);
966 if (!spawned
.entries
.empty()) {
967 f
->open_array_section("spawned");
968 for (auto& i
: spawned
.entries
) {
970 snprintf(buf
, sizeof(buf
), "%p", (void *)i
);
971 encode_json("stack", string(buf
), f
);
975 if (!status
.history
.empty()) {
976 encode_json("history", status
.history
, f
);
979 if (!status
.status
.str().empty()) {
980 f
->open_object_section("status");
981 encode_json("status", status
.status
.str(), f
);
982 encode_json("timestamp", status
.timestamp
, f
);
987 RGWSimpleCoroutine::~RGWSimpleCoroutine()
989 if (!called_cleanup
) {
994 void RGWSimpleCoroutine::call_cleanup()
996 called_cleanup
= true;
1000 int RGWSimpleCoroutine::operate()
1004 yield
return state_init();
1005 yield
return state_send_request();
1006 yield
return state_request_complete();
1007 yield
return state_all_complete();
1010 return set_state(RGWCoroutine_Done
, ret
);
1015 int RGWSimpleCoroutine::state_init()
1020 return set_state(RGWCoroutine_Error
, ret
);
1025 int RGWSimpleCoroutine::state_send_request()
1027 int ret
= send_request();
1030 return set_state(RGWCoroutine_Error
, ret
);
1035 int RGWSimpleCoroutine::state_request_complete()
1037 int ret
= request_complete();
1040 return set_state(RGWCoroutine_Error
, ret
);
1045 int RGWSimpleCoroutine::state_all_complete()
1050 return set_state(RGWCoroutine_Error
, ret
);