diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index 529a8155d..8655bdcf7 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -1,5 +1,6 @@ #pragma once +#include "lib/domain_model/Security.h" #include "lib/logging/logging.h" #include "lib/md/IntPrice.h" #include "lib/md/MDConsumer.h" @@ -10,12 +11,11 @@ #include "lib/mercury/events/TimerEvent.h" #include "lib/orderman/events/BookUpdateEvent.h" #include "lib/orderman/events/MDRequestEvent.h" -#include "lib/orderman/events/TickerUpdateEvent.h" #include "lib/orderman/events/StatusUpdateEvent.h" +#include "lib/orderman/events/TickerUpdateEvent.h" #include "lib/tracing/PerfLog.h" #include "lib/tracing/bts_trace.h" #include "lib/utils/TimeStamp.h" -#include "lib/domain_model/Security.h" #include @@ -31,9 +31,9 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { public: MDSource(md::MDConsumer &consumer, metrics::MetricsContainer &stats, trace::PerfLog &log) - : _handles(), _pending_handles(), _status_subs(), _md_consumer(consumer), - _book_update(), _ticker_update(), _status_update(), - _perf_log(log), _stats(stats), _loop_timer(), _counter(0) { + : _handles(), _pending_handles(), _status_subs(), _md_consumer(consumer), + _book_update(), _ticker_update(), _status_update(), _perf_log(log), + _stats(stats), _loop_timer(), _counter(0) { _trades_buffer.Reserve(1000); auto cme_channels = md::get_configured_cme_channels(); @@ -67,9 +67,8 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { auto ticker_channel_cout_metric = std::make_shared>( "spark_mdsource_ticker_channel_count"); - auto status_count_metric = - std::make_shared>( - "spark_mdsource_status_count"); + auto status_count_metric = std::make_shared>( + "spark_mdsource_status_count"); _stats.publish(security_count_metric); _stats.publish(synthetic_count_metric); _stats.publish(ticker_channel_cout_metric); @@ -78,37 +77,40 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { _book_update = make_stream(); _ticker_update = make_stream(); _status_update = make_stream(); - subscribe( - [this, security_count_metric, synthetic_count_metric, - ticker_channel_cout_metric, status_count_metric](auto const &update) { - if (update.events) { - for (auto const &ev : *update.events) { - auto k = ev.security_key; + subscribe([this, security_count_metric, + synthetic_count_metric, + ticker_channel_cout_metric, + status_count_metric](auto const &update) { + if (update.events) { + for (auto const &ev : *update.events) { + auto k = ev.security_key; - switch (ev.md_type) { - case MDRequestType::BOOK: - on_book_sub(k, *synthetic_count_metric); - security_count_metric->set(_handles.size()); - break; - case MDRequestType::TICKER: - on_ticker_sub(k, *ticker_channel_cout_metric); - break; - case MDRequestType::STATUS: - on_status_sub(k); - status_count_metric->set(_status_subs.size()); - break; - } - } - } - }); + switch (ev.md_type) { + case MDRequestType::BOOK: + on_book_sub(k, *synthetic_count_metric); + security_count_metric->set(_handles.size()); + break; + case MDRequestType::TICKER: + on_ticker_sub(k, *ticker_channel_cout_metric); + break; + case MDRequestType::STATUS: + on_status_sub(k); + status_count_metric->set(_status_subs.size()); + break; + } + } + } + }); security_count_metric->set(_handles.size()); } private: - void on_poll(trace::PerfLog& log) override { + void on_poll(trace::PerfLog &log) override { - auto scoped_do_work = trace::log_scope(log, {}, "MDSource::on_poll", utils::TimeStamp::Interval::from_microseconds(2)); + auto scoped_do_work = + trace::log_scope(log, {}, "MDSource::on_poll", + utils::TimeStamp::Interval::from_microseconds(2)); // Book Updates Book book; @@ -158,11 +160,11 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { } // Status updates - for (auto& s : _status_subs) { + for (auto &s : _status_subs) { auto status = s.handle.trading_status(); if (status != s.status) { s.status = status; - publish(_status_update, SecurityKey { s.handle.key() }, status); + publish(_status_update, SecurityKey{s.handle.key()}, status); } } @@ -171,7 +173,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { _loop_timer->observe((start - stop).nanoseconds()); _counter += 1; - if (_counter % (2<<20) == 0) { + if (_counter % (2 << 20) == 0) { // Invoke periodic work every 2M calls on on_poll // This is approximately 20-30 seconds do_periodic_work(); @@ -180,35 +182,39 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { void do_periodic_work() { if (!_pending_handles.empty()) { - for (auto iter = _pending_handles.begin(); iter != _pending_handles.end(); ++iter) { + 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: + 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()); + std::remove_if( + _pending_handles.begin(), _pending_handles.end(), + [](const auto &h) { return h.handle->is_initialized(); }), + _pending_handles.end()); } } @@ -229,18 +235,18 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { 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()))}); + _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. + } 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 }); + _pending_handles.push_back({mhandle, MDRequestType::BOOK}); } } @@ -253,8 +259,17 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { void on_ticker_sub(SecurityKey sk, metrics::CounterMetric &channel_count_metric) { + auto handle = _md_consumer.get_managed_handle(sk.value()); - auto user_symbol = _md_consumer.get_handle(sk.value()).get_usersymbol(); + if (!handle->is_initialized()) { + LOG(OrderMan, Warn, + "Received ticker subscription for security {} which does not have " + "valid market data. Ignoring", + sk.value()); + return; + } + + auto user_symbol = handle->get_usersymbol(); for (auto &ch : _channels) { if (ch.user_symbol == user_symbol) { auto it = @@ -262,7 +277,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { [sk](auto const &ev) { return ev.key() == sk; }); if (it == ch.securities.end()) { // first time we are asked for this security, we will - // publish the trades for it next time next round + // publish the trades for it next time around LOG(OrderMan, Warn, "Starting to publish ticker updates on user symbol {} for " "security key: {} (sec count for the symbol: {} )", @@ -294,7 +309,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { void on_status_sub(SecurityKey sk) { auto k = sk.value(); - for (auto& h : _status_subs) { + for (auto &h : _status_subs) { if (h.handle.key() == k) { // reset status so it get resent next on_poll h.status = co2::TRADINGSTATUS_UNKNOWN; @@ -303,11 +318,11 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { } auto handle = _md_consumer.get_managed_handle(k); if (handle->is_initialized()) { - _status_subs.push_back(StatusRecord{ *handle, co2::TRADINGSTATUS_UNKNOWN }); - } - else { + _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 }); + _pending_handles.push_back({handle, MDRequestType::STATUS}); } } @@ -337,7 +352,7 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { md::MDConsumerHandle handle; co2::TradingStatus status; }; - boost::container::small_vector _status_subs; + boost::container::small_vector _status_subs; md::MDConsumer _md_consumer; mercury::Stream _book_update;