Merge from master

This commit is contained in:
Eric Harding committed 2018-10-19 12:23:02 -04:00
commit 7867cc934b
172 files changed
+3802 -1972

No files matched your search

+152 -60
View File
@@ -3,10 +3,10 @@
#include "lib/api/services/AdminService.client.pb.h"
#include "lib/config/config.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/config/protos/orderman_cfg.pb.h"
#include "lib/md/MDConsumer.h"
#include "lib/mercury/actors/Timer.h"
#include "lib/mercury/broker/AsyncEventBroker.h"
#include "lib/mercury/mercury_utils/Executor.h"
#include "lib/mercury/executor/Executor.h"
#include "lib/metrics/metrics.h"
#include "lib/orderman/actors/AsyncFactory.h"
#include "lib/orderman/actors/ExecutionGateway.h"
@@ -20,30 +20,86 @@
#include "lib/utils/bts_chrono.h"
#include "lib/plugin/plugin.h"
#include "lib/plugin/SparkServices.h"
#include "lib/utils/thread_manager.h"
#include "lib/utils/type_name.h"
#include <boost/algorithm/string/predicate.hpp>
namespace bts {
namespace orderman {
struct ActorThread {
ActorThread(const config::OrdermanCfg::Executor &cfg,
metrics::MetricsContainer &stats)
: cfg(cfg), thread(), executor(), loop_metric(), done(false) {
std::array<double, 30> buckets;
auto i = 0;
for (auto &b : buckets)
b = (i += 1000);
loop_metric = std::make_shared<metrics::HistogramMetric<30>>(
fmt::format("spark_metrics_{}_thread_loop_ns", cfg.thread_name()),
buckets);
stats.publish(loop_metric);
}
void start() {
if (cfg.poll())
thread = threading::make_thread(cfg.thread_name(), [this]() {
for (;;)
executor.do_work();
});
else
thread = threading::make_thread(cfg.thread_name(), [this]() {
const auto inner_iter = 1000;
while (!done) {
auto start = Timer::now();
for (auto i = 0; i < inner_iter; i++)
executor.do_work();
auto stop = Timer::now();
loop_metric->observe(ns_diff(stop, start) / inner_iter);
std::this_thread::sleep_for(10ms);
}
});
}
~ActorThread() {
done = true;
thread.join();
}
config::OrdermanCfg::Executor cfg;
std::thread thread;
mercury::Executor executor;
std::shared_ptr<metrics::HistogramMetric<30>> loop_metric;
std::atomic<bool> done;
};
ActorThread *find_thread_for_actor(
std::string const &actor_name,
std::vector<std::unique_ptr<ActorThread>> const &all_executors) {
for (auto const &ex : all_executors)
for (auto const &hosted : ex->cfg.hosted_actor())
if (hosted == "*" || boost::algorithm::to_lower_copy(hosted) ==
boost::algorithm::to_lower_copy(actor_name))
return ex.get();
return nullptr;
}
struct OrderServiceServer::PImpl {
PImpl(bts::api::ServiceContext &ctx,
std::shared_ptr<api::ISyntheticSecurityService> &ss,
DynamicOrderRegistry const &d,
metrics::MetricsContainer &stats)
: fast_broker(std::make_shared<mercury::AsyncEventBroker>()),
slow_broker(std::make_shared<mercury::AsyncEventBroker>()),
service_bridge(), fast_exe(*fast_broker), slow_exe(*slow_broker),
fast_thread(), slow_thread(), service_ready(false), known_orders(),
known_fills(), publish_orders(), publish_fills(),
: service_bridge(), service_ready(false), known_orders(), known_fills(),
publish_orders(), publish_fills(),
admin_service(ctx.locate<api::admin::AdminService>()), md_consumer(ss),
stats(stats), dyn_order_registry(d) {}
std::shared_ptr<mercury::AsyncEventBroker> fast_broker;
std::shared_ptr<mercury::AsyncEventBroker> slow_broker;
std::shared_ptr<ServiceBridge> service_bridge;
mercury::Executor fast_exe;
mercury::Executor slow_exe;
std::thread fast_thread;
std::thread slow_thread;
ServiceBridge *service_bridge;
std::vector<std::unique_ptr<ActorThread>> actor_threads;
std::atomic<bool> service_ready;
std::unordered_set<OrderKey> known_orders;
std::unordered_set<std::string> known_fills;
@@ -60,13 +116,87 @@ OrderServiceServer::OrderServiceServer(
bts::api::ServiceContext &ctx,
std::shared_ptr<api::ISyntheticSecurityService> &ss,
DynamicOrderRegistry const&d,
metrics::MetricsContainer &stats)
metrics::MetricsContainer &stats,
PerfTraceLog &log)
: api::orderman::OrderServiceBase(ctx), me(new PImpl(ctx, ss, d,stats)) {
me->publish_orders = make_orders_publisher();
me->publish_fills = make_fills_publisher();
me->service_bridge = std::make_shared<ServiceBridge>(
std::vector<mercury::Executor *> all_executors;
for (auto const &ex_cfg : config::get<config::OrdermanCfg>().executor()) {
auto at = std::make_unique<ActorThread>(ex_cfg, stats);
all_executors.push_back(&at->executor);
me->actor_threads.push_back(std::move(at));
}
for (auto &t : me->actor_threads)
t->executor.init_neighbours(all_executors);
// MDSource
auto thread = find_thread_for_actor("MDSource", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for MDSource. See the "
"OrdermanCfg key");
thread->executor.spawn<MDSource>(me->md_consumer, me->stats);
// OrderManager
thread = find_thread_for_actor("OrderManager", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for OrderManager. See "
"the OrdermanCfg key");
thread->executor.spawn<OrderManager>(d);
// ExecutionGateway's
thread = find_thread_for_actor("ExecutionGateway", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for ExecutionGateway "
"instances. See the OrdermanCfg key");
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
thread->executor.spawn<ExecutionGateway>(c, log);
// Timer
thread = find_thread_for_actor("Timer", me->actor_threads);
bts_assert_msg(
thread,
"Could not find a configured thread for Timer. See the OrdermanCfg key");
thread->executor.spawn<mercury::Timer>();
// AsyncFactory
thread = find_thread_for_actor("AsyncFactory", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for AsyncFactory. See "
"the OrdermanCfg key");
thread->executor.spawn<AsyncFactory>(d);
// OrderStore
thread = find_thread_for_actor("OrderStore", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for OrderStore. See "
"the OrdermanCfg key");
thread->executor.spawn<OrderStore>();
// FillStore
thread = find_thread_for_actor("FillStore", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for FilllStore. See "
"the OrdermanCfg key");
thread->executor.spawn<FillStore>();
// SecuritySrc
thread = find_thread_for_actor("SecuritySrc", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for SecuritySrc. See "
"the OrdermanCfg key");
thread->executor.spawn<SecuritySrc>(me->md_consumer);
// ServiceBridge
thread = find_thread_for_actor("ServiceBridge", me->actor_threads);
bts_assert_msg(thread,
"Could not find a configured thread for ServiceBridge. See "
"the OrdermanCfg key");
me->service_bridge = new ServiceBridge(
[this]() { me->service_ready = true; },
[this, &ctx](auto const &orders) {
@@ -76,53 +206,15 @@ OrderServiceServer::OrderServiceServer(
[this, &ctx](auto const &fill) {
ctx.post([this, fill] { this->on_fills(fill); });
},
me->dyn_order_registry,
thread->executor.make_proxy());
me->dyn_order_registry);
me->fast_broker->init_neighbours({me->slow_broker});
me->slow_broker->init_neighbours({me->fast_broker});
thread->executor.activate(me->service_bridge);
// fast actors
me->fast_exe.spawn<MDSource>(me->md_consumer, me->stats);
me->fast_exe.spawn<OrderManager>(me->dyn_order_registry);
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
me->fast_exe.spawn<ExecutionGateway>(c, stats);
me->fast_thread = std::thread([this]() {
os::set_thread_name("fast_exe");
std::array<double, 30> buckets;
auto i = 1;
for (auto &b : buckets)
b = (i *= 2);
auto loop_timer = std::make_shared<metrics::HistogramMetric<30>>(
"spark_metrics_fast_thread_loop_ns", buckets);
me->stats.publish(loop_timer);
for (;;) {
auto start = Timer::now();
me->fast_exe.do_work();
auto stop = Timer::now();
loop_timer->observe(ns_diff(stop, start));
}
});
// slow actors
me->slow_exe.activate(me->service_bridge);
me->slow_exe.spawn<mercury::Timer>();
me->slow_exe.spawn<AsyncFactory>(me->dyn_order_registry);
me->slow_exe.spawn<OrderStore>();
me->slow_exe.spawn<FillStore>();
me->slow_exe.spawn<SecuritySrc>(me->md_consumer);
me->slow_thread = std::thread([this]() {
os::set_thread_name("slow_exe");
for (;;)
if (!me->slow_exe.do_work())
std::this_thread::sleep_for(10ms);
});
// spin up threads
for (auto &t : me->actor_threads)
t->start();
// wait for the service bridge to be ready
me->service_bridge->block_until_ready();