diff --git a/docker/linux b/docker/linux index d4a32d325..26e293ebd 160000 --- a/docker/linux +++ b/docker/linux @@ -1 +1 @@ -Subproject commit d4a32d32592c688c3a1f042750fd3323bcdfd38a +Subproject commit 26e293ebd49519ddba4da773431cb1f260045e12 diff --git a/src/server/sparkd/mercury/actors/ContentSubscription.h b/src/server/sparkd/mercury/actors/ContentSubscription.h index 30f795db5..c8427e721 100644 --- a/src/server/sparkd/mercury/actors/ContentSubscription.h +++ b/src/server/sparkd/mercury/actors/ContentSubscription.h @@ -1,25 +1,21 @@ #pragma once +#include "server/sparkd/mercury/actors/Actor.h" +#include #include #include -#include -#include "server/sparkd/mercury/actors/Actor.h" #include "lib/utils/bts_assert.h" namespace bts { namespace mercury { -struct KeyExtractor -{ - template - auto operator()(const EV&ev) {return ev.key();} +struct KeyExtractor { + template auto operator()(const EV &ev) { return ev.key(); } }; -struct NoKey -{ - template - auto operator()(const EV&) {return 0;} +struct NoKey { + template auto operator()(const EV &) { return 0; } }; // a simple helper class used to create light-weight, i.e. efficient, @@ -28,88 +24,102 @@ struct NoKey // interest has a key() member returning a hashable type which can be // used to select relevant event instances. No support for synopsis // (late join) or reentrancy (unsub while handling an update). -template -class ContentSubscription -{ -public: - using Key = decltype( (*((KE *) nullptr)) (*((EV*) nullptr))); - using CB = std::function; +template class ContentSubscription { + public: + using Key = decltype((*((KE *)nullptr))(*((EV *)nullptr))); + using CB = std::function; struct SubID { - Key k;uint32_t sid; - bool operator==(SubID const&rhs) const {return k == rhs.k && sid == rhs.sid;} + Key k; + uint32_t sid; + bool operator==(SubID const &rhs) const { + return k == rhs.k && sid == rhs.sid; + } }; - - ContentSubscription(Actor &actor) : - _actor(&actor), - _sub (_actor->subscribe([this] (auto const&u) {this->on_update(u);})), - _my_subs(), - _dispatching(false), - _current_sub_id(0) - {} + + ContentSubscription(Actor &actor) + : _actor(&actor), _sub(_actor->subscribe( + [this](auto const &u) { this->on_update(u); })), + _my_subs(), _dispatching(false), _current_sub_id(0), + _pre_sub([](auto) {}) {} + + ContentSubscription(Actor &actor, std::function const &pscb) + : _actor(&actor), _sub(_actor->subscribe( + [this](auto const &u) { this->on_update(u); })), + _my_subs(), _dispatching(false), _current_sub_id(0), _pre_sub(pscb) {} template - ContentSubscription(Actor &actor,Tag const&tag): - _actor(&actor), - _sub (_actor->subscribe(tag,[this] (auto const&u) {this->on_update(u);})), - _my_subs(), - _dispatching(false), - _current_sub_id(0) - {} + ContentSubscription(Actor &actor, Tag const &tag) + : _actor(&actor), + _sub(_actor->subscribe( + tag, [this](auto const &u) { this->on_update(u); })), + _my_subs(), _dispatching(false), _current_sub_id(0), + _pre_sub([](auto) {}) {} - template - SubID sub(Key k,CBT const&cb) { + template + ContentSubscription(Actor &actor, Tag const &tag, + std::function const &pscb) + : _actor(&actor), + _sub(_actor->subscribe( + tag, [this](auto const &u) { this->on_update(u); })), + _my_subs(), _dispatching(false), _current_sub_id(0), + _pre_sub(pscb) {} + + template SubID sub(Key k, CBT const &cb) { bts_assert(!_dispatching); - auto sid = SubID {k,_current_sub_id++}; + _pre_sub(k); + auto sid = SubID{k, _current_sub_id++}; auto &k_subs = _my_subs[k]; - k_subs.emplace_back(Subscriber {sid,CB (cb)}); + k_subs.emplace_back(Subscriber{sid, CB(cb)}); return sid; } - template - SubID sub(CBT &&cb) {return sub (0,std::forward(cb));} + template SubID sub(CBT &&cb) { + return sub(0, std::forward(cb)); + } - void unsub(SubID sid) - { + void unsub(SubID sid) { bts_assert(!_dispatching); auto &subs = _my_subs[sid.k]; - auto it = std::find_if(subs.begin(),subs.end(), [sid](auto const&subscriber) { - return subscriber.id == sid; - }); - bts_assert (it != subs.end()); + auto it = + std::find_if(subs.begin(), subs.end(), [sid](auto const &subscriber) { + return subscriber.id == sid; + }); + bts_assert(it != subs.end()); subs.erase(it); } - + ~ContentSubscription() { bts_assert(!_dispatching); _actor->unsubscribe(_sub); } -private: - struct Subscriber { SubID id; CB cb; }; - + private: + struct Subscriber { + SubID id; + CB cb; + }; + using SubList = std::vector; - using Subscriptions = std::unordered_map; - + using Subscriptions = std::unordered_map; + Actor *_actor; Actor::SubHandle _sub; Subscriptions _my_subs; bool _dispatching; uint32_t _current_sub_id; - - void on_update(Update const&u) - { + std::function _pre_sub; + + void on_update(Update const &u) { KE ke; _dispatching = true; - if (u.events) + if (u.events) for (auto const &ev : *(u.events)) { auto k = ke(ev); for (auto const &sub : _my_subs[k]) sub.cb(ev); } _dispatching = false; - } - + } }; - -}} - +} +} diff --git a/src/server/sparkd/orderman/actors/MDSource.h b/src/server/sparkd/orderman/actors/MDSource.h index 1be019653..7c252a426 100644 --- a/src/server/sparkd/orderman/actors/MDSource.h +++ b/src/server/sparkd/orderman/actors/MDSource.h @@ -5,10 +5,10 @@ #include "lib/md/MDRecoverableConsumer.h" #include "lib/utils/TimeStamp.h" #include "server/sparkd/mercury/actors/Actor.h" +#include "server/sparkd/mercury/events/TimerEvent.h" #include "server/sparkd/mercury/mercury_utils/IPollableActor.h" #include "server/sparkd/orderman/events/BookUpdateEvent.h" #include "server/sparkd/orderman/events/MDRequestEvent.h" -#include "server/sparkd/mercury/events/TimerEvent.h" namespace bts { namespace orderman { @@ -21,8 +21,9 @@ namespace orderman { class MDSource : public mercury::Actor, public mercury::IPollableActor { public: void init() override { - _book_update = make_stream(); + LOG(OrderMan, Warn, "Starting MDSource actor"); + _book_update = make_stream(); subscribe([this](auto const &update) { if (update.events) for (auto const &ev : *update.events) { @@ -36,8 +37,8 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { return; } - LOG(OrderMan,Warn, - "Starting to publish book updates for security key: {}",k); + LOG(OrderMan, Warn, + "Starting to publish book updates for security key: {}", k); auto managed_handle = _md_consumer.get_managed_handle(k); _keep_alive.push_back(managed_handle); _handles.push_back({utils::TimeStamp(), managed_handle.get()}); diff --git a/src/server/sparkd/orderman/actors/order_manager/OrderManager.cpp b/src/server/sparkd/orderman/actors/order_manager/OrderManager.cpp index 10608f439..c26e67cec 100644 --- a/src/server/sparkd/orderman/actors/order_manager/OrderManager.cpp +++ b/src/server/sparkd/orderman/actors/order_manager/OrderManager.cpp @@ -275,34 +275,6 @@ void OrderManager::init_order_services() { subscribe( me->gw_name, [this](auto const &ev) { this->on_new_fix_buffer(ev); }); - // Subscription setup/teardown is a relatively heavy - // operation. For that reason we use coarse-grained topics to - // which we subscribe only once at startup. Individual orders - // can impose more fine-grained and light weight filtering on - // top of those topic subscriptions through the - // ContentSubscription helper class. - me->services_data.md_subs = - std::make_unique>(*this); - me->services_data.child_order_subs = - std::make_unique>( - *this); - me->services_data.md_subs = - std::make_unique>(*this); - me->services_data.exe_report_subs = - std::make_unique>( - *this, me->gw_name); - me->services_data.cancel_reject_subs = - std::make_unique>(*this, - me->gw_name); - me->services_data.gateway_status_subs = - std::make_unique>( - *this); - me->services_data.time_subs = - std::make_unique>(*this); - me->services_data.order_creation_subs = - std::make_unique>( - *this, me->gw_name); - // Outputs me->services_data.order_state_pub = make_stream(); me->services_data.out_fix_pub = make_stream(me->gw_name); @@ -322,6 +294,40 @@ void OrderManager::init_order_services() { me->services_data.report_order_state_pub = make_stream(); me->pub_ready = make_stream(me->gw_name); + + // Subscription setup/teardown is a relatively heavy + // operation. For that reason we use coarse-grained topics to + // which we subscribe only once at startup. Individual orders + // can impose more fine-grained and light weight filtering on + // top of those topic subscriptions through the + // ContentSubscription helper class. + me->services_data.md_subs = + std::make_unique>(*this); + me->services_data.child_order_subs = + std::make_unique>( + *this); + // md subs are peculiar in that we try to make them on demand, so + // we publish an event announcing the subscription + std::function on_md_sub = [this](SecurityKey k) { + publish(me->services_data.md_request_pub,k); + }; + me->services_data.md_subs = + std::make_unique>(*this,on_md_sub); + + me->services_data.exe_report_subs = + std::make_unique>( + *this, me->gw_name); + me->services_data.cancel_reject_subs = + std::make_unique>(*this, + me->gw_name); + me->services_data.gateway_status_subs = + std::make_unique>( + *this); + me->services_data.time_subs = + std::make_unique>(*this); + me->services_data.order_creation_subs = + std::make_unique>( + *this, me->gw_name); } } } diff --git a/src/server/sparkd/orderman/events/MDRequestEvent.h b/src/server/sparkd/orderman/events/MDRequestEvent.h index 562478033..2b5a9e973 100644 --- a/src/server/sparkd/orderman/events/MDRequestEvent.h +++ b/src/server/sparkd/orderman/events/MDRequestEvent.h @@ -11,7 +11,14 @@ namespace orderman { class MDRequestEvent : public mercury::Event { public: using SynopsisPolicy = mercury::CacheAll; + MDRequestEvent(SecurityKey k) : security_key(k) {} + MDRequestEvent &operator=(MDRequestEvent const&rhs) + { + security_key = rhs.security_key; + return *this; + } SecurityKey security_key; }; }} +