diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 9952d2396..529a8155d 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,47 @@ 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()) { + 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; + } + } + } + _pending_handles.erase( + std::remove_if(_pending_handles.begin(), + _pending_handles.end(), + [](const auto& h) { return h.handle->is_initialized(); }), + _pending_handles.end()); + } } void on_book_sub(SecurityKey sk, @@ -184,14 +225,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); + _pending_handles.push_back({ mhandle, MDRequestType::BOOK }); + } } const char *get_external_event_channel(const md::UserSymbol &user_symbol) { @@ -205,7 +255,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 +272,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 +301,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 +318,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 +347,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