From ef3b39361a4e6e3893af8b3d43dac0afa3b65dad Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 26 Sep 2018 19:16:09 +0000 Subject: [PATCH] Cleaning up mercury. --- src/lib/mercury/actors/ActorBroker.h | 8 ++----- src/lib/mercury/broker/BrokerProxy.cpp | 2 +- src/lib/mercury/broker/BrokerProxy.h | 17 ++++++------- src/lib/mercury/executor/Executor.cpp | 10 +++++--- src/lib/mercury/executor/Executor.h | 5 ++-- src/lib/orderman/actors/ServiceBridge.cpp | 10 ++++---- .../orderman/server/OrderServiceServer.cpp | 24 ++++++++++--------- 7 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/lib/mercury/actors/ActorBroker.h b/src/lib/mercury/actors/ActorBroker.h index 302e32709..c49e42f8f 100644 --- a/src/lib/mercury/actors/ActorBroker.h +++ b/src/lib/mercury/actors/ActorBroker.h @@ -9,7 +9,7 @@ namespace mercury { class ActorBroker { public: - ActorBroker (ILocalBroker &real_broker) : _proxy(real_broker),_proxy_id(_proxy.id()),_current_suid(0) {} + ActorBroker (BrokerProxy &proxy) : _proxy(proxy),_proxy_id(_proxy.id()),_current_suid(0) {} BrokerID id() {return _proxy_id;} @@ -55,16 +55,12 @@ public: _proxy.send_notify(_proxy_id, stream.sid, SubID(0), *msg); } - void do_work() { - _proxy.do_work(); - } - void schedule_action (std::function const&action) { _proxy.schedule_action(action); } private: - BrokerProxy _proxy; + BrokerProxy &_proxy; BrokerID _proxy_id; LocalSubID _current_suid; }; diff --git a/src/lib/mercury/broker/BrokerProxy.cpp b/src/lib/mercury/broker/BrokerProxy.cpp index ba32b94a0..cdec1a863 100644 --- a/src/lib/mercury/broker/BrokerProxy.cpp +++ b/src/lib/mercury/broker/BrokerProxy.cpp @@ -82,7 +82,7 @@ void BrokerProxy::local_subscribe(LocalSubID lsid, TopicFilter const &tf, void BrokerProxy::local_unsubscribe(LocalSubID lsid) { auto m = _action_pool.construct(); - m->msg_type = BrokerMsgType::LOCAL_SUBSCRIBE; + m->msg_type = BrokerMsgType::LOCAL_UNSUBSCRIBE; m->local_sub_id = lsid; send(*m); } diff --git a/src/lib/mercury/broker/BrokerProxy.h b/src/lib/mercury/broker/BrokerProxy.h index 8ee0106e0..8ff35a792 100644 --- a/src/lib/mercury/broker/BrokerProxy.h +++ b/src/lib/mercury/broker/BrokerProxy.h @@ -12,7 +12,7 @@ namespace bts { namespace mercury { class BrokerProxy : public ILocalBroker { -protected: + protected: friend class ActorBroker; enum class BrokerMsgType { ADD_NEIGHBOUR, @@ -61,19 +61,16 @@ protected: void schedule_action(std::function const &action); - - // these are just for performance, we want to avoid to allow for + // these are just for performance, we want to allow for // notifications to be initialized in place but this class does not // know how to do that, so we need to expose the notification // member to the outside. - void alloc_notify(BrokerMsg **msg, Notification **n) - { + void alloc_notify(BrokerMsg **msg, Notification **n) { auto m = _action_pool.construct(); *msg = m; *n = &m->notification; } - void send_notify(BrokerID bid, StreamID sid, SubID suid, BrokerMsg &m) - { + void send_notify(BrokerID bid, StreamID sid, SubID suid, BrokerMsg &m) { m.msg_type = BrokerMsgType::NOTIFY; m.broker_id = bid; m.stream_id = sid; @@ -82,7 +79,7 @@ protected: } void do_work() { - _outgoing.consume_all([this](BrokerMsg *m) { + _outgoing.consume_all([this](BrokerMsg *m) { switch (m->msg_type) { case BrokerMsgType::ADD_NEIGHBOUR: _real_broker.add_neighbour(*m->neighbour); @@ -104,11 +101,11 @@ protected: break; case BrokerMsgType::NOTIFY: _real_broker.notify(m->broker_id, m->stream_id, m->sub_id, - m->notification); + m->notification); break; case BrokerMsgType::LOCAL_SUBSCRIBE: _real_broker.local_subscribe(m->local_sub_id, m->filter, - m->local_handler); + m->local_handler); break; case BrokerMsgType::LOCAL_UNSUBSCRIBE: _real_broker.local_unsubscribe(m->local_sub_id); diff --git a/src/lib/mercury/executor/Executor.cpp b/src/lib/mercury/executor/Executor.cpp index a2e6fe50e..9199a6942 100644 --- a/src/lib/mercury/executor/Executor.cpp +++ b/src/lib/mercury/executor/Executor.cpp @@ -7,7 +7,11 @@ namespace bts { namespace mercury { -Executor::Executor() : _broker(), _actor_broker(_broker),_actors() {} +Executor::Executor() : + _broker(), + _proxies(), + _actor_broker(make_proxy()), + _actors() {} Executor::~Executor() { for (auto &a : _actors) @@ -36,8 +40,8 @@ void Executor::activate(Actor *actor) BrokerProxy& Executor::make_proxy() { - _neighbour_proxies.push_back(std::make_unique(_broker)); - return *_neighbour_proxies.back(); + _proxies.push_back(std::make_unique(_broker)); + return *_proxies.back(); } } // namespace mercury diff --git a/src/lib/mercury/executor/Executor.h b/src/lib/mercury/executor/Executor.h index a5de258ee..80b397399 100644 --- a/src/lib/mercury/executor/Executor.h +++ b/src/lib/mercury/executor/Executor.h @@ -23,10 +23,9 @@ class Executor { } void do_work() { - _actor_broker.do_work(); for (auto p : _pollable_actors) p->on_poll(); - for (auto &bp : _neighbour_proxies) + for (auto &bp : _proxies) bp->do_work(); } @@ -35,9 +34,9 @@ class Executor { private: Broker _broker; + std::vector> _proxies; ActorBroker _actor_broker; std::vector _pollable_actors; - std::vector> _neighbour_proxies; std::vector> _actors; }; } // namespace mercury diff --git a/src/lib/orderman/actors/ServiceBridge.cpp b/src/lib/orderman/actors/ServiceBridge.cpp index 73f0ffa4c..0d8309ec6 100644 --- a/src/lib/orderman/actors/ServiceBridge.cpp +++ b/src/lib/orderman/actors/ServiceBridge.cpp @@ -137,7 +137,7 @@ ServiceBridge::create_order(api::orderman::Create const &c) { std::promise p; auto f = p.get_future(); - schedule_action([this, &c, &p]() { + me->service_call_marshaller.schedule_action([this, &c, &p]() { api::orderman::Response r; if (is_ready()) { @@ -163,7 +163,7 @@ ServiceBridge::update_order(api::orderman::Update const &c) { std::promise p; auto f = p.get_future(); - schedule_action([this, &c, &p]() { + me->service_call_marshaller.schedule_action([this, &c, &p]() { api::orderman::Response r; if (is_ready()) { @@ -190,7 +190,7 @@ ServiceBridge::cancel_order(api::orderman::Cancel const &c) { std::promise p; auto f = p.get_future(); - schedule_action([this, &c, &p]() { + me->service_call_marshaller.schedule_action([this, &c, &p]() { api::orderman::Response r; if (is_ready()) { @@ -213,7 +213,7 @@ ServiceBridge::crack_order(api::orderman::Crack const &c) { std::promise p; auto f = p.get_future(); - schedule_action([this, &c, &p]() { + me->service_call_marshaller.schedule_action([this, &c, &p]() { api::orderman::Response r; if (is_ready()) { @@ -233,7 +233,7 @@ ServiceBridge::crack_order(api::orderman::Crack const &c) { void ServiceBridge::update_admin_data( std::shared_ptr const &admin) { - schedule_action([this, admin]() { + me->service_call_marshaller.schedule_action([this, admin]() { me->admin_data_updates.push_back(admin); publish(me->new_admin_data_pub, *admin); }); diff --git a/src/lib/orderman/server/OrderServiceServer.cpp b/src/lib/orderman/server/OrderServiceServer.cpp index 6cd021f25..ec5e9f4b3 100644 --- a/src/lib/orderman/server/OrderServiceServer.cpp +++ b/src/lib/orderman/server/OrderServiceServer.cpp @@ -125,17 +125,6 @@ OrderServiceServer::OrderServiceServer( me->publish_orders = make_orders_publisher(); me->publish_fills = make_fills_publisher(); - me->service_bridge = new ServiceBridge( - [this]() { me->service_ready = true; }, - - [this, &ctx](auto const &orders) { - ctx.post([this, orders] { this->on_orders(orders); }); - }, - - [this, &ctx](auto const &fill) { - ctx.post([this, fill] { this->on_fills(fill); }); - }); - std::vector all_executors; for (auto const &thread_cfg : config::get().thread()) { auto at = std::make_unique(thread_cfg,stats); @@ -201,6 +190,19 @@ OrderServiceServer::OrderServiceServer( thread = find_thread_for_actor("ServiceBridge", me->actor_threads); bts_assert_msg(thread, "Could not find a configured thread for ServiceBridge. See " "the OrdermanCfg key"); + me->service_bridge = new ServiceBridge( + [this]() { me->service_ready = true; }, + + [this, &ctx](auto const &orders) { + ctx.post([this, orders] { this->on_orders(orders); }); + }, + + [this, &ctx](auto const &fill) { + ctx.post([this, fill] { this->on_fills(fill); }); + }, + thread->executor.make_proxy() + ); + thread->executor.activate(me->service_bridge); // spin up threads