Files
spark/src/app_sparkd/sparkd/cmds/Run.cpp
T
John Colvin 4801ce6b83 refactor: Wrap md run logic in class MarketDataSession
Start in the constructor, gracefully exit in the destructor.
2026-03-20 17:02:44 +00:00

170 lines
6.5 KiB
C++

#include "Run.h"
#include "python_interop/plugin_manager.h"
#include "spark/api/public/ServiceContext.h"
#include "spark/config/config.h"
#include "spark/config/protos/ports.pb.h"
#include "spark/config/protos/quickbus_cfg.pb.h"
#include "spark/io/http/curl_session.h"
#include "spark/logging/logging.h"
#include "spark/md/MBOProcessor.h"
#include "spark/md/MBOServiceServer.h"
#include "spark/md/MDClient.h"
#include "spark/md/SyntheticSecurityService.h"
#include "spark/md/relay/query_server.h"
#include "spark/md/service/MarketDataServiceServer.h"
#include "spark/metrics/metrics.h"
#include "spark/metrics/system_metrics.h"
#include "spark/orderman/OrderServiceServer.h"
#include "spark/orderman/events/TradeSummarySignalDispatcher.h"
#include "spark/orderman/order_factory/DynamicOrderRegistry.h"
#include "spark/os/spark_data_directory.h"
#include "spark/persistence/PersistenceServiceServer.h"
#include "spark/plugin/SparkServices.h"
#include "spark/plugin/plugin.h"
#include "spark/quickbus/core/Quickbus.h"
#include "spark/tracing/PerfLog.h"
#include "spark/utils/Dispatcher.h"
#include "spark/version.h"
#include "sparkd/algo/AlgoDefinitionServiceServer.h"
#include "sparkd/analytics/AnalyticsServiceServer.h"
#include "sparkd/cmds/md/cmds/Run.h"
#include "sparkd/execution/execution_gateway_provider.h"
#include "sparkd/http/WebServer.h"
#include "sparkd/md/MarketDataAuxiliaryServiceServer.h"
#include "sparkd/md/market_data_session.h"
#include "sparkd/md/recording/MDRecorder.h"
#include "sparkd/position/MarginServiceServer.h"
#include "sparkd/position/PositionServiceServer.h"
#include "sparkd/position/import/BasePositionServiceServer.h"
#include "sparkd/position/import/MoneyFileServiceServer.h"
#include "sparkd/subprocess/SubProcessRunner.h"
#include "sparkd/support/ErrorReportService.h"
#include "sparkd/support/ShellLatencyServer.h"
#include "sparkd/user/AdminServiceServer.h"
#include "sparkd/user/AuthServiceServer.h"
namespace bts::cmds {
using bts::api::ServiceContext;
using bts::md::MarketDataSession;
using bts::md::MDClient;
using bts::md::TradeSummarySignalDispatcher;
using bts::metrics::MetricsContainer;
using bts::orderman::DynamicOrderRegistry;
using bts::orderman::ExecutionGatewayProvider;
using bts::orderman::IExecutionGatewayProvider;
using bts::orderman::OrderServiceServer;
using bts::trace::PerfLog;
using std::shared_ptr;
using std::unique_ptr;
using std::vector;
static OrderServiceServer create_order_service_server(
ServiceContext &sc,
MDClient &mdclient,
DynamicOrderRegistry &dyn_order_registry,
MetricsContainer &stats,
PerfLog &perf_log,
TradeSummarySignalDispatcher &tss_dispatcher,
shared_ptr<python_interop::PluginManager> py_plugin_man) {
vector<unique_ptr<IExecutionGatewayProvider>> gw_pros;
gw_pros.push_back(std::make_unique<ExecutionGatewayProvider>(
&perf_log, mdclient.oracle(), config::get<config::JetFIX>(),
[](const std::string &name) {
const spark::SparkDataDirectory data_dir;
return data_dir.load_config_json(name);
},
std::move(py_plugin_man)));
return OrderServiceServer(
sc, mdclient, dyn_order_registry, std::move(gw_pros), stats, perf_log,
tss_dispatcher);
}
static void execute() {
using namespace bts;
LOG(Gen, Warn,
"------------------------------------ Sparkd " BTS_SPARK_VERSION_FULL
" launched "
"--------------------------------");
// Initializes curl for the application, and eventual cleanup in destructor
http::CurlSession global_curl_session;
auto qb_cfg = config::get<config::QuickbusCfg>().node();
auto &ports = config::get<config::Ports>();
qb_cfg.set_my_port(ports.qbus_port());
qbus::Quickbus quickbus(qb_cfg, qbus::QuickbusThreadOptions::DEFERRED_START);
api::ServiceContext sc(&quickbus);
trace::PerfLog perf_log;
metrics::MetricsContainer stats;
metrics::publish_system_metrics(stats);
http::SparkWebServer webserver(sc, stats, perf_log, ports.config_port());
persistence::PersistenceServiceServer settings_saver(sc, "settings");
user::AuthServiceServer login_server(sc);
bts::md::MBOProcessor mbo_processor;
bts::md::TradeSummarySignalDispatcher tss_dispatcher;
// The py plugin manager is shared by md and execution, so it lives in this scope.
auto py_plugin_man = std::make_shared<python_interop::PluginManager>();
// start work loop in background
py_plugin_man->start_work_thread();
MarketDataSession md_session{py_plugin_man};
api::synthetic::SyntheticSecurityService synthetic_service(sc);
bts::md::MDClient mdclient(
&utils::Dispatcher::background(), &synthetic_service,
&mbo_processor.reader());
bts::md::QueryServerPtr md_query_server(
bts::md::run_query_server(sc, mdclient, stats));
bts::md::MBOServiceServer mboserver(
sc, utils::Dispatcher::background(), mdclient);
bts::md::MarketDataService mdserver(mdclient, sc, stats);
// initialize order plugins
orderman::DynamicOrderRegistry dyn_order_registry;
SparkServices plugin_services(dyn_order_registry);
plugin::init_plugins(plugin_services);
// finished initializing order plugins
auto order_service_server = create_order_service_server(
sc, mdclient, dyn_order_registry, stats, perf_log, tss_dispatcher,
py_plugin_man);
support::ErrorReportServer error_report_service(sc);
admin::AdminServiceServer admin_service(sc);
bts::md::auxiliary::MarketDataAuxiliaryServiceServer md_aux_server(sc);
bts::api::orderman::algo::AlgoDefinitionServiceServer algo_definition_server(
sc);
bts::analytics::AnalyticsServiceServer analytics_server(sc);
position::BasePositionServiceServer base_pos_server(sc);
position::MoneyFileServiceServer money_file_server(sc);
position::PositionServiceServer position_service(sc, mdclient);
position::MarginServiceServer margin_service(sc, mdclient);
metrics::ShellLatencyServer shell_latency_service(
sc, stats, utils::Dispatcher::background());
bts::md::recording::MDRecorder::start_recording(mdclient);
auto bg_health =
std::make_shared<metrics::IntCounterMetric>("spark_background_thread");
utils::DispatcherTimer timer(utils::Dispatcher::background(), 5000, [=]() {
bg_health->increment();
});
timer.start();
utils::DispatcherTimer timer2(utils::Dispatcher::background(), 1000, [&]() {
perf_log.store_log_entries();
});
timer2.start();
stats.publish(bg_health);
subprocess::run_all();
quickbus.run();
}
Run::Run() : CLI::App("Run default sparkd services", "run") {
callback([]() { execute(); });
}
} // namespace bts::cmds