Repository navigation
rgw/notification: start/stop endpoint managers in notification manager #56979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fdfa667
cb9a09b
15536cf
70e5af8
184b9be
1f509da
9d56bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,7 @@ void publish_commit_completion(rados_completion_t completion, void *arg) { | |
| }; | ||
|
|
||
| class Manager : public DoutPrefixProvider { | ||
| bool shutdown = false; | ||
| const size_t max_queue_size; | ||
| const uint32_t queues_update_period_ms; | ||
| const uint32_t queues_update_retry_ms; | ||
|
|
@@ -289,7 +290,7 @@ class Manager : public DoutPrefixProvider { | |
| cct); | ||
| ldpp_dout(this, 20) << "INFO: push endpoint created: " << event_entry.push_endpoint << | ||
| " for entry: " << entry.marker << dendl; | ||
| const auto ret = push_endpoint->send_to_completion_async(cct, event_entry.event, optional_yield(io_context, yield)); | ||
| const auto ret = push_endpoint->send(event_entry.event, optional_yield(io_context, yield)); | ||
| if (ret < 0) { | ||
| ldpp_dout(this, 5) << "WARNING: push entry marker: " << entry.marker | ||
| << " failed. error: " << ret | ||
|
|
@@ -311,7 +312,7 @@ class Manager : public DoutPrefixProvider { | |
|
|
||
| // clean stale reservation from queue | ||
| void cleanup_queue(const std::string& queue_name, spawn::yield_context yield) { | ||
| while (true) { | ||
| while (!shutdown) { | ||
| ldpp_dout(this, 20) << "INFO: trying to perform stale reservation cleanup for queue: " << queue_name << dendl; | ||
| const auto now = ceph::coarse_real_time::clock::now(); | ||
| const auto stale_time = now - std::chrono::seconds(stale_reservations_period_s); | ||
|
|
@@ -346,6 +347,27 @@ class Manager : public DoutPrefixProvider { | |
| boost::system::error_code ec; | ||
| timer.async_wait(yield[ec]); | ||
| } | ||
| ldpp_dout(this, 5) << "INFO: manager stopped. done cleanup for queue: " << queue_name << dendl; | ||
| } | ||
|
|
||
| // unlock (lose ownership) queue | ||
| int unlock_queue(const std::string& queue_name, spawn::yield_context yield) { | ||
| librados::ObjectWriteOperation op; | ||
| op.assert_exists(); | ||
| rados::cls::lock::unlock(&op, queue_name+"_lock", lock_cookie); | ||
| auto& rados_ioctx = rados_store.getRados()->get_notif_pool_ctx(); | ||
| const auto ret = rgw_rados_operate(this, rados_ioctx, queue_name, &op, optional_yield(io_context, yield)); | ||
| if (ret == -ENOENT) { | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name | ||
| << ". was removed. nothing to unlock" << dendl; | ||
| return 0; | ||
| } | ||
| if (ret == -EBUSY) { | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name | ||
| << ". already owned by another RGW. no need to unlock" << dendl; | ||
| return 0; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| // processing of a specific queue | ||
|
|
@@ -361,7 +383,7 @@ class Manager : public DoutPrefixProvider { | |
|
|
||
| CountersManager queue_counters_container(queue_name, this->get_cct()); | ||
|
|
||
| while (true) { | ||
| while (!shutdown) { | ||
| // if queue was empty the last time, sleep for idle timeout | ||
| if (is_idle) { | ||
| Timer timer(io_context); | ||
|
|
@@ -446,7 +468,7 @@ class Manager : public DoutPrefixProvider { | |
| if (result == EntryProcessingResult::Successful || result == EntryProcessingResult::Expired | ||
| || result == EntryProcessingResult::Migrating) { | ||
| ldpp_dout(this, 20) << "INFO: processing of entry: " << entry.marker | ||
| << " (" << entry_idx << "/" << total_entries << ") from: " << queue_name | ||
| << " (" << entry_idx << "/" << total_entries << ") from: " << queue_name << " " | ||
| << entryProcessingResultString[static_cast<unsigned int>(result)] << dendl; | ||
| remove_entries = true; | ||
| needs_migration_vector[entry_idx - 1] = (result == EntryProcessingResult::Migrating); | ||
|
|
@@ -588,6 +610,7 @@ class Manager : public DoutPrefixProvider { | |
| queue_counters_container.set(l_rgw_persistent_topic_size, entries_size); | ||
| } | ||
| } | ||
| ldpp_dout(this, 5) << "INFO: manager stopped. done processing for queue: " << queue_name << dendl; | ||
| } | ||
|
|
||
| // lits of owned queues | ||
|
|
@@ -598,6 +621,7 @@ class Manager : public DoutPrefixProvider { | |
| void process_queues(spawn::yield_context yield) { | ||
| auto has_error = false; | ||
| owned_queues_t owned_queues; | ||
| size_t processed_queue_count = 0; | ||
|
|
||
| // add randomness to the duration between queue checking | ||
| // to make sure that different daemons are not synced | ||
|
|
@@ -609,7 +633,8 @@ class Manager : public DoutPrefixProvider { | |
|
|
||
| std::vector<std::string> queue_gc; | ||
| std::mutex queue_gc_lock; | ||
| while (true) { | ||
| auto& rados_ioctx = rados_store.getRados()->get_notif_pool_ctx(); | ||
| while (!shutdown) { | ||
| Timer timer(io_context); | ||
| const auto duration = (has_error ? | ||
| std::chrono::milliseconds(queues_update_retry_ms) : std::chrono::milliseconds(queues_update_period_ms)) + | ||
|
|
@@ -640,7 +665,7 @@ class Manager : public DoutPrefixProvider { | |
| failover_time, | ||
| LOCK_FLAG_MAY_RENEW); | ||
|
|
||
| ret = rgw_rados_operate(this, rados_store.getRados()->get_notif_pool_ctx(), queue_name, &op, optional_yield(io_context, yield)); | ||
| ret = rgw_rados_operate(this, rados_ioctx, queue_name, &op, optional_yield(io_context, yield)); | ||
| if (ret == -EBUSY) { | ||
| // lock is already taken by another RGW | ||
| ldpp_dout(this, 20) << "INFO: queue: " << queue_name << " owned (locked) by another daemon" << dendl; | ||
|
|
@@ -662,12 +687,21 @@ class Manager : public DoutPrefixProvider { | |
| if (owned_queues.insert(queue_name).second) { | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name << " now owned (locked) by this daemon" << dendl; | ||
| // start processing this queue | ||
| spawn::spawn(io_context, [this, &queue_gc, &queue_gc_lock, queue_name](spawn::yield_context yield) { | ||
| spawn::spawn(io_context, [this, &queue_gc, &queue_gc_lock, queue_name, &processed_queue_count](spawn::yield_context yield) { | ||
| ++processed_queue_count; | ||
| process_queue(queue_name, yield); | ||
| // if queue processing ended, it means that the queue was removed or not owned anymore | ||
| const auto ret = unlock_queue(queue_name, yield); | ||
| if (ret < 0) { | ||
| ldpp_dout(this, 5) << "WARNING: failed to unlock queue: " << queue_name << " with error: " << | ||
| ret << " (ownership would still move if not renewed)" << dendl; | ||
| } else { | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name << " not locked (ownership can move)" << dendl; | ||
| } | ||
| // mark it for deletion | ||
| std::lock_guard lock_guard(queue_gc_lock); | ||
| queue_gc.push_back(queue_name); | ||
| --processed_queue_count; | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name << " marked for removal" << dendl; | ||
| }, make_stack_allocator()); | ||
| } else { | ||
|
|
@@ -680,21 +714,55 @@ class Manager : public DoutPrefixProvider { | |
| std::for_each(queue_gc.begin(), queue_gc.end(), [this, &owned_queues](const std::string& queue_name) { | ||
| topics_persistency_tracker.erase(queue_name); | ||
| owned_queues.erase(queue_name); | ||
| ldpp_dout(this, 20) << "INFO: queue: " << queue_name << " removed" << dendl; | ||
| ldpp_dout(this, 10) << "INFO: queue: " << queue_name << " was removed" << dendl; | ||
| }); | ||
| queue_gc.clear(); | ||
| } | ||
| } | ||
| Timer timer(io_context); | ||
| while (processed_queue_count > 0) { | ||
| ldpp_dout(this, 5) << "INFO: manager stopped. " << processed_queue_count << " queues are still being processed" << dendl; | ||
| timer.expires_from_now(std::chrono::milliseconds(queues_update_retry_ms)); | ||
| boost::system::error_code ec; | ||
| timer.async_wait(yield[ec]); | ||
| } | ||
| ldpp_dout(this, 5) << "INFO: manager stopped. done processing all queues" << dendl; | ||
| } | ||
|
|
||
| public: | ||
|
|
||
| ~Manager() { | ||
| } | ||
|
|
||
| void stop() { | ||
| shutdown = true; | ||
| work_guard.reset(); | ||
| io_context.stop(); | ||
| std::for_each(workers.begin(), workers.end(), [] (auto& worker) { worker.join(); }); | ||
| } | ||
|
|
||
| void init() { | ||
| spawn::spawn(io_context, [this](spawn::yield_context yield) { | ||
| process_queues(yield); | ||
| }, make_stack_allocator()); | ||
|
|
||
| // start the worker threads to do the actual queue processing | ||
| const std::string WORKER_THREAD_NAME = "notif-worker"; | ||
| for (auto worker_id = 0U; worker_id < worker_count; ++worker_id) { | ||
| workers.emplace_back([this]() { | ||
| try { | ||
| io_context.run(); | ||
| } catch (const std::exception& err) { | ||
| ldpp_dout(this, 1) << "ERROR: notification worker failed with error: " << err.what() << dendl; | ||
| throw err; | ||
| } | ||
| }); | ||
| const auto rc = ceph_pthread_setname(workers.back().native_handle(), | ||
| (WORKER_THREAD_NAME+std::to_string(worker_id)).c_str()); | ||
| ceph_assert(rc == 0); | ||
| } | ||
| ldpp_dout(this, 10) << "INfO: started notification manager with: " << worker_count << " workers" << dendl; | ||
| } | ||
|
|
||
| // ctor: start all threads | ||
| Manager(CephContext* _cct, uint32_t _max_queue_size, uint32_t _queues_update_period_ms, | ||
| uint32_t _queues_update_retry_ms, uint32_t _queue_idle_sleep_us, u_int32_t failover_time_ms, | ||
|
|
@@ -714,28 +782,7 @@ class Manager : public DoutPrefixProvider { | |
| reservations_cleanup_period_s(_reservations_cleanup_period_s), | ||
| site(site), | ||
| rados_store(*store) | ||
| { | ||
| spawn::spawn(io_context, [this](spawn::yield_context yield) { | ||
| process_queues(yield); | ||
| }, make_stack_allocator()); | ||
|
|
||
| // start the worker threads to do the actual queue processing | ||
| const std::string WORKER_THREAD_NAME = "notif-worker"; | ||
| for (auto worker_id = 0U; worker_id < worker_count; ++worker_id) { | ||
| workers.emplace_back([this]() { | ||
| try { | ||
| io_context.run(); | ||
| } catch (const std::exception& err) { | ||
| ldpp_dout(this, 10) << "Notification worker failed with error: " << err.what() << dendl; | ||
| throw(err); | ||
| } | ||
| }); | ||
| const auto rc = ceph_pthread_setname(workers.back().native_handle(), | ||
| (WORKER_THREAD_NAME+std::to_string(worker_id)).c_str()); | ||
| ceph_assert(rc == 0); | ||
| } | ||
| ldpp_dout(this, 10) << "Started notification manager with: " << worker_count << " workers" << dendl; | ||
| } | ||
| {} | ||
|
|
||
| int add_persistent_topic(const std::string& topic_queue, optional_yield y) { | ||
| if (topic_queue == Q_LIST_OBJECT_NAME) { | ||
|
|
@@ -771,10 +818,7 @@ class Manager : public DoutPrefixProvider { | |
| } | ||
| }; | ||
|
|
||
| // singleton manager | ||
| // note that the manager itself is not a singleton, and multiple instances may co-exist | ||
| // TODO make the pointer atomic in allocation and deallocation to avoid race conditions | ||
| static Manager* s_manager = nullptr; | ||
| std::unique_ptr<Manager> s_manager; | ||
|
|
||
| constexpr size_t MAX_QUEUE_SIZE = 128*1000*1000; // 128MB | ||
| constexpr uint32_t Q_LIST_UPDATE_MSEC = 1000*30; // check queue list every 30seconds | ||
|
|
@@ -785,24 +829,31 @@ constexpr uint32_t WORKER_COUNT = 1; // 1 worker thread | |
| constexpr uint32_t STALE_RESERVATIONS_PERIOD_S = 120; // cleanup reservations that are more than 2 minutes old | ||
| constexpr uint32_t RESERVATIONS_CLEANUP_PERIOD_S = 30; // reservation cleanup every 30 seconds | ||
|
|
||
| bool init(CephContext* cct, rgw::sal::RadosStore* store, | ||
| const SiteConfig& site, const DoutPrefixProvider *dpp) { | ||
| bool init(const DoutPrefixProvider* dpp, rgw::sal::RadosStore* store, | ||
| const SiteConfig& site) { | ||
| if (s_manager) { | ||
| ldpp_dout(dpp, 1) << "ERROR: failed to init notification manager: already exists" << dendl; | ||
| return false; | ||
| } | ||
| if (!RGWPubSubEndpoint::init_all(dpp->get_cct())) { | ||
| return false; | ||
| } | ||
| // TODO: take conf from CephContext | ||
| s_manager = new Manager(cct, MAX_QUEUE_SIZE, | ||
| s_manager = std::make_unique<Manager>(dpp->get_cct(), MAX_QUEUE_SIZE, | ||
| Q_LIST_UPDATE_MSEC, Q_LIST_RETRY_MSEC, | ||
| IDLE_TIMEOUT_USEC, FAILOVER_TIME_MSEC, | ||
| STALE_RESERVATIONS_PERIOD_S, RESERVATIONS_CLEANUP_PERIOD_S, | ||
| WORKER_COUNT, | ||
| store, site); | ||
| s_manager->init(); | ||
| return true; | ||
| } | ||
|
|
||
| void shutdown() { | ||
| delete s_manager; | ||
| s_manager = nullptr; | ||
| if (!s_manager) return; | ||
| RGWPubSubEndpoint::shutdown_all(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this guaranteed to trigger cancellation of outstanding completions?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the endpoints are all stopped, there is nothing to invoke the callbacks for the completions.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if outstanding completions aren't canceled, i would expect separately, what happens if we call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
the "manager" pointers should be deleted and set to null. this should return an "-ESRCH" error. probably a race condition with the deletion of the pointer.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it looks like |
||
| s_manager->stop(); | ||
| s_manager.reset(); | ||
| } | ||
|
|
||
| int add_persistent_topic(const std::string& topic_name, optional_yield y) { | ||
|
|
@@ -842,7 +893,7 @@ int remove_persistent_topic(const std::string& topic_queue, optional_yield y) { | |
| if (!s_manager) { | ||
| return -EAGAIN; | ||
| } | ||
| return remove_persistent_topic(s_manager, s_manager->rados_store.getRados()->get_notif_pool_ctx(), topic_queue, y); | ||
| return remove_persistent_topic(s_manager.get(), s_manager->rados_store.getRados()->get_notif_pool_ctx(), topic_queue, y); | ||
| } | ||
|
|
||
| rgw::sal::Object* get_object_with_attributes( | ||
|
|
@@ -1203,8 +1254,7 @@ int publish_commit(rgw::sal::Object* obj, | |
| dpp->get_cct()); | ||
| ldpp_dout(res.dpp, 20) << "INFO: push endpoint created: " | ||
| << topic.cfg.dest.push_endpoint << dendl; | ||
| const auto ret = push_endpoint->send_to_completion_async( | ||
| dpp->get_cct(), event_entry.event, res.yield); | ||
| const auto ret = push_endpoint->send(event_entry.event, res.yield); | ||
| if (ret < 0) { | ||
| ldpp_dout(dpp, 1) | ||
| << "ERROR: failed to push sync notification event with error: " | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want the
unlock_queue()cleanup stuff to happen, you should avoid the call toio_context.stop(). that means that any suspended coroutines won't ever resume, so the worker threads would join immediatelyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cbodley if i'm not stopping the io_context, i can get a a crash if the async
unlock()call completes after the manager already destroyed.the other option, is to call the sync version of
unlock()(as i did before), but if we have many queues, this would be an issue.so, i guess that i would risk that sometimes the unlock is being called and sometimes it does not (i will eventually get the lock, once the lock timer expires).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you don't call
io_context::stop(), then any threads callingio_context.run()would continue to run until all of their coroutines finish. that means the calls tothread::join()just below will block until that happens. so i don't think there's a risk of racing with Manager's destructionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i tried commenting out this
io_context.stop()line and the cleanup/shutdown mostly works:the only strange part is that
process_queues()raced to lock the queue again afterprocess_queue()unlocked/returnedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried testing locally without
io_context.stop()when more than one RGW exists:but the tests (
data_path_v2_test) got stuck in "migrating" state.with the above call the tests are running fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you tell which coroutine gets stuck on shutdown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trying with these steps:
i see both rgws crashing on Manager shutdown:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's because
queue_gc_lock(andqueue_gc) are variables on the stack ofprocess_queues(), which can return before theprocess_queue()coroutines returnon shutdown, maybe
process_queues()should keep looping untilowned_queuesis empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this gets too complicated, we might just revert the shutdown/unlock changes and leave io_context.stop() there for now