Fixed MD subscription service in OrderServices.

This commit is contained in:
Pedro Pinto committed 2018-02-11 16:30:12 -05:00
1 parent 1d2a21a636
commit db47b28cbd
5 files changed
+117 -93

No files matched your search

@@ -1,25 +1,21 @@
#pragma once
#include "server/sparkd/mercury/actors/Actor.h"
#include <functional>
#include <memory>
#include <unordered_map>
#include <functional>
#include "server/sparkd/mercury/actors/Actor.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace mercury {
struct KeyExtractor
{
template <typename EV>
auto operator()(const EV&ev) {return ev.key();}
struct KeyExtractor {
template <typename EV> auto operator()(const EV &ev) { return ev.key(); }
};
struct NoKey
{
template <class EV>
auto operator()(const EV&) {return 0;}
struct NoKey {
template <class EV> 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 <typename EV,typename KE = KeyExtractor>
class ContentSubscription
{
public:
using Key = decltype( (*((KE *) nullptr)) (*((EV*) nullptr)));
using CB = std::function<void(const EV&)>;
template <typename EV, typename KE = KeyExtractor> class ContentSubscription {
public:
using Key = decltype((*((KE *)nullptr))(*((EV *)nullptr)));
using CB = std::function<void(const EV &)>;
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<EV>([this] (auto const&u) {this->on_update(u);})),
_my_subs(),
_dispatching(false),
_current_sub_id(0)
{}
ContentSubscription(Actor &actor)
: _actor(&actor), _sub(_actor->subscribe<EV>(
[this](auto const &u) { this->on_update(u); })),
_my_subs(), _dispatching(false), _current_sub_id(0),
_pre_sub([](auto) {}) {}
ContentSubscription(Actor &actor, std::function<void(Key)> const &pscb)
: _actor(&actor), _sub(_actor->subscribe<EV>(
[this](auto const &u) { this->on_update(u); })),
_my_subs(), _dispatching(false), _current_sub_id(0), _pre_sub(pscb) {}
template <class Tag>
ContentSubscription(Actor &actor,Tag const&tag):
_actor(&actor),
_sub (_actor->subscribe<EV>(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<EV>(
tag, [this](auto const &u) { this->on_update(u); })),
_my_subs(), _dispatching(false), _current_sub_id(0),
_pre_sub([](auto) {}) {}
template <class CBT>
SubID sub(Key k,CBT const&cb) {
template <class Tag>
ContentSubscription(Actor &actor, Tag const &tag,
std::function<void(Key)> const &pscb)
: _actor(&actor),
_sub(_actor->subscribe<EV>(
tag, [this](auto const &u) { this->on_update(u); })),
_my_subs(), _dispatching(false), _current_sub_id(0),
_pre_sub(pscb) {}
template <class CBT> 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 <class CBT>
SubID sub(CBT &&cb) {return sub (0,std::forward<CBT>(cb));}
template <class CBT> SubID sub(CBT &&cb) {
return sub(0, std::forward<CBT>(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<Subscriber>;
using Subscriptions = std::unordered_map<Key,SubList>;
using Subscriptions = std::unordered_map<Key, SubList>;
Actor *_actor;
Actor::SubHandle _sub;
Subscriptions _my_subs;
bool _dispatching;
uint32_t _current_sub_id;
void on_update(Update<EV> const&u)
{
std::function<void(Key k)> _pre_sub;
void on_update(Update<EV> 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;
}
}
};
}}
}
}
+5 -4
View File
@@ -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<BookUpdateEvent>();
LOG(OrderMan, Warn, "Starting MDSource actor");
_book_update = make_stream<BookUpdateEvent>();
subscribe<MDRequestEvent>([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()});
@@ -275,34 +275,6 @@ void OrderManager::init_order_services() {
subscribe<CreateFIXBufferReplyEvent>(
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<ContentSubscription<BookUpdateEvent>>(*this);
me->services_data.child_order_subs =
std::make_unique<ContentSubscription<OrderStateUpdateEvent, ParentKey>>(
*this);
me->services_data.md_subs =
std::make_unique<ContentSubscription<BookUpdateEvent>>(*this);
me->services_data.exe_report_subs =
std::make_unique<ContentSubscription<NewExecutionReportEvent>>(
*this, me->gw_name);
me->services_data.cancel_reject_subs =
std::make_unique<ContentSubscription<CancelRejectEvent>>(*this,
me->gw_name);
me->services_data.gateway_status_subs =
std::make_unique<ContentSubscription<GatewayStatusChangeEvent, NoKey>>(
*this);
me->services_data.time_subs =
std::make_unique<ContentSubscription<TimerEvent, NoKey>>(*this);
me->services_data.order_creation_subs =
std::make_unique<ContentSubscription<CreateOrderReplyEvent>>(
*this, me->gw_name);
// Outputs
me->services_data.order_state_pub = make_stream<OrderStateUpdateEvent>();
me->services_data.out_fix_pub = make_stream<OutFIXMsgEvent>(me->gw_name);
@@ -322,6 +294,40 @@ void OrderManager::init_order_services() {
me->services_data.report_order_state_pub =
make_stream<OrderStateUpdateEvent>();
me->pub_ready = make_stream<OrderManagerReadyEvent>(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<ContentSubscription<BookUpdateEvent>>(*this);
me->services_data.child_order_subs =
std::make_unique<ContentSubscription<OrderStateUpdateEvent, ParentKey>>(
*this);
// md subs are peculiar in that we try to make them on demand, so
// we publish an event announcing the subscription
std::function<void(SecurityKey)> on_md_sub = [this](SecurityKey k) {
publish(me->services_data.md_request_pub,k);
};
me->services_data.md_subs =
std::make_unique<ContentSubscription<BookUpdateEvent>>(*this,on_md_sub);
me->services_data.exe_report_subs =
std::make_unique<ContentSubscription<NewExecutionReportEvent>>(
*this, me->gw_name);
me->services_data.cancel_reject_subs =
std::make_unique<ContentSubscription<CancelRejectEvent>>(*this,
me->gw_name);
me->services_data.gateway_status_subs =
std::make_unique<ContentSubscription<GatewayStatusChangeEvent, NoKey>>(
*this);
me->services_data.time_subs =
std::make_unique<ContentSubscription<TimerEvent, NoKey>>(*this);
me->services_data.order_creation_subs =
std::make_unique<ContentSubscription<CreateOrderReplyEvent>>(
*this, me->gw_name);
}
}
}
@@ -11,7 +11,14 @@ namespace orderman {
class MDRequestEvent : public mercury::Event<MDRequestEvent> {
public:
using SynopsisPolicy = mercury::CacheAll<MDRequestEvent>;
MDRequestEvent(SecurityKey k) : security_key(k) {}
MDRequestEvent &operator=(MDRequestEvent const&rhs)
{
security_key = rhs.security_key;
return *this;
}
SecurityKey security_key;
};
}}