build: Resolve app data dir and shared mem prefix at runtime
We can't define them at compile time anymore since we build multiple exes that
depend on libspark in the same dir (e.g., api-server, pubsub, sparkd, etc.),
and each one needs its own shared memory and app data filespaces.
For shared memory:
* Prefix all shm files with the proc name and an environment variable,
BTS_SHARED_MEMORY_PREFIX (if defined). The env var is only necessary
when 2 exe's with the same name run simultaneously.
For app data:
* On linux, the default is:
/home/<user_name>/.local/share/bts/<proc_name>
* On windows, the default is:
/User/<user_name>/AppData/Roaming/bts/<proc_name>
* This path can be overridden in the environment variable:
<proc_name>__APPDATA
- For example, SPARKD__APPDATA, PUBSUB__APPDATA, etc.
This commit is contained in:
1 parent
26853d597a
commit
ea21c3e7ff
9 files changed
+117
-57
No files matched your search
+3
-4
@@ -21,7 +21,7 @@ set(
|
||||
# SHARED_MEMORY_PREFIX serves as a kind of namespace so multiple applications
|
||||
# can run simultaneously without fighting over the same shm files (e.g., sparkd,
|
||||
# api server, etc.).
|
||||
set(SHARED_MEMORY_PREFIX "bts" CACHE STRING "Prefix to use when creating shared memory files")
|
||||
set(SHARED_MEMORY_PREFIX "\${BTS_SHARED_MEMORY_PREFIX}" CACHE STRING "Prefix to use when creating shared memory files")
|
||||
set(EXTERNALS_BIN "${EXTERNALS_ROOT}/bin")
|
||||
set(EXTERNALS_INCLUDE "${EXTERNALS_ROOT}/include")
|
||||
set(EXTERNALS_LIB "${EXTERNALS_ROOT}/lib/$<CONFIG>")
|
||||
@@ -40,9 +40,8 @@ include_directories(BEFORE SYSTEM ${EXTERNALS_INCLUDE} ${EXTERNALS_ROOT}/include
|
||||
link_directories(${EXTERNALS_LIB})
|
||||
|
||||
add_compile_definitions(SHARED_MEMORY_PREFIX="${SHARED_MEMORY_PREFIX}")
|
||||
add_compile_definitions(APP_DATA_DIR="${APP_DATA_DIR}")
|
||||
if(DEFINED APP_DATA_DIR_OVERRIDE)
|
||||
add_compile_definitions(APP_DATA_DIR_OVERRIDE="${APP_DATA_DIR_OVERRIDE}")
|
||||
if(DEFINED APP_DATA_DIR)
|
||||
add_compile_definitions(APP_DATA_DIR="${APP_DATA_DIR}")
|
||||
endif()
|
||||
add_compile_definitions(EXPORT_MD_CONSUMER="${EXPORT_MD_CONSUMER}")
|
||||
if(WIN32)
|
||||
|
||||
+6
-22
@@ -20,8 +20,6 @@
|
||||
"SPARK_CMAKE_CXX_FLAGS": "/MP /WX /wd4250 /wd4251 /wd4244 /wd4267 /wd4355 /wd4275 /wd4345 /bigobj /EHsc /DCURL_STATICLIB /D_WINSOCK_DEPRECATED_NO_WARNINGS /D__TBB_NO_IMPLICIT_LINKAGE /DBOOST_ALL_NO_LIB /std:c++20 /D_SCL_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS /D_WIN32_WINNT=0x0600"
|
||||
},
|
||||
"cacheVariables": {
|
||||
"APP_DATA_DIR": "${dollar}{APPDATA}/bts",
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_DATA_DIR}",
|
||||
"CMAKE_CXX_FLAGS": "$env{SPARK_CMAKE_CXX_FLAGS}",
|
||||
"CMAKE_CXX_FLAGS_DEBUG": "$env{SPARK_CMAKE_CXX_FLAGS} /MTd",
|
||||
"CMAKE_CXX_FLAGS_RELEASE": "$env{SPARK_CMAKE_CXX_FLAGS} /MT",
|
||||
@@ -36,8 +34,7 @@
|
||||
"EXPORT_MD_CONSUMER": {
|
||||
"type": "BOOL",
|
||||
"value": "ON"
|
||||
},
|
||||
"SHARED_MEMORY_PREFIX": "spark"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -53,8 +50,6 @@
|
||||
"binaryDir": "${sourceDir}/build/${presetName}",
|
||||
"cacheVariables": {
|
||||
"platform": "",
|
||||
"APP_DATA_DIR": "${dollar}{HOME}/sparkdata",
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{SPARKD_DATA_DIR}",
|
||||
"CMAKE_BUILD_WITH_INSTALL_RPATH": {
|
||||
"type": "BOOL",
|
||||
"value": "OFF"
|
||||
@@ -77,8 +72,7 @@
|
||||
"BUILD_ADMIN_WEB_APP": {
|
||||
"type": "BOOL",
|
||||
"value": "ON"
|
||||
},
|
||||
"SHARED_MEMORY_PREFIX": "sparkd"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -159,11 +153,7 @@
|
||||
{
|
||||
"name": "spark-api-server-windows",
|
||||
"inherits": "spark",
|
||||
"displayName": "Spark Api Server (Windows)",
|
||||
"cacheVariables": {
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_API_SERVER_DATA_DIR}",
|
||||
"SHARED_MEMORY_PREFIX": "spark_api_server"
|
||||
}
|
||||
"displayName": "Spark Api Server (Windows)"
|
||||
},
|
||||
|
||||
{
|
||||
@@ -172,7 +162,6 @@
|
||||
"displayName": "Spark Api Server (Linux)",
|
||||
"generator": "Ninja Multi-Config",
|
||||
"cacheVariables": {
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_API_SERVER_DATA_DIR}",
|
||||
"BUILD_ADMIN_WEB_APP": {
|
||||
"type": "BOOL",
|
||||
"value": "OFF"
|
||||
@@ -191,7 +180,7 @@
|
||||
"value": "ON"
|
||||
},
|
||||
"CONFIG_DEFAULTS_PATH": "spark/config/profiles/default/*.conf",
|
||||
"DOTNET_BINARY_DIR": "${sourceDir}/build/$<CONFIG>/bin/net8.0",
|
||||
"DOTNET_BINARY_DIR": "${sourceDir}/build/Spark.Api.Server/$<CONFIG>/bin/net8.0",
|
||||
"EXPORT_MD_CONSUMER": {
|
||||
"type": "BOOL",
|
||||
"value": "ON"
|
||||
@@ -229,10 +218,8 @@
|
||||
"description": "Data Stream utils ported over from Edge",
|
||||
"binaryDir": "${sourceDir}/build/${presetName}",
|
||||
"cacheVariables": {
|
||||
"APP_DATA_DIR": "${dollar}{APPDATA}/bts/DataStream",
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{DATA_STREAM_DATA_DIR}",
|
||||
"CONFIG_DEFAULTS_PATH": "spark/config/profiles/default/*.conf;spark/config/profiles/data_stream/*.conf",
|
||||
"SHARED_MEMORY_PREFIX": "data_stream"
|
||||
"DOTNET_BINARY_DIR": "${sourceDir}/build/$<CONFIG>/bin/net8.0;${sourceDir}/build/$<CONFIG>/bin/net8.0-windows"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -248,8 +235,6 @@
|
||||
"inherits": "sparkd",
|
||||
"generator": "Ninja Multi-Config",
|
||||
"cacheVariables": {
|
||||
"APP_DATA_DIR": "${dollar}{HOME}/.local/share/DataStream",
|
||||
"APP_DATA_DIR_OVERRIDE": "${dollar}{DATA_STREAM_DATA_DIR}",
|
||||
"BUILD_ADMIN_WEB_APP": {
|
||||
"type": "BOOL",
|
||||
"value": "OFF"
|
||||
@@ -271,8 +256,7 @@
|
||||
"EXPORT_MD_CONSUMER": {
|
||||
"type": "BOOL",
|
||||
"value": "ON"
|
||||
},
|
||||
"SHARED_MEMORY_PREFIX": "data_stream"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
@@ -84,6 +84,7 @@ function(get_cpp_sources dest)
|
||||
${PROJECT_NAME}/md/SecurityMarketState.cpp
|
||||
${PROJECT_NAME}/md/SecurityOracle.cpp
|
||||
${PROJECT_NAME}/md/SecurityStore.cpp
|
||||
${PROJECT_NAME}/md/ShmSegmentName.cpp
|
||||
${PROJECT_NAME}/md/Shm/FixedSecurity.cpp
|
||||
${PROJECT_NAME}/md/SyntheticSecurityService.cpp
|
||||
${PROJECT_NAME}/md/cme/MDP3Utils.cpp
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "ShmSegmentName.h"
|
||||
#include "spark/os/os.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
|
||||
namespace bts::md {
|
||||
|
||||
bool ShmSegmentName::prefix_initialized_ = false;
|
||||
std::string ShmSegmentName::prefix_ = {};
|
||||
std::mutex ShmSegmentName::prefix_mx_ = {};
|
||||
|
||||
std::string ShmSegmentName::prefix() {
|
||||
// Optimistically check if prefix has been initialized without using a lock
|
||||
if (!prefix_initialized_) {
|
||||
// It hasn't been initialized. Use a lock to initialize it
|
||||
std::lock_guard<std::mutex> lock{prefix_mx_};
|
||||
// Verify it hasn't been initialized again (since the initial check was done without a lock).
|
||||
if (!prefix_initialized_) {
|
||||
prefix_ = os::expand_env(SHARED_MEMORY_PREFIX, false);
|
||||
auto proc_name = os::process_path().filename().string();
|
||||
if (prefix_.empty()) {
|
||||
prefix_ = std::move(proc_name);
|
||||
} else {
|
||||
prefix_.insert(0, proc_name + "_");
|
||||
}
|
||||
}
|
||||
bts_assert(!prefix_.empty());
|
||||
prefix_initialized_ = true;
|
||||
}
|
||||
return prefix_ + "_" +
|
||||
config::get<md::PublisherNamespace>().shared_memory_namespace();
|
||||
}
|
||||
|
||||
} // namespace bts::md
|
||||
@@ -3,8 +3,12 @@
|
||||
#include "spark/config/config.h"
|
||||
#include "spark/config/protos/market_data.pb.h"
|
||||
#include "spark/domain_model/Security.pb.h"
|
||||
#include "spark/os/os.h"
|
||||
#include "spark/md/protos/co2.pb.h"
|
||||
#include "spark/md/shared_memory_prefix.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace bts::md {
|
||||
@@ -28,17 +32,16 @@ class ShmSegmentName {
|
||||
api::SecurityType type,
|
||||
api::Venue exchange,
|
||||
const std::string &suffix);
|
||||
|
||||
static bool prefix_initialized_;
|
||||
static std::string prefix_;
|
||||
static std::mutex prefix_mx_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::string ShmSegmentName::prefix() {
|
||||
return std::string{SHARED_MEMORY_PREFIX} + "_" +
|
||||
config::get<md::PublisherNamespace>().shared_memory_namespace();
|
||||
}
|
||||
|
||||
inline std::string ShmSegmentName::prefix(
|
||||
const std::string &symbol, api::SecurityType type, api::Venue exchange) {
|
||||
return prefix() + "_" + symbol + "_" + api::SecurityType_Name(type) + "_" +
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef SHARED_MEMORY_PREFIX
|
||||
#define SHARED_MEMORY_PREFIX "${BTS_SHARED_MEMORY_PREFIX}"
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef APP_DATA_DIR
|
||||
#ifdef _WIN32
|
||||
#define APP_DATA_DIR "${APPDATA}"
|
||||
#else
|
||||
#define APP_DATA_DIR "${HOME}/.local/share"
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,10 +1,12 @@
|
||||
#include "os.h"
|
||||
#include "spark/config/config.h"
|
||||
#include "spark/config/protos/system_config.pb.h"
|
||||
#include "spark/os/app_data_dir.h"
|
||||
#include "spark/utils/bts_array_size.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
#include "spark/utils/bts_debugger.h"
|
||||
#include "spark/utils/helper_macros.h"
|
||||
#include "spark/utils/string_utils.h"
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
@@ -65,6 +67,8 @@
|
||||
namespace bts {
|
||||
namespace os {
|
||||
|
||||
using Path = boost::filesystem::path;
|
||||
|
||||
unsigned int process_id() {
|
||||
#ifdef _WIN32
|
||||
return GetCurrentProcessId();
|
||||
@@ -73,6 +77,24 @@ unsigned int process_id() {
|
||||
#endif
|
||||
}
|
||||
|
||||
Path process_path() {
|
||||
#ifdef _WIN32
|
||||
char process_name[MAX_PATH];
|
||||
if (GetModuleFileName(NULL, process_name, MAX_PATH)) {
|
||||
return Path(process_name);
|
||||
}
|
||||
#else
|
||||
char process_name[PATH_MAX];
|
||||
ssize_t count = readlink("/proc/self/exe", process_name, PATH_MAX);
|
||||
if (count != -1) {
|
||||
// Null terminate the string
|
||||
process_name[count] = '\0';
|
||||
return Path(process_name);
|
||||
}
|
||||
#endif
|
||||
bts_assert_msg(false, "Failed to get process name");
|
||||
}
|
||||
|
||||
bool is_running(unsigned int pid) {
|
||||
#ifdef _WIN32
|
||||
HANDLE handle = OpenProcess(SYNCHRONIZE, false, pid);
|
||||
@@ -649,6 +671,7 @@ std::string expand_env(std::string name, bool assert_vars_exist) {
|
||||
bts_assert_msgf(
|
||||
!assert_vars_exist, "Environment variable '{}' is undefined",
|
||||
std::string{name});
|
||||
name.replace(from.first, from.second, "");
|
||||
continue;
|
||||
}
|
||||
name.replace(from.first, from.second, var);
|
||||
@@ -677,23 +700,29 @@ bool expand_env_in_place(std::string &name, bool assert_vars_exist) {
|
||||
}
|
||||
|
||||
std::optional<std::string> try_expand_env(const std::string &name) {
|
||||
std::string result;
|
||||
if (try_expand_env(name, result)) {
|
||||
return result;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool try_expand_env(const std::string &name, std::string &result) {
|
||||
result = name;
|
||||
static const std::regex env_re{R"--(\$\{([^}]+)\})--"};
|
||||
std::smatch match;
|
||||
std::string result{name};
|
||||
while (std::regex_search(result, match, env_re)) {
|
||||
const auto from = match[0];
|
||||
const auto var_name = match[1].str();
|
||||
const auto var = std::getenv(var_name.c_str());
|
||||
if (!var) {
|
||||
return std::nullopt;
|
||||
return false;
|
||||
}
|
||||
result.replace(from.first, from.second, var);
|
||||
}
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
using Path = boost::filesystem::path;
|
||||
|
||||
Path app_data_path() {
|
||||
// Since the app data path is expanded from environment variables, we only
|
||||
// compute it once and save the result in a static singleton. This way, even
|
||||
@@ -709,28 +738,23 @@ Path app_data_path() {
|
||||
if (app_data_path_) {
|
||||
return *app_data_path_;
|
||||
}
|
||||
// APP_DATA_DIR_OVERRIDE is the name of an environment variable which can be used
|
||||
// to override the default app data dir (%APPDATA% on windows, $HOME on linux).
|
||||
// The name itself is a variable because this library is built by multiple
|
||||
// executables, which may run simultaneously on the same machine. For example,
|
||||
// to run 'sparkd' and 'spark api server' on the same box with different data
|
||||
// dirs, one might define the environment variables:
|
||||
// SPARKD_DATA_DIR=~/sparkdata
|
||||
// SPARK_API_SERVER_DATA_DIR=~/spark_api_data
|
||||
// In this example, sparkd and the api server were built with (respectively):
|
||||
// APP_DATA_DIR_OVERRIDE="${SPARKD_DATA_DIR}",
|
||||
// APP_DATA_DIR_OVERRIDE="${SPARK_API_SERVER_DATA_DIR}",
|
||||
#ifdef APP_DATA_DIR_OVERRIDE
|
||||
const auto data_dir = try_expand_env(APP_DATA_DIR_OVERRIDE);
|
||||
if (data_dir) {
|
||||
app_data_path_ = Path{*data_dir};
|
||||
std::string path_override_key =
|
||||
fmt::format("{}__APPDATA", get_application_name());
|
||||
system::string_utils::to_upper(path_override_key);
|
||||
const char *path_override = std::getenv(path_override_key.c_str());
|
||||
if (path_override) {
|
||||
std::string path_override_str = path_override;
|
||||
if (path_override_str.ends_with('/')) {
|
||||
path_override_str =
|
||||
path_override_str.substr(0, path_override_str.size() - 1);
|
||||
}
|
||||
app_data_path_ = path_override_str;
|
||||
} else {
|
||||
std::string base_path;
|
||||
bts_assert(try_expand_env(APP_DATA_DIR, base_path));
|
||||
app_data_path_ = Path{base_path} / "bts" / process_path().stem();
|
||||
app_data_path_ = app_data_path_->make_preferred();
|
||||
}
|
||||
#endif
|
||||
if (!app_data_path_) {
|
||||
app_data_path_ = Path{expand_env(APP_DATA_DIR)};
|
||||
}
|
||||
bts_assert(app_data_path_);
|
||||
app_data_path_ = app_data_path_->make_preferred();
|
||||
return *app_data_path_;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ inline bool is_windows() {
|
||||
}
|
||||
BTS_EXPORT char const *platform_name();
|
||||
BTS_EXPORT unsigned int process_id();
|
||||
boost::filesystem::path process_path();
|
||||
BTS_EXPORT bool is_running(unsigned int pid);
|
||||
struct ProcInfo {
|
||||
ProcInfo() : pid(0), exe(), args() {}
|
||||
@@ -92,6 +93,7 @@ boost::filesystem::path app_data_path(
|
||||
|
||||
std::string expand_env(std::string name, bool assert_vars_exist = true);
|
||||
bool expand_env_in_place(std::string &name, bool assert_vars_exist = true);
|
||||
bool try_expand_env(const std::string &name, std::string &result);
|
||||
|
||||
std::string username();
|
||||
std::string find_newest_file(
|
||||
|
||||
Reference in new issue
Block a user