Big refactor.

This commit is contained in:
Pedro Pinto committed 2017-09-25 20:18:28 +00:00
1 parent 3a519f748b
commit 6157decd62
88 files changed
+123 -3964

No files matched your search

+1 -1
View File
@@ -1 +1 @@
set(sources ;fabric/Actor.cpp;fabric/Domain.cpp;fabric/Executor.cpp;fabric/IActor.cpp;fabric/IDomain.cpp;fabric/TopicDirectory.cpp;fabric/tests/baseline_perf_fabric_test.cpp;fabric/tests/fabric_test.cpp;grid/Node.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/store_tests.cpp;main.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/events/TimeChange.cpp;orderman/fix_order_codec.cpp;orderman/native_orders/NativeOrder.cpp;orderman/orderman_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SharedRiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;setup/BootstrapActor.cpp;setup/ServiceHostActor.cpp;spreader/QuoteLegPriceQuantityState.cpp;spreader/SimpleSpreader.cpp;spreader/SingleLegQuoter.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp)
set(sources ;grid/Node.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/store_tests.cpp;main.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/events/TimeChange.cpp;orderman/native_orders/NativeOrder.cpp;orderman/utils/fix_order_codec.cpp;orderman/utils/orderman_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SharedRiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;spreader/QuoteLegPriceQuantityState.cpp;spreader/SimpleSpreader.cpp;spreader/SingleLegQuoter.cpp;spreader/SpreaderBase.cpp;testing/catch_test_runner.cpp;user/UserServiceServer.cpp)
-10
View File
@@ -1,10 +0,0 @@
#include "Actor.h"
namespace bts {
}
-290
View File
@@ -1,290 +0,0 @@
#pragma once
#include "IActor.h"
#include "Msg.h"
#include "Executor.h"
#include "RPC.h"
#include "TopicDirectory.h"
#include "lib/utils/bts_assert.h"
#include <boost/container/flat_map.hpp>
namespace bts {
namespace fabric {
template <class ActorT,class RPCT>
constexpr int method_idx() {return ActorT::idx((RPCT const*) nullptr);}
constexpr int max_methods() {return std::numeric_limits<MethodID::Value>::max();}
using RequestHandler = std::function<void(MethodArg const&arg, Replier const&r)>;
template <class A>
struct Ref {
using Actor = A;
Ref(const Ref&) = default;
Ref() : address () {}
Ref(Address address) : address(address) {}
Ref& operator=(Ref const&) = default;
Address address;
};
template <int N, typename... RPCs>
class ActorHelper : public IActor
{
static_assert(N < max_methods(), "Too many methods for Actor");
ActorHelper(ActorHelper const&) = delete;
ActorHelper& operator=(ActorHelper const&) = delete;
public:
ActorHelper() : _my_address(),
_executor(),
_request_handlers(),
_notification_handlers(),
_topic_dir()
{}
void set_ctx(Address my_address,Executor& d,TopicDirectory &tdir ) override {
bts_assert(!_executor);
_my_address = my_address;
_executor= &d;
_topic_dir = &tdir;
}
void on_request(MethodID mid, MethodArg const&arg, Replier const&r) override {
bts_assert(mid.value() < N);
_request_handlers[mid.value()](arg,r);
}
NotificationHandler get_notification_handler(TopicID for_topic) override {
auto it = _notification_handlers.find(for_topic);
if (it != _notification_handlers.end())
return it->second;
else
return [] (auto const&) {};
}
protected:
Address _my_address;
Executor* _executor;
std::array<RequestHandler,N> _request_handlers;
boost::container::flat_map<TopicID,NotificationHandler> _notification_handlers;
TopicDirectory *_topic_dir;
void set_handler(int n,RequestHandler const &h) {
bts_assert(n < N);
_request_handlers[n] = h;
}
};
template <int N,typename FirstRPC, typename... OtherRPC>
class ActorHelper<N,FirstRPC,OtherRPC...> : public ActorHelper<N+1,OtherRPC...> {
public:
using In = typename FirstRPC::In;
using Out = typename FirstRPC::Out;
using OnReply = typename FirstRPC::OnReply;
using RPC = FirstRPC;
using Base = ActorHelper<N+1,OtherRPC...>;
ActorHelper()
{
auto make_handler = HandlerMaker<In,Out,int>();
this->set_handler(N,make_handler(this));
}
static constexpr int idx(FirstRPC const*) {return N;}
virtual void on(RPC const&) = 0;
private:
// non void arg, non void return
template<typename In, typename Out,typename Dummy>
struct HandlerMaker {
RequestHandler operator()(ActorHelper*self) {
RequestHandler handler = [self] (MethodArg const&arg,Replier const&r) {
// wrap raw replier in a type safe way
OnReply on_reply = [self, &r] (Out const &out) {
r(RequestError::no_error,[&out] (MethodArg &dst) {safe_placement_new<Out> (&dst,out);});
};
self->on (RPC { *(const In*) &arg[0], on_reply});
};
return handler;
}
};
// void arg, non void return
template<typename Out,typename Dummy>
struct HandlerMaker<void,Out,Dummy> {
RequestHandler operator()(ActorHelper*self) {
RequestHandler handler = [self] (MethodArg const&,Replier const&r) {
OnReply on_reply = [self, &r] (Out const &out) {
r(RequestError::no_error,[&out] (MethodArg &dst) {safe_placement_new<Out> (&dst,out);});
};
self->on (RPC {on_reply});
};
return handler;
}
};
// non void arg, void return
template<typename In,typename Dummy>
struct HandlerMaker<In,void,Dummy> {
RequestHandler operator()(ActorHelper*self) {
RequestHandler handler = [self] (MethodArg const&arg,Replier const&r) {
OnReply on_reply = [self,&r] {
r(RequestError::no_error,[] (auto) {});
};
self->on (RPC { *(const In*) &arg[0],on_reply });
};
return handler;
}
};
// void arg, void return
template<typename Dummy>
struct HandlerMaker<void,void,Dummy> {
RequestHandler operator()(ActorHelper*self) {
RequestHandler handler = [self] (MethodArg const&,Replier const&r) {
OnReply on_reply = [self,&r] {
r(RequestError::no_error,[] (auto) {});
};
self->on (RPC { on_reply });
};
return handler;
}
};
};
template <typename... RPCs>
class Actor : public ActorHelper<0,RPCs...>
{
using Base = ActorHelper<sizeof...(RPCs)>;
public:
// non void arg
template <class RPCT, class RefT, typename OnReply>
void call (RefT const&ref,typename RPCT::In const&in, OnReply const &r) {
bts_assert(Base::_executor);
auto mid = MethodID(method_idx<typename RefT::Actor,RPCT>());
MethodArgCopier mcopier = [&in] (MethodArg&arg) {
safe_placement_new<typename RPCT::In>(&arg,in);
};
ReplyHandler on_reply = [r] (RequestError e, ReplyArg const&arg) {
if (e == RequestError::no_error)
r (e,(typename RPCT::Out*) &arg[0]);
else
r (e,nullptr);
};
Base::_executor->send_request(Base::_my_address,ref.address,mid,on_reply, mcopier);
}
// void arg
template <class RPCT, class RefT, typename OnReply>
void call (RefT const&ref, OnReply const &r)
{
bts_assert(Base::_executor);
std::enable_if_t<std::is_void<typename RPCT::In>::value,Msg> m;
auto mid = MethodID(method_idx<typename RefT::Actor,RPCT>());
ReplyHandler on_reply = [r] (RequestError e, ReplyArg const&arg) {
if (e == RequestError::no_error)
r (e,(typename RPCT::Out*) &arg[0]);
else
r (e,nullptr);
};
Base::_executor->send_request(Base::_my_address,ref.address,mid,on_reply,[](auto){});
}
template <class ActorT>
void spawn(ExecutorID target_exe)
{
spawn<ActorT> (target_exe,[](auto,auto) {});
}
template <class ActorT, typename Callable, typename... ConsArgs>
void spawn(ExecutorID target_exe, Callable on_address, ConsArgs&&... args)
{
bts_assert(Base::_executor);
auto ptr = std::make_unique<ActorT> (std::forward<ConsArgs>(args)...);
Base::_executor->spawn(Base::_my_address,target_exe,
std::move(ptr),
[on_address] (RequestError e,Address *addr) {
if (e == RequestError::no_error) {
auto ref = Ref<ActorT> (*addr);
on_address(e,&ref);
}
else
on_address(e,nullptr);
});
}
template <class RefT,class Callable>
void kill(RefT const&ref,Callable on_kill)
{
bts_assert(Base::_executor);
Base::_executor->kill(Base::_my_address,ref.address(),on_kill);
}
template <class TopicT, class Callable>
void subscribe (TopicT const&topic, Callable const&cb) {
bts_assert(Base::_executor);
bts_assert(Base::_notification_handlers.find(topic.id) == Base::_notification_handlers.end());
Base::_notification_handlers[topic.id] = [cb] (NotificationArg const&arg) {
using ValueType = typename TopicT::Type;
cb(*(ValueType*) &arg[0]);
};
Base::_executor->subscribe( Base::_my_address.actor(), topic.id);
}
template <class TopicT>
void unsubscribe (TopicT const&topic) {
bts_assert(Base::_executor);
bts_assert(Base::_notification_handlers.find(topic.id) != Base::_notification_handlers.end());
Base::_notification_handlers[topic.id] = [] (auto &) {};
Base::_executor->unsubscribe( Base::_my_address.actor(), topic.id);
}
template <class V>
Topic<V> lookup (std::string const&uniq="")
{
bts_assert(Base::_executor);
TopicDirectory *dir = Base::_topic_dir;
return Topic<V> {dir->lookup<V>(uniq)};
}
template <class TopicT>
void publish(TopicT const&t,typename TopicT::Type const&v)
{
bts_assert(Base::_executor);
Base::_executor->publish(Base::_my_address.actor(),t.id,[&v] (NotificationArg &arg) {
safe_placement_new<typename TopicT::Type> (&arg,v);
});
}
};
}
}
-209
View File
@@ -1,209 +0,0 @@
#include "Domain.h"
#include "Executor.h"
#include "Msg.h"
#include "lib/config/protos/Executor.pb.h"
#include "lib/os/os.h"
#include <moodycamel/concurrentqueue.h>
#include <thread>
#include <fmt/format.h>
namespace bts {
namespace fabric {
struct Domain::ExecutorRecord {
ExecutorRecord (DomainID const&domain_id,
ExecutorID const&exe_id,
config::Executor const&cfg,
IDomain &domain,
TopicDirectory &td) :
executor(Address {executor_aid(),domain_id, exe_id}, domain,td),
queue(12000),
worker([this,cfg] () {run(cfg);})
{}
void run(config::Executor const&cfg) {
auto tn = fmt::format("executor_{}",executor.address().executor().value());
os::set_thread_name(tn.c_str());
if (cfg.affinity() >= 0) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(executor.address().executor().value(), &cpuset);
bts_assert(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0);
}
auto yield = cfg.yield_cpu();
Msg m;
while (true ) {
auto executed = executor.on_poll();
if (queue.try_dequeue(m)) {
executed++;
switch (m.type) {
case Msg::REQUEST:
executor.on_request(m.request);
break;
case Msg::REPLY:
executor.on_reply(m.reply);
break;
case Msg::SUBSCRIPTION:
executor.on_subscription(m.subscription);
break;
case Msg::UNSUBSCRIPTION:
executor.on_unsubscription(m.unsubscription);
break;
case Msg::NOTIFICATION:
executor.on_notification(m.notification);
break;
case Msg::SHUTDOWN:
return;
};
}
if (executed == 0 && yield)
std::this_thread::yield();
}
}
~ExecutorRecord() {
Msg m;
m.type = Msg::SHUTDOWN;
queue.enqueue(m);
worker.join();
}
Executor executor;
moodycamel::ConcurrentQueue<Msg> queue;
std::thread worker;
};
struct Domain::PImpl {
PImpl(DomainID id) :
my_id(id),
executor_record(),
topic_directory()
{}
DomainID my_id;
std::vector<std::unique_ptr<ExecutorRecord>> executor_record;
TopicDirectory topic_directory;
};
Domain::Domain (config::ExecutorCfg const&ecfg, ActorPtr &&bootstrap) :
me (new PImpl(DomainID(0)))
{
bts_assert(ecfg.executor_size());
for (int i = 0; i < ecfg.executor_size(); i++)
me->executor_record.emplace_back(new ExecutorRecord(me->my_id,ExecutorID(i),
ecfg.executor(i),
*this,
me->topic_directory));
me->executor_record[0]->executor.bootstrap(std::move(bootstrap));
}
Domain::~Domain() {}
void Domain::send_request(Address from, Address to, MethodID mid,RequestID rid,MethodArgCopier const&cp)
{
bts_assert_d(to.domain() == me->my_id &&
to.executor().value() < me->executor_record.size());
Msg m;
m.type = Msg::REQUEST;
m.request.sender = from;
m.request.receiver = to;
m.request.set_request_id(rid);
m.request.set_method_id(mid);
cp(m.request.method_arg);
post(to,m);
}
void Domain::send_reply(Address from, Address to, RequestID rid,RequestError e,ReplyArgCopier const &cp)
{
bts_assert_d(to.domain() == me->my_id &&
to.executor().value() < me->executor_record.size());
Msg m;
m.type = Msg::REPLY;
m.reply.sender = from;
m.reply.receiver = to;
m.reply.set_request_id(rid);
m.reply.error = e;
cp(m.reply.reply_arg);
post(to,m);
}
void Domain::send_subscribe(Address from, Address to, TopicID tid)
{
bts_assert_d(to.domain() == me->my_id &&
to.executor().value() < me->executor_record.size());
Msg m;
m.type = Msg::SUBSCRIPTION;
m.subscription.subscriber = from;
m.subscription.set_topic(tid);
post(to,m);
}
void Domain::send_unsubscribe(Address from, Address to, TopicID tid)
{
bts_assert_d(to.domain() == me->my_id &&
to.executor().value() < me->executor_record.size());
Msg m;
m.type = Msg::UNSUBSCRIPTION;
m.unsubscription.subscriber = from;
m.unsubscription.set_topic(tid);
post(to,m);
}
void Domain::forward_subscribe(Address from, TopicID tid) {
Msg m;
m.type = Msg::SUBSCRIPTION;
m.subscription.subscriber = from;
m.subscription.set_topic(tid);
Address to {executor_aid(), me->my_id, ExecutorID(0)};
auto src_exe = from.executor().value();
for (size_t i = 0 ; i < me->executor_record.size();i++)
if (src_exe != i) {
to.set_executor(ExecutorID(i));
post(to,m);
}
}
void Domain::forward_unsubscribe(Address from, TopicID tid) {
Msg m;
m.type = Msg::UNSUBSCRIPTION;
m.unsubscription.subscriber = from;
m.unsubscription.set_topic(tid);
Address to {executor_aid(), me->my_id, ExecutorID(0)};
auto src_exe = from.executor().value();
for (size_t i = 0 ; i < me->executor_record.size();i++)
if (src_exe != i) {
to.set_executor(ExecutorID(i));
post(to,m);
}
}
void Domain::send_notify(Address from,TopicID tid,std::vector<Address> const& to,NotificationCopier const&cp)
{
Msg m;
m.type = Msg::NOTIFICATION;
m.notification.publisher = from;
m.notification.set_topic(tid);
for (auto &a : to ) {
cp(m.notification.notification_arg);
post(a,m);
}
}
void Domain::post(Address to, const Msg &m)
{
me->executor_record[to.executor().value()]->queue.enqueue(m);
}
}
}
-41
View File
@@ -1,41 +0,0 @@
#pragma once
#include "IDomain.h"
#include "IActor.h"
// fwd
namespace bts {
namespace config {
class ExecutorCfg;
}}
// end fws
namespace bts {
namespace fabric {
// Domains manage a set of executors, binding a separate thread to
// each and routing messages from executor to executor
class Domain : IDomain {
public:
Domain (config::ExecutorCfg const&, ActorPtr &&bootstrap);
~Domain() ;
private:
struct PImpl;
struct ExecutorRecord;
std::unique_ptr<PImpl> me;
// from IDomain
void send_request(Address from, Address to, MethodID,RequestID,MethodArgCopier const&) override;
void send_reply(Address from, Address to, RequestID,RequestError,ReplyArgCopier const &) override;
void send_subscribe(Address from, Address to, TopicID) override;
void send_unsubscribe(Address from, Address to, TopicID) override;
void forward_subscribe(Address from, TopicID) override;
void forward_unsubscribe(Address from, TopicID) override;
void send_notify(Address from,TopicID,std::vector<Address> const& to,NotificationCopier const&) override;
// end IDomain
void post(Address to, Msg const&m);
};
}
}
-331
View File
@@ -1,331 +0,0 @@
#include "Executor.h"
#include "IDomain.h"
#include "IPollable.h"
#include <algorithm>
namespace bts {
namespace fabric {
struct Executor::TopicRecord {
TopicRecord() :
local_subscribers(),
external_subscribers()
{}
TopicRecord (TopicRecord&&src) :
local_subscribers(),
external_subscribers()
{operator=(std::move(src));}
TopicRecord (TopicRecord const&src) :
local_subscribers(),
external_subscribers()
{operator=(src);}
TopicRecord& operator=(TopicRecord const &src)
{
local_subscribers = src.local_subscribers;
external_subscribers = src.external_subscribers;
return *this;
}
TopicRecord& operator=(TopicRecord&&src)
{
local_subscribers = std::move(src.local_subscribers);
external_subscribers = std::move(src.external_subscribers);
return *this;
}
std::vector<std::pair<ActorID,NotificationHandler>> local_subscribers;
std::vector<Address> external_subscribers;
};
struct Executor::LifeCycleManager : public IActor {
LifeCycleManager(Executor&owner) :owner(owner) {}
Executor &owner;
void set_ctx(Address, Executor&,TopicDirectory&) override {}
void on_request(MethodID mid, MethodArg const&arg, Replier const&replier) override
{
if (mid == spawn_actor_mid()) {
ActorPtr actor_ptr (*(IActor**)&arg[0] );
auto p = owner.on_spawn_actor(std::move(actor_ptr));
auto error = std::get<0>(p);
auto address = std::get<1>(p);
auto copier = [address] (ReplyArg&arg) {safe_placement_new<Address> (&arg,address);};
replier(error,copier);
}
else if (mid == kill_actor_mid()) {
ActorID aid = *(ActorID*) &arg[0];
auto error = owner.on_kill_actor(aid);
replier (error,[] (auto) {} );
}
else
replier(RequestError::unknown_method,[](auto){});
}
NotificationHandler get_notification_handler(TopicID) override
{return [](const auto&) {};}
};
struct Executor::PImpl
{
PImpl(Address my_address,IDomain &domain,TopicDirectory &topic_directory) :
domain(domain),
actors(),
pollable_actors(),
pending_requests(),
subscriptions(),
next_actor_id(executor_aid().value()+1),
next_request_id(1),
my_address(my_address),
topic_directory(topic_directory)
{}
IDomain& domain;
boost::container::flat_map<ActorID,ActorPtr> actors;
std::vector<std::pair<ActorID,IPollable*>> pollable_actors;
boost::container::flat_map<RequestID,std::pair<ActorID,ReplyHandler>> pending_requests;
boost::container::flat_map<TopicID,TopicRecord> subscriptions;
ActorID::Value next_actor_id;
RequestID next_request_id;
Address my_address;
TopicDirectory &topic_directory;
};
Executor::Executor(Address my_address,
IDomain &domain,
TopicDirectory &directory) : me (new PImpl(my_address,domain,directory))
{
me->actors[executor_aid()].reset (new LifeCycleManager {*this} );
}
Executor::~Executor() {}
int Executor::on_poll()
{
auto actions_executed = 0;
for (auto &ipoll : me->pollable_actors)
actions_executed += ipoll.second->on_poll();
return actions_executed;
}
void Executor::on_request(Request const&r) {
auto it = me->actors.find(r.receiver.actor());
if (it != me->actors.end()) {
auto replier =
[this,
sender=r.sender,
receiver=r.receiver,
rid = r.request_id()]
(RequestError error,ReplyArgCopier const &cp)
{
me->domain.send_reply(receiver,sender,rid,error,cp);
};
it->second->on_request(r.method_id(),r.method_arg,replier);
}
else
me->domain.send_reply(r.receiver,r.sender,r.request_id(),
RequestError::unknown_actor,[](auto) {});
}
void Executor::on_reply(Reply const&r)
{
auto it = me->pending_requests.find(r.request_id());
if (it != me->pending_requests.end())
it->second.second(r.error,r.reply_arg);
}
void Executor::send_request(Address from,
Address to,
MethodID method,
ReplyHandler const&on_reply,
MethodArgCopier const&copier)
{
if (me->next_request_id.value()) {
auto& pr = me->pending_requests[me->next_request_id];
pr.first = from.actor();
pr.second = on_reply;
me->domain.send_request(from,to,method,me->next_request_id,copier);
me->next_request_id = RequestID(me->next_request_id.value()+1);
}
else
on_reply(RequestError::too_many_requests,ReplyArg {});
}
std::tuple<RequestError,Address> Executor::on_spawn_actor(ActorPtr &&actor)
{
if (me->next_actor_id != 0) {
auto new_actor_id = ActorID(me->next_actor_id++);
auto ptr = actor.get();
me->actors[new_actor_id] = std::move(actor);
Address new_actor_address = me->my_address;
new_actor_address.set_actor (new_actor_id);
ptr->set_ctx(new_actor_address,*this,me->topic_directory);
ptr->init();
auto ipoll = dynamic_cast<IPollable*> (ptr);
if (ipoll)
me->pollable_actors.push_back(std::make_pair (new_actor_id,ipoll));
return std::make_tuple (RequestError::no_error,new_actor_address);
}
else
return std::make_tuple (RequestError::too_many_actors,Address{});
}
RequestError Executor::on_kill_actor(ActorID aid)
{
auto it = me->actors.find(aid);
if (it != me->actors.end()) {
it->second->finit();
me->actors.erase(aid);
// get rid of any pending calls by this actor
std::vector<RequestID> discarded;
for (auto &kvp : me->pending_requests) {
auto & k = kvp.first;
auto & v = kvp.second;
if (v.first == aid)
discarded.push_back(k);
}
for (auto rid : discarded)
me->pending_requests.erase(rid);
// remove the actor from the list of pollable actors
auto &pollable = me->pollable_actors;
pollable.erase(std::remove_if(pollable.begin(),pollable.end(),
[aid] (auto p) {return p.first == aid;}),pollable.end());
// remove any subscriptions by this actor
for (auto &kvp : me->subscriptions) {
auto &topic = kvp.first;
auto &tr = kvp.second;
auto &ls = tr.local_subscribers;
if (ls.size()) {
ls.erase(std::remove_if(ls.begin(),ls.end(),
[aid] (auto p) {return p.first == aid;}),ls.end());
if (ls.empty()) // last sub, send out unsub
me->domain.forward_unsubscribe(me->my_address,topic);
}
}
return RequestError::no_error;
}
else
return RequestError::unknown_actor;
}
void Executor::on_subscription(Subscription const&sub)
{
if (local_address(sub.subscriber)) {
auto aid = sub.subscriber.actor();
auto it = me->actors.find(aid);
if (it != me->actors.end()) {
auto handler = it->second->get_notification_handler(sub.topic());
auto &sr = me->subscriptions[sub.topic()];
sr.local_subscribers.push_back(std::make_pair (aid,handler));
if (sr.local_subscribers.size() == 1) {
sr.external_subscribers.push_back(me->my_address);
me->domain.forward_subscribe(me->my_address,sub.topic());
}
}
}
else {
auto &sr = me->subscriptions[sub.topic()];
sr.external_subscribers.push_back(sub.subscriber);
}
}
void Executor::on_unsubscription(Unsubscription const&unsub)
{
if (local_address(unsub.subscriber)) {
auto aid = unsub.subscriber.actor();
auto &s = me->subscriptions[unsub.topic()];
auto &ls = s.local_subscribers;
auto &ex = s.external_subscribers;
ls.erase(std::remove_if(ls.begin(),ls.end(), [aid] (auto p) {return p.first == aid;}),ls.end());
if (ls.empty()) {
ex.erase(std::remove_if(ex.begin(),ex.end(), [this] (auto a) {return me->my_address == a;}),ex.end());
me->domain.forward_unsubscribe(me->my_address,unsub.topic());
}
}
else {
auto &es = me->subscriptions[unsub.topic()].external_subscribers;
es.erase(std::remove_if(es.begin(),es.end(),
[s=unsub.subscriber,this]
(auto a) {return a == s;}),
es.end());
}
}
void Executor::on_notification(Notification const& n)
{
auto &ls = me->subscriptions[n.topic()].local_subscribers;
for (auto &kvp : ls)
kvp.second(n.notification_arg);
}
void Executor::subscribe(ActorID subscriber, TopicID tid)
{
auto from = me->my_address;
from.set_actor (subscriber);
me->domain.send_subscribe(from,me->my_address, tid);
}
void Executor::unsubscribe(ActorID subscriber, TopicID tid)
{
auto from = me->my_address;
from.set_actor(subscriber);
me->domain.send_unsubscribe(from,me->my_address,tid);
}
void Executor::publish(ActorID aid, TopicID tid, NotificationCopier const&cp)
{
auto addr = me->my_address;
addr.set_actor(aid);
me->domain.send_notify(addr,tid,me->subscriptions[tid].external_subscribers, cp);
}
void Executor::bootstrap(ActorPtr &&with_actor)
{on_spawn_actor(std::move(with_actor));}
bool Executor::local_address(Address a) const
{return a.domain() == me->my_address.domain() && a.executor() == me->my_address.executor();}
Address Executor::address() const {return me->my_address;}
void Executor::spawn (Address from, ExecutorID to, ActorPtr &&a, OnSpawn const&on_reply)
{
send_request(from,
Address {executor_aid(),me->my_address.domain(),to},
spawn_actor_mid(),
[on_reply] (RequestError error, ReplyArg const&reply_arg) {
if (error == RequestError::no_error)
on_reply(error,(Address*) &reply_arg[0]);
else
on_reply(error,nullptr);
},
[aptr = a.release() ] (MethodArg &marg) {
safe_placement_new<IActor*> (&marg,aptr);
});
}
void Executor::kill (Address from, Address to, std::function<void(RequestError)> const&on_reply)
{
send_request(from,
Address {executor_aid(),to.domain(),to.executor()},
kill_actor_mid(),
[on_reply] (RequestError error, ReplyArg const&) {on_reply(error);},
[aid = to.actor()] (MethodArg &marg) {safe_placement_new<ActorID> (&marg,aid);});
}
}
}
-55
View File
@@ -1,55 +0,0 @@
#pragma once
#include "Msg.h"
#include "Actor.h"
#include <memory>
#include <tuple>
namespace bts {
namespace fabric {
class IDomain;
class TopicDirectory;
// This class manages a group of actors, including their lifecycle,
// their subscriptions and their RPC's
class Executor {
public:
Executor(Address my_address,IDomain &,TopicDirectory &td);
~Executor();
void bootstrap(ActorPtr &&with_actor);
int on_poll();
void on_request(Request const&r) ;
void on_reply(Reply const&r) ;
void on_subscription(Subscription const&s);
void on_unsubscription(Unsubscription const&s);
void on_notification(Notification const& notification);
void send_request(Address from, Address to,MethodID,ReplyHandler const&,MethodArgCopier const&);
void subscribe(ActorID subscriber, TopicID);
void unsubscribe(ActorID subscriber, TopicID);
void publish(ActorID, TopicID, NotificationCopier const&);
void spawn (Address from, ExecutorID to, ActorPtr &&, OnSpawn const&on_reply);
void kill (Address from, Address to, OnKill const&on_reply);
Address address() const ;
private:
struct LifeCycleManager;
struct TopicRecord;
struct PImpl;
std::unique_ptr<PImpl> me;
std::tuple<RequestError,Address> on_spawn_actor(ActorPtr &&actor);
RequestError on_kill_actor(ActorID aid);
bool local_address(Address a) const;
};
}
}
-7
View File
@@ -1,7 +0,0 @@
#include "IActor.h"
namespace bts {
namespace fabric {
IActor::~IActor () {}
}
}
-27
View File
@@ -1,27 +0,0 @@
#pragma once
#include "Msg.h"
#include <memory>
namespace bts {
namespace fabric {
class Executor;
class TopicDirectory;
class IActor {
public:
virtual ~IActor() ;
virtual void set_ctx(Address my_address, Executor&,TopicDirectory&) = 0;
virtual void on_request(MethodID mid, MethodArg const&arg, Replier const&r) = 0;
virtual NotificationHandler get_notification_handler(TopicID for_topic) = 0;
virtual void init() {};
virtual void finit() {};
};
using ActorPtr = std::unique_ptr<IActor>;
}
}
-7
View File
@@ -1,7 +0,0 @@
#include "IDomain.h"
namespace bts {
namespace fabric {
IDomain::~IDomain() {}
}
}
-25
View File
@@ -1,25 +0,0 @@
#pragma once
#include "Msg.h"
#include <vector>
namespace bts {
namespace fabric {
class IDomain {
public:
virtual ~IDomain() ;
virtual void send_request(Address from, Address to,
MethodID, RequestID, MethodArgCopier const&) = 0;
virtual void send_reply(Address from, Address to,
RequestID, RequestError, ReplyArgCopier const &) = 0;
virtual void send_subscribe(Address from, Address to, TopicID topic) = 0;
virtual void send_unsubscribe(Address from, Address to, TopicID topic) = 0;
virtual void forward_subscribe(Address from, TopicID topic) = 0;
virtual void forward_unsubscribe(Address from, TopicID topic) = 0;
virtual void send_notify(Address from,TopicID,std::vector<Address>const& to,NotificationCopier const&) = 0;
};
}
}
-11
View File
@@ -1,11 +0,0 @@
#pragma once
namespace bts {
namespace fabric {
class IPollable {
public:
virtual ~IPollable() {};
virtual bool on_poll() = 0;
};
}}
-154
View File
@@ -1,154 +0,0 @@
#pragma once
#include "lib/utils/ValueWrapper.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/warn_ctrl.h"
#include <functional>
#include <vector>
namespace bts {
namespace fabric {
GCC_DIAG_OFF("-Weffc++")
template <typename Source,typename Target,typename...Args >
void safe_placement_new (Target *t,Args&&... args) {
static_assert(sizeof(Target) >= sizeof(Source),"Destination is not large enough to construct source type");
new (t) Source(std::forward<Args>(args)...);
}
enum class RequestError {
no_error,
timeout,
unknown_method,
unknown_actor,
unknown_domain,
too_many_actors,
too_many_requests
};
using ExecutorID = utils::ValueWrapper<struct ExecutorIDTag, uint8_t>;
using DomainID = utils::ValueWrapper<struct DomainIDTag, uint8_t>;
using ActorID = utils::ValueWrapper<struct ActorIDTag, uint16_t>;
using TopicID = utils::ValueWrapper<struct TopicIDTag,uint32_t>;
using MethodID = utils::ValueWrapper<struct MethodIDTag, uint8_t>;
using MethodArg = std::array<uint8_t,47> ;
using MethodArgCopier = std::function<void(MethodArg&)>;
using RequestID = utils::ValueWrapper<struct RequestIDTag, uint32_t>;
using ReplyArg = std::array<uint8_t,47> ;
using ReplyArgCopier = std::function<void(ReplyArg&)>;
using Replier = std::function<void(RequestError error, ReplyArgCopier const& )>;
using ReplyHandler = std::function<void(RequestError,ReplyArg const&)>;
using Method = std::function<void(MethodArg const&m,Replier const&replier)>;
using ActorMethodMap = std::vector<Method>;
using NotificationArg = std::array<uint8_t,50> ;
using NotificationHandler = std::function<void(NotificationArg const&)>;
using NotificationCopier = std::function<void(NotificationArg &)>;
inline ActorID executor_aid() {return ActorID(0);}
inline MethodID spawn_actor_mid() {return MethodID(1);}
inline MethodID kill_actor_mid() {return MethodID(0);}
struct Address {
ActorID::Value _actor;
DomainID::Value _domain;
ExecutorID::Value _executor;
Address() = default;
Address& operator=(const Address&)= default;
Address(ActorID aid, DomainID did, ExecutorID eid)
: _actor(aid.value()),
_domain(did.value()),
_executor(eid.value())
{}
ActorID actor() const {return ActorID(_actor);}
DomainID domain() const {return DomainID(_domain);}
ExecutorID executor() const {return ExecutorID(_executor);}
void set_actor(ActorID id) {_actor = id.value();}
void set_domain(DomainID did) {_domain = did.value();}
void set_executor(ExecutorID eid) {_executor = eid.value();}
bool operator==(Address rhs) const {
return
_actor==rhs._actor &&
_domain==rhs._domain &&
_executor==rhs._executor;
}
};
using OnSpawn = std::function<void(RequestError,Address*)> ;
using OnKill = std::function<void(RequestError)> ;
struct Request {
Address sender;
Address receiver;
RequestID request_id() const {return RequestID(_request_id);}
MethodID method_id() const {return MethodID(_method_id);}
void set_request_id(RequestID rid) {_request_id = rid.value();}
void set_method_id(MethodID mid) {_method_id = mid.value();}
RequestID::Value _request_id;
MethodID::Value _method_id;
MethodArg method_arg;
};
struct Reply {
Address sender;
Address receiver;
RequestID request_id() const {return RequestID(_request_id);}
void set_request_id(RequestID rid) {_request_id = rid.value();}
RequestID::Value _request_id;
RequestError error;
ReplyArg reply_arg;
};
struct Subscription {
Address subscriber;
TopicID topic() const {return TopicID(_topic);}
void set_topic(TopicID tid) {_topic = tid.value();}
TopicID::Value _topic;
};
struct Unsubscription {
Address subscriber ;
TopicID topic() const {return TopicID(_topic);}
void set_topic(TopicID tid) {_topic = tid.value();}
TopicID::Value _topic;
};
struct Notification {
Address publisher;
TopicID topic() const {return TopicID(_topic);}
void set_topic(TopicID tid) {_topic = tid.value();}
TopicID::Value _topic;
NotificationArg notification_arg;
};
struct Msg {
union {
Request request;
Reply reply;
Subscription subscription;
Unsubscription unsubscription;
Notification notification;
};
enum Type {
REQUEST,REPLY,SUBSCRIPTION, UNSUBSCRIPTION, NOTIFICATION, SHUTDOWN
} type ;
};
GCC_DIAG_RESTORE()
//static_assert(sizeof(Msg) == 64,"Msg should be 64 bytes (a cache line)");
}
}
-55
View File
@@ -1,55 +0,0 @@
#pragma once
#include <functional>
#include "Msg.h"
namespace bts {
namespace fabric {
template <typename I, typename O,typename Tag=void>
struct RPC {
using In = I;
using Out = O;
using OnReply = std::function<void(const O&)>;
In const &in;
OnReply const &on_reply;
static_assert(sizeof(In) <= sizeof(Request::method_arg),"Invalid RPC. Input type is too large");
static_assert(sizeof(Out) <= sizeof(Reply::reply_arg),"Invalid RPC. Output type is too large");
};
template <typename O,typename Tag>
struct RPC<void,O,Tag> {
using In = void;
using Out = O;
using OnReply = std::function<void(const O&)>;
OnReply const &on_reply;
static_assert(sizeof(Out) <= sizeof(Reply::reply_arg),"Invalid RPC. Output type is too large");
};
template <typename I,typename Tag>
struct RPC<I,void,Tag> {
using In = I;
using Out = void;
using OnReply = std::function<void()>;
OnReply const &on_reply;
static_assert(sizeof(In) <= sizeof(Request::method_arg),"Invalid RPC. Input type is too large");
};
template <typename Tag>
struct RPC<void,void,Tag> {
using In = void;
using Out = void;
using OnReply = std::function<void()>;
OnReply const &on_reply;
};
}
}
-20
View File
@@ -1,20 +0,0 @@
#pragma once
#include "lib/utils/type_name.h"
#include "lib/utils/warn_ctrl.h"
namespace bts {
namespace fabric {
GCC_DIAG_OFF("-Weffc++")
template <class V>
struct Topic
{
using Type = V;
TopicID id;
};
GCC_DIAG_RESTORE()
}
}
@@ -1,7 +0,0 @@
#include "TopicDirectory.h"
namespace bts {
namespace fabric {
}
}
-37
View File
@@ -1,37 +0,0 @@
#pragma once
#include <memory>
#include <unordered_map>
#include <mutex>
#include "lib/utils/type_name.h"
#include "Msg.h"
#include "Topic.h"
namespace bts {
namespace fabric {
class TopicDirectory
{
public:
TopicDirectory() : _mtx(), _map(), _next_topic(1) {}
template <class T>
Topic <T> lookup (std::string const&uniq) {
std::lock_guard<std::mutex> l(_mtx);
auto &t = _map[uniq+":"+utils::type_name<T>()];
if (t.value() == 0) {
t = TopicID(_next_topic++);
bts_assert(_next_topic);
}
return Topic<T> {t};
}
private:
std::mutex _mtx;
std::unordered_map<std::string,TopicID> _map;
TopicID::Value _next_topic;
};
}
}
@@ -1,434 +0,0 @@
#include <vector>
#include <assert.h>
#include <limits>
#include <chrono>
#include <algorithm>
#include <thread>
#include <future>
#include <functional>
#include <mutex>
#include <iostream>
#include <boost/lockfree/queue.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/signals2.hpp>
#include <catch/catch.hpp>
#include <moodycamel/readerwriterqueue.h>
namespace bts {
namespace fabric {
auto n_iterations = 10000;
using TimeStamp = std::chrono::high_resolution_clock::time_point;
using Duration = std::chrono::high_resolution_clock::duration;
using Timer = std::chrono::high_resolution_clock;
std::vector<Duration> durations;
struct Payload {
Payload() : ts(),padding() {}
TimeStamp ts;
char padding[1];
};
void receive(Payload const &src) {durations.push_back(Timer::now() - src.ts);}
struct Functor {
Functor () : ts () {}
Functor (Payload const &pl) : ts (pl) {}
Payload ts;
void operator()()
{receive (ts);}
};
void spin_us(int64_t us) {
auto start = Timer::now();
while (std::chrono::duration_cast<std::chrono::microseconds>(Timer::now()-start).count() < us)
;
}
template <class S>
size_t loop() {
durations.clear();
durations.reserve(n_iterations);
{
S s(n_iterations);
for (int i = 0; i < n_iterations; i++) {
spin_us(1);
Payload p;
p.ts = Timer::now();
s(p);
}
}
std::sort(durations.begin(),durations.end());
return std::chrono::duration_cast<std::chrono::nanoseconds>(durations[durations.size()/2]).count();
}
struct Baseline
{
Baseline (size_t) {}
void operator()(Payload const&ts) {receive(ts);}
};
struct SPSC_POD {
template <class T> using SpscQ = boost::lockfree::spsc_queue<T>;
SPSC_POD (size_t n_iterations)
: n_iterations(n_iterations),
promise(),
queue(n_iterations),
consumer(&SPSC_POD::consume,this)
{
promise.get_future().wait();
}
void operator()(const Payload &t) {queue.push(t);}
void consume() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
promise.set_value();
while(n_iterations) {
Payload t;
if (queue.pop(t)) {
receive(t);
--n_iterations;
}
}
}
~SPSC_POD() {consumer.join();}
size_t n_iterations;
std::promise<void> promise;
SpscQ<Payload> queue;
std::thread consumer;
};
struct MoodyCamel_Local_POD {
template <class T> using Q = moodycamel::ReaderWriterQueue<T>;
MoodyCamel_Local_POD (size_t n_iterations)
: n_iterations(n_iterations),
queue(n_iterations)
{
}
void operator()(const Payload &t)
{
queue.enqueue(t);
Payload p2;
queue.try_dequeue(p2);
receive(p2);
}
size_t n_iterations;
Q<Payload> queue;
};
struct MoodyCamel_POD {
template <class T> using Q = moodycamel::ReaderWriterQueue<T>;
MoodyCamel_POD (size_t n_iterations)
: n_iterations(n_iterations),
promise(),
queue(n_iterations),
consumer(&MoodyCamel_POD::consume,this)
{
promise.get_future().wait();
}
void operator()(const Payload &t) {queue.enqueue(t);}
void consume() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
promise.set_value();
while(n_iterations) {
Payload t;
if (queue.try_dequeue(t)) {
receive(t);
--n_iterations;
}
}
}
~MoodyCamel_POD() {consumer.join();}
size_t n_iterations;
std::promise<void> promise;
Q<Payload> queue;
std::thread consumer;
};
struct MPMC_POD {
template <class T> using Q = boost::lockfree::queue<T>;
MPMC_POD (size_t n_iterations)
: n_iterations(n_iterations),
promise(),
queue(n_iterations),
consumer(&MPMC_POD::consume,this)
{
promise.get_future().wait();
}
void operator()(const Payload &t) {queue.push(t);}
void consume() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
promise.set_value();
while(n_iterations) {
Payload t;
if (queue.pop(t)) {
receive(t);
--n_iterations;
}
}
}
~MPMC_POD() {consumer.join();}
size_t n_iterations;
std::promise<void> promise;
Q<Payload> queue;
std::thread consumer;
};
struct Buffered_POD {
Buffered_POD (size_t n_iterations)
: buffer()
{
buffer.reserve(n_iterations);
}
void operator()(const Payload &t)
{
buffer.push_back(t);
receive(buffer.back());
}
std::vector<Payload> buffer;
};
struct Buffered_Functor {
Buffered_Functor (size_t n_iterations)
: buffer()
{
buffer.reserve(n_iterations);
}
void operator()(const Payload &t)
{
buffer.push_back(Functor(t));
buffer.back()();
}
std::vector<Functor> buffer;
};
struct Buffered_Lambda {
using Action = std::function<void()>;
Buffered_Lambda (size_t n_iterations)
: buffer()
{buffer.reserve(n_iterations);}
void operator()(const Payload &t)
{
Action a= [t] () {receive(t);};
buffer.push_back(a);
Action a2= buffer.back();
a2();
}
std::vector<Action> buffer;
};
struct Signals {
using Signal =
boost::signals2::signal_type<void (Payload const&ts),
boost::signals2::keywords::mutex_type<
boost::signals2::dummy_mutex> >::type ;
Signals(size_t)
: signal ()
{
signal.connect(receive);
}
void operator() (const Payload &ts) {
signal(ts);
}
Signal signal;
};
struct STDFunction {
using Signal = std::function<void(const Payload &)>;
STDFunction(size_t)
: signals ()
{
signals.push_back([] (Payload const &ts) {receive (ts);});
}
void operator() (const Payload &ts) {
for (auto &s : signals)
s(ts);
}
std::vector<Signal> signals;
};
struct SPSC_Functor {
template <class T> using SpscQ = boost::lockfree::spsc_queue<T>;
SPSC_Functor (size_t n_iterations)
: n_iterations(n_iterations),
promise(),
queue(n_iterations),
consumer(&SPSC_Functor::consume,this)
{
promise.get_future().wait();
}
void operator()(const Payload &t) {
queue.push(Functor(t));
}
void consume() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
promise.set_value();
while(n_iterations) {
Functor t;
if (queue.pop(t)) {
t();
--n_iterations;
}
}
}
~SPSC_Functor() {consumer.join();}
size_t n_iterations;
std::promise<void> promise;
SpscQ<Functor> queue;
std::thread consumer;
};
struct SPSC_Lambda {
template <class T> using SpscQ = boost::lockfree::spsc_queue<T>;
using Action = std::function<void()>;
SPSC_Lambda (size_t n_iterations)
: n_iterations(n_iterations),
promise(),
queue(n_iterations),
consumer(&SPSC_Lambda::consume,this)
{
promise.get_future().wait();
}
void operator()(const Payload &t) {
queue.push([t] () {receive(t);});
}
void consume() {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
promise.set_value();
while(n_iterations) {
Action t;
if (queue.pop(t)) {
t();
--n_iterations;
}
}
}
~SPSC_Lambda() {consumer.join();}
size_t n_iterations;
std::promise<void> promise;
SpscQ<Action> queue;
std::thread consumer;
};
struct SPSC_Local_Lambda {
template <class T> using SpscQ = boost::lockfree::spsc_queue<T>;
using Action = std::function<void()>;
SPSC_Local_Lambda (size_t n_iterations)
: queue(n_iterations)
{}
void operator()(const Payload &t) {
queue.push([t] () {receive(t);});
Action a;
queue.pop(a);
a();
}
SpscQ<Action> queue;
};
int run_benchmarks()
{
using namespace bts;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
std::cout << "\tdirect calls baseline: " << loop<Baseline>() << "ns\n";
std::cout << "\tdirect calls buffered functor: " << loop<Buffered_Functor>() << "ns\n";
std::cout << "\tdirect calls signals2: " << loop<Signals>() << "ns\n";
std::cout << "\tdirect calls std_function: " << loop<STDFunction>() << "ns\n";
std::cout << std::endl;
std::cout << "\tbuffered pod: " << loop<Buffered_POD>() << "ns\n";
std::cout << "\tbuffered lambda: " << loop<Buffered_Lambda>() << "ns\n"; //
std::cout << std::endl;
std::cout << "\tspsc functor: " << loop<SPSC_Functor>() << "ns\n";
std::cout << "\tspsc pod: " << loop<SPSC_POD>() << "ns\n";
std::cout << "\tspsc lambda: " << loop<SPSC_Lambda>() << "ns\n";
std::cout << "\tspsc local_lambda: " << loop<SPSC_Local_Lambda>() << "ns\n";
std::cout << std::endl;
std::cout << "\tmpmc pod: " << loop<MPMC_POD>() << "ns\n";
std::cout << "\tmoody pod: " << loop<MoodyCamel_POD>() << "ns\n";
std::cout << "\tmoody pod local: " << loop<MoodyCamel_Local_POD>() << "ns\n";
return 0;
}
}
TEST_CASE( "Performance baseline", "[fabric]") {
REQUIRE (bts::fabric::run_benchmarks() == 0);
}
}
@@ -1,185 +0,0 @@
#include "server/sparkd/fabric/RPC.h"
#include "server/sparkd/fabric/Actor.h"
#include "server/sparkd/fabric/Domain.h"
#include "server/sparkd/fabric/TopicDirectory.h"
#include "server/sparkd/fabric/IPollable.h"
#include "lib/utils/bts_chrono.h"
#include "lib/config/protos/Executor.pb.h"
#include <catch/catch.hpp>
#include <future>
namespace bts {
namespace fabric {
static constexpr auto n_iterations = 10000;
static std::vector<int64_t> durations;
using PingRPC = RPC<TimeStamp,int64_t> ;
struct PongActor : public Actor<PingRPC> {
void on (PingRPC const&ping) override
{
ping.on_reply(ns_diff(Timer::now(),ping.in));
}
};
config::ExecutorCfg get_test_cfg()
{
config::ExecutorCfg result;
auto e = result.add_executor();
e->set_affinity(0);
e->set_yield_cpu(false);
e = result.add_executor();
e->set_affinity(1);
e->set_yield_cpu(false);
return result;
}
struct PingActor : public Actor<> {
std::promise<void> &promise;
Ref<PongActor> pong_ref;
ExecutorID pong_host;
PingActor(ExecutorID pong_host,std::promise<void> &promise)
: promise(promise),
pong_ref(),
pong_host(pong_host)
{}
void init () override {
spawn<PongActor> (pong_host,
[this] (RequestError , Ref<PongActor> const *addr) {
pong_ref = *addr;
on_pong(0);
});
}
void on_pong (int64_t delta)
{
if (delta != 0)
durations.push_back(delta);
if (durations.size() < n_iterations)
call<PingRPC> (pong_ref,Timer::now(),
[this] (RequestError,int64_t *delta) {on_pong (*delta);});
else
promise.set_value();
}
};
void ping_pong_test(ExecutorID pong_exe)
{
std::promise<void> promise;
auto ping_done = promise.get_future();
auto ping_actor = std::make_unique<PingActor> (pong_exe,promise);
Domain d {get_test_cfg(),std::move(ping_actor)};
ping_done.get();
}
int ping_pong_test()
{
durations.clear();
durations.reserve(n_iterations);
ping_pong_test(ExecutorID(0));
std::sort(durations.begin(),durations.end());
std::cout << "\nactor intra_core rpc\n"
<< "\tmedian ns: " << durations[durations.size()/2] << std::endl;
durations.clear();
durations.reserve(n_iterations);
ping_pong_test(ExecutorID(1));
std::sort(durations.begin(),durations.end());
std::cout << "\nactor inter_core rpc\n"
<< "\tmedian ns: " << durations[durations.size()/2] << std::endl;
return 0;
}
struct Pub : public Actor<>, public IPollable {
Topic<TimeStamp> ts_topic;
TimeStamp last_pub;
Pub() : ts_topic(),last_pub(Timer::now()) {}
void init() override {
ts_topic = lookup<TimeStamp>();
}
bool on_poll() override {
auto now = Timer::now();
if (ns_diff(now, last_pub) > 2000) {
publish(ts_topic,now);
last_pub = Timer::now();
return true;
}
else
return false;
}
};
struct Sub : public Actor<> {
ExecutorID pub_host;
Topic<TimeStamp> ts_topic;
std::promise<void> &promise;
Sub(ExecutorID pub_host,std::promise<void> &promise)
: pub_host(pub_host),
ts_topic(),
promise(promise)
{}
void init() {
ts_topic = lookup<TimeStamp> ();
subscribe(ts_topic,[this] (TimeStamp const&ts) {
if (durations.size() < n_iterations)
durations.push_back(ns_diff(Timer::now(),ts));
else {
unsubscribe(ts_topic);
promise.set_value();
}
});
spawn<Pub>(pub_host,[] (auto const&,auto const&) {});
}
};
void pub_sub_test(ExecutorID pub_exe)
{
std::promise<void> promise;
auto done = promise.get_future();
auto sub_actor = std::make_unique<Sub> (pub_exe,promise);
Domain d {get_test_cfg(),std::move(sub_actor)};
done.get();
}
int pub_sub_test()
{
durations.clear();
durations.reserve(n_iterations);
pub_sub_test(ExecutorID(0));
std::sort(durations.begin(),durations.end());
std::cout << "\nactor intra_core pubsub\n"
<< "\tmedian ns: " << durations[durations.size()/2] << std::endl;
durations.clear();
durations.reserve(n_iterations);
pub_sub_test(ExecutorID(1));
std::sort(durations.begin(),durations.end());
std::cout << "\nactor inter_core pubsub\n"
<< "\tmedian ns: " << durations[durations.size()/2] << std::endl;
return 0;
}
}
// TEST_CASE( "RPC Works", "[fabric]") {
// REQUIRE (bts::ping_pong_test() == 0);
// }
// TEST_CASE( "PubSub Works", "[fabric]") {
// REQUIRE (bts::pub_sub_test() == 0);
// }
}
+1 -1
View File
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace grid {
+31 -16
View File
@@ -1,24 +1,42 @@
#include "lib/os/os.h"
#include "lib/logging/logging.h"
#include "lib/config/config.h"
#include "lib/config/protos/logging.pb.h"
#include "lib/config/protos/Executor.pb.h"
#include "lib/utils/init_spark_lib.h"
#include "setup/BootstrapActor.h"
#include "fabric/Domain.h"
#include "testing/catch_test_runner.h"
#include "server/sparkd/order_man/native_orders/NativeOrderHeader.h"
#include "server/sparkd/order_man/native_orders/NativeOrderState.h"
#include "lib/config/config.h"
#include "lib/config/protos/quickbus_cfg.pb.h"
#include "lib/config/protos/Ports.pb.h"
#include "lib/quickbus/quickbus.h"
#include "lib/api/public/ServiceContext.h"
#include "server/sparkd/persistence/PersistenceServiceServer.h"
#include "server/sparkd/user/UserServiceServer.h"
#include "server/sparkd/md/run_md.h"
#include "lib/md/relay/QueryServer.h"
namespace bts {
config::QuickbusNodeCfg make_cfg()
{
config::QuickbusNodeCfg result;
result.set_my_port(config::get<config::Ports>().qbus_port());
return result;
}
void start_services()
{
qbus::Quickbus quickbus;
api::ServiceContext sc(&quickbus);
persistence::PersistenceServiceServer settings_saver(sc,"settings");
user::UserServiceServer login_server(sc);
run_market_datapublishers();
md::QueryServerPtr md_query_server(md::run_query_server(sc));
std::this_thread::sleep_until(std::chrono::time_point<std::chrono::system_clock>::max());
}
}
int main( int argc, char*argv [] )
{
std::cout << sizeof(bts::orderman::NativeOrderState) << std::endl;
std::cout << sizeof(bts::orderman::NativeOrderHeader) << std::endl;
return 1;
using namespace bts;
os::set_thread_name("main");
@@ -26,10 +44,7 @@ int main( int argc, char*argv [] )
bts::run_tests(--argc, ++argv);
else {
utils::init_spark_lib();
auto executor_cfg = config::get<config::ExecutorCfg> ();
fabric::Domain d (executor_cfg,std::make_unique<setup::BootstrapActor>(executor_cfg));
std::this_thread::sleep_until(std::chrono::time_point<std::chrono::system_clock>::max());
start_services();
utils::finit_spark_lib();
}
return 0;
@@ -1,101 +0,0 @@
#include "Gateway.h"
#include "server/sparkd/order_man/events/ExecutionReport.h"
#include "server/sparkd/order_man/events/CancelReject.h"
#include "server/sparkd/order_man/events/GatewayStatusChange.h"
#include "server/sparkd/order_man/events/GatewayBeginReplay.h"
#include "server/sparkd/order_man/events/GatewayEndReplay.h"
#include "server/sparkd/order_man/fix_order_codec.h"
#include "server/sparkd/securities/SecurityDB.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/domain_model/Security.pb.h"
namespace bts {
namespace orderman {
using namespace jetfix;
Gateway::Gateway(config::JetFIX::Connection const&c, securities::SecurityDB &db, Observer &o)
: _gw(c),_obs(o),_sec_db(db),_name(c.name())
{
// On Execution Report
_gw.on<fix::ExecutionReportMsg>() = [this] (fix::ExecutionReportMsg const&msg) {
OrderKey ok;
SecurityKey sk;
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
auto sec = _sec_db.resolve(sk);
if (!sec)
LOG(OrderMan,Error,
"Received report for an unknown security {}. Will not be "
" able to process it. This might lead to lost fills and should "
" be investigated",sk.value());
else {
std::string reject_msg;
if (msg.has<fix::Text>())
reject_msg = msg.get<fix::Text>();
auto rep = event::ExecutionReport {
to<AccountName>(msg.get<fix::Account>()),
to<OperatorID>(msg.get<fix::TargetSubID>()),
ok, sec,
api::BuySell(msg.get<fix::Side>()),
api::orderman::TimeInForce(msg.get<fix::TimeInForce>()),
api::orderman::OrderType(msg.get<fix::OrdType>()),
api::orderman::ExecType(msg.get<fix::ExecType>()),
api::orderman::OrderStatus(msg.get<fix::OrdStatus>()),
msg.get<fix::ManualOrderIndicator>() ? api::orderman::MOI_YES: api::orderman::MOI_NO,
to<ExchangeOrderID>(msg.get<fix::OrderID>()),
Price (msg.get<fix::Price>()),
Quantity(msg.get<fix::OrderQty>()),
Quantity(msg.get<fix::CumQty>()),
Quantity(msg.get<fix::LastShares>()),
securities::decode_exchange_price(*sec,Price(msg.get<fix::LastPx>())),
securities::decode_exchange_price(*sec,Price(msg.get<fix::AvgPx>())),
reject_msg
};
_obs.on(rep);
}
};
// On Cancel Reject
_gw.on<fix::CancelRejectMsg>() = [this] (fix::CancelRejectMsg const&msg) {
OrderKey ok;
SecurityKey sk;
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
std::string reject_msg;
if (msg.has<fix::Text>())
reject_msg = msg.get<fix::Text>();
else if (msg.has<fix::CXlRejReason>())
reject_msg = fmt::format ("Order cancel was rejected with error code {}", msg.get<fix::CXlRejReason>());
_obs.on(event::CancelReject {ok, reject_msg});
};
// On Status Change
_gw.on_ready() = [this] () {_obs.on(event::GatewayStatusChange{_name,true});};
_gw.on_disconnect() = [this] () {_obs.on(event::GatewayStatusChange{_name,false});};
// On Replay
_gw.on_begin_replay() = [this] () {_obs.on(event::GatewayBeginReplay{_name});};
_gw.on_end_replay() = [this] () {_obs.on(event::GatewayEndReplay{_name});};
// On Error msgs
_gw.on<fix::BusinessMessageRejectMsg>() = [this] (fix::BusinessMessageRejectMsg const&m) {
LOG(OrderMan,Error,"Unexpected business reject in {} gateway . Reason: {}. This should be investigated",
_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
};
_gw.on<fix::RejectMsg>() = [this] (fix::RejectMsg const&m) {
LOG(OrderMan,Error,"Unexpected reject message in {} gateway . Reason: {}. This should be investigated",
_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
};
_gw.start();
}
}}
@@ -1,52 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/jetfix/jetfix.h"
#include "server/sparkd/order_man/events/OutGatewayMsg.h"
#include "lib/config/protos/jetfix.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include <fmt/format.h>
namespace bts {
namespace securities {class SecurityDB;}
namespace orderman {
namespace event {
class ExecutionReport;
class CancelReject;
class GatewayStatusChange;
class GatewayBeginReplay;
class GatewayEndReplay;
}
class Gateway
{
public:
class Observer {
public:
virtual void on(event::ExecutionReport const&) = 0;
virtual void on(event::CancelReject const&) = 0;
virtual void on(event::GatewayStatusChange const&) = 0;
virtual void on(event::GatewayBeginReplay const&) = 0;
virtual void on(event::GatewayEndReplay const&) = 0;
};
Gateway(config::JetFIX::Connection const&, securities::SecurityDB &, Observer &);
void on(event::OutGatewayMsg const&m) {_gw.send(m.msg);}
void dispatch_events() {_gw.dispatch_events();}
private:
using FIXGw = jetfix::JetFIXGateway<jetfix::fix::ExecutionReportMsg,
jetfix::fix::BusinessMessageRejectMsg,
jetfix::fix::RejectMsg,
jetfix::fix::CancelRejectMsg>;
FIXGw _gw;
Observer &_obs;
securities::SecurityDB &_sec_db;
GatewayName _name;
};
}}
@@ -1 +0,0 @@
#include "MDSource.h"
@@ -1,58 +0,0 @@
#pragma once
#include "server/sparkd/order_man/events/BookUpdate.h"
#include "lib/utils/TimeStamp.h"
#include "lib/md/MDConsumerHandle.h"
#include "lib/md/MDRecoverableConsumer.h"
#include "server/sparkd/order_man/events/MDSubscription.h"
namespace bts {
namespace orderman {
class MDSource
{
public:
using OnBook = std::function<void(event::BookUpdate const&)>;
MDSource(OnBook const&cb) : _on_book(cb), _handles(),_md_consumer(),_keep_alive() {}
void on(event::MDSubscription const&sub)
{
auto k = sub.for_security->id();
for (auto &h : _handles)
if (h.handle->key() == k)
return ;
auto managed_handle = _md_consumer.get_managed_handle(k);
_keep_alive.push_back(managed_handle);
_handles.push_back({utils::TimeStamp(),managed_handle.get()});
}
void poll_md() {
event::BookUpdate book;
for (auto &h : _handles)
if (h.handle->get_depth(h.last,
std::tuple_size<event::BookSide>(),
&book.bid()[0],
&book.ask()[0]) == md2::HandleReadResult::SUCCESS) {
book.security_key = SecurityKey(h.handle->key());
_on_book(book);
}
}
private:
OnBook _on_book;
struct HandleRecord {
utils::TimeStamp last;
md2::MDConsumerHandle* handle;
};
std::vector<HandleRecord> _handles;
md::MDRecoverableConsumer _md_consumer;
std::vector<std::shared_ptr<md2::MDConsumerHandle>> _keep_alive;
};
}}
@@ -1,294 +0,0 @@
#include "NativeOrderMan.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/logging/logging.h"
#include "lib/utils/PBUtils.h"
#include "lib/logging/logging.h"
#include "lib/domain_model/Security.h"
#include "server/sparkd/order_man/events/NativeOrderStateChange.h"
#include "server/sparkd/order_man/native_orders/NativeOrder.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/securities/SecurityDB.h"
#include "server/sparkd/order_man/events/OutGatewayMsg.h"
#include "server/sparkd/order_man/events/CreateNativeOrder.h"
#include "server/sparkd/order_man/events/UpdateNativeOrder.h"
#include "server/sparkd/order_man/events/CancelNativeOrder.h"
#include "server/sparkd/order_man/events/GatewayStatusChange.h"
#include "server/sparkd/order_man/events/ExecutionReport.h"
#include "server/sparkd/order_man/events/TimeChange.h"
#include "server/sparkd/order_man/events/CancelReject.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "lib/utils/datetime.h"
#include <boost/container/flat_map.hpp>
namespace bts {
namespace orderman {
using OrderMap = boost::container::flat_map<OrderKey, NativeOrderPtr>;
enum class NativeOrderMan::State {REPLAY,DISCONNECTED,CONNECTED};
struct NativeOrderMan::PImpl
{
PImpl(securities::SecurityDB &sec_db,
config::JetFIX::Connection const&jf_cfg,
Observer &obs) :
obs(obs),
state(State::REPLAY),
latest_timestamp(to<OrderTimeStamp> (datetime::FIX_timestamp(datetime::now()))),
order_map(),
sec_db(sec_db),
free_buffers(),
jf_cfg(jf_cfg)
{}
Observer &obs;
State state;
OrderTimeStamp latest_timestamp;
OrderMap order_map;
securities::SecurityDB &sec_db;
std::vector<jetfix::OutFIXPtr> free_buffers;
config::JetFIX::Connection jf_cfg;
};
NativeOrderMan::NativeOrderMan (securities::SecurityDB &sec_db,
std::vector<api::orderman::OrderState> const &existing_orders,
config::JetFIX::Connection const&gw_connection,
Observer &obs)
: me (new PImpl(sec_db,gw_connection,obs))
{
for (auto const &os : existing_orders)
if (os.has_native()) {
auto &native_state = os.native();
auto sk = SecurityKey(native_state.security_key());
auto sec = me->sec_db.resolve(sk);
if (sec)
me->order_map[OrderKey(os.order_key())] = std::make_unique<NativeOrder> (*sec,OrderKey(os.order_key()),native_state);
else {
LOG(OrderMan,Error,
"Cannot resolve security key {} in stored order {}. This is a serious problem. "
"This order will be ignored. Order details are: {}",
sk.value(),os.order_key(),utils::pb::to_string(os));
}
}
pre_allocate_buffers();
}
void NativeOrderMan::on(grid::event::Init const&)
{
LOG(OrderMan,Diag, "NativeOrderMan init");
bts_assert(me->state == State::REPLAY);
for (auto const &kvp : me->order_map) {
auto const &order = *kvp.second;
me->obs.on(event::NativeOrderStateChange {&order.header(),order._state});
}
}
void NativeOrderMan::on(event::CreateNativeOrder const&create)
{
auto &order = *create.order;
auto k = order.key();
LOG(OrderMan,Diag, "NativeOrderMan CreateNativeOrder {}",k.value());
auto &order_ptr = me->order_map[k];
if (order_ptr) {
LOG(OrderMan,Error,
"Someone tried to create a native order "
"using existing id {}. Order was for security: {}"
" This is a dangerous bug" ,
k.value(), securities::descriptor(*order._header.security));
return;
}
order_ptr.reset(&order);
switch (me->state) {
case State::REPLAY:
case State::DISCONNECTED:
order.mark_rejected_by_spark("Could not connect to the order gateway");
break;
case State::CONNECTED:
order.set_status (api::orderman::OS_UNACKNOWLEDGED);
auto buffer = get_fix_bufer();
order.encode_create(me->latest_timestamp,create.price,create.quantity,&buffer->fix[0]);
me->obs.on(event::OutGatewayMsg {buffer});
break;
}
me->obs.on(event::NativeOrderStateChange {&order.header(),order.state()});
}
void NativeOrderMan::on(event::UpdateNativeOrder const&update)
{
auto &order = *update.order;
LOG(OrderMan,Diag, "NativeOrderMan UpdateNativeOrder {}",order.key().value());
auto buffer = get_fix_bufer();
order.encode_update(me->latest_timestamp,update.price,update.quantity,&buffer->fix[0]);
me->obs.on(event::OutGatewayMsg {buffer});
}
void NativeOrderMan::on(event::CancelNativeOrder const&cancel)
{
auto &order = *cancel.order;
LOG(OrderMan,Diag, "NativeOrderMan CancelNativeOrder {}",order.key().value());
auto buffer = get_fix_bufer();
order.encode_cancel(me->latest_timestamp,&buffer->fix[0]);
me->obs.on(event::OutGatewayMsg {buffer});
}
void NativeOrderMan::on(event::GatewayStatusChange const&status)
{
LOG(OrderMan,Diag, "NativeOrderMan GatewayStatusChange {}",status.is_connected);
if (status.is_connected) {
switch (me->state) {
case State::REPLAY:
case State::CONNECTED:
bts_assert(false);
break;
case State::DISCONNECTED:
transition(State::CONNECTED);
break;
}
} else {
switch (me->state) {
case State::REPLAY:
case State::DISCONNECTED:
bts_assert(false);
break;
case State::CONNECTED:
transition(State::DISCONNECTED);
break;
}
}
}
void NativeOrderMan::on(event::GatewayBeginReplay const&)
{
LOG(OrderMan,Diag, "NativeOrderMan GatewayBeginReplay");
bts_assert(me->state == State::REPLAY);
}
void NativeOrderMan::on(event::GatewayEndReplay const&)
{
LOG(OrderMan,Diag, "NativeOrderMan GatewayEndReplay");
bts_assert(me->state == State::REPLAY);
transition(State::DISCONNECTED);
}
void NativeOrderMan::on(event::ExecutionReport const&er)
{
auto k = er.order_key;
LOG(OrderMan,Diag, "NativeOrderMan ExecutionReport for order {}",k.value());
auto &order_ptr = me->order_map[k];
if (order_ptr)
order_ptr->on(er);
else {
LOG(OrderMan,Error,
"Received an execution report for an unknown order with key {} "
". Order state will be recovered from this report but fields "
"CustomerOrFirm and CTI code cannot be recovered and will be set "
"to the gateway's default values", k.value());
api::orderman::NativeOrderState recovered;
recovered.set_account_name(to_str(er.account_name));
recovered.set_operator_id(to_str(er.operator_id));
recovered.set_security_key(er.security->id());
recovered.set_side(er.side);
recovered.set_time_in_force(er.time_in_force);
recovered.set_order_type(er.order_type);
recovered.set_customer_or_firm(api::orderman::CustomerOrFirm(me->jf_cfg.customer_or_firm()));
recovered.set_cti_code (api::orderman::CTICode(me->jf_cfg.cti_code()[0]));
recovered.set_exchange_order_id(to_str(er.exchange_order_id));
recovered.set_manual_order_indicator(er.manual_order_indicator);
recovered.set_price(er.price.value());
recovered.set_order_quantity(er.order_quantity.value());
recovered.set_order_status(er.order_status);
recovered.set_avg_price(er.avg_price.value());
recovered.set_filled_quantity(er.filled_quantity.value());
recovered.set_reject_msg(er.reject_msg);
order_ptr = std::make_unique<NativeOrder>(*er.security,k,recovered);
}
me->obs.on(event::NativeOrderStateChange {&order_ptr->header(),order_ptr->state()});
}
void NativeOrderMan::on(event::CancelReject const&cr)
{
auto k = cr.order_key;
LOG(OrderMan,Diag, "NativeOrderMan CancelReject for order {}",k.value());
auto &order_ptr = me->order_map[k];
if (order_ptr) {
order_ptr->set_reject_msg(cr.reject_msg);
me->obs.on(event::NativeOrderStateChange {&order_ptr->header(),order_ptr->state()});
}
else
LOG(OrderMan,Error,
"Received a cancel reject for an unknown order with key {} "
"This should not happen and should be investigated", k.value());
}
void NativeOrderMan::on(event::TimeChange const&new_time)
{
me->latest_timestamp = new_time.ts_str ;
pre_allocate_buffers();
}
void NativeOrderMan::pre_allocate_buffers()
{
while (me->free_buffers.size() != 1000)
me->free_buffers.push_back(std::make_shared<jetfix::OutFIX>(me->jf_cfg));
}
jetfix::OutFIXPtr NativeOrderMan::get_fix_bufer()
{
if (me->free_buffers.empty())
pre_allocate_buffers();
auto back = me->free_buffers.back();
me->free_buffers.resize(me->free_buffers.size()-1);
return back;
}
const char* NativeOrderMan::state_name(State state)
{
switch (state) {
case State::REPLAY:
return "REPLAY";
case State::CONNECTED:
return "CONNECTED";
case State::DISCONNECTED:
return "DISCONNECTED";
}
return nullptr;
}
void NativeOrderMan::transition(State new_state)
{
LOG(OrderMan,Info,"Transitioning NativeOrderMan from {} to {}",
state_name(me->state),state_name(new_state));
}
}}
@@ -1,69 +0,0 @@
#pragma once
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/config/protos/jetfix.pb.h"
#include <memory>
#include <vector>
namespace bts {
namespace grid {namespace event {class Init;}}
namespace securities {class SecurityDB;}
namespace api {namespace orderman {class OrderState;}}
namespace orderman {
namespace event {
class NativeOrderStateChange ;
class OutGatewayMsg ;
class CreateNativeOrder ;
class UpdateNativeOrder ;
class CancelNativeOrder ;
class GatewayStatusChange ;
class GatewayBeginReplay ;
class GatewayEndReplay ;
class ExecutionReport ;
class CancelReject ;
class TimeChange ;
}
class NativeOrderMan
{
public:
class Observer {
public:
virtual void on(event::NativeOrderStateChange const&) = 0;
virtual void on(event::OutGatewayMsg const&) = 0;
};
NativeOrderMan (securities::SecurityDB &sec_db,
std::vector<api::orderman::OrderState> const &existing_orders,
config::JetFIX::Connection const&gw_connection,
Observer &obs);
void on(grid::event::Init const&);
void on(event::CreateNativeOrder const&);
void on(event::UpdateNativeOrder const&);
void on(event::CancelNativeOrder const&);
void on(event::GatewayStatusChange const&);
void on(event::GatewayBeginReplay const&);
void on(event::GatewayEndReplay const&);
void on(event::ExecutionReport const&);
void on(event::CancelReject const&);
void on(event::TimeChange const&);
private:
enum class State;
struct PImpl;
std::unique_ptr<PImpl> me;
const char* state_name(State new_state);
void transition(State new_state);
void pre_allocate_buffers();
jetfix::OutFIXPtr get_fix_bufer();
};
}}
@@ -1,36 +0,0 @@
#pragma once
#include "server/sparkd/order_man/events/NewSecurities.h"
#include "server/sparkd/order_man/events/BookUpdate.h"
#include "lib/utils/TimeStamp.h"
#include "lib/md/MDConsumerHandle.h"
namespace bts {
namespace orderman {
class TimeSource
{
public:
using OnTime = std::function<void(event::TimeChange const&)>;
MDSource(OnTime const&cb) : _on_time(cb),_last() {}
void check_time() {
auto now = Timer::now();
if (ms_diff(now,_last) > 10) {
_last = now;
event::TimeChange change;
auto ts_str = datetime::FIX_timestamp(datetime::now());
bts_assert(ts_str.size() == std::tuple_size<OrderManTime>());
std::copy(ts_str.begin(),ts_str.end(),&change.ts_str[0]);
_on_time(change);
}
}
private:
OnTime _on_time;
TimeStamp _last;
};
}}
@@ -1,282 +0,0 @@
* References
** To Exchange
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/New+Order][NewOrderSingle]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Order%2BCancel-Replace%2BRequest][CancelReplace]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Order+Cancel+Request][Cancel]]
** From Exchange
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Execution%2BReport%2B-%2BOrder%2BCreation%2BCancel%2Bor%2BModify][Ack]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Execution%2BReport%2B-%2BFill%2BNotice][Fill]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Execution%2BReport%2B-%2BOrder%2BElimination][Unsolicited Order Cancel]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Execution%2BReport%2B-%2BReject][Reject]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Execution+Report+-+Trade+Cancel][Trade Cancel]]
- [[https://www.cmegroup.com/confluence/display/EPICSANDBOX/Order%2BCancel%2BReject][Cancel Reject]]
* Fields used
| Field | Needed to create | Needed to update | Needed to cancel | Recoverable | Needed for Display | Lifecyle |
|--------------------+------------------+------------------+------------------+-------------+--------------------+--------------|
| security | Y | Y | Y | Y | Y | const,~null |
| account | Y | Y | Y | Y | Y | const,~null |
| operatorid | Y | Y | Y | Y | N | const,~null |
| orderkey | Y | Y | Y | Y | Y | const,~null |
| side | Y | Y | Y | Y | Y | const,~null |
| time_in_force | Y | Y | N | Y | Y | const,~null |
| customer_or_firm | Y | Y | N | N | Y | const,~null |
| cti_code | Y | Y | N | N | Y | const,~null |
| login | N | N | N | N | Y | const,~null |
|--------------------+------------------+------------------+------------------+-------------+--------------------+--------------|
| manual_order_indic | Y | Y | Y | Y | Y | ~const,~null |
| order_type | Y | Y | N | Y | Y | ~const,~null |
| price | Y | Y | N | Y | Y | ~const,~null |
| quantity | Y | Y | N | Y | Y | ~const,~null |
| time | Y | Y | Y | Y | Y | ~const,~null |
| exchange_order_id | N | Y | Y | Y | N | const,null |
| avg_fill_price | N | N | N | Y | Y | ~const,null |
| filled_quantity | N | N | N | Y | Y | ~const,null |
| remaining_quantity | N | N | N | Y | Y | ~const,null |
| reject_msg | N | N | N | Y | Y | ~const,null |
* Order Chain trace
** NewOrder
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 271 9=271
MsgType = NewOrderSingle 35=D
MsgSeqNum = 8026 34=8026
SenderCompID = DT9238N 49=DT9238N
SenderSubID = ROR 50=ROR
SendingTime = 20161102-20:36:47.620 52=20161102-20:36:47.620
TargetCompID = CME 56=CME
TargetSubID = 64 57=64
SenderLocationID = US,IL 142=US,IL
LastMsgSeqNumProcessed = 7374 369=7374
Account = X8CM1 1=X8CM1
ClOrdID = 2KjMkw-1C22h9-001v8Q 11=2KjMkw-1C22h9-001v8Q
HandlInst = 1 21=1
OrderQty = 1 38=1
OrdType = 2 40=2
Price = 209075 44=209075
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TimeInForce = 0 59=0
TransactTime = 20161102-20:36:47.620 60=20161102-20:36:47.620
SecurityDesc = ESZ6 107=ESZ6
SecurityType = FUT 167=FUT
MaturityMonthYear = 201612 200=201612
CustomerOrFirm = 0 204=0
MaturityDay = 16 205=16
SecurityExchange = GLBX 207=GLBX
1028 = N 1028=N
7928 = 8056218 7928=8056218
9702 = 2 9702=2
CheckSum = 127 10=127
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 334 9=334
MsgType = ExecutionReport 35=8
MsgSeqNum = 7375 34=7375
LastMsgSeqNumProcessed = 8026 369=8026
SendingTime = 20161102-20:36:47.621 52=20161102-20:36:47.621
SenderCompID = CME 49=CME
SenderSubID = 64 50=64
TargetCompID = DT9238N 56=DT9238N
TargetSubID = ROR 57=ROR
TargetLocationID = US,IL 143=US,IL
Account = X8CM1 1=X8CM1
AvgPx = 0 6=0
ClOrdID = 2KjMkw-1C22h9-001v8Q 11=2KjMkw-1C22h9-001v8Q
CumQty = 0 14=0
ExecID = 64264:14986253 17=64264:14986253
ExecTransType = 0 20=0
OrderID = 644092731924 37=644092731924
OrderQty = 1 38=1
OrdStatus = 0 39=0
OrdType = 2 40=2
OrigClOrdID = 0 41=0
Price = 209075 44=209075
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TimeInForce = 0 59=0
TransactTime = 20161102-20:36:47.620 60=20161102-20:36:47.620
SecurityDesc = ESZ6 107=ESZ6
ExecType = 0 150=0
LeavesQty = 1 151=1
SecurityType = FUT 167=FUT
ExpireDate = 20161102 432=20161102
1028 = N 1028=N
5979 = 1478119007620967425 5979=1478119007620967425
7928 = 8056218 7928=8056218
CheckSum = 038 10=038
** Cancel
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 327 9=327
MsgType = OrderCancelRequest 35=F
MsgSeqNum = 8027 34=8027
SenderCompID = DT9238N 49=DT9238N
SenderSubID = ROR 50=ROR
SendingTime = 20161102-20:36:53.202 52=20161102-20:36:53.202
TargetCompID = CME 56=CME
TargetSubID = 64 57=64
SenderLocationID = US,IL 142=US,IL
LastMsgSeqNumProcessed = 7375 369=7375
Account = X8CM1 1=X8CM1
ClOrdID = 2KjMkw-1C22hf-001v8R 11=2KjMkw-1C22hf-001v8R
HandlInst = 1 21=1
OrderID = 644092731924 37=644092731924
OrderQty = 1 38=1
OrigClOrdID = 2KjMkw-1C22h9-001v8Q 41=2KjMkw-1C22h9-001v8Q
Price = 209075 44=209075
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TransactTime = 20161102-20:36:53.201 60=20161102-20:36:53.201
SecurityDesc = ESZ6 107=ESZ6
SecurityType = FUT 167=FUT
MaturityMonthYear = 201612 200=201612
CustomerOrFirm = 0 204=0
MaturityDay = 16 205=16
SecurityExchange = GLBX 207=GLBX
1028 = N 1028=N
7928 = 8056218 7928=8056218
9702 = 2 9702=2
9717 = 2KjMkw-1C22h9-001v8Q 9717=2KjMkw-1C22h9-001v8Q
CheckSum = 186 10=186
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 379 9=379
MsgType = ExecutionReport 35=8
MsgSeqNum = 7376 34=7376
LastMsgSeqNumProcessed = 8027 369=8027
SendingTime = 20161102-20:36:53.202 52=20161102-20:36:53.202
SenderCompID = CME 49=CME
SenderSubID = 64 50=64
TargetCompID = DT9238N 56=DT9238N
TargetSubID = ROR 57=ROR
TargetLocationID = US,IL 143=US,IL
Account = X8CM1 1=X8CM1
AvgPx = 0 6=0
ClOrdID = 2KjMkw-1C22hf-001v8R 11=2KjMkw-1C22hf-001v8R
CumQty = 0 14=0
ExecID = 64264:14986290 17=64264:14986290
ExecTransType = 0 20=0
OrderID = 644092731924 37=644092731924
OrderQty = 1 38=1
OrdStatus = 4 39=4
OrdType = 2 40=2
OrigClOrdID = 2KjMkw-1C22h9-001v8Q 41=2KjMkw-1C22h9-001v8Q
Price = 209075 44=209075
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TimeInForce = 0 59=0
TransactTime = 20161102-20:36:53.202 60=20161102-20:36:53.202
SecurityDesc = ESZ6 107=ESZ6
ExecType = 4 150=4
LeavesQty = 0 151=0
SecurityType = FUT 167=FUT
ExpireDate = 20161102 432=20161102
1028 = N 1028=N
5979 = 1478119013202272743 5979=1478119013202272743
7928 = 8056218 7928=8056218
9717 = 2KjMkw-1C22h9-001v8Q 9717=2KjMkw-1C22h9-001v8Q
CheckSum = 033 10=033
** Cancel/Replace
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 344 9=344
MsgType = OrderCancelReplaceRequest 35=G
MsgSeqNum = 8028 34=8028
SenderCompID = DT9238N 49=DT9238N
SenderSubID = CAM 50=CAM
SendingTime = 20161102-20:36:58.098 52=20161102-20:36:58.098
TargetCompID = CME 56=CME
TargetSubID = 64 57=64
SenderLocationID = US,IL 142=US,IL
LastMsgSeqNumProcessed = 7376 369=7376
Account = X8CM1 1=X8CM1
ClOrdID = 2KjMkw-1C22hk-001v8S 11=2KjMkw-1C22hk-001v8S
HandlInst = 1 21=1
OrderID = 644092731620 37=644092731620
OrderQty = 1 38=1
OrdType = 2 40=2
OrigClOrdID = 2KjMkw-1C22gp-001v8P 41=2KjMkw-1C22gp-001v8P
Price = 209000 44=209000
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TimeInForce = 0 59=0
TransactTime = 20161102-20:36:58.098 60=20161102-20:36:58.098
SecurityDesc = ESZ6 107=ESZ6
SecurityType = FUT 167=FUT
MaturityMonthYear = 201612 200=201612
CustomerOrFirm = 0 204=0
MaturityDay = 16 205=16
SecurityExchange = GLBX 207=GLBX
1028 = N 1028=N
7928 = 8056218 7928=8056218
9702 = 2 9702=2
9717 = 2KjMkw-1C22gp-001v8P 9717=2KjMkw-1C22gp-001v8P
9768 = Y 9768=Y
CheckSum = 065 10=065
=-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-=
BeginString = FIX.4.2 8=FIX.4.2
BodyLength = 379 9=379
MsgType = ExecutionReport 35=8
MsgSeqNum = 7377 34=7377
LastMsgSeqNumProcessed = 8028 369=8028
SendingTime = 20161102-20:36:58.099 52=20161102-20:36:58.099
SenderCompID = CME 49=CME
SenderSubID = 64 50=64
TargetCompID = DT9238N 56=DT9238N
TargetSubID = CAM 57=CAM
TargetLocationID = US,IL 143=US,IL
Account = X8CM1 1=X8CM1
AvgPx = 0 6=0
ClOrdID = 2KjMkw-1C22hk-001v8S 11=2KjMkw-1C22hk-001v8S
CumQty = 0 14=0
ExecID = 64264:14986328 17=64264:14986328
ExecTransType = 0 20=0
OrderID = 644092731620 37=644092731620
OrderQty = 1 38=1
OrdStatus = 5 39=5
OrdType = 2 40=2
OrigClOrdID = 2KjMkw-1C22gp-001v8P 41=2KjMkw-1C22gp-001v8P
Price = 209000 44=209000
SecurityID = 2928 48=2928
Side = 1 54=1
Symbol = ES 55=ES
TimeInForce = 0 59=0
TransactTime = 20161102-20:36:58.099 60=20161102-20:36:58.099
SecurityDesc = ESZ6 107=ESZ6
ExecType = 5 150=5
LeavesQty = 1 151=1
SecurityType = FUT 167=FUT
ExpireDate = 20161102 432=20161102
1028 = N 1028=N
5979 = 1478119018099133193 5979=1478119018099133193
7928 = 8056218 7928=8056218
9717 = 2KjMkw-1C22gp-001v8P 9717=2KjMkw-1C22gp-001v8P
CheckSum = 151 10=151
* Order fields
- SenderSubID
- Account
- ClOrdID
- OrdType
- Side
- Security
* Open Issues
- Shell has ugly menus
- Self match protection
- fill mitigation
- should cancel on disconnect be on
- handling of business reject
- handling of reject
- garbage collection of orders
- separate fill maintenance
- storage shouldn't be under PersistenceService
- need to get a handle on transact time
- probably best design is to flatten order, making copies cheaper
- consider including restatement reason
@@ -1,30 +0,0 @@
#pragma once
#include <memory>
#include "lib/domain_model/common.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/md/Structure/PriceSize.h"
namespace bts {
namespace orderman {
namespace event {
using BookSide = std::array<md2::PriceSize,5> ;
class BookUpdate {
public:
SecurityKey security_key;
BookSide &bid () {return _book[0];}
BookSide &ask () {return _book[1];}
BookSide& get_side(api::BuySell bs)
{
bts_assert_d(bs != api::UNDEFINED);
return _book[bs - api::BUY];
}
private:
std::array<BookSide,2> _book;
};
}}}
@@ -1,20 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class CancelNativeOrder
{
public:
NativeOrder *order;
};
}}}
@@ -1,21 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class CancelReject
{
public:
OrderKey order_key;
std::string reject_msg;
};
}}}
@@ -1,23 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class CreateNativeOrder
{
public:
Price price;
Quantity quantity;
NativeOrder *order;
};
}}}
@@ -1,37 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class ExecutionReport
{
public:
AccountName account_name;
OperatorID operator_id;
OrderKey order_key;
api::Security const*security;
api::BuySell side;
api::orderman::TimeInForce time_in_force;
api::orderman::OrderType order_type;
api::orderman::ExecType exec_type;
api::orderman::OrderStatus order_status;
api::orderman::ManualOrderIndicator manual_order_indicator;
ExchangeOrderID exchange_order_id;
Price price;
Quantity order_quantity;
Quantity filled_quantity;
Quantity last_shares;
Price last_price;
Price avg_price;
std::string reject_msg;
};
}}}
@@ -1,15 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class GatewayBeginReplay {
public:
GatewayName name;
};
}}}
@@ -1,15 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class GatewayEndReplay {
public:
GatewayName name;
};
}}}
@@ -1,16 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class GatewayStatusChange {
public:
GatewayName name;
bool is_connected;
};
}}}
@@ -1,20 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class MDSubscription
{
public:
api::Security const* for_security;
};
}}}
@@ -1,18 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/native_orders/NativeOrderState.h"
namespace bts {
namespace orderman {
class NativeOrderHeader;
namespace event {
class NativeOrderStateChange
{
public:
NativeOrderHeader const *header;
NativeOrderState current_state;
};
}}}
@@ -1,13 +0,0 @@
#pragma once
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
namespace bts {
namespace orderman {
namespace event {
class OutGatewayMsg {
public:
jetfix::OutFIXPtr msg;
};
}}}
@@ -1 +0,0 @@
#include "TimeChange.h"
@@ -1,18 +0,0 @@
#pragma once
#include <memory>
#include "lib/utils/bts_chrono.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class TimeChange
{
public:
TimeStamp ts;
OrderTimeStamp ts_str;
};
}}}
@@ -1,22 +0,0 @@
#pragma once
#include <memory>
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
namespace event {
class UpdateNativeOrder
{
public:
Price price;
Quantity quantity;
NativeOrder *order;
};
}}}
@@ -1 +0,0 @@
#include "fix_order_codec.h"
@@ -1,49 +0,0 @@
#pragma once
#include <memory>
#include <cstring>
#include "order_man_utils.h"
#include "server/sparkd/jetfix/codec/i32_toa.h"
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
constexpr int clordid_sz() {return 20;}
constexpr int price_sz() {return 20;}
constexpr int quantity_sz() {return 6;}
inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) {
u32_toa(sk.value(),&((*out)[0]),clordid_sz()/2);
u32_toa(ok.value(),&((*out)[clordid_sz()/2]),clordid_sz()/2);
}
inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk)
{
bts_assert(in.size() == clordid_sz());
char buffer[clordid_sz()/2+1];
buffer[clordid_sz()/2] = 0;
std::memcpy(buffer,&in[0],clordid_sz()/2);
*sk = SecurityKey(std::atoi(buffer));
std::memcpy(buffer,&in[clordid_sz()/2],clordid_sz()/2);
*ok = OrderKey(std::atoi(buffer));
}
template <class MSG>
OrderKey get_order_key(MSG const &msg)
{
bts_assert(msg.template has<jetfix::fix::ClOrdID> ());
OrderKey result;
SecurityKey dummy;
decode_clorid(msg.template get<jetfix::fix::ClOrdID>(),&result,&dummy);
return result;
}
}}
@@ -1,120 +0,0 @@
#include "NativeOrder.h"
#include "server/sparkd/order_man/fix_order_codec.h"
#include "server/sparkd/jetfix/codec/FieldDefinitions.h"
namespace bts {
namespace orderman {
static auto const PriceFieldSz=20;
static auto const SizeFieldSz=6;
void NativeOrder::init_update_fix()
{
auto &encoding = _fix_data.update_encoding;
using namespace jetfix;
// price, quantity, transact time will all be changing on every order update
_fix_data.update_price_bm = encoding.add<fix::Price> (0,PriceFieldSz);
_fix_data.update_quantity_bm = encoding.add<fix::OrderQty> (0,SizeFieldSz);
_fix_data.update_transact_time_bm =
encoding.add<fix::TransactTimeStr> (datetime::FIX_timestamp(datetime::now()));
encoding.add<fix::Account> (to_str(_header.account));
encoding.add<fix::SenderSubID> (to_str(_header.operator_id));
encoding.add<fix::Side> (_header.side);
encoding.add<fix::TimeInForce> (_header.time_in_force);
encoding.add<fix::OrdType> (_header.order_type);
encoding.add<fix::CustomerOrFirm> (_header.customer_or_firm);
encoding.add<fix::CtiCode> (_header.cti_code);
encoding.add<fix::SecurityDesc> (_header.security->glbx().security_desc());
std::string clordid(clordid_sz(),' ');
encode_clorid(_header.order_key,SecurityKey(_header.security->id()),&clordid);
encoding.add<fix::ClOrdID> (clordid);
encoding.add<fix::OrigClOrdID> (clordid);
encoding.add<fix::CorrelationClOrdID> (clordid);
// if we have an exchange order id use it, otherwise we will
// do it when get our first execution report
if (_state.exchange_order_id)
update_exchange_order_id(*_state.exchange_order_id);
}
void NativeOrder::init_create_fix()
{
auto &encoding = _fix_data.update_encoding;
using namespace jetfix;
// price, quantity, transact time will all be changing on every order update
_fix_data.create_price_bm = encoding.add<fix::Price> (0,PriceFieldSz);
_fix_data.create_quantity_bm = encoding.add<fix::OrderQty> (0,SizeFieldSz);
_fix_data.create_transact_time_bm =
encoding.add<fix::TransactTimeStr> (datetime::FIX_timestamp(datetime::now()));
encoding.add<fix::Account> (to_str(_header.account));
encoding.add<fix::SenderSubID> (to_str(_header.operator_id));
encoding.add<fix::Side> (_header.side);
encoding.add<fix::TimeInForce> (_header.time_in_force);
encoding.add<fix::OrdType> (_header.order_type);
encoding.add<fix::CustomerOrFirm> (_header.customer_or_firm);
encoding.add<fix::CtiCode> (_header.cti_code);
encoding.add<fix::SecurityDesc> (_header.security->glbx().security_desc());
std::string clordid(clordid_sz(),' ');
encode_clorid(_header.order_key,SecurityKey(_header.security->id()),&clordid);
encoding.add<fix::ClOrdID> (clordid);
}
void NativeOrder::init_cancel_fix()
{
auto &encoding = _fix_data.update_encoding;
using namespace jetfix;
_fix_data.cancel_transact_time_bm =
encoding.add<fix::TransactTimeStr> (datetime::FIX_timestamp(datetime::now()));
encoding.add<fix::Account> (to_str(_header.account));
encoding.add<fix::SenderSubID> (to_str(_header.operator_id));
encoding.add<fix::Side> (_header.side);
encoding.add<fix::TimeInForce> (_header.time_in_force);
encoding.add<fix::OrdType> (_header.order_type);
encoding.add<fix::CustomerOrFirm> (_header.customer_or_firm);
encoding.add<fix::CtiCode> (_header.cti_code);
encoding.add<fix::SecurityDesc> (_header.security->glbx().security_desc());
std::string clordid(clordid_sz(),' ');
encode_clorid(_header.order_key,SecurityKey(_header.security->id()),&clordid);
encoding.add<fix::ClOrdID> (clordid);
// if we have an exchange order id use it, otherwise we will
// do it when get our first execution report
if (_state.exchange_order_id)
update_exchange_order_id(*_state.exchange_order_id);
}
void NativeOrder::init_fix()
{
init_update_fix();
init_create_fix();
init_cancel_fix();
}
void NativeOrder::update_exchange_order_id(ExchangeOrderID const&id)
{
using namespace jetfix;
bts_assert(_state.exchange_order_id == nullptr);
_exchange_order_id = id;
_state.exchange_order_id = &_exchange_order_id;
auto tstr = to_trim_str(_exchange_order_id.value());
_fix_data.update_encoding.add<fix::OrderID> (tstr.first,tstr.second);
_fix_data.cancel_encoding.add<fix::OrderID> (tstr.first,tstr.second);
}
}}
@@ -1,211 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/order_man/native_orders/NativeOrderState.h"
#include "server/sparkd/order_man/native_orders/NativeOrderHeader.h"
#include "server/sparkd/order_man/native_orders/NativeOrderFIXData.h"
#include "server/sparkd/order_man/events/ExecutionReport.h"
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
#include "lib/domain_model/Security.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "server/sparkd/order_man/fix_order_codec.h"
#include "lib/api/services/OrderService.pb.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
class NativeOrderMan;
class NativeOrder
{
public:
NativeOrder(api::Security const&sec,
OrderKey k,
api::orderman::NativeOrderState const&pb) :
_fix_data(),
_state(),
_header(),
_exchange_order_id(),
_keep_alive_rej()
{
_exchange_order_id = to<ExchangeOrderID> (pb.exchange_order_id());
_header = NativeOrderHeader {
k,
&sec,
to<AccountName>(pb.account_name()),
to<OperatorID>(pb.operator_id()),
pb.side(),
pb.time_in_force(),
pb.order_type(),
pb.manual_order_indicator(),
pb.customer_or_firm(),
pb.cti_code()
};
_state = NativeOrderState {
Price(pb.price()),
Price(pb.last_price()),
Quantity(pb.order_quantity()),
Quantity(pb.filled_quantity()),
pb.order_status(),
&_exchange_order_id,
nullptr
};
if (pb.reject_msg().size())
set_reject_msg(pb.reject_msg());
init_fix();
}
NativeOrder(NativeOrderHeader const&header) :
_fix_data(),
_state(),
_header(header),
_exchange_order_id(),
_keep_alive_rej()
{
_state.exchange_order_id = nullptr;
init_fix();
}
void mark_rejected_by_spark(std::string const&msg)
{
_state.order_status = api::orderman::OS_REJECTED_BY_SPARK;
set_reject_msg(msg);
}
void set_reject_msg(std::string const &msg)
{
auto cached_msg = std::make_shared<std::string> (msg);
_keep_alive_rej.push_back(cached_msg);
_state.reject_msg = cached_msg->c_str();
}
bool on(event::ExecutionReport const&er)
{
if (_state.exchange_order_id == nullptr)
update_exchange_order_id(er.exchange_order_id);
switch (er.exec_type) {
case api::orderman::ET_PENDING_NEW:
case api::orderman::ET_PENDING_REPLACE:
case api::orderman::ET_PENDING_CANCEL:
case api::orderman::ET_CANCEL_ACK:
// no ops
return false;
default: {
_state.last_price = er.last_price;
_state.avg_price = er.avg_price;
_state.order_quantity = er.order_quantity;
_state.filled_quantity = er.filled_quantity;
_state.order_status = er.order_status;
if (er.reject_msg.size())
set_reject_msg(er.reject_msg);
else
_state.reject_msg = nullptr;
return true;
}
}
return false;
}
bool can_be_canceled() {return _state.exchange_order_id != nullptr;}
bool can_be_udpated() {return _state.exchange_order_id != nullptr;}
inline void encode_create(OrderTimeStamp const&ts,Price p,Quantity q,jetfix::FIXEncodingStream *stream)
{
auto &encoding = _fix_data.create_encoding;
// price
_fix_data.create_price_bm =
encoding.overwrite(_fix_data.create_price_bm,
securities::encode_exchange_price(*_header.security,p).value(),
price_sz());
// size
_fix_data.create_quantity_bm =
encoding.overwrite(_fix_data.create_quantity_bm,
q.value(),
quantity_sz());
// time
_fix_data.create_transact_time_bm =
encoding.overwrite(_fix_data.create_transact_time_bm,
&ts.value()[0],ts.value().size());
auto &f = stream->new_msg(jetfix::MessageTypes::NewOrderSingle);
f.append(_fix_data.create_encoding);
}
void encode_update(OrderTimeStamp const&ts,Price p,Quantity q, jetfix::FIXEncodingStream *stream)
{
bts_assert(can_be_udpated());
auto &encoding = _fix_data.update_encoding;
// price
_fix_data.update_price_bm =
encoding.overwrite(_fix_data.update_price_bm,
securities::encode_exchange_price(*_header.security,p).value(),
price_sz());
// size
_fix_data.update_quantity_bm =
encoding.overwrite(_fix_data.update_quantity_bm,
q.value(),
quantity_sz());
// time
_fix_data.update_transact_time_bm =
encoding.overwrite(_fix_data.update_transact_time_bm,
&ts.value()[0],ts.value().size());
auto &f = stream->new_msg(jetfix::MessageTypes::CancelReplace);
f.append(_fix_data.update_encoding);
}
void encode_cancel(OrderTimeStamp const&ts,jetfix::FIXEncodingStream *stream)
{
bts_assert(can_be_canceled());
auto &encoding = _fix_data.cancel_encoding;
// time
_fix_data.cancel_transact_time_bm =
encoding.overwrite(_fix_data.cancel_transact_time_bm,
&ts.value()[0],ts.value().size());
auto &f = stream->new_msg(jetfix::MessageTypes::CancelReplace);
f.append(_fix_data.cancel_encoding);
}
OrderKey key() const {return _header.order_key;}
NativeOrderHeader const &header() const {return _header;};
NativeOrderState const &state() const {return _state;};
void set_status(api::orderman::OrderStatus status) {_state.order_status = status;}
private:
friend class NativeOrderMan;
NativeOrderFIXData _fix_data;
NativeOrderState _state;
NativeOrderHeader _header;
ExchangeOrderID _exchange_order_id;
std::vector<std::shared_ptr<std::string>> _keep_alive_rej;
void init_fix();
void init_update_fix();
void init_create_fix();
void init_cancel_fix();
void update_exchange_order_id(ExchangeOrderID const&id);
};
using NativeOrderPtr = std::unique_ptr<NativeOrder>;
}}
@@ -1,27 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/jetfix/codec/FIXFragment.h"
#include "server/sparkd/jetfix/codec/FieldDefinitions.h"
namespace bts {
namespace orderman {
class NativeOrderFIXData
{
public:
jetfix::FIXFragment update_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> update_price_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> update_quantity_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr> update_transact_time_bm;
jetfix::FIXFragment create_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::Price> create_price_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::OrderQty> create_quantity_bm;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr> create_transact_time_bm;
jetfix::FIXFragment cancel_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr> cancel_transact_time_bm;
};
}}
@@ -1,27 +0,0 @@
#pragma once
#include <memory>
#include "lib/domain_model/Security.pb.h"
#include "lib/api/services/OrderService.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
class NativeOrderHeader
{
public:
OrderKey order_key;
const api::Security *security;
AccountName account;
OperatorID operator_id;
api::BuySell side;
api::orderman::TimeInForce time_in_force;
api::orderman::OrderType order_type;
api::orderman::ManualOrderIndicator manual_order_indicator;
api::orderman::CustomerOrFirm customer_or_firm;
api::orderman::CTICode cti_code;
};
}}
@@ -1,22 +0,0 @@
#pragma once
#include <memory>
#include "lib/domain_model/common.h"
#include "lib/api/services/OrderService.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
namespace bts {
namespace orderman {
class NativeOrderState
{
public:
Price last_price;
Price avg_price;
Quantity order_quantity;
Quantity filled_quantity;
api::orderman::OrderStatus order_status;
ExchangeOrderID const *exchange_order_id;
const char*reject_msg;
};
}}
@@ -1,6 +0,0 @@
#include "order_man_utils.h"
namespace bts {
namespace orderman {
}}
@@ -1,65 +0,0 @@
#pragma once
#include <cstdint>
#include <string>
#include <array>
#include <cstring>
#include "lib/utils/ValueWrapper.h"
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
using OrderKey = utils::ValueWrapper<struct OrderKeyTag,uint32_t>;
using FIXSessionName = utils::ValueWrapper<struct FIXSessionNameTag,std::string>;
using GatewayName = utils::ValueWrapper<struct GatewayNameTag,std::string>;
using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::array<char,18>>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::array<char,17>>;
using OrderTimeStamp = utils::ValueWrapper<struct OrderTimeStampTag,std::array<char, sizeof("20121204-14:44:02.000")-1>> ;
using SecurityPtr = const api::Security *;
using AccountName = utils::ValueWrapper<struct AccountNameTag,std::array<char,12>>;
template <size_t N> void to_array(std::string const&str,std::array<char,N>*out )
{
auto &result = *out;
bts_assert(str.size() <= N);
memset(&result[0],' ', N);
memcpy(&result[0],str.data(),str.size());
}
template <size_t N> void to_str(std::array<char,N> const&in ,std::string *out)
{
auto &result = *out;
result.resize(N);
memcpy(&result[0],in.data(),N);
}
template <class T>
T to(std::string const&str)
{
T result;
to_array(str,&result.value());
return result;
}
template <class T>
inline std::string to_str (T const&t)
{
std::string result;
to_str(t.value(),&result);
return result;
}
template <size_t N>
std::pair<const char*,size_t> to_trim_str(std::array<char,N> const&in)
{
for (auto i = (size_t) 0; i < N; i++)
if (in[i] == ' ')
return std::make_pair (&in[0],i);
return std::make_pair (&in[0],N);
}
}}
@@ -5,7 +5,7 @@
#include "server/sparkd/orderman/events/GatewayStatusChange.h"
#include "server/sparkd/orderman/events/GatewayBeginReplay.h"
#include "server/sparkd/orderman/events/GatewayEndReplay.h"
#include "server/sparkd/orderman/fix_order_codec.h"
#include "server/sparkd/orderman/utils/fix_order_codec.h"
#include "server/sparkd/securities/SecurityDB.h"
#include "lib/domain_model/Security.h"
@@ -4,7 +4,7 @@
#include "server/sparkd/jetfix/jetfix.h"
#include "server/sparkd/orderman/events/OutGatewayMsg.h"
#include "lib/config/protos/jetfix.pb.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include <fmt/format.h>
@@ -9,7 +9,7 @@
#include "server/sparkd/orderman/events/NativeOrderStateChange.h"
#include "server/sparkd/orderman/native_orders/NativeOrder.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "server/sparkd/securities/SecurityDB.h"
#include "server/sparkd/orderman/events/OutGatewayMsg.h"
#include "server/sparkd/orderman/events/CreateNativeOrder.h"
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -1,7 +1,7 @@
#pragma once
#include <memory>
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -1,7 +1,7 @@
#pragma once
#include <memory>
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -1,7 +1,7 @@
#pragma once
#include <memory>
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -2,7 +2,7 @@
#include <memory>
#include "lib/utils/bts_chrono.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -4,7 +4,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/common.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -1 +0,0 @@
#include "fix_order_codec.h"
@@ -1,49 +0,0 @@
#pragma once
#include <memory>
#include <cstring>
#include "orderman_utils.h"
#include "server/sparkd/jetfix/codec/i32_toa.h"
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
constexpr int clordid_sz() {return 20;}
constexpr int price_sz() {return 20;}
constexpr int quantity_sz() {return 6;}
inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) {
u32_toa(sk.value(),&((*out)[0]),clordid_sz()/2);
u32_toa(ok.value(),&((*out)[clordid_sz()/2]),clordid_sz()/2);
}
inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk)
{
bts_assert(in.size() == clordid_sz());
char buffer[clordid_sz()/2+1];
buffer[clordid_sz()/2] = 0;
std::memcpy(buffer,&in[0],clordid_sz()/2);
*sk = SecurityKey(std::atoi(buffer));
std::memcpy(buffer,&in[clordid_sz()/2],clordid_sz()/2);
*ok = OrderKey(std::atoi(buffer));
}
template <class MSG>
OrderKey get_order_key(MSG const &msg)
{
bts_assert(msg.template has<jetfix::fix::ClOrdID> ());
OrderKey result;
SecurityKey dummy;
decode_clorid(msg.template get<jetfix::fix::ClOrdID>(),&result,&dummy);
return result;
}
}}
@@ -1,5 +1,5 @@
#include "NativeOrder.h"
#include "server/sparkd/orderman/fix_order_codec.h"
#include "server/sparkd/orderman/utils/fix_order_codec.h"
#include "server/sparkd/jetfix/codec/FieldDefinitions.h"
namespace bts {
@@ -9,7 +9,7 @@
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
#include "lib/domain_model/Security.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "server/sparkd/orderman/fix_order_codec.h"
#include "server/sparkd/orderman/utils/fix_order_codec.h"
#include "lib/api/services/OrderService.pb.h"
@@ -4,7 +4,7 @@
#include "lib/domain_model/Security.pb.h"
#include "lib/api/services/OrderService.pb.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -3,7 +3,7 @@
#include <memory>
#include "lib/domain_model/common.h"
#include "lib/api/services/OrderService.pb.h"
#include "server/sparkd/orderman/orderman_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
@@ -1,6 +0,0 @@
#include "orderman_utils.h"
namespace bts {
namespace orderman {
}}
@@ -1,65 +0,0 @@
#pragma once
#include <cstdint>
#include <string>
#include <array>
#include <cstring>
#include "lib/utils/ValueWrapper.h"
#include "lib/domain_model/common.h"
#include "lib/utils/bts_assert.h"
namespace bts {
namespace api {class Security;}
namespace orderman {
using OrderKey = utils::ValueWrapper<struct OrderKeyTag,uint32_t>;
using FIXSessionName = utils::ValueWrapper<struct FIXSessionNameTag,std::string>;
using GatewayName = utils::ValueWrapper<struct GatewayNameTag,std::string>;
using OperatorID = utils::ValueWrapper<struct OperatorIDTag,std::array<char,18>>;
using ExchangeOrderID = utils::ValueWrapper<struct ExchangeOrderIDTag,std::array<char,17>>;
using OrderTimeStamp = utils::ValueWrapper<struct OrderTimeStampTag,std::array<char, sizeof("20121204-14:44:02.000")-1>> ;
using SecurityPtr = const api::Security *;
using AccountName = utils::ValueWrapper<struct AccountNameTag,std::array<char,12>>;
template <size_t N> void to_array(std::string const&str,std::array<char,N>*out )
{
auto &result = *out;
bts_assert(str.size() <= N);
memset(&result[0],' ', N);
memcpy(&result[0],str.data(),str.size());
}
template <size_t N> void to_str(std::array<char,N> const&in ,std::string *out)
{
auto &result = *out;
result.resize(N);
memcpy(&result[0],in.data(),N);
}
template <class T>
T to(std::string const&str)
{
T result;
to_array(str,&result.value());
return result;
}
template <class T>
inline std::string to_str (T const&t)
{
std::string result;
to_str(t.value(),&result);
return result;
}
template <size_t N>
std::pair<const char*,size_t> to_trim_str(std::array<char,N> const&in)
{
for (auto i = (size_t) 0; i < N; i++)
if (in[i] == ' ')
return std::make_pair (&in[0],i);
return std::make_pair (&in[0],N);
}
}}
@@ -4,7 +4,7 @@
#include <functional>
#include <vector>
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "RiskLimitState.h"
namespace bts
@@ -1,6 +1,6 @@
#pragma once
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts
{
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "lib/domain_model/Security.pb.h"
#include <memory>
@@ -2,7 +2,7 @@
#include "lib/config/protos/Account.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "SymbolKey.h"
#include <memory>
+1 -1
View File
@@ -2,7 +2,7 @@
#include "lib/config/protos/Account.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include <memory>
#include <functional>
@@ -6,7 +6,7 @@
#include "../RiskLimitState.h"
#include "../IRiskLimitObserver.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "lib/config/config.h"
#include "lib/config/protos/Account.pb.h"
+1 -1
View File
@@ -5,7 +5,7 @@
#include "lib/utils/bts_assert.h"
#include "lib/domain_model/Security.h"
#include "lib/logging/logging.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "server/sparkd/persistence/SqliteStore.h"
#include <unordered_set>
@@ -1,41 +0,0 @@
#include "BootstrapActor.h"
#include "lib/config/protos/Executor.pb.h"
#include "lib/config/config.h"
#include "lib/utils/type_name.h"
#include "server/sparkd/setup/ServiceHostActor.h"
namespace bts {
namespace setup {
struct BootstrapActor::PImpl
{
PImpl(config::ExecutorCfg const&cfg) : cfg(cfg) {}
config::ExecutorCfg const cfg;
};
template <class A>
fabric::ExecutorID find_executor ()
{
auto cfg = config::get<config::ExecutorCfg>();
auto tn = utils::type_name<A>();
size_t exe_idx = 0;
for (auto const &e : cfg.executor()) {
for (auto const &exe_actor : e.actor())
if (exe_actor == tn)
return fabric::ExecutorID(exe_idx);
exe_idx++;
}
return fabric::ExecutorID(cfg.executor_size()-1);
}
BootstrapActor::BootstrapActor (config::ExecutorCfg const&cfg) : me (new PImpl(cfg)) {}
BootstrapActor::~BootstrapActor () {}
void BootstrapActor::init()
{
spawn<ServiceHostActor> (find_executor<ServiceHostActor>());
}
}}
-27
View File
@@ -1,27 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/fabric/Actor.h"
// fwd
namespace bts {
namespace config {
class ExecutorCfg;
}}
// end fws
namespace bts {
namespace setup {
class BootstrapActor : public fabric::Actor<>
{
public:
BootstrapActor(config::ExecutorCfg const&cfg);
~BootstrapActor ();
void init() override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
}}
@@ -1,51 +0,0 @@
#include "ServiceHostActor.h"
#include "lib/config/protos/Executor.pb.h"
#include "lib/quickbus/quickbus.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/config/config.h"
#include "lib/config/protos/quickbus_cfg.pb.h"
#include "lib/config/protos/Ports.pb.h"
#include "server/sparkd/persistence/PersistenceServiceServer.h"
#include "server/sparkd/user/UserServiceServer.h"
#include "server/sparkd/md/run_md.h"
#include "lib/md/relay/QueryServer.h"
namespace bts {
namespace setup {
config::QuickbusNodeCfg make_cfg()
{
config::QuickbusNodeCfg result;
result.set_my_port(config::get<config::Ports>().qbus_port());
return result;
}
struct ServiceHostActor::PImpl
{
PImpl() : quickbus(make_cfg(),qbus::Quickbus::ThreadOptions::outside_thread),
service_context(&quickbus),
settings_saver(service_context,"settings"),
login_server(service_context),
md_query_server()
{
run_market_datapublishers();
md_query_server = md::run_query_server(service_context);
}
qbus::Quickbus quickbus;
api::ServiceContext service_context;
// hosted services
persistence::PersistenceServiceServer settings_saver;
user::UserServiceServer login_server;
md::QueryServerPtr md_query_server;
};
ServiceHostActor::ServiceHostActor () : me (new PImpl()) {}
ServiceHostActor::~ServiceHostActor () {}
bool ServiceHostActor::on_poll() {return me->quickbus.do_work();
}
}}
@@ -1,21 +0,0 @@
#pragma once
#include <memory>
#include "server/sparkd/fabric/Actor.h"
#include "server/sparkd/fabric/IPollable.h"
namespace bts {
namespace setup {
class ServiceHostActor : public fabric::Actor<>, public fabric::IPollable
{
public:
ServiceHostActor();
~ServiceHostActor ();
bool on_poll() override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
};
}}
+21 -21
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
@@ -37,13 +37,13 @@
<ClCompile Include="jetfix\tests\jetfix_performance_tests.cpp" />
<ClCompile Include="jetfix\tests\store_tests.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="order_man\IFIXGateway.cpp" />
<ClCompile Include="order_man\IOrderObserver.cpp" />
<ClCompile Include="order_man\Order.cpp" />
<ClCompile Include="order_man\OrderMan.cpp" />
<ClCompile Include="order_man\OrderState.cpp" />
<ClCompile Include="order_man\OrderStore.cpp" />
<ClCompile Include="order_man\WorkingOrder.cpp" />
<ClCompile Include="orderman\IFIXGateway.cpp" />
<ClCompile Include="orderman\IOrderObserver.cpp" />
<ClCompile Include="orderman\Order.cpp" />
<ClCompile Include="orderman\OrderMan.cpp" />
<ClCompile Include="orderman\OrderState.cpp" />
<ClCompile Include="orderman\OrderStore.cpp" />
<ClCompile Include="orderman\WorkingOrder.cpp" />
<ClCompile Include="persistence\IStore.cpp" />
<ClCompile Include="persistence\PersistenceServiceServer.cpp" />
<ClCompile Include="persistence\SqliteStore.cpp" />
@@ -113,17 +113,17 @@
<ClInclude Include="jetfix\store\FIXFileStore.h" />
<ClInclude Include="jetfix\store\IFIXStore.h" />
<ClInclude Include="jetfix\store\session_name.h" />
<ClInclude Include="order_man\IFIXGateway.h" />
<ClInclude Include="order_man\IOrderObserver.h" />
<ClInclude Include="order_man\IOrderStore.h" />
<ClInclude Include="order_man\Order.h" />
<ClInclude Include="order_man\OrderCancel.h" />
<ClInclude Include="order_man\OrderCreate.h" />
<ClInclude Include="order_man\OrderMan.h" />
<ClInclude Include="order_man\OrderState.h" />
<ClInclude Include="order_man\OrderUpdate.h" />
<ClInclude Include="order_man\order_man_utils.h" />
<ClInclude Include="order_man\WorkingOrder.h" />
<ClInclude Include="orderman\IFIXGateway.h" />
<ClInclude Include="orderman\IOrderObserver.h" />
<ClInclude Include="orderman\IOrderStore.h" />
<ClInclude Include="orderman\Order.h" />
<ClInclude Include="orderman\OrderCancel.h" />
<ClInclude Include="orderman\OrderCreate.h" />
<ClInclude Include="orderman\OrderMan.h" />
<ClInclude Include="orderman\OrderState.h" />
<ClInclude Include="orderman\OrderUpdate.h" />
<ClInclude Include="orderman\utils/orderman_utils.h" />
<ClInclude Include="orderman\WorkingOrder.h" />
<ClInclude Include="persistence\IStore.h" />
<ClInclude Include="persistence\PersistenceServiceServer.h" />
<ClInclude Include="persistence\SqliteStore.h" />
@@ -146,7 +146,7 @@
</ItemGroup>
<ItemGroup>
<None Include="jetfix\tests\sample_fix.dat" />
<None Include="order_man\doc\order_man_notes.org" />
<None Include="orderman\doc\orderman_notes.org" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
@@ -212,4 +212,4 @@
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
</Project>
+42 -42
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="algo_man">
@@ -31,7 +31,7 @@
<Filter Include="jetfix\tests">
<UniqueIdentifier>{bfde0b4d-9221-4003-a71a-f895f2d426cb}</UniqueIdentifier>
</Filter>
<Filter Include="order_man">
<Filter Include="orderman">
<UniqueIdentifier>{07528a65-a74a-4e3a-b57c-8e109ba7648d}</UniqueIdentifier>
</Filter>
<Filter Include="persistence">
@@ -58,7 +58,7 @@
<Filter Include="user">
<UniqueIdentifier>{45a2299b-caa9-400f-9f35-9d3f60d7736d}</UniqueIdentifier>
</Filter>
<Filter Include="order_man\doc">
<Filter Include="orderman\doc">
<UniqueIdentifier>{7c15123a-161f-41d0-8c67-351ba12ed261}</UniqueIdentifier>
</Filter>
</ItemGroup>
@@ -138,26 +138,26 @@
<ClCompile Include="jetfix\tests\store_tests.cpp">
<Filter>jetfix\tests</Filter>
</ClCompile>
<ClCompile Include="order_man\IFIXGateway.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\IFIXGateway.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\IOrderObserver.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\IOrderObserver.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\Order.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\Order.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\OrderMan.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\OrderMan.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\OrderState.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\OrderState.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\OrderStore.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\OrderStore.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="order_man\WorkingOrder.cpp">
<Filter>order_man</Filter>
<ClCompile Include="orderman\WorkingOrder.cpp">
<Filter>orderman</Filter>
</ClCompile>
<ClCompile Include="persistence\IStore.cpp">
<Filter>persistence</Filter>
@@ -363,38 +363,38 @@
<ClInclude Include="jetfix\store\session_name.h">
<Filter>jetfix\store</Filter>
</ClInclude>
<ClInclude Include="order_man\IFIXGateway.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\IFIXGateway.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\IOrderObserver.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\IOrderObserver.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\IOrderStore.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\IOrderStore.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\Order.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\Order.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\OrderCancel.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\OrderCancel.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\OrderCreate.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\OrderCreate.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\OrderMan.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\OrderMan.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\OrderState.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\OrderState.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\OrderUpdate.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\OrderUpdate.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\order_man_utils.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\utils/orderman_utils.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="order_man\WorkingOrder.h">
<Filter>order_man</Filter>
<ClInclude Include="orderman\WorkingOrder.h">
<Filter>orderman</Filter>
</ClInclude>
<ClInclude Include="persistence\IStore.h">
<Filter>persistence</Filter>
@@ -458,11 +458,11 @@
<None Include="jetfix\tests\sample_fix.dat">
<Filter>jetfix\tests</Filter>
</None>
<None Include="order_man\doc\order_man_notes.org">
<Filter>order_man\doc</Filter>
<None Include="orderman\doc\orderman_notes.org">
<Filter>orderman\doc</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>
</Project>
@@ -2,7 +2,7 @@
#include <tuple>
#include "lib/domain_model/Security.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/utils/bts_chrono.h"
@@ -73,4 +73,4 @@ namespace bts
Quantity remaining_to_fill();
};
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
#include <tuple>
#include "lib/domain_model/Security.pb.h"
#include "server/sparkd/order_man/order_man_utils.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/utils/bts_chrono.h"