From be45938b51b0716de9c1e411ec7f089e05a66db1 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 22 Jan 2020 14:08:26 -0500 Subject: [PATCH 1/3] Fix for assert when algo subs to security w/o MD --- src/lib/orderman/actors/MDSource.h | 83 +++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 9952d2396..136d0ac2b 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -31,9 +31,9 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { public: MDSource(md::MDConsumer &consumer, metrics::MetricsContainer &stats, trace::PerfLog &log) - : _handles(), _status_subs(), _md_consumer(consumer), + : _handles(), _pending_handles(), _status_subs(), _md_consumer(consumer), _book_update(), _ticker_update(), _status_update(), - _perf_log(log), _stats(stats), _loop_timer() { + _perf_log(log), _stats(stats), _loop_timer(), _counter(0) { _trades_buffer.Reserve(1000); auto cme_channels = md::get_configured_cme_channels(); @@ -169,6 +169,43 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { // Publish stats auto stop = utils::TimeStamp::now(); _loop_timer->observe((start - stop).nanoseconds()); + + _counter += 1; + if (_counter % (2<<20) == 0) { + // Invoke periodic work every 2M calls on on_poll + // This is approximately 20-30 seconds + do_periodic_work(); + } + } + + void do_periodic_work() { + if (!_pending_handles.empty()) { + for (auto iter = _pending_handles.begin(); iter != _pending_handles.end(); ++iter) { + if (iter->handle->is_initialized()) { + _pending_handles.erase(iter); + auto handle = *(iter->handle); + LOG(OrderMan, Info, "Found md for {}. Pending handle removed.", handle.key()); + switch(iter->type) { + case MDRequestType::BOOK: + _handles.push_back({utils::TimeStamp(), handle, + get_external_event_channel(handle.get_usersymbol()), + std::make_unique(securities::descriptor( + handle.get_normalized_security()))}); + break; + case MDRequestType::STATUS: + for (auto& h : _status_subs) { + // no need to reset status + if (h.handle.key() == handle.key()) break; + } + _status_subs.push_back( + StatusRecord{ handle, co2::TRADINGSTATUS_UNKNOWN }); + case MDRequestType::TICKER: + // We aren't currently monitoring for pending ticker requests + break; + } + } + } + } } void on_book_sub(SecurityKey sk, @@ -184,14 +221,23 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { LOG(OrderMan, Warn, "Starting to publish book updates for security key: {}", k); - auto handle = _md_consumer.get_handle(k); - _handles.push_back({utils::TimeStamp(), handle, - get_external_event_channel(handle.get_usersymbol()), - std::make_unique(securities::descriptor( - handle.get_normalized_security()))}); - if (handle.is_synthetic()) - synthetic_count_metric.increment(); + auto mhandle = _md_consumer.get_managed_handle(k); + if (mhandle->is_initialized()) { + auto handle = *mhandle; + _handles.push_back({utils::TimeStamp(), handle, + get_external_event_channel(handle.get_usersymbol()), + std::make_unique(securities::descriptor( + handle.get_normalized_security()))}); + if (handle.is_synthetic()) + synthetic_count_metric.increment(); + } + else { + // A pending handle is a handle for security we are reasonbly sure exists + // but we haven't yet found live market data for. + LOG(OrderMan, Warn, "No market data for {}. Pending handle added.", k.value()); + _pending_handles.push_back({ mhandle, MDRequestType::BOOK }); + } } const char *get_external_event_channel(const md::UserSymbol &user_symbol) { @@ -205,7 +251,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { metrics::CounterMetric &channel_count_metric) { auto user_symbol = _md_consumer.get_handle(sk.value()).get_usersymbol(); - for (auto &ch : _channels) + for (auto &ch : _channels) { if (ch.user_symbol == user_symbol) { auto it = std::find_if(ch.securities.begin(), ch.securities.end(), @@ -222,6 +268,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { } return; } + } // this is a channel we are not subscribed to channel_count_metric.increment(); @@ -250,8 +297,14 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { return; } } - auto handle = _md_consumer.get_handle(k); - _status_subs.push_back(StatusRecord{ handle, co2::TRADINGSTATUS_UNKNOWN }); + auto handle = _md_consumer.get_managed_handle(k); + if (handle->is_initialized()) { + _status_subs.push_back(StatusRecord{ *handle, co2::TRADINGSTATUS_UNKNOWN }); + } + else { + LOG(OrderMan, Warn, "No market data for {}. Pending handle added.", k); + _pending_handles.push_back({ handle, MDRequestType::STATUS }); + } } struct HandleRecord { @@ -261,6 +314,11 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { std::unique_ptr symbol; }; boost::container::small_vector _handles; + struct PendingRecord { + std::shared_ptr handle; + MDRequestType type; + }; + boost::container::small_vector _pending_handles; struct ChannelRecord { int64_t cursor; @@ -285,6 +343,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { metrics::MetricsContainer &_stats; std::shared_ptr> _loop_timer; std::map _symbol_to_channel; + uint64_t _counter; }; } // namespace orderman } // namespace bts From 130794c5383d13a0b2011490348fc1d644f76071 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 11 Feb 2020 11:22:02 -0500 Subject: [PATCH 2/3] Merge fixup --- src/lib/orderman/actors/MDSource.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 136d0ac2b..047dfe2a5 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -235,7 +235,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { else { // A pending handle is a handle for security we are reasonbly sure exists // but we haven't yet found live market data for. - LOG(OrderMan, Warn, "No market data for {}. Pending handle added.", k.value()); + LOG(OrderMan, Warn, "No market data for {}. Pending handle added.", k); _pending_handles.push_back({ mhandle, MDRequestType::BOOK }); } } From dc4033875a15324d2fe3690b39fadd326ab3c689 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 11 Feb 2020 11:43:04 -0500 Subject: [PATCH 3/3] Fixes from code review feedback --- src/lib/orderman/actors/MDSource.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 047dfe2a5..529a8155d 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -182,7 +182,6 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { if (!_pending_handles.empty()) { for (auto iter = _pending_handles.begin(); iter != _pending_handles.end(); ++iter) { if (iter->handle->is_initialized()) { - _pending_handles.erase(iter); auto handle = *(iter->handle); LOG(OrderMan, Info, "Found md for {}. Pending handle removed.", handle.key()); switch(iter->type) { @@ -205,6 +204,11 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { } } } + _pending_handles.erase( + std::remove_if(_pending_handles.begin(), + _pending_handles.end(), + [](const auto& h) { return h.handle->is_initialized(); }), + _pending_handles.end()); } }