diff --git a/src/server/sparkd/orderman2/actors/OrderManager.cpp b/src/server/sparkd/orderman2/actors/OrderManager.cpp index 0ab0d4024..7b628c3de 100644 --- a/src/server/sparkd/orderman2/actors/OrderManager.cpp +++ b/src/server/sparkd/orderman2/actors/OrderManager.cpp @@ -1,110 +1,111 @@ -// #include "OrderManager.h" -// #include "server/sparkd/orderman2/order_factory/OrderFactory.h" -// #include "server/sparkd/securities/ISecurityDB.h" -// #include "server/sparkd/securities/MapSecurityDB.h" -// #include "server/sparkd/orderman2/events/NewSecuritiesEvent.h" -// #include "server/sparkd/orderman2/events/StoredOrderEvent.h" -// #include "server/sparkd/orderman2/events/ActivateOrderEvent.h" -// #include "server/sparkd/orderman2/events/UpdateOrderEvent.h" -// #include "server/sparkd/orderman2/events/NewExecutionReportEvent.h" +#include "OrderManager.h" +#include "server/sparkd/orderman2/order_factory/OrderFactory.h" +#include "server/sparkd/securities/ISecurityDB.h" +#include "server/sparkd/securities/MapSecurityDB.h" +#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h" +#include "server/sparkd/orderman2/events/StoredOrderEvent.h" +#include "server/sparkd/orderman2/events/ActivateOrderEvent.h" +#include "server/sparkd/orderman2/events/UpdateOrderEvent.h" +#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h" +#include "server/sparkd/orderman2/orders/Order.h" -// #include +#include -// namespace bts { -// namespace orderman2 { +namespace bts { +namespace orderman2 { -// enum -// class OrderManager::State {WAIT_SEC, WAIT_ORDER_RECOVERY, READY } ; +enum +class OrderManager::State {WAIT_SEC, WAIT_ORDERS, READY } ; -// struct OrderManager::PImpl -// { -// PImpl(config::JetFIX::Connection const &cfg) : -// state (State::WAIT_SEC), -// sec_db(), -// orders(), -// order_factory(sec_db), -// stored_order_sub(), -// cfg(cfg), -// gw_name(GatewayName(cfg.name())) -// {} +struct OrderManager::PImpl +{ + PImpl(config::JetFIX::Connection const &cfg) : + state (State::WAIT_SEC), + sec_db(), + orders(), + order_factory(sec_db), + stored_order_sub(), + cfg(cfg), + gw_name(GatewayName(cfg.name())) + {} -// State state; -// securities::MapSecurityDB sec_db; -// std::unordered_map orders; -// OrderFactory order_factory; -// mercury::Actor::SubHandle stored_order_sub; -// const config::JetFIX::Connection cfg; -// const GatewayName gw_name; -// }; + State state; + securities::MapSecurityDB sec_db; + std::unordered_map orders; + OrderFactory order_factory; + mercury::Actor::SubHandle stored_order_sub; + const config::JetFIX::Connection cfg; + const GatewayName gw_name; +}; -// OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {} -// OrderManager::~OrderManager () {} +OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {} +OrderManager::~OrderManager () {} -// void OrderManager::init() -// { -// me->state = State::WAIT_SEC; -// subscribe([this] (auto const&sec_update) {this->on_securities(sec_update);}); -// } +void OrderManager::init() +{ + me->state = State::WAIT_SEC; + subscribe([this] (auto const&sec_update) {this->on_securities(sec_update);}); +} -// void OrderManager::on_securities(const mercury::Update &update) -// { -// if (update.event) { +void OrderManager::on_securities(const mercury::Update &update) +{ + if (update.events) { -// // update our security db -// for (auto sec_ptr : update.event->securities) -// me->sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr; + // update our security db + for (auto ev : *(update.events)) { + auto sec_ptr = ev.security; + me->sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr; + } -// if (me->state == State::WAIT_SEC) { - -// // obtain recovered orders -// me->state = State::WAIT_ORDER_RECOVERY; -// me->stored_order_sub = -// subscribe (me->gw_name, -// [this] (auto const&orders) -// {this->on_order_recovery(orders);}); -// } -// } -// } + if (me->state == State::WAIT_SEC) { + // obtain recovered orders + me->state = State::WAIT_ORDERS; + me->stored_order_sub = + subscribe (me->gw_name, + [this] (auto const&orders) + {this->on_order_recovery(orders);}); + } + } +} -// void OrderManager::on_order_recovery(const mercury::Update &update) -// { -// if (update.event) -// if (me->state == State::WAIT_ORDER_RECOVERY) { +void OrderManager::on_order_recovery(const mercury::Update &update) +{ + if (update.events) + if (me->state == State::WAIT_ORDERS) { -// // Got the first batch of persisted orders which has to match -// // the orders stored on disk -// for (auto &order_rec : update.event->orders) { -// auto order = me->order_factory.recover(order_rec); -// me->orders[order.key()] = order; -// order.pre_activate(me->sec_db, me->order_factory); -// order.activate(); -// } + // Got the first batch of persisted orders which has to match + // the orders stored on disk (since we are the only ones + // producing new orders) + for (auto const &ev : *(update.events)) { + auto &order = me->order_factory.recover(*ev.order_record); + me->orders[order.key()] = ℴ + order.pre_activate(me->sec_db, me->order_factory); + order.activate(); + } -// // don't need to monitor persisted orders anymore, all the -// // orders we care about will be pass through this class -// unsubscribe(me->stored_order_sub); + // Don't need to monitor persisted orders anymore, all the + // orders we care about will be generated by us. + unsubscribe(me->stored_order_sub); -// // Now we are ready to subscribe to the execution gateway -// // events. Since we know all the orders that were stored in -// // disk we will be able to match any execution reports we -// // might get to persisted orders. Had we subscribed to -// // gatewat events earlier we wouldn't be able to tell wheter -// // an order was already stored in disk or not when we -// // received an execution report for it. Given that we know -// // for sure that we have all the persisted orders, anything -// // we can't match will signify that we crashed after we sent -// // an order to the gateway but before we were able to persist -// // it. In that case we will do our best to recover the order -// // from the execution report. -// subscribe (me->gw_name, -// [this] (auto const&ev) { -// on_execution_report(ev); -// }); -// } -// } + // Now we are ready to subscribe to the execution gateway + // events. Since we know all the orders that were stored in + // disk we will be able to match any execution reports we + // might get to persisted orders. Had we subscribed to + // gatewat events earlier we would not be able to tell wheter + // an order was already stored in disk or not when we + // received an execution report for it. Given that we know + // for sure that we have all the persisted orders, anything + // we can't match will signify that we crashed after we sent + // an order to the gateway but before we were able to persist + // it. In that case we will do our best to recover the order + // from the execution report. + subscribe (me->gw_name, + [this] (auto const&ev) {on_execution_report(ev);}); + } +} -// void OrderManager::on_execution_report(const mercury::Update &update) -// { -// } +void OrderManager::on_execution_report(const mercury::Update &) +{ +} -// }} +}} diff --git a/src/server/sparkd/orderman2/actors/OrderManager.h b/src/server/sparkd/orderman2/actors/OrderManager.h index 6b529336a..6ea3417e3 100644 --- a/src/server/sparkd/orderman2/actors/OrderManager.h +++ b/src/server/sparkd/orderman2/actors/OrderManager.h @@ -18,8 +18,12 @@ namespace orderman2 { // - a pool of fix buffers (OutFIXPtr) // - an order factory (OrderFactory) // - a security database (SecurityDB) -// - an actor base class, which allows orders to publish and subscribe events - +// - subscriptions to interesting events such as: +// - execution gateway messages (execution reports, rejects, etc) +// - market data book updates +// - security creation events +// - timer updates +// - order state changes class OrderManager : public mercury::Actor { public: diff --git a/src/server/sparkd/orderman2/doc/order_man_notes.org b/src/server/sparkd/orderman2/doc/order_man_notes.org index 5047ac37d..40d314715 100644 --- a/src/server/sparkd/orderman2/doc/order_man_notes.org +++ b/src/server/sparkd/orderman2/doc/order_man_notes.org @@ -296,6 +296,11 @@ ExpireDate = 20161102 432=20161102 CheckSum = 151 10=151 * Open Issues +- We need to decouple OM from fix gateway, so that we can have a slow + OM and a fast OM going against the same gateway +- Once we have multiple OM's we will need to guarantee that they can + see each others order, which means we need to use some sort of + synopsis or other mechanism to ensure that they are synchronized - Self match protection - fill mitigation - should cancel on disconnect be on diff --git a/src/server/sparkd/orderman2/events/ActivateOrderEvent.h b/src/server/sparkd/orderman2/events/ActivateOrderEvent.h index 8c2cbea19..be07a2249 100644 --- a/src/server/sparkd/orderman2/events/ActivateOrderEvent.h +++ b/src/server/sparkd/orderman2/events/ActivateOrderEvent.h @@ -8,11 +8,13 @@ namespace bts { namespace orderman2 { -class ActivateOrderEvent : public mercury::Event +class ActivateOrderEvent : public mercury::Event { public: using Tag = GatewayName; - ActivateOrderEvent (Order &o) : order (&i(o)) {} + using SynopsysPolicy = mercury::NoSynopsis; + + ActivateOrderEvent (Order &o) : order (&o) {} Order* order; }; diff --git a/src/server/sparkd/orderman2/events/GatewayStatusChangeEvent.h b/src/server/sparkd/orderman2/events/GatewayStatusChangeEvent.h index 164a974af..568e4fc2e 100644 --- a/src/server/sparkd/orderman2/events/GatewayStatusChangeEvent.h +++ b/src/server/sparkd/orderman2/events/GatewayStatusChangeEvent.h @@ -11,6 +11,7 @@ class GatewayStatusChangeEvent : public mercury::Event public: using Tag = GatewayName; using SynopsisPolicy = mercury::CacheLast; + GatewayStatusChangeEvent(bool c) : is_connected(c) {} bool is_connected; }; diff --git a/src/server/sparkd/orderman2/events/StoredOrderEvent.h b/src/server/sparkd/orderman2/events/StoredOrderEvent.h index 9f6e51e2e..7c82d68a9 100644 --- a/src/server/sparkd/orderman2/events/StoredOrderEvent.h +++ b/src/server/sparkd/orderman2/events/StoredOrderEvent.h @@ -2,20 +2,21 @@ #include #include -#include "server/sparkd/mercury/synopsys/AccumulateVector.h" +#include "lib/api/services/OrderService2.pb.h" +#include "server/sparkd/mercury/synopsys/MergeByKey.h" #include "lib/api/services/OrderService2.pb.h" namespace bts { namespace orderman2 { -class StoredOrderEvent : public mercury::Event +class StoredOrderEvent : public mercury::Event { public: using Tag = GatewayName; - using SynopsysBuilder = mercury::AccumulateVector; - - boost::container::small_vector orders; - boost::container::small_vector_base &vector() {return orders;} + using SynopsysPolicy = mercury::MergeByKey; + + auto key() {return order_record->key();} + api::orderman2::OrderRecord *order_record; }; }} diff --git a/src/server/sparkd/orderman2/events/UpdateOrderEvent.h b/src/server/sparkd/orderman2/events/UpdateOrderEvent.h index 1a8fb72ab..cb6285f24 100644 --- a/src/server/sparkd/orderman2/events/UpdateOrderEvent.h +++ b/src/server/sparkd/orderman2/events/UpdateOrderEvent.h @@ -8,15 +8,14 @@ namespace bts { namespace orderman2 { -class UpdateOrderEvent : public mercury::Event +class UpdateOrderEvent : public mercury::Event { public: using Tag = GatewayName; - + using SynopsysPolicy = mercury::NoSynopsis; + UpdateOrderEvent (Order &o, Price p, Quantity q) : - order (&i(o)), - price(p), - quantity(q) + order (&o), price(p), quantity(q) {} Order* order; diff --git a/src/server/sparkd/orderman2/orders/Order.h b/src/server/sparkd/orderman2/orders/Order.h index 853a5ec0d..c11a709ea 100644 --- a/src/server/sparkd/orderman2/orders/Order.h +++ b/src/server/sparkd/orderman2/orders/Order.h @@ -6,7 +6,7 @@ // fwd namespace bts { namespace securities {class ISecurityDB;} -namespace orderman2 {class ISecurityDB; class IOrderFactory;} +namespace orderman2 {class IOrderFactory;} namespace api { namespace orderman2 { class OrderDefinition; @@ -17,8 +17,6 @@ class OrderState; namespace bts { namespace orderman2 { -class ISecurityDB; - class Order { public: @@ -39,10 +37,8 @@ public: private: friend class OrderFactory; friend class OrderManager; - void pre_init(OrderKey , OrderKey , - securities::ISecurityDB &, - IOrderFactory &) {}; - void pre_activate(ISecurityDB &, IOrderFactory &) {}; + void pre_init(OrderKey , OrderKey , securities::ISecurityDB &, IOrderFactory &) {}; + void pre_activate(securities::ISecurityDB &, IOrderFactory &) {}; struct PImpl; std::unique_ptr me;