From 20f42749dd1a8be2f2a9955732991e8cc5ff7573 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 31 Jan 2019 11:16:25 -0500 Subject: [PATCH] SPARK-1997 Update OrderServiceServer/FillStore to use SecurityOracle --- src/lib/orderman/actors/FillStore.cpp | 115 +++++++----------- src/lib/orderman/actors/FillStore.h | 3 +- src/lib/orderman/actors/SODPosition.cpp | 10 +- src/lib/orderman/actors/SODPosition.h | 3 +- .../orderman/server/OrderServiceServer.cpp | 15 +-- src/lib/orderman/server/OrderServiceServer.h | 3 +- src/server/sparkd/cmds/Run.cpp | 7 +- 7 files changed, 68 insertions(+), 88 deletions(-) diff --git a/src/lib/orderman/actors/FillStore.cpp b/src/lib/orderman/actors/FillStore.cpp index a7d2fab18..76f1c62a0 100644 --- a/src/lib/orderman/actors/FillStore.cpp +++ b/src/lib/orderman/actors/FillStore.cpp @@ -15,100 +15,71 @@ namespace orderman { auto const fills_ns = "fills"; -enum class FillStore::State { WAIT_SEC, READY }; - struct FillStore::UnresolvedFills { std::vector fills; api::Security const *security; }; struct FillStore::PImpl { - PImpl() - : state(State::WAIT_SEC), store(new persistence::SqliteStore("fills")), - known_orders(), fills_with_unresolved_accounts(), sod_pos_map() {} + PImpl(md::SecurityOracle& oracle) + : store(new persistence::SqliteStore("fills")), + known_orders(), fills_with_unresolved_accounts(), sod_pos_map(), oracle(oracle) {} - State state; std::unique_ptr store; std::unordered_set known_fills; std::unordered_map known_orders; std::unordered_map fills_with_unresolved_accounts; mercury::Stream fill_stream; - mercury::Actor::SubHandle stored_sec_sub; SODPosition sod_pos_map; + md::SecurityOracle oracle; }; -FillStore::FillStore() : me(new PImpl()) {} +FillStore::FillStore(md::SecurityOracle& o) : me(new PImpl(o)) {} void FillStore::init() { - me->state = State::WAIT_SEC; - me->stored_sec_sub = subscribe([this]( - auto const &update) { - if (update.events) { - if (me->state == State::WAIT_SEC) { + // Subscribe to NewExecutionReportEvent for new fills + subscribe( + [this](auto const &ev) { this->on_execution_report(ev); }); - LOG(OrderMan, Info, - "Fill store received initial list of {} securities", - update.events->size()); + // Load stored fills, resolve their securities and publish the lot + api::orderman::Fill fill; + std::vector stored_batch; + LOG(persist, Info, "Loading load all fills"); + auto all = me->store->retrieve_all()[fills_ns]; + LOG(persist, Info, "Finished loading all {} fills", all.size()); + for (auto const &kvp : all) + if (utils::pb::from_string(kvp.second, + &fill)) { - // don't need securities anymore, NewExecutionReportEvent - // events will carry the security with them - unsubscribe(me->stored_sec_sub); + auto sec = me->oracle.get_security(SecurityKey(fill.security_key())); + if (sec) { + auto fk = kvp.first; + if (!me->sod_pos_map.add_fill_to_sod_pos(fill)) + stored_batch.emplace_back(fill, *sec); + me->known_fills.insert(fk); + } else + LOG(persist, Error, + "Stored fill {} contains unresolved security {}. ", + kvp.first, fill.security_key()); + } else + LOG(persist, Error, + "Could not recover fill {} from storage. " + "This should be investigated", + kvp.first); + me->fill_stream = make_stream(); + LOG(OrderMan, Info, + "Fill Store published {} fills recoved from disk out of {} " + "total fills", + stored_batch.size(), all.size()); + me->sod_pos_map.add_sod_pos_as_fill_events(stored_batch, me->oracle); + publish_batch(me->fill_stream, stored_batch); - // Subscribe to NewExecutionReportEvent for new fills - subscribe( - [this](auto const &ev) { this->on_execution_report(ev); }); - - // index securities - std::map sec_idx; - for (auto const &ev : *(update.events)) { - auto sec_ptr = ev.security; - sec_idx[SecurityKey(sec_ptr->id())] = sec_ptr; - } - - // Load stored fills, resolve their securities and publish the lot - api::orderman::Fill fill; - std::vector stored_batch; - LOG(persist, Info, "Loading load all fills"); - auto all = me->store->retrieve_all()[fills_ns]; - LOG(persist, Info, "Finished loading all {} fills", all.size()); - for (auto const &kvp : all) - if (utils::pb::from_string(kvp.second, - &fill)) { - - auto sec = sec_idx[SecurityKey(fill.security_key())]; - if (sec) { - auto fk = kvp.first; - if (!me->sod_pos_map.add_fill_to_sod_pos(fill)) - stored_batch.emplace_back(fill, *sec); - me->known_fills.insert(fk); - } else - LOG(persist, Diag, - "Stored fill {} contains unresolved security {}. ", - kvp.first, fill.security_key()); - } else - LOG(persist, Error, - "Could not recover fill {} from storage. " - "This should be investigated", - kvp.first); - me->fill_stream = make_stream(); - LOG(OrderMan, Info, - "Fill Store published {} fills recoved from disk out of {} " - "total fills", - stored_batch.size(), all.size()); - me->sod_pos_map.add_sod_pos_as_fill_events(stored_batch, sec_idx); - publish_batch(me->fill_stream, stored_batch); - - // subscribe to order updates so we can resolve - // account ids for future execution reports - subscribe( - [this](auto const &ev) { this->on_order_state_update(ev); }); - - me->state = State::READY; - } - } - }); + // subscribe to order updates so we can resolve + // account ids for future execution reports + subscribe( + [this](auto const &ev) { this->on_order_state_update(ev); }); } FillStore::~FillStore() {} diff --git a/src/lib/orderman/actors/FillStore.h b/src/lib/orderman/actors/FillStore.h index 7d1c7b909..d4309e734 100644 --- a/src/lib/orderman/actors/FillStore.h +++ b/src/lib/orderman/actors/FillStore.h @@ -2,6 +2,7 @@ #include #include "lib/mercury/actors/Actor.h" +#include "lib/md/SecurityOracle.h" namespace bts { namespace orderman { @@ -14,7 +15,7 @@ class OrderStateUpdateEvent; class FillStore : public mercury::Actor { public: - FillStore(); + FillStore(md::SecurityOracle&); ~FillStore(); void init() override; diff --git a/src/lib/orderman/actors/SODPosition.cpp b/src/lib/orderman/actors/SODPosition.cpp index ee9515be7..18f6d83c4 100644 --- a/src/lib/orderman/actors/SODPosition.cpp +++ b/src/lib/orderman/actors/SODPosition.cpp @@ -171,22 +171,22 @@ bool SODPosition::add_fill_to_sod_pos(const Fill &fill) { void SODPosition::add_sod_pos_as_fill_events( std::vector &fe, - std::map sec_map) { + md::SecurityOracle& oracle) { if (_enabled) for (auto &acct_p : _sod_map) { for (auto &sec_p : acct_p.second) { Fill &long_pos = sec_p.second.first; Fill &short_pos = sec_p.second.second; bts_assert(long_pos.security_key() == short_pos.security_key()); - auto sec_it = sec_map.find(SecurityKey(long_pos.security_key())); - if (sec_it == sec_map.end()) + auto sec = oracle.get_security(SecurityKey(long_pos.security_key())); + if (!sec) LOG(persist, Error, "Could not recover fill for key:{} from storage. " "This should be investigated", long_pos.security_key()); else { - fe.emplace_back(long_pos, *sec_it->second); - fe.emplace_back(short_pos, *sec_it->second); + fe.emplace_back(long_pos, *sec); + fe.emplace_back(short_pos, *sec); } } } diff --git a/src/lib/orderman/actors/SODPosition.h b/src/lib/orderman/actors/SODPosition.h index d31e39446..860e0cf2d 100644 --- a/src/lib/orderman/actors/SODPosition.h +++ b/src/lib/orderman/actors/SODPosition.h @@ -4,6 +4,7 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/mercury/actors/Actor.h" #include "lib/orderman/events/StoredFillEvent.h" +#include "lib/md/SecurityOracle.h" using namespace bts::api::orderman; @@ -34,7 +35,7 @@ public: static timeval get_order_cutoff_time(const std::map& ordermap); bool add_fill_to_sod_pos(const Fill& fill); void add_sod_pos_as_fill_events(std::vector& fill_events, - std::map sec_map); + md::SecurityOracle&); }; }} diff --git a/src/lib/orderman/server/OrderServiceServer.cpp b/src/lib/orderman/server/OrderServiceServer.cpp index 0cee13d43..caf1c67b2 100644 --- a/src/lib/orderman/server/OrderServiceServer.cpp +++ b/src/lib/orderman/server/OrderServiceServer.cpp @@ -5,6 +5,7 @@ #include "lib/config/protos/jetfix.pb.h" #include "lib/config/protos/orderman_cfg.pb.h" #include "lib/md/MDConsumer.h" +#include "lib/md/MDClient.h" #include "lib/mercury/actors/Timer.h" #include "lib/mercury/executor/Executor.h" #include "lib/metrics/metrics.h" @@ -88,11 +89,11 @@ ActorThread *find_thread_for_actor( struct OrderServiceServer::PImpl { PImpl(bts::api::ServiceContext &ctx, - std::shared_ptr &ss, + md::MDClient& mdc, DynamicOrderRegistry const &d, metrics::MetricsContainer &stats, trace::PerfLog &log) : service_bridge(), service_ready(false), known_orders(), known_fills(), publish_orders(), publish_fills(), - admin_service(ctx.locate()), cache(), md_consumer(ss, cache), + admin_service(ctx.locate()), md_consumer(mdc.current_consumer()), stats(stats), dyn_order_registry(d),log(log) {} ServiceBridge *service_bridge; @@ -106,8 +107,7 @@ struct OrderServiceServer::PImpl { OrderServiceServer::OrdersPublisher publish_orders; OrderServiceServer::FillsPublisher publish_fills; api::admin::AdminService admin_service; - md::SecurityCache cache; - bts::md::MDConsumer md_consumer; + bts::md::MDConsumer& md_consumer; metrics::MetricsContainer &stats; DynamicOrderRegistry const &dyn_order_registry; trace::PerfLog &log; @@ -115,10 +115,10 @@ struct OrderServiceServer::PImpl { OrderServiceServer::OrderServiceServer( bts::api::ServiceContext &ctx, - std::shared_ptr &ss, + md::MDClient& mdc, DynamicOrderRegistry const &d, metrics::MetricsContainer &stats, trace::PerfLog &log) - : api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, ss, d, stats,log)) { + : api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, mdc, d, stats,log)) { me->publish_orders = make_orders_publisher(); me->publish_fills = make_fills_publisher(); @@ -138,6 +138,7 @@ OrderServiceServer::OrderServiceServer( bts_assert_msg(thread, "Could not find a configured thread for MDSource. See the " "OrdermanCfg key"); + thread->executor.spawn(me->md_consumer, me->stats,log); // OrderManager @@ -173,7 +174,7 @@ OrderServiceServer::OrderServiceServer( bts_assert_msg(thread, "Could not find a configured thread for FilllStore. See " "the OrdermanCfg key"); - thread->executor.spawn(); + thread->executor.spawn(mdc.oracle()); // SecuritySrc thread = find_thread_for_actor("SecuritySrc", me->actor_threads); diff --git a/src/lib/orderman/server/OrderServiceServer.h b/src/lib/orderman/server/OrderServiceServer.h index 4c23c13f9..75c558932 100644 --- a/src/lib/orderman/server/OrderServiceServer.h +++ b/src/lib/orderman/server/OrderServiceServer.h @@ -3,6 +3,7 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/api/services/OrderService.server.pb.h" #include "lib/mercury/actors/IPollableActor.h" +#include "lib/md/MDClient.h" #include namespace bts { @@ -19,7 +20,7 @@ class DynamicOrderRegistry; class OrderServiceServer : public api::orderman::OrderServiceBase { public: OrderServiceServer(bts::api::ServiceContext &context, - std::shared_ptr &, + md::MDClient&, DynamicOrderRegistry const&, metrics::MetricsContainer &, trace::PerfLog &); diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index a73561dec..a1840cf77 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -11,6 +11,7 @@ #include "lib/utils/Dispatcher.h" #include "lib/config/protos/toggles.pb.h" +#include "lib/md/MDClient.h" #include "lib/md/SyntheticSecurityService.h" #include "lib/md/relay/QueryServer.h" #include "lib/metrics/metrics.h" @@ -56,8 +57,12 @@ int Run::execute(std::vector const &) { persistence::PersistenceServiceServer settings_saver(sc, "settings"); user::AuthServiceServer login_server(sc); md::run_market_datapublishers(); + std::shared_ptr synthetic_service = std::make_shared(sc); + md::MDClient mdclient(utils::Dispatcher::current(), synthetic_service); + + md::QueryServerPtr md_query_server( md::run_query_server(sc, synthetic_service, stats)); @@ -68,7 +73,7 @@ int Run::execute(std::vector const &) { // finished initializing order plugins orderman::OrderServiceServer order_service_server( - sc, synthetic_service, dyn_order_registry, stats, perf_log); + sc, mdclient, dyn_order_registry, stats, perf_log); support::ErrorReportServer error_report_service(sc); admin::AdminServiceServer admin_service(sc); md::auxiliary::MarketDataAuxiliaryServiceServer md_aux_server(sc);