Files
spark/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp
T

360 lines
13 KiB
C++

#include "MDP3Main.h"
#include "lib/md/cme/MDP3Utils.h"
#include "lib/md/MBOProcessor.h"
#include "lib/config/config.h"
#include "lib/md/coconut2.h"
#include "lib/os/os.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/proto_helper.h"
#include "lib/md/protos/co2.pb.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/config/protos/system.pb.h"
#include "lib/utils/thread_manager.h"
#include <memory>
#include <boost/filesystem.hpp>
#include <set>
using namespace std;
using namespace bts::co2;
using std::size_t;
namespace bts {
namespace md {
MDP3Main::MDP3Main() :
_index(),
_recorder ({ utils::Dispatcher::current(), [=](ExchangeKey k) { return SecurityKey(_index.get_key(k.value())); } }),
_producer(PLATFORM_MDP3ONIXS, _recorder ),
// _settlement_recorder(PLATFORM_MDP3ONIXS),
_engines(),
_handlers(),
_listeners(),
_recovery()
{
}
MDP3Main::~MDP3Main() {}
const CMEOptions::CMEChannel channel_settings_for(const string& id) {
const auto& config = config::get<CMEOptions>();
for (const auto& ch : config.channel()) {
bool found = std::find_if(ch.id().begin(), ch.id().end(), [&](const string& chan_id) { return chan_id == id || chan_id == "*"; }) != ch.id().end();
if (found) {
return ch;
}
}
return CMEOptions::CMEChannel();
}
bool feed_engine_exists(const string& name) {
const auto& config = config::get<CMEOptions>();
for (const auto& feed : config.feed_engine()) {
if (feed.name() == name) return true;
}
return false;
}
const onixs::FeedEngineSettings feed_engine_settings_from_config(const CMEOptions::FeedEngineSettings& settings, int feed_count) {
onixs::FeedEngineSettings result;
if (settings.has_data_wait()) result.dataWaitTime(settings.data_wait());
if (settings.has_spin_time()) result.spinBeforeIdleTime(settings.spin_time());
result.socketBufferSize(settings.udp_socket_buffer_size());
auto affinity = threading::get_affinity(settings.name());
for (auto& core : affinity) {
result.threadAffinity().cpus().insert(core);
}
// num_threads() overrides anything else
if (settings.has_num_threads()) {
result.threadCount(settings.num_threads());
}
else {
// If num_threads is absent determine thread count based on #channels
result.threadCount((feed_count / settings.channels_per_thread()) + 1);
}
return result;
}
size_t count_engine_uses(const vector<CMEChannel>& channels, const string& feed_name) {
return count_if(channels.begin(), channels.end(), [&](const CMEChannel& ch) {
const auto& settings = channel_settings_for(ch.id);
return settings.has_feed_engine() && settings.feed_engine() == feed_name;
});
}
map<string,onixs::FeedEngineSettings> create_feed_engine_settings(const std::vector<CMEChannel>& channels) {
map<string, onixs::FeedEngineSettings> settings;
const auto& config = config::get<CMEOptions>();
// For each feed engine in feed_engine
for (const auto& feed : config.feed_engine()) {
// - see if it is in use
size_t use_count = count_engine_uses(channels, feed.name());
// - Only create if this feed engine is in use
if (use_count > 0) {
settings[feed.name()] = feed_engine_settings_from_config(feed, use_count);
}
}
return settings;
}
map<string, unique_ptr<onixs::FeedEngine>> create_feed_engines(const vector<CMEChannel>& channels) {
map<string, unique_ptr<onixs::FeedEngine>> result;
auto settings = create_feed_engine_settings(channels);
// Loop through settings and instantiate feed engines
for (const auto& s : settings) {
unique_ptr<onixs::FeedEngine> temp(new onixs::FeedEngine(s.second));
result.insert(make_pair(s.first, move(temp)));
}
return result;
}
onixs::SessionSettings session_settings_for_channel(const string& channel_id, MBOWriter* mbo) {
const auto& options = channel_settings_for(channel_id);
onixs::SessionSettings settings;
if (mbo) {
settings.marketRecovery(onixs::MarketRecoveryOptions::BothMboAndMbp);
}
else {
settings.marketRecovery(onixs::MarketRecoveryOptions::MbpOnly);
}
if (!options.join_snapshot_on_gap()) {
#ifdef DEBUG
settings.gapAndErrorHandling(onixs::GapAndErrorHandlingOptions::RecoverMarketState);
#else
settings.gapAndErrorHandling(onixs::GapAndErrorHandlingOptions::ContinueProcessing);
#endif
}
if (options.use_natural_refresh()) {
settings.joinRecovery(onixs::JoinRecoveryOptions::Disabled);
settings.gapAndErrorHandling(onixs::GapAndErrorHandlingOptions::ContinueProcessing);
}
return settings;
}
void MDP3Main::start(std::vector<UserSymbol>& symbols, MBOWriter* mbo) {
await_cme_config();
initialize();
const auto& channels = parse_channels(symbols);
_engines = create_feed_engines(channels);
for (const auto& channel : channels) {
unique_ptr<onixs::Handler> phandler = prepare_handler(channel.id, mbo);
_listeners.push_back(prepare_listener(*phandler, channel, mbo));
_handlers.insert(std::make_pair(channel.id, move(phandler)));
std::stringstream symstr;
for (const auto& sym : channel.symbols) { symstr << sym.symbol() << "-" << SecurityType_Name(sym.type()) << " "; }
LOG(md,Info, "CME: Initialized channel {} ({})",channel.id,symstr.str());
}
for (auto& pair : _handlers) {
onixs::SessionSettings settings = session_settings_for_channel(pair.first, mbo);
pair.second->start(settings);
}
LOG(md,Info, "** CME MDP3 Publisher started **");
}
void MDP3Main::send_heartbeats() {
for (const auto& listener: _listeners)
listener->heartbeat();
// utils::DispatcherTimer settlement_timer(disp, 100, [&]() {
// _settlement_recorder.do_work();
// });
// settlement_timer.start();
}
void MDP3Main::initialize() {
const auto& options = config::get<CMEOptions>();
auto license_location = os::bts_data_path() + "/licenses/";
if (!boost::filesystem::is_directory(license_location)) {
auto license_path =os::sparkd_wd() + "/data_files/Onixs.BTS.lic" ;
boost::filesystem::create_directories(license_location);
boost::filesystem::copy_file(license_path,license_location+"/Onixs.BTS.lic");
}
//
// License path
onixs::InitializationSettings init_settings;
auto license = options.onixs_license();
init_settings.licenseStore(license);
if (license == "{default}") {
init_settings.licenseStore(license_location);
}
LOG(md,Info,"Loading onixs license from {}", init_settings.licenseStore());
onixs::initialize(init_settings);
}
// //
// Initialize HandlerSettings and allocate the channel Handler
unique_ptr<onixs::Handler> MDP3Main::prepare_handler(const string& channel_id, MBOWriter* mbo) {
const auto& pub_options = config::get<PublisherOptions>();
const auto& options = channel_settings_for(channel_id);
LOG(md,Info,"Loaded channel options for channel {}:\n{}",channel_id ,options.DebugString());
onixs::HandlerSettings settings;
settings.channel(std::stoul(channel_id));
settings.feeds().connectivityConfigurationFile(config_path());
// Market data options
settings.bookManagement().directBooks().maintain(true);
settings.bookManagement().impliedBooks().maintain(true);
settings.bookManagement().consolidatedBooks().maintain(false);
if (options.validate_mbo() && mbo != nullptr) {
settings.bookManagement().mboBooks().maintain(true);
}
// 10k default is *far* too low
settings.recoveryQueueMaxSize(options.recovery_queue_size());
//
// Network settings
if (options.has_lost_packet_wait_timeout_ms()) {
settings.feeds().incrementalFeeds().lostPacketWaitTime((size_t)options.lost_packet_wait_timeout_ms() * 1000);
}
if (pub_options.has_network_interface()) {
settings.feeds().feedANetworkInterfaces(pub_options.network_interface());
settings.feeds().feedBNetworkInterfaces(pub_options.network_interface());
}
else {
if (pub_options.has_network_interface_a()) {
settings.feeds().feedANetworkInterfaces(pub_options.network_interface_a());
}
if (pub_options.has_network_interface_b()) {
settings.feeds().feedBNetworkInterfaces(pub_options.network_interface_b());
}
}
if (pub_options.has_network_interface() ||
(pub_options.has_network_interface_a() &&
pub_options.has_network_interface_b()) ) {
if (options.prefer_backup_feeds()) {
settings.feeds().instrumentFeeds().layout(onixs::RecoveryFeedLayout::FeedBWithFailoverToFeedA);
settings.feeds().incrementalFeeds().layout(onixs::RealtimeFeedLayout::BothFeedsWithArbitrage);
settings.feeds().snapshotFeeds().layout(onixs::RecoveryFeedLayout::FeedBWithFailoverToFeedA);
settings.feeds().mboSnapshotFeeds().layout(onixs::RecoveryFeedLayout::FeedBWithFailoverToFeedA);
}
else {
auto layout = onixs::RecoveryFeedLayout::FeedAWithFailoverToFeedB;
settings.feeds().instrumentFeeds().layout(layout);
settings.feeds().incrementalFeeds().layout(onixs::RealtimeFeedLayout::BothFeedsWithArbitrage);
settings.feeds().snapshotFeeds().layout(layout);
settings.feeds().mboSnapshotFeeds().layout(layout);
}
}
else if (pub_options.has_network_interface_a()) {
auto layout = onixs::RecoveryFeedLayout::FeedAOnly;
settings.feeds().instrumentFeeds().layout(layout);
settings.feeds().incrementalFeeds().layout(onixs::RealtimeFeedLayout::FeedAOnly);
settings.feeds().snapshotFeeds().layout(layout);
settings.feeds().mboSnapshotFeeds().layout(layout);
}
else if (pub_options.has_network_interface_b()) {
auto layout = onixs::RecoveryFeedLayout::FeedBOnly;
settings.feeds().instrumentFeeds().layout(layout);
settings.feeds().incrementalFeeds().layout(onixs::RealtimeFeedLayout::FeedBOnly);
settings.feeds().snapshotFeeds().layout(layout);
settings.feeds().mboSnapshotFeeds().layout(layout);
}
//
// Misc settings
settings.bookManagement().updateNotification(onixs::BookUpdateNotification::OnEndOfEvent);
settings.tradeProcessing(
config::get<CMEOptions>().verbose_ticker() ?
onixs::TradeProcessing::OrderDetails :
onixs::TradeProcessing::SummaryLevel);
//
// Logging
onixs::Logger* logger;
if (options.onixs_log_mode() == CMEOptions_OnixSLogMode_None) {
logger = new onixs::NullLogger();
}
else {
onixs::FileLoggerSettings loggerSettings;
loggerSettings.filename(log_path() + "/log_" + channel_id);
loggerSettings.severityLevel((onixs::LogSeverity::Enum)options.onixs_log_mode());
logger = new onixs::FileLogger(loggerSettings);
if (options.onixs_log_mode() == CMEOptions::Debug) {
// trace all the things
settings.debugLogging().traceBooks(true);
settings.debugLogging().traceFeeds(true);
settings.debugLogging().traceMessages(onixs::MessageTracing::FixFormat);
}
}
//
// Tcp Replay options
std::unique_ptr<onixs::TcpRecoveryService> recovery;
if (options.use_tcp_replay()) {
LOG(md,Info,"CME: Enabled TCP replay");
onixs::TcpRecoverySettings tcpSettings;
tcpSettings.username(options.tcp_replay_username());
tcpSettings.password(options.tcp_replay_password());
tcpSettings.reconnectAttempts(options.tcp_replay_connect_attempts());
tcpSettings.reconnectInterval(options.tcp_replay_reconnect_interval_ms());
recovery.reset(new onixs::TcpRecoveryService(tcpSettings));
}
//
// Associate with feed handler
try {
auto handler = unique_ptr<onixs::Handler>(new onixs::Handler(settings));
handler->bindFeedEngine(*_engines.at(options.feed_engine()));
if (recovery) {
handler->bindTcpRecovery(*recovery);
_recovery.push_back(std::move(recovery));
}
if (logger) {
handler->bindLogger(*logger);
}
return handler;
}
catch (std::exception& e) {
LOG(md,Error,"{}", std::string(e.what()));
throw;
}
}
//
// Create a listener for this channel and register callbacks
unique_ptr<MDP3Listener> MDP3Main::prepare_listener(onixs::Handler& handler, const CMEChannel& channel, MBOWriter* mbo) {
// We need to get a handler to the producer channels so we can produce heartbeats
vector<MDProducerChannel> producer_channels;
for (auto& symbol : channel.symbols) {
producer_channels.push_back(_producer.get_channel(symbol.symbol().c_str(), symbol.type(), symbol.exchange()));
}
unique_ptr<MDP3Listener> listener(new MDP3Listener(_index, _producer, mbo, producer_channels));
for (auto& symbol : channel.symbols) {
listener->add_filter_symbol(symbol.symbol());
}
handler.registerListener(*listener);
handler.registerMarketDataListener(*listener);
handler.registerSecurityListener(*listener);
return listener;
}
}}//namespace