Files
spark/src/lib/orderman/actors/GLBXiLink3ExecutionGateway.cpp
T

301 lines
9.2 KiB
C++

#include "lib/orderman/actors/GLBXiLink3ExecutionGateway.h"
#include "lib/config/config.h"
#include "lib/config/protos/orderman_cfg.pb.h"
#include "lib/domain_model/Security.h"
#include "lib/jetfix/common/OutFIX.h"
#include "lib/jetfix/store/session_name.h"
#include "lib/orderman/actors/GLBXExecutionGateway.h"
#include "lib/orderman/actors/ilink3/IL3GW.h"
#include "lib/orderman/actors/ilink3/IL3MessageFramer.h"
#include "lib/orderman/actors/ilink3/IL3Reactor.h"
#include "lib/orderman/actors/ilink3/IL3SecDB.h"
#include "lib/orderman/actors/ilink3/SessionStore.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
#include "lib/orderman/orderman_utils/fix_order_codec.h"
namespace bts {
namespace orderman {
namespace ilink3 {
enum class GLBXiLink3ExecutionGateway::State {
WAITING_FOR_SECURITIES,
PAUSED,
CONNECTING,
READY
};
struct GLBXiLink3ExecutionGateway::PImpl {
PImpl(config::JetFIX::Connection const &cfg, trace::PerfLog &log,
md::SecurityOracle o)
: state(), cfg(cfg), reactor(), start_of_pause(), on_exe_report(),
on_gw_status_change(), on_cancel_reject(), perf_log(log), db(o),
gw_name(cfg.name()), session_store(), gw(), connection(nullptr),
connection_failures(0), started(false), replay_messages(),
has_replayed(false) {
TCPEndpoint endpoint{cfg.socket_connect_host_ip(),
(ushort)cfg.socket_connect_port()};
connection = &reactor.register_connection({endpoint});
}
State state;
config::JetFIX::Connection const cfg;
IL3Reactor reactor;
IL3MessageFramer framer;
TimeStamp start_of_pause;
OnExeReport on_exe_report;
OnGwStatusChange on_gw_status_change;
orderman::OnCancelReject on_cancel_reject;
trace::PerfLog &perf_log;
IL3SecDB db;
GatewayName gw_name;
std::optional<SessionStore> session_store;
std::optional<IL3GW> gw;
IL3Connection *connection;
size_t connection_failures;
bool started;
// We need to keep this collection alive for the duration of the
// program because we will be passing pointers to ExecutionReport
// to other threads
std::vector<ExecutionReport> replay_messages;
bool has_replayed;
};
GLBXiLink3ExecutionGateway::GLBXiLink3ExecutionGateway(
config::JetFIX::Connection const &cfg, trace::PerfLog &log,
md::SecurityOracle o)
: me(new PImpl(cfg, log, o)) {
me->state = State::WAITING_FOR_SECURITIES;
}
GLBXiLink3ExecutionGateway::~GLBXiLink3ExecutionGateway() {}
void GLBXiLink3ExecutionGateway::start() {
bts_assert(!me->started);
me->started = true;
}
void GLBXiLink3ExecutionGateway::dispatch_incoming_fix_messages() {
if (me->started) {
switch (me->state) {
case State::WAITING_FOR_SECURITIES:
break;
case State::PAUSED:
on_time();
break;
case State::READY:
case State::CONNECTING:
me->reactor.do_work();
me->gw->on_time();
break;
}
}
}
void GLBXiLink3ExecutionGateway::on_time() {
bts_assert(me->state == State::PAUSED);
// If we are still paused bail early, this avoids logging too much
// or spending too much CPU
if (pause_in_effect())
return;
me->start_of_pause = Timer::now();
// If we alread exceed the maximum number of failures per run
// bail. This is a safety measure to avoid overwhelming the
// exchange in the event of a bug or operational problem that
// results in repeated connection failures.
if (max_failures_reached()) {
LOG(iLink3, Warn,
"Maximum number of connection failures reached for {}. Reboot to try "
"again",
me->gw_name.value());
return;
}
// If we reach this point we are no longer in the pause window and
// we have not exceed our connection failure limit. The only thing
// to check now is wheter we are inside the exchange's active
// hours.
auto current_session = jetfix::session_name(me->cfg);
if (current_session.empty()) {
LOG(iLink3, Info, "Outside of session in ", me->gw_name.value());
return;
}
// Yep, the stars are all aligned, lets try to connect
connect(current_session);
}
void GLBXiLink3ExecutionGateway::connect(jetfix::SessionName const &sn) {
bts_assert(me->state == State::PAUSED);
transition(State::CONNECTING);
Config c;
c.cme_session = me->cfg.sender_compid().substr(0, 3);
c.firm = me->cfg.sender_compid().substr(3);
c.failover_on_disconnect = false;
me->session_store.emplace(me->db, sn, &me->cfg);
if (!me->has_replayed) {
me->has_replayed = true;
me->replay_messages = me->session_store.value().replay();
for (auto const &er : me->replay_messages)
me->on_exe_report(er, {});
}
c.raw_message_logger =
[store = &me->session_store.value()](auto d, auto sn, auto const &msg) {
store->async_store_raw_msg(d, sn, msg);
};
me->gw.emplace(*me->connection, me->framer, &me->db,
me->session_store->starting_session_descriptor(), c,
&me->cfg);
me->gw->subscribe_cancel_reject([this](auto &cr) {
bts_assert(me->state == State::READY || me->state == State::CONNECTING);
me->on_cancel_reject(cr);
});
me->gw->subscribe_execution_reports([this](auto er) {
bts_assert(me->state == State::READY || me->state == State::CONNECTING);
me->on_exe_report(*er, {});
});
me->gw->subscribe_session(
[this](auto const &s) { me->session_store->async_update_session(s); });
me->gw->subscribe_state([this](auto gw_state) {
LOG(iLink3, Info, "Internal gateway state change in {}. State is now {}",
me->gw_name.value(),
log::CharPtrWrapper{ilink3::state_string(gw_state)});
switch (me->state) {
case State::CONNECTING:
if (gw_state == ilink3::State::READY) {
transition(State::READY);
me->on_gw_status_change(true);
} else if (gw_state == ilink3::State::ERROR) {
me->connection_failures++;
pause();
}
break;
case State::READY:
if (gw_state == ilink3::State::ERROR) {
me->connection_failures++;
pause();
me->on_gw_status_change(false);
}
break;
default:
bts_assert(false);
break;
}
});
me->gw->connect();
}
void GLBXiLink3ExecutionGateway::pause() {
me->start_of_pause = Timer::now();
me->gw.reset();
transition(State::PAUSED);
}
bool GLBXiLink3ExecutionGateway::pause_in_effect() const {
return sec_diff(Timer::now(), me->start_of_pause) <=
me->cfg.seconds_to_pause();
}
bool GLBXiLink3ExecutionGateway::max_failures_reached() const {
return me->connection_failures > me->cfg.max_connection_attempts();
}
void GLBXiLink3ExecutionGateway::on_securities(
const mercury::Update<NewSecuritiesEvent> &update) {
if (update.events) {
for (auto const &ev : *(update.events)) {
auto sec_ptr = ev.security;
me->db.add_security(*sec_ptr);
}
if (me->state == State::WAITING_FOR_SECURITIES) {
LOG(iLink3, Warn,
"Received first set of {} securities. Proceeding "
"with initialization of execution gateway {}",
update.events->size(), me->gw_name.value());
// got our security synopsis, ready to start the GW
transition(State::PAUSED);
}
}
}
void GLBXiLink3ExecutionGateway::on_admin_data(
const mercury::Update<NewAdminDataEvent> &) {}
void GLBXiLink3ExecutionGateway::send_msgs(std::vector<OrderMsg> const &msgs,
trace::CorID) {
if (me->state == State::READY) {
for (auto &m : msgs) {
// NewOrderSingle
auto create_ptr = std::get_if<CreateOrderMsg>(&m);
if (create_ptr) {
me->gw->send_order(*create_ptr);
continue;
}
// CancelReplace
auto cancel_replace_ptr = std::get_if<CancelReplaceOrderMsg>(&m);
if (cancel_replace_ptr) {
me->gw->send_cancel_replace(*cancel_replace_ptr);
continue;
}
// Cancel
auto cancel_ptr = std::get_if<CancelOrderMsg>(&m);
if (cancel_ptr) {
me->gw->send_cancel(*cancel_ptr);
continue;
}
}
me->gw->flush();
}
}
OnExeReport &GLBXiLink3ExecutionGateway::on_exe_report() {
return me->on_exe_report;
}
OnGwStatusChange &GLBXiLink3ExecutionGateway::on_gw_status_change() {
return me->on_gw_status_change;
}
orderman::OnCancelReject &GLBXiLink3ExecutionGateway::on_cancel_reject() {
return me->on_cancel_reject;
}
log::CharPtrWrapper GLBXiLink3ExecutionGateway::state_name(State st) {
switch (st) {
case State::WAITING_FOR_SECURITIES:
return {"WAITING_FOR_SECURITIES"};
case State::PAUSED:
return {"PAUSED"};
case State::CONNECTING:
return {"CONNECTING"};
case State::READY:
return {"READY"};
}
bts_assert(false);
return {""};
}
void GLBXiLink3ExecutionGateway::transition(State new_state) {
LOG(iLink3, Info, "Transitioning {} from {} to {}", me->gw_name.value(),
state_name(me->state), state_name(new_state));
me->state = new_state;
}
} // namespace ilink3
} // namespace orderman
} // namespace bts