Gateway work.
This commit is contained in:
1 parent
70e58edacb
commit
929c19f4b2
4 files changed
+112
-108
No files matched your search
@@ -17,6 +17,12 @@
|
||||
namespace bts {
|
||||
namespace orderman_v2 {
|
||||
|
||||
// utilities
|
||||
GW_OrderReport make_ack_order_report(GW_OrderRecord *gw_rec, GW_Op_Ack ack);
|
||||
template <class Req> GW_Op_Ack make_ack(Req const &r) {
|
||||
return GW_Op_Ack{GW_OP_Result::OK, r.req_id, nullptr};
|
||||
}
|
||||
|
||||
struct GW_OrderRecord {
|
||||
GW_OrderRecord(OM_ID om_id, GW_BoundOrder const &order)
|
||||
: order(order), cancel_replace(nullptr), cancel(nullptr),
|
||||
@@ -55,15 +61,15 @@ struct Gateway::Hot {
|
||||
std::unordered_map<UserAccountVenue, MsgTemplates, UserAccountVenueHash>;
|
||||
|
||||
Hot(IVenue &v, ILimiter &l)
|
||||
: state(State::W_BOOTSTRAP), venue(v), limiter(l), working_orders(),
|
||||
msg_templates(), orec_pool(1024 * 1024) {}
|
||||
: state(State::W_BOOTSTRAP), venue(v), limiter(l), pending_msgs(),
|
||||
working_orders(), msg_templates(), orec_pool(1024 * 1024) {}
|
||||
|
||||
State state;
|
||||
IVenue &venue;
|
||||
ILimiter &limiter;
|
||||
boost::container::small_vector<IVenueMsg *, 8> pending_msgs;
|
||||
OrderMap working_orders;
|
||||
MsgTemplatesMap msg_templates;
|
||||
boost::container::small_vector<IVenueMsg *, 8> pending_msg;
|
||||
boost::object_pool<GW_OrderRecord> orec_pool;
|
||||
};
|
||||
|
||||
@@ -71,6 +77,7 @@ struct Gateway::Cold {
|
||||
Cold(IRouter &r, IClock &c, GatewayCfg const &cfg)
|
||||
: router(r), clock(c), admin_data(), cfg(cfg), start_of_pause(),
|
||||
current_exponential_backoff(0), bootstrap_orders() {}
|
||||
|
||||
IRouter &router;
|
||||
IClock &clock;
|
||||
api::admin::AdminData const *admin_data;
|
||||
@@ -99,8 +106,8 @@ void Gateway::do_slow_work() {
|
||||
switch (me->hot.state) {
|
||||
case State::W_BOOTSTRAP:
|
||||
if (has_bootstrap_data()) {
|
||||
transition(State::W_SCHEDULE);
|
||||
setup_bootstrap_orders();
|
||||
transition(State::W_SCHEDULE);
|
||||
}
|
||||
break;
|
||||
case State::W_SCHEDULE:
|
||||
@@ -115,8 +122,8 @@ void Gateway::do_slow_work() {
|
||||
if (sec_diff(now, me->cold.start_of_pause) >
|
||||
(me->cold.cfg.pause_seconds
|
||||
<< me->cold.current_exponential_backoff)) {
|
||||
transition(State::W_LOGOUT);
|
||||
me->hot.venue.login();
|
||||
transition(State::W_LOGOUT);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -134,8 +141,8 @@ void Gateway::do_slow_work() {
|
||||
}
|
||||
|
||||
void Gateway::on_om_bootstrap_orders(OM_BootstrapOrders const &orders) {
|
||||
auto &bso_map = me->cold.bootstrap_orders;
|
||||
bts_assert(me->hot.state == State::W_BOOTSTRAP);
|
||||
auto &bso_map = me->cold.bootstrap_orders;
|
||||
bts_assert(bso_map.size() < me->cold.cfg.om_count);
|
||||
bts_assert(bso_map.count(orders.originator) == 0);
|
||||
bts_assert(orders.target == me->cold.cfg.my_id);
|
||||
@@ -145,7 +152,7 @@ void Gateway::on_om_bootstrap_orders(OM_BootstrapOrders const &orders) {
|
||||
|
||||
void Gateway::on_gw_ops(const GW_Op_Batch &op_batch) {
|
||||
op_batch.visit(*this);
|
||||
send_pending_msg();
|
||||
send_pending_msgs();
|
||||
}
|
||||
|
||||
void Gateway::on_account_pnl(const AccountPnLUpdate &pnl) {
|
||||
@@ -161,12 +168,12 @@ void Gateway::on_excess_liquidity(const ExcessLiquidityUpdate &l) {
|
||||
}
|
||||
|
||||
void Gateway::on_admin_data(AdminDataUpdate const &admin) {
|
||||
me->cold.admin_data = admin.admin_data;
|
||||
me->hot.limiter.update(*me->cold.admin_data);
|
||||
me->cold.router.update(*me->cold.admin_data);
|
||||
|
||||
switch (me->hot.state) {
|
||||
case State::W_BOOTSTRAP:
|
||||
me->cold.admin_data = admin.admin_data;
|
||||
break;
|
||||
default:
|
||||
rebuild_msg_templates();
|
||||
@@ -181,7 +188,7 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
|
||||
auto gw_rec = me->hot.orec_pool.construct(om_id, bound_order);
|
||||
auto &order = gw_rec->order;
|
||||
|
||||
GW_OrderReport reply;
|
||||
auto ack = make_ack(create);
|
||||
switch (me->hot.state) {
|
||||
case State::READY: {
|
||||
|
||||
@@ -190,7 +197,7 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
|
||||
INewOrderSingleMsg *new_order_single;
|
||||
auto it = msg_templates.find(order.uav);
|
||||
if (it == msg_templates.end()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_NO_ROUTE);
|
||||
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -198,17 +205,18 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
|
||||
new_order_single = &it->second.new_order->clone();
|
||||
gw_rec->set_msgs(*it->second.cancel_replace, *it->second.cancel);
|
||||
|
||||
// Validate limit
|
||||
// Validate risk limit
|
||||
auto lqt = me->hot.limiter.can_send_new_order(
|
||||
order.uav.account, *order.security, order.side, order.quantity);
|
||||
if (lqt.error()) {
|
||||
reply.set_ack(lqt.result, lqt.message);
|
||||
ack.result = lqt.result;
|
||||
ack.message = lqt.message;
|
||||
break;
|
||||
}
|
||||
|
||||
// Validate messaging limits
|
||||
if (!me->hot.venue.under_messaging_limit()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_MESSAGING_BREACH);
|
||||
ack.result = GW_OP_Result::ERROR_MESSAGING_BREACH;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -216,14 +224,14 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
|
||||
// the order message and queue the message for sending.
|
||||
me->hot.working_orders.insert({bound_order.key, gw_rec});
|
||||
new_order_single->set_order_params(bound_order);
|
||||
me->hot.pending_msg.push_back(new_order_single);
|
||||
me->hot.pending_msgs.push_back(new_order_single);
|
||||
|
||||
// For speed we do not send acks for the operation until we hear
|
||||
// from the exchange.
|
||||
return;
|
||||
}
|
||||
default:
|
||||
reply.set_ack(GW_OP_Result::ERROR_GATEWAY_DISCONNECTED);
|
||||
ack.result = GW_OP_Result::ERROR_GATEWAY_DISCONNECTED;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -231,22 +239,17 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
|
||||
// the order failed. We skip the indexing of the order record,
|
||||
// since we will not need to lookup this order by key, and instead
|
||||
// send out a order report that details the cause of the failure.
|
||||
bts_assert_d(reply.last_ack.result != GW_OP_Result::OK);
|
||||
gw_rec->clear_msgs();
|
||||
reply.last_ack.req_id = create.req_id;
|
||||
reply.target = om_id;
|
||||
reply.last_exe_report = gw_rec->order.last_exe_report;
|
||||
reply.om_record = bound_order.om_record;
|
||||
reply.gw_record = gw_rec;
|
||||
on_order_report(reply);
|
||||
bts_assert_d(ack.result != GW_OP_Result::OK);
|
||||
on_order_report(make_ack_order_report(gw_rec, ack));
|
||||
}
|
||||
|
||||
// When an order is recovered from disk or from the exchange (meaning
|
||||
// the exchange sends a report on an order we do not have a record of
|
||||
// the exchange sends a report for an order we do not have a record of
|
||||
// creating) we are left in a situation where we have to create the
|
||||
// order record that represents the order but we do not have to
|
||||
// actually send the order. This is what the bind order operation is
|
||||
// for.
|
||||
// actually send the order to the exchange. This is what the bind
|
||||
// order operation is for.
|
||||
void Gateway::on_gw_op(OM_ID om_id, const GW_OrderBind &bind) {
|
||||
auto &order = bind.bound_order;
|
||||
bts_assert_d(me->hot.working_orders.count(order.key) == 0);
|
||||
@@ -256,33 +259,37 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderBind &bind) {
|
||||
bts_assert(!gw_rec);
|
||||
gw_rec = me->hot.orec_pool.construct(om_id, order);
|
||||
|
||||
GW_OrderReport reply;
|
||||
auto ack = make_ack(bind);
|
||||
|
||||
// Find the message templates for this UAV and clone them for this
|
||||
// order
|
||||
auto &msg_templates = me->hot.msg_templates;
|
||||
auto it = msg_templates.find(order.uav);
|
||||
if (it == msg_templates.end())
|
||||
reply.set_ack(GW_OP_Result::ERROR_NO_ROUTE);
|
||||
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
|
||||
|
||||
reply.last_ack.req_id = bind.req_id;
|
||||
reply.target = om_id;
|
||||
reply.last_exe_report = order.last_exe_report;
|
||||
reply.om_record = order.om_record;
|
||||
reply.gw_record = gw_rec;
|
||||
on_order_report(reply);
|
||||
// In this case we have to send a reply to the bind operation
|
||||
// because there is no guarantee that we will be able to piggy back
|
||||
// the reply on the next exchange acknowledgement (which may never
|
||||
// come).
|
||||
on_order_report(make_ack_order_report(gw_rec, ack));
|
||||
}
|
||||
|
||||
void Gateway::on_gw_op(OM_ID om_id, const GW_OrderQuickUpdate &quick_update) {
|
||||
// A quick update is an update of just price and quantity. This is the
|
||||
// most common type of order change so we special case it so that we
|
||||
// can re-use the update message fields that will not change (which is
|
||||
// everything but quantity and price)
|
||||
void Gateway::on_gw_op(OM_ID, const GW_OrderQuickUpdate &quick_update) {
|
||||
auto gw_rec = quick_update.gw_order_record;
|
||||
auto &order = gw_rec->order;
|
||||
|
||||
GW_OrderReport reply;
|
||||
auto ack = make_ack(quick_update);
|
||||
switch (me->hot.state) {
|
||||
case State::READY: {
|
||||
|
||||
// Make sure the order can be routed
|
||||
if (!gw_rec->is_routable()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_ORDER_IS_NOT_ACTIVE);
|
||||
ack.result = GW_OP_Result::ERROR_ORDER_IS_NOT_ACTIVE;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -291,19 +298,20 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderQuickUpdate &quick_update) {
|
||||
auto lqr = me->hot.limiter.can_update_order(
|
||||
order.uav.account, *order.security, order.side, delta_q);
|
||||
if (lqr.error()) {
|
||||
reply.set_ack(lqr.result, lqr.message);
|
||||
ack.result = lqr.result;
|
||||
ack.message = lqr.message;
|
||||
break;
|
||||
}
|
||||
|
||||
// Validate messaging limits
|
||||
if (!me->hot.venue.under_messaging_limit()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_MESSAGING_BREACH);
|
||||
ack.result = GW_OP_Result::ERROR_MESSAGING_BREACH;
|
||||
break;
|
||||
}
|
||||
order.price = quick_update.price;
|
||||
order.quantity = quick_update.quantity;
|
||||
gw_rec->cancel_replace->update(order);
|
||||
me->hot.pending_msg.push_back(gw_rec->cancel_replace);
|
||||
me->hot.pending_msgs.push_back(gw_rec->cancel_replace);
|
||||
|
||||
// We do not send out a order report for performance
|
||||
// reasons. Instead we delay the report until the exchange
|
||||
@@ -311,7 +319,7 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderQuickUpdate &quick_update) {
|
||||
return;
|
||||
}
|
||||
default:
|
||||
reply.set_ack(GW_OP_Result::ERROR_GATEWAY_DISCONNECTED);
|
||||
ack.result = GW_OP_Result::ERROR_GATEWAY_DISCONNECTED;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -319,56 +327,8 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderQuickUpdate &quick_update) {
|
||||
// is the exceptional path and it should almost never happen. In
|
||||
// this case we send out a new order report detailing the reasons
|
||||
// for the failure.
|
||||
bts_assert_d(reply.last_ack.result != GW_OP_Result::OK);
|
||||
reply.last_ack.req_id = quick_update.req_id;
|
||||
reply.target = om_id;
|
||||
reply.last_exe_report = order.last_exe_report;
|
||||
reply.om_record = order.om_record;
|
||||
reply.gw_record = gw_rec;
|
||||
on_order_report(reply);
|
||||
}
|
||||
|
||||
void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCancel &cancel) {
|
||||
auto gw_rec = cancel.gw_order_record;
|
||||
auto last_er = gw_rec->order.last_exe_report;
|
||||
auto &order = gw_rec->order;
|
||||
|
||||
bts_assert_d(last_er);
|
||||
|
||||
GW_OrderReport reply;
|
||||
switch (me->hot.state) {
|
||||
case State::READY: {
|
||||
|
||||
// Make sure the order is routable
|
||||
if (!gw_rec->is_routable()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_NO_ROUTE);
|
||||
break;
|
||||
}
|
||||
|
||||
gw_rec->cancel->update(order);
|
||||
me->hot.pending_msg.push_back(gw_rec->cancel);
|
||||
|
||||
// We do not send out a order report for performance
|
||||
// reasons. Instead we delay the report until the exchange
|
||||
// reponds to our cancel
|
||||
return;
|
||||
}
|
||||
default:
|
||||
reply.set_ack(GW_OP_Result::ERROR_GATEWAY_DISCONNECTED);
|
||||
break;
|
||||
}
|
||||
|
||||
// If we got here something went wrong and the cancel failed. This
|
||||
// is the exceptional path and it should almost never happen. In
|
||||
// this case we send out a new order report detailing the reasons
|
||||
// for the failure.
|
||||
bts_assert_d(reply.last_ack.result != GW_OP_Result::OK);
|
||||
reply.last_ack.req_id = cancel.req_id;
|
||||
reply.target = om_id;
|
||||
reply.last_exe_report = order.last_exe_report;
|
||||
reply.om_record = order.om_record;
|
||||
reply.gw_record = gw_rec;
|
||||
on_order_report(reply);
|
||||
bts_assert(ack.result != GW_OP_Result::OK);
|
||||
on_order_report(make_ack_order_report(gw_rec, ack));
|
||||
}
|
||||
|
||||
// The difference between a full update and a quick update is that in
|
||||
@@ -379,30 +339,31 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) {
|
||||
auto gw_rec = full_update.gw_order_record;
|
||||
auto &order = gw_rec->order;
|
||||
|
||||
GW_OrderReport reply;
|
||||
auto ack = make_ack(full_update);
|
||||
switch (me->hot.state) {
|
||||
case State::READY: {
|
||||
|
||||
auto &msg_templates = me->hot.msg_templates;
|
||||
auto it = msg_templates.find(order.uav);
|
||||
if (it == msg_templates.end()) {
|
||||
reply.set_ack(GW_OP_Result::ERROR_NO_ROUTE);
|
||||
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
|
||||
gw_rec->clear_msgs();
|
||||
break;
|
||||
}
|
||||
|
||||
// Update existing record with the non quick parts of the full
|
||||
// order update. There is a bit of inconsistency here. We are
|
||||
// already setting the message tamplates, uav, moi and tif
|
||||
// already setting the message templates, uav, moi and tif
|
||||
// fields below, even though the quick update might later fail
|
||||
// to set the quantity and price, leaving the order in a
|
||||
// half-updated half not-updated state. This should be benign
|
||||
// enough.
|
||||
// enough though, so we live with it.
|
||||
gw_rec->set_msgs(*it->second.cancel_replace, *it->second.cancel);
|
||||
gw_rec->order.uav = full_update.uav;
|
||||
gw_rec->order.moi = full_update.moi;
|
||||
gw_rec->order.tif = full_update.tif;
|
||||
|
||||
// Fake a quick update for the rest
|
||||
// Fake a quick update, now that we already set the other fields
|
||||
GW_OrderQuickUpdate quick_update;
|
||||
quick_update.gw_order_record = full_update.gw_order_record;
|
||||
quick_update.quantity = full_update.quantity;
|
||||
@@ -411,25 +372,57 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) {
|
||||
on_gw_op(om_id, quick_update);
|
||||
}
|
||||
default:
|
||||
reply.set_ack(GW_OP_Result::ERROR_GATEWAY_DISCONNECTED);
|
||||
ack.result = GW_OP_Result::ERROR_GATEWAY_DISCONNECTED;
|
||||
break;
|
||||
}
|
||||
|
||||
// If we got to this point something went wrong and the update of
|
||||
// the order failed. This should be very rare. In this case we send
|
||||
// a reply right away.
|
||||
bts_assert_d(reply.last_ack.result != GW_OP_Result::OK);
|
||||
gw_rec->clear_msgs();
|
||||
reply.last_ack.req_id = full_update.req_id;
|
||||
reply.target = om_id;
|
||||
reply.last_exe_report = gw_rec->order.last_exe_report;
|
||||
reply.om_record = order.om_record;
|
||||
reply.gw_record = gw_rec;
|
||||
on_order_report(reply);
|
||||
bts_assert(ack.result != GW_OP_Result::OK);
|
||||
on_order_report(make_ack_order_report(gw_rec, ack));
|
||||
}
|
||||
|
||||
void Gateway::send_pending_msg() {
|
||||
auto &pending = me->hot.pending_msg;
|
||||
void Gateway::on_gw_op(OM_ID, const GW_OrderCancel &cancel) {
|
||||
auto gw_rec = cancel.gw_order_record;
|
||||
auto last_er = gw_rec->order.last_exe_report;
|
||||
auto &order = gw_rec->order;
|
||||
|
||||
bts_assert_d(last_er);
|
||||
|
||||
auto ack = make_ack(cancel);
|
||||
switch (me->hot.state) {
|
||||
case State::READY: {
|
||||
|
||||
// Make sure the order is routable
|
||||
if (!gw_rec->is_routable()) {
|
||||
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
|
||||
break;
|
||||
}
|
||||
|
||||
gw_rec->cancel->update(order);
|
||||
me->hot.pending_msgs.push_back(gw_rec->cancel);
|
||||
|
||||
// We do not send out a order report yet for performance
|
||||
// reasons. Instead we delay the report until the exchange
|
||||
// reponds to our cancel
|
||||
return;
|
||||
}
|
||||
default:
|
||||
ack.result = GW_OP_Result::ERROR_GATEWAY_DISCONNECTED;
|
||||
break;
|
||||
}
|
||||
|
||||
// If we got here something went wrong and the cancel failed. This
|
||||
// is the exceptional path and it should almost never happen. In
|
||||
// this case we send out a new order report detailing the reasons
|
||||
// for the failure.
|
||||
bts_assert(ack.result != GW_OP_Result::OK);
|
||||
on_order_report(make_ack_order_report(gw_rec, ack));
|
||||
}
|
||||
|
||||
void Gateway::send_pending_msgs() {
|
||||
auto &pending = me->hot.pending_msgs;
|
||||
me->hot.venue.send(pending.data(), pending.data() + pending.size());
|
||||
pending.clear();
|
||||
}
|
||||
@@ -502,5 +495,15 @@ log::CharPtrWrapper Gateway::state_name(State state) {
|
||||
}
|
||||
}
|
||||
|
||||
GW_OrderReport make_ack_order_report(GW_OrderRecord *gw_rec, GW_Op_Ack ack) {
|
||||
GW_OrderReport report;
|
||||
report.last_exe_report = gw_rec->order.last_exe_report;
|
||||
report.om_record = gw_rec->order.om_record;
|
||||
report.gw_record = gw_rec;
|
||||
report.last_ack = ack;
|
||||
report.target = gw_rec->originator;
|
||||
return report;
|
||||
}
|
||||
|
||||
} // namespace orderman_v2
|
||||
} // namespace bts
|
||||
@@ -50,7 +50,7 @@ class Gateway : public Actor, ISlowWorker, IGW_Op_Visitor {
|
||||
void on_gw_op(OM_ID, const GW_OrderCancel &) override;
|
||||
void on_gw_op(OM_ID, const GW_OrderFullUpdate &) override;
|
||||
|
||||
void send_pending_msg();
|
||||
void send_pending_msgs();
|
||||
bool has_bootstrap_data() const;
|
||||
bool in_active_schedule() const;
|
||||
void transition(State);
|
||||
|
||||
@@ -113,6 +113,7 @@ Gateways will wait until they have:
|
||||
- Full positions are now calculated outside the OM
|
||||
* On limiter enforcing
|
||||
- Because we now have potentially multiple threads generating updates to the working position it is no longer possible for the order manager, now the gateway, to assume it knows all the working positions. We have two options here, we can either use shared state and have all the threads coordinate access to it, or we can have the full position (filled + working) to be computed elsewhere. Since we are already late in observing the filled position we are really no worse off by also being late in observing the filled position. So we opt for having it all entirely computed elsewhere.
|
||||
- On further tought, this would allow an out of control algo to increase the working position to clip size*max messages per second. This likely needs addressing, perhaps by pessimistically adding the in flight order messages and accounting for them separately until they g
|
||||
* Work Log
|
||||
** <2019-11-25 Mon>
|
||||
*** Goals
|
||||
|
||||
@@ -19,7 +19,7 @@ struct GatewayCfg {
|
||||
};
|
||||
|
||||
template <class EV> constexpr bool meets_event_constraints() {
|
||||
return std::is_trivial<EV>::value && sizeof(EV) <= MaxEventSize;
|
||||
return std::is_trivially_copy_constructible<EV>::value && sizeof(EV) <= MaxEventSize;
|
||||
}
|
||||
|
||||
enum class MDRequestType { BOOK, TICKER, STATUS, SETTLEMENT };
|
||||
|
||||
Reference in new issue
Block a user