Gateway refactor.

This commit is contained in:
Pedro Pinto committed 2019-12-09 14:18:42 -05:00
1 parent 929c19f4b2
commit 1bfdeeded4
25 files changed
+378 -84

No files matched your search

+1 -1
View File
@@ -1 +1 @@
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/service_helpers/PositionUtilities.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/BasePositionService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/MoneyFileService.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/position_cfg.conf;config/defaults/span.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/position_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/sod.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityMapper.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;md/vendor/OnixsUtils.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/FillStore.cpp;orderman/actors/GLBXExecutionGateway.cpLine truncated
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/service_helpers/PositionUtilities.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/BasePositionService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarginService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/MoneyFileService.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/position_cfg.conf;config/defaults/span.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/position_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/span.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/sod.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/.#SocketConnection.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityMapper.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;md/vendor/OnixsUtils.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/FillStore.cpLine truncated
@@ -1,14 +1,15 @@
#include "lib/orderman_v2/actors/Gateway.h"
#include "Gateway.h"
#include "lib/logging/logging.h"
#include "lib/orderman/limiter/Limiter.h"
#include "lib/orderman_v2/execution/ICancelMsg.h"
#include "lib/orderman_v2/execution/ICancelReplaceMsg.h"
#include "lib/orderman_v2/execution/ILimiter.h"
#include "lib/orderman_v2/execution/INewOrderSingleMsg.h"
#include "lib/orderman_v2/execution/IRouter.h"
#include "lib/orderman_v2/execution/IVenue.h"
#include "lib/orderman_v2/execution/UserAccountVenue.h"
#include "lib/orderman_v2/actors/gateway/ICancelMsg.h"
#include "lib/orderman_v2/actors/gateway/ICancelReplaceMsg.h"
#include "lib/orderman_v2/actors/gateway/ILimiter.h"
#include "lib/orderman_v2/actors/gateway/INewOrderSingleMsg.h"
#include "lib/orderman_v2/actors/gateway/IRouter.h"
#include "lib/orderman_v2/actors/gateway/IVenue.h"
#include "lib/orderman_v2/actors/gateway/UserAccountVenue.h"
#include "lib/orderman_v2/utils/FAFStore.h"
#include "lib/utils/bts_chrono.h"
#include <boost/pool/object_pool.hpp>
@@ -18,13 +19,13 @@ 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)
struct Gateway::OrderRec {
OrderRec() = default;
OrderRec(OM_ID om_id, GW_BoundOrder const &order)
: order(order), cancel_replace(nullptr), cancel(nullptr),
originator(om_id) {}
@@ -46,6 +47,20 @@ struct GW_OrderRecord {
}
};
class Gateway::OrderMap {
public:
OrderRec *lookup(OrderKey k) { return _store.lookup(k.value); }
OrderRec *make_and_store(OM_ID om_id, GW_BoundOrder const &bo) {
return _store.make_and_index(bo.key.value, om_id, bo);
}
void remove(OrderKey k) { _store.remove(k.value); }
std::vector<OrderRec *> get_all() { return _store.get_all(); }
private:
FAFStore<OrderRec> _store;
};
enum class Gateway::State {
W_BOOTSTRAP,
W_SCHEDULE,
@@ -56,21 +71,19 @@ enum class Gateway::State {
};
struct Gateway::Hot {
using OrderMap = std::unordered_map<OrderKey, GW_OrderRecord *>;
using MsgTemplatesMap =
std::unordered_map<UserAccountVenue, MsgTemplates, UserAccountVenueHash>;
Hot(IVenue &v, ILimiter &l)
: state(State::W_BOOTSTRAP), venue(v), limiter(l), pending_msgs(),
working_orders(), msg_templates(), orec_pool(1024 * 1024) {}
: state(State::W_BOOTSTRAP), venue(v), limiter(l), working_orders(),
pending_msgs(), msg_templates() {}
State state;
IVenue &venue;
ILimiter &limiter;
boost::container::small_vector<IVenueMsg *, 8> pending_msgs;
OrderMap working_orders;
boost::container::small_vector<IVenueMsg *, 8> pending_msgs;
MsgTemplatesMap msg_templates;
boost::object_pool<GW_OrderRecord> orec_pool;
};
struct Gateway::Cold {
@@ -181,14 +194,15 @@ void Gateway::on_admin_data(AdminDataUpdate const &admin) {
}
void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
auto &bound_order = create.bound_order;
bts_assert_d(me->hot.working_orders.count(bound_order.key) == 0);
bts_assert_d(me->hot.working_orders.lookup(create.bound_order.key) ==
nullptr);
// Construct new order record
auto gw_rec = me->hot.orec_pool.construct(om_id, bound_order);
auto gw_rec =
me->hot.working_orders.make_and_store(om_id, create.bound_order);
auto &order = gw_rec->order;
auto ack = make_ack(create);
switch (me->hot.state) {
case State::READY: {
@@ -220,10 +234,9 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
break;
}
// All is good. Index the order record, copy the order params to
// 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);
// All is good. Copy the order params to the order message and
// queue the message for sending.
new_order_single->set_order_params(order);
me->hot.pending_msgs.push_back(new_order_single);
// For speed we do not send acks for the operation until we hear
@@ -236,12 +249,14 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
}
// If we got to this point something went wrong and the creation of
// 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.
gw_rec->clear_msgs();
// the order failed. Since the order is already indexed we need to
// remove it from the store. Additionally, because we will never
// hear from the exchange on this order we have to send our ack
// now. This is an exceptional path, it should almost never happen.
bts_assert_d(ack.result != GW_OP_Result::OK);
on_order_report(make_ack_order_report(gw_rec, ack));
auto report = make_ack_order_report(gw_rec, ack);
me->hot.working_orders.remove(order.key);
on_order_report(report);
}
// When an order is recovered from disk or from the exchange (meaning
@@ -251,14 +266,11 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) {
// 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);
// Construct and index new order record
auto gw_rec = me->hot.working_orders[order.key];
bts_assert(!gw_rec);
gw_rec = me->hot.orec_pool.construct(om_id, order);
bts_assert_d(me->hot.working_orders.lookup(bind.bound_order.key) == nullptr);
// Construct new order record
auto gw_rec = me->hot.working_orders.make_and_store(om_id, bind.bound_order);
auto &order = gw_rec->order;
auto ack = make_ack(bind);
// Find the message templates for this UAV and clone them for this
@@ -280,19 +292,25 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderBind &bind) {
// 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;
auto gw_rec = me->hot.working_orders.lookup(quick_update.order_key);
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()) {
// Do we know about this order?
if (!gw_rec) {
ack.result = GW_OP_Result::ERROR_ORDER_IS_NOT_ACTIVE;
break;
}
// Can we route it?
if (!gw_rec->is_routable()) {
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
break;
}
auto &order = gw_rec->order;
// Validate trading limits
auto delta_q = quick_update.quantity.value - order.quantity.value;
auto lqr = me->hot.limiter.can_update_order(
@@ -336,13 +354,25 @@ void Gateway::on_gw_op(OM_ID, const GW_OrderQuickUpdate &quick_update) {
// case we have to re-create the messages for cancel and update since
// these messages embed in them account and user specific information.
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;
auto gw_rec = me->hot.working_orders.lookup(full_update.order_key);
auto ack = make_ack(full_update);
switch (me->hot.state) {
case State::READY: {
// Do we know about this order?
if (!gw_rec) {
ack.result = GW_OP_Result::ERROR_ORDER_IS_NOT_ACTIVE;
break;
}
// Can we route it?
if (!gw_rec->is_routable()) {
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
break;
}
auto &order = gw_rec->order;
auto &msg_templates = me->hot.msg_templates;
auto it = msg_templates.find(order.uav);
if (it == msg_templates.end()) {
@@ -355,9 +385,9 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) {
// order update. There is a bit of inconsistency here. We are
// 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 though, so we live with it.
// to set the quantity and price. This could leave the order in
// a half-updated half not-updated state. This should be rare
// and benign enough 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;
@@ -365,11 +395,12 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) {
// 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.order_key = full_update.order_key;
quick_update.quantity = full_update.quantity;
quick_update.price = full_update.price;
quick_update.req_id = full_update.req_id;
on_gw_op(om_id, quick_update);
break;
}
default:
ack.result = GW_OP_Result::ERROR_GATEWAY_DISCONNECTED;
@@ -384,22 +415,25 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) {
}
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 gw_rec = me->hot.working_orders.lookup(cancel.order_key);
auto ack = make_ack(cancel);
switch (me->hot.state) {
case State::READY: {
// Make sure the order is routable
// Do we know about this order?
if (!gw_rec) {
ack.result = GW_OP_Result::ERROR_ORDER_IS_NOT_ACTIVE;
break;
}
// Can we route it?
if (!gw_rec->is_routable()) {
ack.result = GW_OP_Result::ERROR_NO_ROUTE;
break;
}
auto &order = gw_rec->order;
gw_rec->cancel->update(order);
me->hot.pending_msgs.push_back(gw_rec->cancel);
@@ -457,8 +491,8 @@ void Gateway::rebuild_msg_templates() {
}
// Now go through every order and update its messages
for (auto &p : me->hot.working_orders) {
auto orec = p.second;
auto working = me->hot.working_orders.get_all();
for (auto &orec : working) {
orec->clear_msgs();
if (auto it = me->hot.msg_templates.find(orec->order.uav);
it != me->hot.msg_templates.end()) {
@@ -476,6 +510,15 @@ void Gateway::setup_bootstrap_orders() {
for (auto [om_id, order_vec] : me->cold.bootstrap_orders)
for (auto &bind_order : *order_vec)
on_gw_op(om_id, bind_order);
me->cold.bootstrap_orders.clear();
}
GW_OrderReport Gateway::make_ack_order_report(OrderRec *gw_rec, GW_Op_Ack ack) {
GW_OrderReport report;
report.last_exe_report = gw_rec->order.last_exe_report;
report.last_ack = ack;
report.target = gw_rec->originator;
return report;
}
log::CharPtrWrapper Gateway::state_name(State state) {
@@ -493,16 +536,7 @@ log::CharPtrWrapper Gateway::state_name(State state) {
case State::W_LOGOUT:
return {"W_LOGOUT"};
}
}
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;
return {""};
}
} // namespace orderman_v2
@@ -1,6 +1,6 @@
#pragma once
#include "Actor.h"
#include "lib/orderman_v2/actors/Actor.h"
#include "lib/orderman_v2/common.h"
#include "lib/orderman_v2/events/events.h"
#include "lib/orderman_v2/interconnect/ISlowWorker.h"
@@ -38,12 +38,14 @@ class Gateway : public Actor, ISlowWorker, IGW_Op_Visitor {
private:
enum class State;
class OrderMap;
struct OrderRec;
struct PImpl;
struct Hot;
struct Cold;
std::unique_ptr<PImpl> me;
// Destructuring of GW_Op_Batch
// Unpacking GW_Op_Batch
void on_gw_op(OM_ID, const GW_OrderCreate &) override;
void on_gw_op(OM_ID, const GW_OrderBind &) override;
void on_gw_op(OM_ID, const GW_OrderQuickUpdate &) override;
@@ -56,6 +58,7 @@ class Gateway : public Actor, ISlowWorker, IGW_Op_Visitor {
void transition(State);
void rebuild_msg_templates();
void setup_bootstrap_orders();
GW_OrderReport make_ack_order_report(OrderRec *, GW_Op_Ack);
static log::CharPtrWrapper state_name(State) ;
};
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include "lib/orderman_v2/execution/IVenueMsg.h"
#include "lib/orderman_v2/actors/gateway/IVenueMsg.h"
namespace bts {
namespace orderman_v2 {
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include "lib/orderman_v2/execution/IVenueMsg.h"
#include "lib/orderman_v2/actors/gateway/IVenueMsg.h"
namespace bts {
namespace orderman_v2 {
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include "lib/orderman_v2/execution/IVenueMsg.h"
#include "lib/orderman_v2/actors/gateway/IVenueMsg.h"
namespace bts {
namespace orderman_v2 {
+5 -10
View File
@@ -1,15 +1,13 @@
#pragma once
#include "lib/orderman_v2/common.h"
#include "lib/orderman_v2/execution/ExecReport.h"
#include "lib/orderman_v2/actors/gateway/ExecReport.h"
#include "lib/orderman_v2/interconnect/Action.h"
#include "lib/orderman_v2/execution/UserAccountVenue.h"
#include "lib/orderman_v2/actors/gateway/UserAccountVenue.h"
#include <boost/container/small_vector.hpp>
namespace bts {
namespace orderman_v2 {
struct GW_OrderRecord;
struct OM_NativeOrderRecord;
class ExecReport;
struct GatewayCfg {
@@ -132,7 +130,6 @@ struct GW_OrderReport;
struct GW_BoundOrder {
api::Security const *security;
OM_NativeOrderRecord const *om_record;
ExecReport const *last_exe_report;
OrderKey key;
UserAccountVenue uav;
@@ -157,7 +154,7 @@ struct GW_OrderBind {
struct GW_OrderQuickUpdate {
static const auto code = GW_Op_Code::QUICK_UPDATE;
GW_OrderRecord *gw_order_record;
OrderKey order_key;
Quantity quantity;
Price price;
GW_Request_ID req_id;
@@ -165,7 +162,7 @@ struct GW_OrderQuickUpdate {
struct GW_OrderFullUpdate {
static const auto code = GW_Op_Code::FULL_UPDATE;
GW_OrderRecord *gw_order_record;
OrderKey order_key;
Quantity quantity;
Price price;
ManualOrderIndicator moi;
@@ -176,14 +173,12 @@ struct GW_OrderFullUpdate {
struct GW_OrderCancel {
static const auto code = GW_Op_Code::CANCEL;
GW_OrderRecord *gw_order_record;
OrderKey order_key;
GW_Request_ID req_id;
};
struct GW_OrderReport {
ExecReport const *last_exe_report;
OM_NativeOrderRecord const *om_record;
GW_OrderRecord const *gw_record;
GW_Op_Ack last_ack;
OM_ID target;
@@ -2,11 +2,11 @@
#include "lib/config/protos/orderman_cfg.pb.h"
#include "lib/md/MDConsumer.h"
#include "lib/orderman_v2/actors/Gateway.h"
#include "lib/orderman_v2/actors/MDSource.h"
#include "lib/orderman_v2/actors/OrderManager.h"
#include "lib/orderman_v2/actors/ServiceBridge.h"
#include "lib/orderman_v2/actors/Storage.h"
#include "lib/orderman_v2/actors/gateway/Gateway.h"
#include "Thread.h"
+1
View File
@@ -0,0 +1 @@
#include "FAFStore.h"
+151
View File
@@ -0,0 +1,151 @@
#pragma once
#include <array>
#include <stdint.h>
#include <unordered_map>
#include "lib/utils/bts_assert.h"
namespace bts {
namespace orderman_v2 {
// -------------------------------------------------------------
// Benchmark Time CPU Iterations
// -------------------------------------------------------------
// fafstore_store 3 ns 3 ns 204249318
// fafstore_lookup 2 ns 2 ns 437752868
// fafstore_store_remove 4 ns 4 ns 197335848
// -------------------------------------------------------------
// Some Restrictions Apply:
// - No more than 65535 items
// - Keys are 32 bits
// - Key 0 is invalid
// - Key Colisions on the lower 16 bits are very rare
// - Lookups will almost always find a value
template <class Item>
class FAFStore {
struct Node {
uint32_t key;
union {
Item item;
uint16_t next_free;
};
};
struct Bucket {
uint16_t k_high;
uint16_t node_idx;
};
public:
FAFStore() {
for (size_t i = 0; i < _store.size()-1; i++) {
_store[i].key = 0;
_store[i].next_free = i + 1;
}
_store[_store.size()-1].key = 0;
_store[_store.size()-1].next_free = 0;
for (auto &b : _idx) {
b.k_high = 0;
b.node_idx = 0;
}
}
// Returns a record for this key or null if no such item is
// found
inline Item *lookup(uint32_t k) {
uint16_t high = k >> 16;
uint16_t low = k;
auto &bucket = _idx[low];
if (bucket.k_high == high && bucket.node_idx) {
bts_assert_d(_store[bucket.node_idx].key == k);
return &_store[bucket.node_idx].item;
} else
return lookup_fallback(k);
}
template <typename... Args>
inline Item* make_and_index(uint32_t k, Args &&... args) {
// Get new node
auto new_node_idx = _store[0].next_free;
_store[0].next_free = _store[new_node_idx].next_free;
if (new_node_idx) {
auto &node = _store[new_node_idx];
node.key = k;
new (&node.item) Item(std::forward<Args>(args)...);
uint16_t high = k >> 16;
uint16_t low = k;
auto &bucket = _idx[low];
if (bucket.node_idx == 0) {
bucket.k_high = high;
bucket.node_idx = new_node_idx;
}
else
store_fallback(k,new_node_idx);
return &node.item;
}
return nullptr;
}
inline void remove(uint32_t k) {
uint16_t high = k >> 16;
uint16_t low = k;
auto &bucket = _idx[low];
uint16_t idx = bucket.node_idx;
if (bucket.k_high == high && idx)
bucket.node_idx = 0;
else
remove_fallback(k);
// Free node
_store[idx].next_free = _store[0].next_free;
_store[0].next_free = idx;
}
std::vector<Item*> get_all() {
std::vector<Item*> result;
for (auto &item : _store)
if (item.key)
result.push_back(&item.item);
return result;
}
private:
Item *lookup_fallback(uint32_t k) {
auto idx = _fallback[k];
if (idx) {
bts_assert_d(_store[idx].key == k);
return &_store[idx].item;
}
else
return nullptr;
}
uint16_t remove_fallback(uint32_t k) {
bts_assert(false);
auto result = _fallback[k];
_fallback[k] = 0;
return result;
}
void store_fallback(uint32_t k,uint16_t idx) {
_fallback[k] = idx;
}
uint16_t get_free_node() {
auto free = _store[0].next_free;
_store[0].next_free = _store[free].next_free;
return free;
}
std::array<Bucket,1<<16> _idx;
std::array<Node, 1 << 16> _store;
std::unordered_map<uint32_t, uint16_t> _fallback;
};
} // namespace orderman_v2
} // namespace bts
+110
View File
@@ -45,6 +45,7 @@
#include <date/date.h>
#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_vector.h>
#include "lib/orderman_v2/utils/FAFStore.h"
namespace bts {
namespace cmd {
@@ -904,6 +905,115 @@ static void clocking_the_clock(benchmark::State &state) {
}
BENCHMARK(clocking_the_clock);
struct OKIdx {
struct Bucket {
uint16_t ok_high = 0;
uint16_t order_idx = 0;
};
std::array<Bucket,1<<16> ok_by_low;
uint16_t lookup(uint32_t ok) {
uint16_t high = ok >> 16;
uint16_t low = ok;
if (ok_by_low[low].ok_high == high)
return ok_by_low[low].order_idx;
else
return 0;
}
void store(uint32_t ok, uint16_t idx) {
uint16_t high = ok >> 16;
uint16_t low = ok;
ok_by_low[low].ok_high = high;
ok_by_low[low].order_idx = idx;
}
};
void seed_idx(OKIdx &idx) {
for (int i = 0; i < 256; i++)
idx.store(i,i);
}
static void order_key_idx_lookup(benchmark::State &state) {
OKIdx idx;
seed_idx(idx);
uint8_t i = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(idx.lookup(i++));
}
}
BENCHMARK(order_key_idx_lookup);
static void order_key_idx_store(benchmark::State &state) {
OKIdx idx;
seed_idx(idx);
uint32_t i = 0;
for (auto _ : state) {
idx.store(i,i);
i++;
}
benchmark::DoNotOptimize(idx.lookup(2));
}
BENCHMARK(order_key_idx_store);
void seed_idx(std::unordered_map<uint32_t,uint32_t> &idx) {
for (int i = 0; i < 256; i++)
idx[i]=1;
}
static void order_key_map_lookup(benchmark::State &state) {
std::unordered_map<uint32_t,uint32_t> idx;
seed_idx(idx);
uint8_t i = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(idx.find(i++));
}
}
BENCHMARK(order_key_map_lookup);
static void order_key_map_store(benchmark::State &state) {
std::unordered_map<uint32_t,uint32_t> idx;
seed_idx(idx);
uint8_t i = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(idx.insert({i,i}));
i++;
}
}
BENCHMARK(order_key_map_store);
static void fafstore_store(benchmark::State &state) {
orderman_v2::FAFStore<uint32_t> store;
uint16_t i = 1;
for (auto _ : state) {
store.make_and_index(i,i);
}
benchmark::DoNotOptimize(store.lookup(2));
}
BENCHMARK(fafstore_store);
static void fafstore_lookup(benchmark::State &state) {
orderman_v2::FAFStore<uint32_t> store;
for (int i = 1; i < 256; i++)
store.make_and_index(i,i);
uint8_t i = 1;
for (auto _ : state)
benchmark::DoNotOptimize(store.lookup(i++));
}
BENCHMARK(fafstore_lookup);
static void fafstore_store_remove(benchmark::State &state) {
orderman_v2::FAFStore<uint32_t> store;
uint8_t i = 1;
for (auto _ : state) {
store.make_and_index(i,i);
store.remove(i);
}
}
BENCHMARK(fafstore_store_remove);
std::string Benchmark::help() {
return "Run micro-benchmarks. Use --benchmark_filter=regex to run only "
"specific benchmarks.";