From c3f88bd5055804c582c3143dbdc6ae15f08aeecf Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 25 Sep 2024 04:01:13 +0000 Subject: [PATCH] fix: Resolve onixs log & data dirs relative to app data root When our logging code moved to lib_system, it lost access to the config system. As a result, the "log_path" function, which was used by ICEMain and MDP3Main, broke. I added a new class, SparkDataDirectory, to be the central/common authority on paths to local storage. --- .../platforms/ice_onixs/ICEListenerHelpers.h | 4 +- .../sparkd/md/platforms/ice_onixs/ICEMain.cpp | 132 +++++++++--------- .../sparkd/md/platforms/ice_onixs/ICEMain.h | 39 ++---- .../md/platforms/mdp3_onixs/MDP3Main.cpp | 103 ++++++++++---- .../sparkd/md/platforms/mdp3_onixs/MDP3Main.h | 23 ++- src/lib_spark/spark/md/cme/MDP3Utils.cpp | 33 ++--- src/lib_spark/spark/md/cme/MDP3Utils.h | 11 +- .../spark/md/cme/mdp3_data_directory.h | 32 +++++ src/lib_spark/spark/md/ice/ICEUtils.cpp | 44 ++---- src/lib_spark/spark/md/ice/ICEUtils.h | 20 +-- src/lib_spark/spark/md/ice/ice_channel.cpp | 39 ++++++ src/lib_spark/spark/md/ice/ice_channel.h | 23 +++ .../spark/md/ice/ice_data_directory.h | 36 +++++ src/lib_spark/spark/spark_data_directory.cpp | 31 ++++ src/lib_spark/spark/spark_data_directory.h | 23 +++ 15 files changed, 378 insertions(+), 215 deletions(-) create mode 100644 src/lib_spark/spark/md/cme/mdp3_data_directory.h create mode 100644 src/lib_spark/spark/md/ice/ice_channel.cpp create mode 100644 src/lib_spark/spark/md/ice/ice_channel.h create mode 100644 src/lib_spark/spark/md/ice/ice_data_directory.h create mode 100644 src/lib_spark/spark/spark_data_directory.cpp create mode 100644 src/lib_spark/spark/spark_data_directory.h diff --git a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h index 10cfaea13..985d222ff 100644 --- a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h +++ b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEListenerHelpers.h @@ -1,7 +1,7 @@ #pragma once #include "spark/md/coconut2.h" -#include "spark/md/ice/ICEUtils.h" +#include "spark/md/ice/ice_channel.h" #include "spark/utils/bts_assert.h" #include "spark/utils/helper_macros.h" @@ -49,4 +49,4 @@ inline datetime::Date date_of_timestamp(const onixs_ice::DateTime &dt) { } // namespace ice } // namespace md -} // namespace bts \ No newline at end of file +} // namespace bts diff --git a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.cpp b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.cpp index b053ef3af..1996c47ed 100644 --- a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.cpp +++ b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.cpp @@ -1,68 +1,61 @@ #include "ICEMain.h" #include "spark/config/config.h" -#include "spark/md/SecurityDB.h" -#include "spark/md/coconut2.h" -#include "spark/md/ice/ICEUtils.h" -#include "spark/md/protos/co2.pb.h" -#include "spark/md/vendor/OnixsUtils.h" -#include "spark/utils/Dispatcher.h" -#include "spark/utils/bts_chrono.h" -#include "spark/utils/helper_macros.h" - +#include "spark/config/protos/deployment_type.pb.h" #include "spark/config/protos/market_data.pb.h" -#include "spark/utils/thread_manager.h" -#include "system/os.h" -#include "system/protobufs/proto_helper.h" +#include "spark/md/ice/ICEUtils.h" +#include "spark/md/ice/ice_channel.h" +#include "spark/md/vendor/OnixsUtils.h" +#include "spark/spark_data_directory.h" #include "system/protobufs/config/system_config.pb.h" +#include #include #include -#include - -void recursive_copy( - const boost::filesystem::path &src, const boost::filesystem::path &dst) { - if (boost::filesystem::exists(dst)) - throw std::runtime_error(dst.generic_string() + " exists"); - if (boost::filesystem::is_directory(src)) { - boost::filesystem::create_directories(dst); - for (boost::filesystem::directory_entry &item : - boost::filesystem::directory_iterator(src)) - recursive_copy(item.path(), dst / item.path().filename()); - } else if (boost::filesystem::is_regular_file(src)) - boost::filesystem::copy(src, dst); - else - throw std::runtime_error(dst.generic_string() + " not dir or file"); -} using namespace std; using namespace bts::co2; using std::size_t; -namespace bts { -namespace md { -namespace ice { +namespace bts::md::ice { using bts::system::protobufs::SystemConfig; using Path = boost::filesystem::path; -ICEMain::ICEMain() - : _index(), _recorder( - {utils::Dispatcher::current(), - [=, this](ExchangeKey k) { - return SecurityKey(_index.get_key(k.value())); - }}), - _producer(_recorder), _handlers(), _listeners() {} +struct ICEMain::PImpl { + ICEOptions options; + std::string config_file; + ICESecurityIndex index; + SecurityIDRecorder recorder; + MDProducer producer; + std::vector>> + handlers; + std::vector> listeners; -ICEMain::~ICEMain() {} + PImpl(ICEOptions options) + : options(options), + config_file((Path{config::get().data_path()} / + "onixs_ice" / this->options.ice_config_file()) + .string()), + index(), recorder( + {utils::Dispatcher::current(), + [=, this](ExchangeKey k) { + return SecurityKey(index.get_key(k.value())); + }}), + producer(recorder), handlers(), listeners() {} -const IceChannelConfig channel_settings_for(const ICEChannel &channel) { - const auto &config = config::get(); - for (const auto &ch : config.channel()) { - if (ch.name() == channel.name) - return ch; + IceChannelConfig channel_settings_for(const ICEChannel &channel) { + for (const auto &ch : options.channel()) { + if (ch.name() == channel.name) + return ch; + } + return {}; } - return {}; +}; + +ICEMain::ICEMain(ICEOptions options) + : me_(std::make_unique(std::move(options))) { + me_->options = std::move(options); } onixs_ice::MarketSubscriptions market_subcription_for_channel( @@ -90,23 +83,30 @@ onixs_ice::MarketSubscriptions market_subcription_for_channel( return subs; } -void ICEMain::start(const std::vector &symbols) { - auto ice_conf_location = - Path{config::get().data_path()} / "onixs_ice/"; - if (!boost::filesystem::is_directory(ice_conf_location)) { - auto conf_path = Path{os::sparkd_wd()} / "data_files" / "onixs_ice/"; - recursive_copy(Path{conf_path}, ice_conf_location); +static void validate_config_file(const std::string &config_file) { + if (!boost::filesystem::exists(config_file)) { + const std::string config_url = + config::get().type() == "dev" + ? "https://community.theice.com/docs/DOC-19989" + : "https://drive.google.com/" + "open?id=13_eyF2pKtn3F7chgsa_75ftUdf7emMbp"; + bts_assert_msgf( + false, "Ice config file not found '{}'. Please download from {}", + config_file, config_url); } +} +void ICEMain::start(const std::vector &symbols) { await_ice_config(); + validate_config_file(me_->config_file); auto license = onixs_create_temporary_license(); - const auto &channels = parse_ice_channels(symbols); + const auto &channels = ICEChannel::parse_all(symbols); for (const auto &channel : channels) { unique_ptr phandler = prepare_handler(channel, license.path.string()); - _listeners.push_back(prepare_listener(*phandler, channel)); - _handlers.push_back(std::make_pair(channel, std::move(phandler))); + me_->listeners.push_back(prepare_listener(*phandler, channel)); + me_->handlers.push_back(std::make_pair(channel, std::move(phandler))); std::stringstream symstr; for (const auto &sym : channel.symbols) @@ -115,7 +115,7 @@ void ICEMain::start(const std::vector &symbols) { symstr.str()); } - for (auto &pair : _handlers) { + for (auto &pair : me_->handlers) { LOG(md, Info, "ICE: Init Handler {}", pair.first.to_string()); sleep_for(16s); onixs_ice::MarketSubscriptions subs = @@ -132,7 +132,7 @@ void ICEMain::start(const std::vector &symbols) { } void ICEMain::send_heartbeats() { - for (const auto &listener : _listeners) + for (const auto &listener : me_->listeners) listener->heartbeat(); } @@ -140,20 +140,16 @@ void ICEMain::send_heartbeats() { // Initialize HandlerSettings and allocate the channel Handler unique_ptr ICEMain::prepare_handler( const ICEChannel &channel, const std::string &license) { - const auto &options = channel_settings_for(channel); + const auto &options = me_->channel_settings_for(channel); LOG(md, Info, "Loaded channel options for channel {}:\n{}", channel.to_string(), options.DebugString()); onixs_ice::HandlerSettings settings; settings.licenseDirectory = license; - // - // Logging settings.logLevel = (onixs_ice::LogLevels::Enum)options.onixs_log_mode(); - settings.logDirectory = ice_log_path(); - - // - // Config file - settings.connectivityConfiguration = ice_config_path(); + settings.logDirectory = + (spark::SparkDataDirectory{}.log_dir() / "onixs_ice/").string(); + settings.connectivityConfiguration = me_->config_file; // // Network settings @@ -231,12 +227,12 @@ unique_ptr ICEMain::prepare_listener( // heartbeats vector producer_channels; for (auto &symbol : channel.symbols) { - producer_channels.push_back(_producer.get_channel( + producer_channels.push_back(me_->producer.get_channel( symbol.symbol().c_str(), symbol.type(), symbol.exchange())); } unique_ptr listener(new ICEListener( - _index, _producer, /*_settlement_recorder,*/ producer_channels)); + me_->index, me_->producer, /*_settlement_recorder,*/ producer_channels)); for (auto &symbol : channel.symbols) listener->add_filter_symbol(symbol.symbol()); @@ -254,6 +250,4 @@ unique_ptr ICEMain::prepare_listener( return listener; } -} // namespace ice -} // namespace md -} // namespace bts +} // namespace bts::md::ice diff --git a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.h b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.h index 4c0f5f986..90e7f8c91 100644 --- a/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.h +++ b/src/app_sparkd/sparkd/md/platforms/ice_onixs/ICEMain.h @@ -1,21 +1,13 @@ #pragma once #include "ICEListener.h" -#include "ICESecurityIndex.h" -#include "spark/md/coconut2.h" -#include "spark/utils/bts_no_copy.h" -#include "onixs.h" +#include "spark/config/protos/market_data.pb.h" #include "sparkd/md/platforms/IMDPlatform.h" -#include #include #include -namespace bts { -namespace md { -namespace ice { - -// void run_onixs_ice_server(UserSymbol* sym, size_t symcount); +namespace bts::md::ice { struct ICEChannel; @@ -28,27 +20,22 @@ struct ICEChannel; class ICEMain : public IMDPlatform { public: ICEMain(); - ~ICEMain(); + explicit ICEMain(ICEOptions options); + ICEMain(const ICEMain &) = delete; + ICEMain &operator=(const ICEMain &) = delete; + ~ICEMain() = default; void start(const std::vector &symbols); void send_heartbeats() override; - std::unique_ptr - prepare_handler(const ICEChannel &channel, const std::string &license); - std::unique_ptr - prepare_listener(onixs_ice::Handler &handler, const ICEChannel &channel); + std::unique_ptr prepare_handler( + const ICEChannel &channel, const std::string &license); + std::unique_ptr prepare_listener( + onixs_ice::Handler &handler, const ICEChannel &channel); private: - BTS_NO_COPY(ICEMain); - - ICESecurityIndex _index; - SecurityIDRecorder _recorder; - MDProducer _producer; - std::vector>> - _handlers; - std::vector> _listeners; + struct PImpl; + std::unique_ptr me_; }; -} // namespace ice -} // namespace md -} // namespace bts +} // namespace bts::md::ice diff --git a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp index c0c886991..a785f2a2a 100644 --- a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp +++ b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp @@ -1,24 +1,20 @@ #include "MDP3Main.h" -#include "domain/protobufs/trading_status.pb.h" #include "spark/config/config.h" +#include "spark/config/protos/deployment_type.pb.h" #include "spark/md/MBOProcessor.h" #include "spark/md/SecurityDB.h" #include "spark/md/cme/MDP3Utils.h" -#include "spark/md/coconut2.h" -#include "spark/md/protos/co2.pb.h" +#include "spark/md/cme/mdp3_data_directory.h" #include "spark/md/vendor/OnixsUtils.h" -#include "spark/utils/Dispatcher.h" #include "spark/config/protos/market_data.pb.h" +#include "spark/spark_data_directory.h" #include "spark/utils/thread_manager.h" #include "system/logging/logging.h" -#include "system/os.h" -#include "system/protobufs/proto_helper.h" #include #include -#include #include #include @@ -31,16 +27,32 @@ namespace md { #define TRADING_STATUS(x) bts::domain::protobufs::TRADINGSTATUS_##x -MDP3Main::MDP3Main() - : _index(), _recorder( - {utils::Dispatcher::current(), - [=, this](ExchangeKey k) { - return SecurityKey(_index.get_key(k.value())); - }}), - _producer(_recorder), _engines(), _handlers(), _listeners(), _recovery() { -} +using Path = boost::filesystem::path; -MDP3Main::~MDP3Main() {} +struct MDP3Main::PImpl { + boost::filesystem::path data_path; + boost::filesystem::path log_path; + CMESecurityIndex index; + SecurityIDRecorder recorder; + MDProducer producer; + std::map> engines; + std::map> handlers; + std::vector> listeners; + std::vector> recovery; + PImpl() + : data_path(spark::SparkDataDirectory{}.root() / "cme_mdp3/"), + log_path(spark::SparkDataDirectory{}.log_dir() / "onixs/"), index(), + recorder( + {utils::Dispatcher::current(), + [=, this](ExchangeKey k) { + return SecurityKey(index.get_key(k.value())); + }}), + producer(recorder), engines(), handlers(), listeners(), recovery() {} +}; + +MDP3Main::MDP3Main() : me_(std::make_unique()) {} + +MDP3Main::~MDP3Main() = default; const CmeChannelConfig channel_settings_for(const string &id) { const auto &config = config::get(); @@ -162,24 +174,53 @@ onixs::SessionSettings session_settings_for_channel( return settings; } +static void validate_config_file(const std::string &config_file) { + if (!boost::filesystem::exists(config_file)) { + const bool is_dev = config::get().type() == "dev"; + const std::string config_url = + is_dev ? "ftp://ftp.cmegroup.com/SBEFix/Cert/Configuration/config.xml" + : "ftp://ftp.cmegroup.com/SBEFix/Production/Configuration/" + "config.xml"; + bts_assert_msgf( + false, "MDP3 config file not found '{}'. Please download from {}", + config_file, config_url); + } +} + +static void validate_templates_file(const std::string &templates_file) { + if (!boost::filesystem::exists(templates_file)) { + const bool is_dev = config::get().type() == "dev"; + const std::string templates_url = + is_dev ? "ftp://ftp.cmegroup.com/SBEFix/Cert/Templates/" + "templates_FixBinary.xml" + : "ftp://ftp.cmegroup.com/SBEFix/Cert/Templates/" + "templates_FixBinary.xml"; + bts_assert_msgf( + false, "MDP3 templates file not found '{}'. Please download from {}", + templates_file, templates_url); + } +} + void MDP3Main::start( const std::vector &symbols, MBOWriter *mbo, TradeSummarySignalDispatcher *tssd) { + Mdp3DataDirectory data_dir; + validate_config_file(data_dir.config_file().string()); + validate_templates_file(data_dir.templates_file().string()); std::ostringstream oss; for (const auto &us : symbols) { oss << us << " "; } LOG(md, Info, "MDP3Main::start symbols: {}", oss.str()); - await_cme_config(); auto license = onixs_create_temporary_license(); initialize(license.path.string()); const auto &channels = parse_channels(symbols); - _engines = create_feed_engines(channels); + me_->engines = create_feed_engines(channels); for (const auto &channel : channels) { unique_ptr phandler = prepare_handler(channel.id, mbo); - _listeners.push_back(prepare_listener(*phandler, channel, mbo, tssd)); - _handlers.insert(std::make_pair(channel.id, std::move(phandler))); + me_->listeners.push_back(prepare_listener(*phandler, channel, mbo, tssd)); + me_->handlers.insert(std::make_pair(channel.id, std::move(phandler))); std::stringstream symstr; for (const auto &sym : channel.symbols) { symstr << sym.symbol() << "-" << SecurityType_Name(sym.type()) << " "; @@ -188,7 +229,7 @@ void MDP3Main::start( symstr.str()); } - for (auto &pair : _handlers) { + for (auto &pair : me_->handlers) { onixs::SessionSettings settings = session_settings_for_channel(pair.first, mbo); pair.second->start(settings); @@ -198,7 +239,7 @@ void MDP3Main::start( } void MDP3Main::send_heartbeats() { - for (const auto &listener : _listeners) { + for (const auto &listener : me_->listeners) { listener->heartbeat(); } } @@ -221,7 +262,8 @@ unique_ptr MDP3Main::prepare_handler( onixs::HandlerSettings settings; settings.channel(std::stoul(channel_id)); - settings.feeds().connectivityConfigurationFile(config_path()); + settings.feeds().connectivityConfigurationFile( + (me_->data_path / "config.xml").string()); // Market data options settings.bookManagement().directBooks().maintain(true); @@ -308,7 +350,7 @@ unique_ptr MDP3Main::prepare_handler( logger = new onixs::NullLogger(); } else { onixs::FileLoggerSettings loggerSettings; - loggerSettings.filename(log_path() + "/log_" + channel_id); + loggerSettings.filename((me_->log_path / ("log_" + channel_id)).string()); loggerSettings.severityLevel( (onixs::LogSeverity::Enum)options.onixs_log_mode()); logger = new onixs::FileLogger(loggerSettings); @@ -340,19 +382,19 @@ unique_ptr MDP3Main::prepare_handler( try { auto handler = unique_ptr(new onixs::Handler(settings)); - auto iter = _engines.find(options.feed_engine()); - if (iter == _engines.end()) { + auto iter = me_->engines.find(options.feed_engine()); + if (iter == me_->engines.end()) { LOG(md, Fatal, "Missing feed engine definition '{}' for channel id '{}'. Add a " "feed_engine entry with name:'{}' to the CMEOptions config key.", options.feed_engine(), channel_id, options.feed_engine()); bts_assert_msg( - iter != _engines.end(), "Missing feed engine definition."); + iter != me_->engines.end(), "Missing feed engine definition."); } handler->bindFeedEngine(*(iter->second)); if (recovery) { handler->bindTcpRecovery(*recovery); - _recovery.push_back(std::move(recovery)); + me_->recovery.push_back(std::move(recovery)); } if (logger) { handler->bindLogger(*logger); @@ -377,12 +419,13 @@ unique_ptr MDP3Main::prepare_listener( // heartbeats vector producer_channels; for (auto &symbol : channel.symbols) { - producer_channels.push_back(_producer.get_channel( + producer_channels.push_back(me_->producer.get_channel( symbol.symbol().c_str(), symbol.type(), symbol.exchange())); } unique_ptr listener(new MDP3Listener( - _index, _producer, mbo, producer_channels, trade_summary_dispatcher)); + me_->index, me_->producer, mbo, producer_channels, + trade_summary_dispatcher)); for (auto &symbol : channel.symbols) { listener->add_filter_symbol(symbol.symbol()); } diff --git a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.h b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.h index 449b49ad9..4ba00fc07 100644 --- a/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.h +++ b/src/app_sparkd/sparkd/md/platforms/mdp3_onixs/MDP3Main.h @@ -2,9 +2,9 @@ #include "CMESecurityIndex.h" #include "MDP3Listener.h" +#include "onixs.h" #include "spark/md/coconut2.h" #include "spark/utils/bts_no_copy.h" -#include "onixs.h" #include "sparkd/md/platforms/IMDPlatform.h" #include @@ -29,7 +29,10 @@ struct CMEChannel; */ class MDP3Main : public IMDPlatform { public: - MDP3Main(); + MDP3Main(); + explicit MDP3Main(const std::string &data_path, const std::string &log_path); + MDP3Main(const MDP3Main &) = delete; + MDP3Main &operator=(const MDP3Main &) = delete; ~MDP3Main(); void start( @@ -39,8 +42,8 @@ class MDP3Main : public IMDPlatform { void send_heartbeats() override; void initialize(const std::string &license_store); - std::unique_ptr - prepare_handler(const std::string &channel, MBOWriter *); + std::unique_ptr prepare_handler( + const std::string &channel, MBOWriter *); std::unique_ptr prepare_listener( onixs::Handler &handler, const CMEChannel &channel, @@ -48,16 +51,8 @@ class MDP3Main : public IMDPlatform { TradeSummarySignalDispatcher *); private: - BTS_NO_COPY(MDP3Main); - - CMESecurityIndex _index; - SecurityIDRecorder _recorder; - MDProducer _producer; - // co2::SettlementRecorder _settlement_recorder; - std::map> _engines; - std::map> _handlers; - std::vector> _listeners; - std::vector> _recovery; + struct PImpl; + std::unique_ptr me_; }; } // namespace md diff --git a/src/lib_spark/spark/md/cme/MDP3Utils.cpp b/src/lib_spark/spark/md/cme/MDP3Utils.cpp index 8e45c3624..6ef56c36e 100644 --- a/src/lib_spark/spark/md/cme/MDP3Utils.cpp +++ b/src/lib_spark/spark/md/cme/MDP3Utils.cpp @@ -5,18 +5,15 @@ #include "spark/config/protos/deployment_type.pb.h" #include "spark/config/protos/market_data.pb.h" #include "spark/md/SymbolLoader.h" -#include "spark/utils/bts_chrono.h" -#include "spark/utils/datetime.h" +#include "spark/md/cme/mdp3_data_directory.h" #include "spark/utils/download.h" #include "system/FMT.h" #include "system/logging/logging.h" -#include "system/protobufs/config/system_config.pb.h" #include -#include -#include #include #include +#include using namespace std; using namespace bts::api; @@ -25,16 +22,6 @@ namespace bts { namespace md { using bts::domain::SecurityCriteria; -using bts::system::protobufs::SystemConfig; - -string data_path() { - auto p = config::get().data_path() + "cme_mdp3/"; - os::normalize_path(p); - return p; -} -const string config_path() { return data_path() + "config.xml"; } -const string template_path() { return data_path() + "templates.xml"; } -const std::string log_path() { return logging::log_path().string() + "/onix"; } bool check_file_exists_with_instructions( const string &filename, const string &url, bool log) { @@ -75,8 +62,9 @@ void await_cme_config() { "templates_FixBinary.xml"; } - boost::filesystem::create_directories(data_path()); - boost::filesystem::create_directories(log_path()); + Mdp3DataDirectory data_dir; + boost::filesystem::create_directories(data_dir.data_dir()); + boost::filesystem::create_directories(data_dir.log_dir()); // Check config is present and give instructions for downloading it if not // If the file is not present log an error and wait a second. Continue to log @@ -84,13 +72,14 @@ void await_cme_config() { // killed. uint32_t count = 0; while (!check_file_exists_with_instructions( - config_path(), config_url, is_pow_two_or_0(count))) { + data_dir.config_file().string(), config_url, is_pow_two_or_0(count))) { ++count; std::this_thread::sleep_for(1s); } count = 0; while (!check_file_exists_with_instructions( - template_path(), templates_url, is_pow_two_or_0(count))) { + data_dir.templates_file().string(), templates_url, + is_pow_two_or_0(count))) { ++count; std::this_thread::sleep_for(1s); } @@ -133,14 +122,14 @@ std::string to_str(const std::vector &vec) { return ss.str(); } const vector parse_channels(const vector &symbols) { + Mdp3DataDirectory data_dir{}; LOG(md, Info, "parse_channels symbols: {}", FMT_JOIN(symbols, ", ")); - await_cme_config(); vector result; bool skip_options = config::get().skip_options(); - pugi::xml_document doc; bts_assert_msg( - doc.load_file(config_path().c_str()), "Error parsing config.xml as xml"); + doc.load_file(data_dir.config_file().c_str()), + "Error parsing config.xml as xml"); auto configuration = doc.root().child("configuration"); bts_assert(configuration); diff --git a/src/lib_spark/spark/md/cme/MDP3Utils.h b/src/lib_spark/spark/md/cme/MDP3Utils.h index 6e1e241fe..5d220330b 100644 --- a/src/lib_spark/spark/md/cme/MDP3Utils.h +++ b/src/lib_spark/spark/md/cme/MDP3Utils.h @@ -1,24 +1,17 @@ #pragma once +#include "spark/md/Shm/FixedSecurity.h" #include "spark/md/cme/CMEChannel.h" -#include "spark/md/cme/CMEConnection.h" -#include "spark/md/coconut2.h" -#include "system/FMT_IMPL.h" -#include -#include #include namespace bts::md { -void await_cme_config(); const std::vector get_configured_cme_channels(); + const std::vector parse_channels( const std::vector &symbols); -const std::string config_path(); -const std::string template_path(); -const std::string log_path(); //https://www.cmegroup.com/confluence/display/EPICSANDBOX/MDP%203.0%20-%20CME%20Globex%20Pricing inline void decode_ticks(int vtt, double mintick, FixedSecurity &dest) { switch (vtt) { diff --git a/src/lib_spark/spark/md/cme/mdp3_data_directory.h b/src/lib_spark/spark/md/cme/mdp3_data_directory.h new file mode 100644 index 000000000..4cf8f7ae3 --- /dev/null +++ b/src/lib_spark/spark/md/cme/mdp3_data_directory.h @@ -0,0 +1,32 @@ +#pragma once + +#include "spark/spark_data_directory.h" + +#include +#include + +namespace bts::md { + +struct Mdp3DataDirectory { + boost::filesystem::path log_dir() const { return root.log_dir() / "onixs"; } + + boost::filesystem::path data_dir() const { return root.root() / "cme_mdp3"; } + + boost::filesystem::path config_file() const { + return data_dir() / "config.xml"; + } + + boost::filesystem::path templates_file() const { + return data_dir() / "templates.xml"; + } + + void create_dirs() { + boost::filesystem::create_directories(data_dir()); + boost::filesystem::create_directories(log_dir()); + } + + private: + spark::SparkDataDirectory root{}; +}; + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/ice/ICEUtils.cpp b/src/lib_spark/spark/md/ice/ICEUtils.cpp index 87b41e933..4f22e3605 100644 --- a/src/lib_spark/spark/md/ice/ICEUtils.cpp +++ b/src/lib_spark/spark/md/ice/ICEUtils.cpp @@ -4,6 +4,7 @@ #include "domain/security_criteria.h" #include "spark/config/config.h" #include "spark/md/SymbolLoader.h" +#include "spark/md/ice/ice_data_directory.h" #include "spark/utils/bts_chrono.h" #include "spark/utils/datetime.h" #include "spark/utils/download.h" @@ -24,25 +25,15 @@ namespace md { namespace ice { using bts::domain::SecurityCriteria; -using bts::system::protobufs::SystemConfig; - -std::string ICEChannel::to_string() const { - std::string mt = name; - for (const auto &x : market_types) - mt += " " + std::to_string(x); - for (const auto &x : uds_market_types) - mt += " " + std::to_string(x); - return mt; -} // We are sort of punting on symbols here because there is no symbol -> channel // mapping supplied by ice therefore we must 1) enumerate the channels we want // in config, and smash them together with the list of symbols we care about. // This has the unfortunate side effect of not knowing what channel should // heartbeat what symbol but Eric says this is ok -const vector parse_ice_channels(const vector &symbols) { +const vector parse_ice_channels( + const vector &symbols, const ICEOptions &ice_options) { vector result; - const auto &ice_options = config::get(); for (const auto &x : ice_options.channel()) { md::ice::ICEChannel chan; chan.name = x.name(); @@ -57,22 +48,6 @@ const vector parse_ice_channels(const vector &symbols) { return result; } -string ice_data_path() { - auto p = config::get().data_path() + "onixs_ice/"; - os::normalize_path(p); - return p; -} -const string ice_config_file() { - return config::get().ice_config_file(); -} -const string ice_config_path() { - return ice_data_path() + "/" + ice_config_file(); -} - -const std::string ice_log_path() { - return logging::log_path().string() + "/onix_ice"; -} - bool ice_check_file_exists_with_instructions( const string &filename, const string &url, bool log) { // if (!boost::filesystem::exists(filename)) { @@ -104,14 +79,15 @@ void await_ice_config() { config_url = "https://community.theice.com/docs/DOC-19989"; } - boost::filesystem::create_directories(ice_data_path()); - boost::filesystem::create_directories(ice_log_path()); + IceDataDirectory data_dir; + boost::filesystem::create_directories(data_dir.data_dir()); + boost::filesystem::create_directories(data_dir.log_dir()); // Check config is present and give instructions for downloading it if not // If the file is not present log an error and wait a second. Continue to log // the error ever power of 2 seconds until the file appears or the process is // killed. - auto configpath = ice_config_path(); + const auto configpath = data_dir.config_file().string(); uint32_t count = 0; while (!ice_check_file_exists_with_instructions( configpath, config_url, is_pow_two_or_0(count))) { @@ -127,9 +103,9 @@ void await_ice_config() { const std::vector get_configured_ice_channels() { // Get all ice symbols - const auto symbols = - SymbolLoader::load(SecurityCriteria::create({domain::protobufs::VENUE_ICE})); - return parse_ice_channels({symbols.begin(), symbols.end()}); + const auto symbols = SymbolLoader::load( + SecurityCriteria::create({domain::protobufs::VENUE_ICE})); + return ICEChannel::parse_all({symbols.begin(), symbols.end()}); } } // namespace ice diff --git a/src/lib_spark/spark/md/ice/ICEUtils.h b/src/lib_spark/spark/md/ice/ICEUtils.h index 8a1b418c5..653fedd93 100644 --- a/src/lib_spark/spark/md/ice/ICEUtils.h +++ b/src/lib_spark/spark/md/ice/ICEUtils.h @@ -1,6 +1,8 @@ #pragma once #include "spark/md/coconut2.h" +#include "spark/md/ice/ice_channel.h" + #include #include #include @@ -9,18 +11,18 @@ namespace bts { namespace md { namespace ice { -struct ICEChannel { - std::string name; - std::vector market_types; - std::vector uds_market_types; - std::set symbols; - std::string to_string() const; -}; +// struct ICEChannel { +// std::string name; +// std::vector market_types; +// std::vector uds_market_types; +// std::set symbols; +// std::string to_string() const; +// }; void await_ice_config(); const std::vector get_configured_ice_channels(); -const std::vector -parse_ice_channels(const std::vector &symbols); +// const std::vector parse_ice_channels( +// const std::vector &symbols); const std::string ice_config_path(); const std::string ice_log_path(); diff --git a/src/lib_spark/spark/md/ice/ice_channel.cpp b/src/lib_spark/spark/md/ice/ice_channel.cpp new file mode 100644 index 000000000..36ca63419 --- /dev/null +++ b/src/lib_spark/spark/md/ice/ice_channel.cpp @@ -0,0 +1,39 @@ +#include "ice_channel.h" +#include "spark/config/config.h" +#include "spark/config/protos/market_data.pb.h" + +namespace bts::md::ice { + +std::string ICEChannel::to_string() const { + std::string mt = name; + for (const auto &x : market_types) + mt += " " + std::to_string(x); + for (const auto &x : uds_market_types) + mt += " " + std::to_string(x); + return mt; +} + +// We are sort of punting on symbols here because there is no symbol -> channel +// mapping supplied by ice therefore we must 1) enumerate the channels we want +// in config, and smash them together with the list of symbols we care about. +// This has the unfortunate side effect of not knowing what channel should +// heartbeat what symbol but Eric says this is ok +std::vector ICEChannel::parse_all( + const std::vector &symbols) { + std::vector result; + const auto &ice_options = config::get(); + for (const auto &x : ice_options.channel()) { + md::ice::ICEChannel chan; + chan.name = x.name(); + chan.symbols.insert(symbols.begin(), symbols.end()); + for (const auto &xx : x.market_type()) + chan.market_types.push_back(xx); + for (const auto &xx : x.uds_market_type()) + chan.uds_market_types.push_back(xx); + + result.push_back(chan); + } + return result; +} + +} // namespace bts::md::ice diff --git a/src/lib_spark/spark/md/ice/ice_channel.h b/src/lib_spark/spark/md/ice/ice_channel.h new file mode 100644 index 000000000..47d077e6c --- /dev/null +++ b/src/lib_spark/spark/md/ice/ice_channel.h @@ -0,0 +1,23 @@ +#pragma once + +#include "spark/md/UserSymbol.h" + +#include +#include +#include +#include + +namespace bts::md::ice { + +struct ICEChannel { + std::string name; + std::vector market_types; + std::vector uds_market_types; + std::set symbols; + std::string to_string() const; + + static std::vector parse_all( + const std::vector &symbols); +}; + +} // namespace bts::md::ice diff --git a/src/lib_spark/spark/md/ice/ice_data_directory.h b/src/lib_spark/spark/md/ice/ice_data_directory.h new file mode 100644 index 000000000..ccec8b2e7 --- /dev/null +++ b/src/lib_spark/spark/md/ice/ice_data_directory.h @@ -0,0 +1,36 @@ +#pragma once + +#include "spark/spark_data_directory.h" + +#include +#include + +namespace bts::md { + +struct IceDataDirectory { + boost::filesystem::path log_dir() const { + return root.log_dir() / "onixs_ice/"; + } + + boost::filesystem::path data_dir() const { + return root.root() / "onixs_ice/"; + } + + boost::filesystem::path config_file() const { + return data_dir() / "config.xml"; + } + + boost::filesystem::path templates_file() const { + return data_dir() / "templates.xml"; + } + + void create_dirs() { + boost::filesystem::create_directories(data_dir()); + boost::filesystem::create_directories(log_dir()); + } + + private: + spark::SparkDataDirectory root{}; +}; + +} // namespace bts::md diff --git a/src/lib_spark/spark/spark_data_directory.cpp b/src/lib_spark/spark/spark_data_directory.cpp new file mode 100644 index 000000000..ae6a63f16 --- /dev/null +++ b/src/lib_spark/spark/spark_data_directory.cpp @@ -0,0 +1,31 @@ +#include "spark_data_directory.h" +#include "spark/config/config.h" +#include "system/protobufs/config/system_config.pb.h" + +#include +#include +#include + +namespace bts::spark { + +SparkDataDirectory::SparkDataDirectory() + : SparkDataDirectory( + config::get().data_path()) {} + +SparkDataDirectory::SparkDataDirectory(const std::string &root) : root_(root) {} + +boost::filesystem::path SparkDataDirectory::root() const { return root_; } + +boost::filesystem::path SparkDataDirectory::log_dir() const { + return root_ / "logging"; +} + +boost::filesystem::path SparkDataDirectory::log_file() const { + return log_dir() / "log"; +} + +boost::filesystem::path SparkDataDirectory::log_cache_file() const { + return log_dir() / "log_cache"; +} + +} // namespace bts::spark diff --git a/src/lib_spark/spark/spark_data_directory.h b/src/lib_spark/spark/spark_data_directory.h new file mode 100644 index 000000000..9805bb373 --- /dev/null +++ b/src/lib_spark/spark/spark_data_directory.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace bts::spark { + +class SparkDataDirectory { + public: + SparkDataDirectory(); + + explicit SparkDataDirectory(const std::string &root); + + boost::filesystem::path root() const; + boost::filesystem::path log_dir() const; + boost::filesystem::path log_file() const; + boost::filesystem::path log_cache_file() const; + + private: + boost::filesystem::path root_; +}; + +} // namespace bts::spark