From 17402ce94a413455e47d0549db476f784c48815d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 18 Mar 2023 12:06:21 -0400 Subject: [PATCH] refactor: Replace os::bts_data_path w/ SystemConfig::data_path --- src/lib/config/ConfigurationState.cpp | 2 +- src/lib/jetfix/store/FIXFileStore.cpp | 6 ++- src/lib/logging/logging.cpp | 21 ++++------ src/lib/md/vendor/OnixsUtils.cpp | 6 ++- src/lib/orderman/ilink3/SessionStore.cpp | 6 ++- src/lib/os/os.cpp | 12 ------ src/lib/os/os.h | 1 - src/lib/persistence/SqliteStore.cpp | 6 +-- src/lib/persistence/persistence_utils.cpp | 11 ++--- src/lib/persistence/persistence_utils.h | 3 -- src/lib/utils/Puid.cpp | 42 +++++++++++-------- src/lib/utils/file_utils.cpp | 7 ++-- .../cmds/spartan/back_testing/sim/run_sim.cpp | 4 +- .../gateway/detail/IL3SessionFileStore.cpp | 4 +- .../sparkd/exchange_sim/ExchangeSim.cpp | 6 ++- .../sparkd/md/platforms/ice_onixs/ICEMain.cpp | 12 +++--- 16 files changed, 75 insertions(+), 74 deletions(-) diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index 179b97923..69b65247f 100644 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -142,7 +142,7 @@ void ConfigurationState::initialize_builtins() { sys.set_platform(os::platform_name()); sys.set_qualifier(os::get_qualifier()); sys.set_unittest_environment(utils::internal::is_unittest_environment()); - sys.set_data_path(os::bts_data_path()); + sys.set_data_path(os::app_data_path("data/").string()); sys.set_os_user(os::username()); #ifndef NDEBUG sys.set_build_type("debug"); diff --git a/src/lib/jetfix/store/FIXFileStore.cpp b/src/lib/jetfix/store/FIXFileStore.cpp index 2f8b5e9c6..e759da9ee 100644 --- a/src/lib/jetfix/store/FIXFileStore.cpp +++ b/src/lib/jetfix/store/FIXFileStore.cpp @@ -1,6 +1,7 @@ #include "FIXFileStore.h" #include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/jetfix/codec/FIXEncodingStream.h" #include "lib/jetfix/common/InMsgBuffer.h" #include "lib/jetfix/common/OutFIX.h" @@ -106,8 +107,9 @@ std::string store_path( std::string base_path, std::string const &connection_name, std::string const &session_name) { - if (base_path.empty()) - base_path = os::bts_data_path(); + if (base_path.empty()) { + base_path = config::get().data_path(); + } boost::filesystem::path result = utils::normalize_data_path(base_path); result /= "jetfix"; result /= connection_name; diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index ab2f8c161..a1b2c1702 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -4,6 +4,7 @@ #include "lib/config/config.h" #include "lib/config/protos/deployment_type.pb.h" #include "lib/config/protos/logging.pb.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/os/os.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/bts_assert.h" @@ -28,6 +29,9 @@ namespace bts { namespace log { +using bts::config::SystemConfig; +using Path = boost::filesystem::path; + static LoggerHub _default_logger_hub("logger"); thread_local LoggerHandle _thread_logger(_default_logger_hub.make_logger()); @@ -40,12 +44,8 @@ find_key(M const &m, typename M::mapped_type const &v) { return m.end(); } -boost::filesystem::path log_path() { - auto store_root = os::bts_data_path(); - std::string base_path = store_root; - boost::filesystem::path result = utils::normalize_data_path(base_path); - result /= "logging"; - return result; +Path log_path() { + return Path{config::get().data_path()} / "logging/"; } void start_default_logging_thread() { _default_logger_hub.start(); } @@ -125,12 +125,9 @@ void configure_logging( configure_logging(real_cfg); } -boost::filesystem::path log_cache_file_path() { - auto store_root = os::bts_data_path(); - boost::filesystem::path result = utils::normalize_data_path(store_root); - result /= "logging"; - result /= "log_control"; - return result; +Path log_cache_file_path() { + return Path{config::get().data_path()} / "logging" / + "log_control"; } LoggingConfigPoller::LoggingConfigPoller( diff --git a/src/lib/md/vendor/OnixsUtils.cpp b/src/lib/md/vendor/OnixsUtils.cpp index 04d90d38a..95b1c288b 100644 --- a/src/lib/md/vendor/OnixsUtils.cpp +++ b/src/lib/md/vendor/OnixsUtils.cpp @@ -2,6 +2,7 @@ #include "OnixsUtils.h" #include "lib/config/config.h" #include "lib/config/protos/market_data.pb.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/logging/logging.h" #include #include @@ -9,6 +10,9 @@ namespace bts { namespace md { +using bts::config::SystemConfig; +using Path = boost::filesystem::path; + // The onixs checksum includes the line endings as \r\n // to avoid the possibility of git mangling the line endings we store // an array of lines instead of a single string and restore the lines @@ -38,7 +42,7 @@ OnixsTemplLicense onixs_create_temporary_license() { OnixsTemplLicense linfo; linfo.path = options.onixs_license(); if (linfo.path == "{default}") { - linfo.path = boost::filesystem::path{os::bts_data_path()} / "licenses"; + linfo.path = Path{config::get().data_path()} / "licenses"; auto ident = boost::filesystem::unique_path("%%%%-%%%%-%%%%-%%%%.lic"); linfo.license = {linfo.path / ident}; } diff --git a/src/lib/orderman/ilink3/SessionStore.cpp b/src/lib/orderman/ilink3/SessionStore.cpp index ac3eee808..c2415514e 100644 --- a/src/lib/orderman/ilink3/SessionStore.cpp +++ b/src/lib/orderman/ilink3/SessionStore.cpp @@ -1,4 +1,5 @@ #include "SessionStore.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/jetfix/store/session_name.h" #include "lib/orderman/ilink3/IL3GW.h" #include "lib/orderman/ilink3/IL3SecDB.h" @@ -24,8 +25,9 @@ static std::string base_store_path( std::string base_path, std::string const &connection_name, std::string const &session_name) { - if (base_path.empty()) - base_path = os::bts_data_path(); + if (base_path.empty()) { + base_path = config::get().data_path(); + } boost::filesystem::path result = utils::normalize_data_path(base_path); result /= "ilink3"; result /= connection_name; diff --git a/src/lib/os/os.cpp b/src/lib/os/os.cpp index f39630db4..edc03c5d3 100644 --- a/src/lib/os/os.cpp +++ b/src/lib/os/os.cpp @@ -688,8 +688,6 @@ boost::filesystem::path app_data_path( return path; } -std::string bts_data_path() { return app_data_path("data/").string(); } - std::string username() { #ifdef _WIN32 char buffer[1024]; @@ -1031,16 +1029,6 @@ int cache_size_l3_bytes() { // namespace interop { -extern "C" BTS_EXPORT char *bts_data_path() { - std::string p = os::bts_data_path(); - char *result = new char[p.size() + 1]; - result[p.size()] = '\0'; - std::copy(p.begin(), p.end(), result); - return result; -} - -extern "C" BTS_EXPORT void free_bts_data_path(char *dp) { delete[] dp; } - extern "C" BTS_EXPORT unsigned int setup_process_job_object() { #ifdef _WIN32 if (IsDebuggerPresent()) diff --git a/src/lib/os/os.h b/src/lib/os/os.h index 0cf2c55e9..89b816add 100644 --- a/src/lib/os/os.h +++ b/src/lib/os/os.h @@ -90,7 +90,6 @@ boost::filesystem::path app_data_path(); boost::filesystem::path app_data_path( const boost::filesystem::path &relative_path, bool mkdirs = true); -std::string bts_data_path(); std::string username(); BTS_EXPORT char *strncpy(char *dest, const char *src, size_t count); diff --git a/src/lib/persistence/SqliteStore.cpp b/src/lib/persistence/SqliteStore.cpp index 578fad1e4..fb8eac840 100644 --- a/src/lib/persistence/SqliteStore.cpp +++ b/src/lib/persistence/SqliteStore.cpp @@ -1,5 +1,7 @@ #include "SqliteStore.h" +#include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/logging/logging.h" #include "lib/os/os.h" #include "lib/persistence/persistence_utils.h" @@ -37,9 +39,7 @@ struct SqliteStore::PImpl { }; SqliteStore::SqliteStore(std::string const &db) - : me(new PImpl(os::bts_data_path(), db)) { - init_db(); -} + : SqliteStore(config::get().data_path(), db) {} SqliteStore::SqliteStore(std::string const &root_path, std::string const &db) : me(new PImpl(root_path, db)) { diff --git a/src/lib/persistence/persistence_utils.cpp b/src/lib/persistence/persistence_utils.cpp index 8156055c5..4ea6b12d8 100644 --- a/src/lib/persistence/persistence_utils.cpp +++ b/src/lib/persistence/persistence_utils.cpp @@ -1,4 +1,6 @@ #include "lib/persistence/persistence_utils.h" +#include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/os/os.h" #include "lib/utils/file_utils.h" @@ -7,12 +9,7 @@ namespace bts { namespace persistence { -std::string db_location() { - boost::filesystem::path result = - utils::normalize_data_path(os::bts_data_path()); - result /= "databases"; - return result.string(); -} +using bts::config::SystemConfig; std::string storage_location(std::string const &db, std::string const &store_root) { @@ -25,7 +22,7 @@ storage_location(std::string const &db, std::string const &store_root) { } std::string storage_location(std::string const &db) { - return storage_location(db, os::bts_data_path()); + return storage_location(db, config::get().data_path()); } } // namespace persistence diff --git a/src/lib/persistence/persistence_utils.h b/src/lib/persistence/persistence_utils.h index 9924a1b21..4a65fad86 100644 --- a/src/lib/persistence/persistence_utils.h +++ b/src/lib/persistence/persistence_utils.h @@ -8,9 +8,6 @@ namespace persistence { // default storage for database named db std::string storage_location(std::string const &db); -// default storage location for all databases -std::string db_location(); - // custom storage root (for testing) std::string storage_location(std::string const &db, std::string const &storage_root); diff --git a/src/lib/utils/Puid.cpp b/src/lib/utils/Puid.cpp index a242bde75..b54ea6b00 100644 --- a/src/lib/utils/Puid.cpp +++ b/src/lib/utils/Puid.cpp @@ -1,5 +1,8 @@ +#include #ifndef _WIN32 #include "Puid.h" +#include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "../os/os.h" #include "../utils/bts_assert.h" @@ -23,6 +26,10 @@ namespace bts { namespace utils { +using bts::config::SystemConfig; +using DirectoryIterator = boost::filesystem::directory_iterator; +using Path = boost::filesystem::path; + // use a single global lock for simplicity. we can do a per domain // lock if we ever need it. we need a lock because posix locks files // per process not per thread. @@ -76,15 +83,13 @@ void init_store( } } -std::string find_file(std::string const &path, std::string const &ending) { - for (boost::filesystem::directory_iterator p = - boost::filesystem::directory_iterator(path); - p != boost::filesystem::directory_iterator(); p++) { - std::string fn(p->path().string()); - if (boost::algorithm::ends_with(fn, ending)) - return fn; +Path find_file(std::string const &path, std::string const &ending) { + for (DirectoryIterator p(path); p != DirectoryIterator(); p++) { + if (boost::algorithm::ends_with(p->path().string(), ending)) { + return p->path(); + } } - return std::string(); + return {}; } // boost::filesystem::copy_file is broken for C++ 11 unless one compiles boost @@ -154,20 +159,21 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) { // we risk race conditions if the directory is not there. // but that's OK, this is rare and easily manageable. - std::string path = os::bts_data_path() + "/puids/" + scope; - std::string lock_path = path + "/" + lock_file_name; - std::string counter_path = path + "/" + counter_file_name; + const auto path = + Path{config::get().data_path()} / "puids" / scope; + auto lock_path = path / lock_file_name; + auto counter_path = path / counter_file_name; - init_store(path, lock_path, counter_path); + init_store(path.string(), lock_path.string(), counter_path.string()); // W1 boost::interprocess::file_lock f_lock(lock_path.c_str()); boost::interprocess::scoped_lock scope_lock( f_lock); - std::string tmp_file = find_file(path, ".tmp"); - std::string alt_file = find_file(path, ".alt"); - std::string counter_file = find_file(path, counter_file_name); + auto tmp_file = find_file(path.string(), ".tmp"); + auto alt_file = find_file(path.string(), ".alt"); + const auto counter_file = find_file(path.string(), counter_file_name); // r1? if (counter_file.size() == 0 && tmp_file.size() && alt_file.size()) { @@ -194,7 +200,7 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) { } else { // W2, copy counter file to a temp file - tmp_file = path + "/" + Uuid().to_text_string() + ".tmp"; + tmp_file = path / (Uuid().to_text_string() + ".tmp"); copy_file(counter_path, tmp_file); // W3, W4 update temp file, close and flush @@ -203,7 +209,7 @@ uint64_t reserve_ids(std::string const &scope, int n_ids) { std::ofstream(tmp_file.c_str()) << counter_value + n_ids; // W5, rename counter file to an alt file (renames are atomic) - alt_file = path + "/" + Uuid().to_text_string() + ".alt"; + alt_file = path / (Uuid().to_text_string() + ".alt"); boost::filesystem::rename(counter_path, alt_file); // W6, rename temp file to counter file @@ -282,4 +288,4 @@ Puid gen_puid(std::string const &scope) { } // namespace utils } // namespace bts -#endif \ No newline at end of file +#endif diff --git a/src/lib/utils/file_utils.cpp b/src/lib/utils/file_utils.cpp index bd8fdcb5d..dfa943533 100644 --- a/src/lib/utils/file_utils.cpp +++ b/src/lib/utils/file_utils.cpp @@ -20,9 +20,10 @@ bool create_if_not_there(std::string const &path) { } std::string normalize_data_path(std::string const &input_path) { - std::string result(input_path); - boost::algorithm::replace_first(result, "$data_path", os::bts_data_path()); - return result; + // TODO: Remove this method (just need to be 100% sure this substitution + // isn't used in practice) + bts_assert(input_path.find("$data_path") == std::string::npos); + return input_path; } } // namespace utils diff --git a/src/server/sparkd/cmds/spartan/back_testing/sim/run_sim.cpp b/src/server/sparkd/cmds/spartan/back_testing/sim/run_sim.cpp index b3aea9769..935ef025f 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/sim/run_sim.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/sim/run_sim.cpp @@ -5,6 +5,8 @@ #include "back_testing/sim/SimDB.h" #include "back_testing/sim/Sim.h" #include "back_testing/sim/PCapEventSource.h" +#include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/utils/PBUtils.h" #include #include @@ -13,7 +15,7 @@ namespace bts::spartan::mdp3 { std::string pcap_location(std::string const &channel, std::string const &date) { - auto base_path = os::bts_data_path(); + auto base_path = config::get().data_path(); auto pcap_path = fmt::format( "{}pcaps/{}/{}_{}_24h.pcap", base_path, channel, date, channel); auto xz_pcap_path = pcap_path + ".xz"; diff --git a/src/server/sparkd/cmds/spartan/gateway/detail/IL3SessionFileStore.cpp b/src/server/sparkd/cmds/spartan/gateway/detail/IL3SessionFileStore.cpp index e08b36a51..1f2f13774 100644 --- a/src/server/sparkd/cmds/spartan/gateway/detail/IL3SessionFileStore.cpp +++ b/src/server/sparkd/cmds/spartan/gateway/detail/IL3SessionFileStore.cpp @@ -1,4 +1,6 @@ #include "IL3SessionFileStore.h" +#include "lib/config/config.h" +#include "lib/config/protos/system_config.pb.h" #include "lib/utils/datetime.h" #include "lib/utils/file_utils.h" #include "lib/utils/type_name.h" @@ -12,7 +14,7 @@ namespace bts::spartan { static std::string store_path(std::string const &session_kind, std::string const &session_name) { - auto base_path = os::bts_data_path(); + auto base_path = config::get().data_path(); boost::filesystem::path result = utils::normalize_data_path(base_path); result /= "spartan"; result /= "il3"; diff --git a/src/server/sparkd/exchange_sim/ExchangeSim.cpp b/src/server/sparkd/exchange_sim/ExchangeSim.cpp index 3c4a0d775..8405ce74e 100644 --- a/src/server/sparkd/exchange_sim/ExchangeSim.cpp +++ b/src/server/sparkd/exchange_sim/ExchangeSim.cpp @@ -13,7 +13,7 @@ #include "lib/config/config.h" #include "lib/config/protos/exchange_sim_cfg.pb.h" #include "lib/config/protos/ports.pb.h" - +#include "lib/config/protos/system_config.pb.h" #include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" #include "lib/os/os.h" @@ -141,7 +141,9 @@ int start_exchange_sim(bts::api::Venue venue, md::MBOWriter *mbo) { os::get_thread_id(), t_name); auto str_settings = std::string(cfg); auto venue_string = boost::to_lower_copy(api::Venue_Name(venue)); - boost::replace_all(str_settings, "$BTS_DATA_PATH", os::bts_data_path()); + boost::replace_all( + str_settings, "$BTS_DATA_PATH", + config::get().data_path()); boost::replace_all(str_settings, "$SIM_VENUE", venue_string); std::string port_string; switch (venue) { diff --git a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp index dad398552..9dc1c37f9 100644 --- a/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp +++ b/src/server/sparkd/md/platforms/ice_onixs/ICEMain.cpp @@ -43,6 +43,9 @@ namespace bts { namespace md { namespace ice { +using bts::config::SystemConfig; +using Path = boost::filesystem::path; + ICEMain::ICEMain() : _index(), _recorder( {utils::Dispatcher::current(), @@ -88,12 +91,11 @@ market_subcription_for_channel(const ICEChannel &chan) { } void ICEMain::start(std::vector &symbols) { - auto ice_conf_location = os::bts_data_path() + "/onixs_ice/"; + auto ice_conf_location = + Path{config::get().data_path()} / "onixs_ice/"; if (!boost::filesystem::is_directory(ice_conf_location)) { - auto conf_path = os::sparkd_wd() + "/data_files/onixs_ice/"; - recursive_copy( - boost::filesystem::path(conf_path), - boost::filesystem::path(ice_conf_location)); + auto conf_path = Path{os::sparkd_wd()} / "data_files" / "onixs_ice/"; + recursive_copy(Path{conf_path}, ice_conf_location); } await_ice_config();