From 29eef7709961af0e4bf2376af2ed25f93ea744c0 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Sun, 4 Dec 2022 20:36:10 -0500 Subject: [PATCH] ServiceBridge now periodically publishes the TheoEvent (with a flag indicating such). This helps the algos get the theos without waiting for the next update --- src/lib/orderman/OrderServiceServer.cpp | 15 +++++++++-- src/lib/orderman/OrderServiceServer.h | 1 + src/lib/orderman/actors/ServiceBridge.cpp | 25 +++++++++++++++---- src/lib/orderman/actors/ServiceBridge.h | 1 + .../orderman/calculations/ITheoCalculator.h | 3 +++ src/lib/orderman/events/TheoEvent.h | 9 ++++--- .../orderman/orders/algo/AlgoWrapperOrder.cpp | 10 ++++++-- 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/lib/orderman/OrderServiceServer.cpp b/src/lib/orderman/OrderServiceServer.cpp index dcca7db21..cbd514cfb 100644 --- a/src/lib/orderman/OrderServiceServer.cpp +++ b/src/lib/orderman/OrderServiceServer.cpp @@ -128,8 +128,8 @@ struct OrderServiceServer::PImpl { DynamicOrderRegistry const &d, metrics::MetricsContainer &stats, trace::PerfLog &log) - : service_bridge(), service_ready(false), known_orders(), known_fills(), - publish_orders(), publish_fills(), + : context(ctx), service_bridge(), service_ready(false), known_orders(), + known_fills(), publish_orders(), publish_fills(), exchange_pos_publisher(exchange_pos_publisher), open_orders_publisher(open_orders_publisher), admin_service(ctx.locate()), @@ -139,6 +139,7 @@ struct OrderServiceServer::PImpl { dyn_order_registry(d), log(log), in_flight_order_ops(), current_req_op_id(0) {} + bts::api::ServiceContext &context; ServiceBridge *service_bridge; std::vector> actor_threads; @@ -323,6 +324,11 @@ OrderServiceServer::OrderServiceServer( [this](auto const &update) { me->service_bridge->update_excess_liquidity_report(update); }); + // We wait long enough for the server to be fully ready. This is not great, + // we should watch for some signal but right now I am not sure if such a + // signal exist. We cant do it too early either or some other problem + // happens. We will come to this later and fix it + me->context.post([this]() { _do_work(); }, 300000); } OrderServiceServer::~OrderServiceServer() { @@ -589,5 +595,10 @@ void OrderServiceServer::_on_exchange_open_orders( me->open_orders_publisher.flush(); } +void OrderServiceServer::_do_work() { + me->service_bridge->do_work(); + me->context.post([this]() { _do_work(); }, 500); +} + } // namespace orderman } // namespace bts diff --git a/src/lib/orderman/OrderServiceServer.h b/src/lib/orderman/OrderServiceServer.h index f2110fd7b..ed5ab64f6 100644 --- a/src/lib/orderman/OrderServiceServer.h +++ b/src/lib/orderman/OrderServiceServer.h @@ -68,6 +68,7 @@ class OrderServiceServer : public api::orderman::OrderServiceBase { void on_order_op_result(OrderOpResultEvent const &); void _on_exchange_position(const ExchangePositionEvent &); void _on_exchange_open_orders(const ExchangeOpenOrdersEvent &); + void _do_work(); }; } // namespace orderman diff --git a/src/lib/orderman/actors/ServiceBridge.cpp b/src/lib/orderman/actors/ServiceBridge.cpp index 3637df54f..2d6ad6d84 100644 --- a/src/lib/orderman/actors/ServiceBridge.cpp +++ b/src/lib/orderman/actors/ServiceBridge.cpp @@ -83,6 +83,7 @@ struct ServiceBridge::PImpl { mercury::BrokerProxy &service_call_marshaller; std::promise ready; bts::orderman::analytics::TheoCalculator theo_calculator = {}; + bool theos_set = false; }; ServiceBridge::ServiceBridge( @@ -356,13 +357,27 @@ void ServiceBridge::update_excess_liquidity_report( // updates the calculator and publishes the event (with a pointer to it) void ServiceBridge::set_precomputed_theos( const bts::api::orderman::PrecomputedTheos &precomputed_theos) { - LOG(Gen, Info, "ServiceBridge::update_theos size: {}", - precomputed_theos.theos_size()); + LOG(Gen, Info, "ServiceBridge::update_theos size: {} t_id: {} t_name: {}", + precomputed_theos.theos_size(), os::get_thread_id(), + os::get_thread_name()); me->theo_calculator.set_data(precomputed_theos); me->service_call_marshaller.schedule_action([this]() { - bts::orderman::analytics::ITheoCalculator const *theo_calc_ptr = - &(me->theo_calculator); - publish(me->theo_pub, theo_calc_ptr); + publish( + me->theo_pub, analytics::ITheoCalculator::Impetus::DATA, + &(me->theo_calculator)); + me->theos_set = true; + }); +} + +void ServiceBridge::do_work() { + LOG(Gen, Diag, "ServiceBridge::do_work t_id: {} t_name: {}", + os::get_thread_id(), os::get_thread_name()); + if (!me->theos_set) + return; + me->service_call_marshaller.schedule_action([this]() { + publish( + me->theo_pub, analytics::ITheoCalculator::Impetus::PERIODIC_LATEJOIN, + &(me->theo_calculator)); }); } diff --git a/src/lib/orderman/actors/ServiceBridge.h b/src/lib/orderman/actors/ServiceBridge.h index 7546066f8..d88692fac 100644 --- a/src/lib/orderman/actors/ServiceBridge.h +++ b/src/lib/orderman/actors/ServiceBridge.h @@ -103,6 +103,7 @@ class ServiceBridge : public mercury::Actor { api::position::Excess_liquidity_reportUpdate const &); void set_precomputed_theos( const bts::api::orderman::PrecomputedTheos &precomputed_theos); + void do_work(); private: struct PImpl; diff --git a/src/lib/orderman/calculations/ITheoCalculator.h b/src/lib/orderman/calculations/ITheoCalculator.h index 143e9bdbe..95293f66f 100644 --- a/src/lib/orderman/calculations/ITheoCalculator.h +++ b/src/lib/orderman/calculations/ITheoCalculator.h @@ -13,6 +13,9 @@ namespace bts::orderman::analytics { // ITheoCalculator is passed to the algo, which can then used it to compute // theos. class ITheoCalculator { + public: + enum class Impetus { DATA = 0, PERIODIC_LATEJOIN, CLIENT_HEARTBEAT }; + public: // computes the theo for a security with id sec_id and returns the result in // the last arg diff --git a/src/lib/orderman/events/TheoEvent.h b/src/lib/orderman/events/TheoEvent.h index 1bd53f691..ab5fcd0b9 100644 --- a/src/lib/orderman/events/TheoEvent.h +++ b/src/lib/orderman/events/TheoEvent.h @@ -11,11 +11,14 @@ namespace bts::orderman { // pointer safely class TheoEvent : public mercury::Event { public: + bts::orderman::analytics::ITheoCalculator::Impetus impetus; bts::orderman::analytics::ITheoCalculator const *theo_calc; using SynopsisPolicy = mercury::CacheAll; - TheoEvent(bts::orderman::analytics::ITheoCalculator const *theo_calc) - : theo_calc(theo_calc) { - LOG(OrderMan, Info, "TheoEvent::TheoEvent"); + TheoEvent( + bts::orderman::analytics::ITheoCalculator::Impetus impetus, + bts::orderman::analytics::ITheoCalculator const *theo_calc) + : impetus(impetus), theo_calc(theo_calc) { + LOG(OrderMan, Diag, "TheoEvent::TheoEvent"); } }; diff --git a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp index 44913f4e9..6258250dd 100644 --- a/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp +++ b/src/lib/orderman/orders/algo/AlgoWrapperOrder.cpp @@ -569,7 +569,9 @@ void AlgoWrapperOrder::unsubscribe_position() { } void AlgoWrapperOrder::subscribe_theos() { - LOG(OrderMan, Info, "AlgoWrapperOrder::subscribe_theos"); + LOG(OrderMan, Info, + "AlgoWrapperOrder::subscribe_theos key: {} is_working: {}", order_key(), + is_working()); if (!is_working()) return; bts_assert(!me->has_theo_sub); @@ -581,7 +583,9 @@ void AlgoWrapperOrder::subscribe_theos() { } void AlgoWrapperOrder::unsubscribe_theos() { - LOG(OrderMan, Info, "AlgoWrapperOrder::unsubscribe_theos"); + LOG(OrderMan, Info, + "AlgoWrapperOrder::unsubscribe_theos key: {} is_working: {}", + order_key(), is_working()); if (!is_working()) return; bts_assert(me->has_theo_sub); @@ -1054,6 +1058,8 @@ void AlgoWrapperOrder::cleanup() { c.second.order.cancel(); if (me->has_timer_sub) unsubscribe_timer(); + if (me->has_theo_sub) + unsubscribe_theos(); me->pending_update.reset(); }