Use SecurityOracle in OrderStore
This commit is contained in:
1 parent
4264928cc2
commit
e08cd7bdbf
3 files changed
+48
-75
No files matched your search
@@ -5,7 +5,6 @@
|
||||
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
||||
#include "lib/config/protos/toggles.pb.h"
|
||||
#include "lib/orderman/actors/SODPosition.h"
|
||||
#include "lib/orderman/events/NewSecuritiesEvent.h"
|
||||
#include "lib/orderman/events/OrderStateUpdateEvent.h"
|
||||
#include "lib/orderman/events/StoredOrderEvent.h"
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
@@ -18,99 +17,72 @@ namespace orderman {
|
||||
|
||||
static auto const orders_ns = "orders";
|
||||
|
||||
enum class OrderStore::State { WAIT_SEC, READY };
|
||||
|
||||
struct OrderStore::PImpl {
|
||||
PImpl()
|
||||
: state(State::WAIT_SEC), known_orders(),
|
||||
PImpl(md::SecurityOracle o)
|
||||
: known_orders(),
|
||||
store(new persistence::SqliteStore("orders")), order_stream(),
|
||||
soft_cleanup_enabled(
|
||||
config::get<config::toggles::OrdersSoftCleanup>().enabled()) {}
|
||||
config::get<config::toggles::OrdersSoftCleanup>().enabled()),
|
||||
oracle(o) {}
|
||||
|
||||
State state;
|
||||
const GatewayName gw_name;
|
||||
std::unordered_map<OrderKey, api::orderman::OrderState> known_orders;
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
mercury::Stream<StoredOrderEvent> order_stream;
|
||||
mercury::Actor::SubHandle stored_sec_sub;
|
||||
bool soft_cleanup_enabled;
|
||||
md::SecurityOracle oracle;
|
||||
timeval cutoff_time;
|
||||
};
|
||||
|
||||
OrderStore::OrderStore() : me(new PImpl()) {}
|
||||
OrderStore::OrderStore(md::SecurityOracle o) : me(new PImpl(o)) {}
|
||||
OrderStore::~OrderStore() {}
|
||||
|
||||
void OrderStore::init() {
|
||||
me->state = State::WAIT_SEC;
|
||||
|
||||
me->stored_sec_sub = subscribe<NewSecuritiesEvent>([this](
|
||||
auto const &update) {
|
||||
if (update.events) {
|
||||
if (me->state == State::WAIT_SEC) {
|
||||
LOG(OrderMan, Info,
|
||||
"Order store received initial list of {} securities",
|
||||
update.events->size());
|
||||
// subscribe to order updates so we can store them
|
||||
subscribe<OrderStateUpdateEvent>(
|
||||
[this](auto const &ev) { this->on_order_state_update(ev); });
|
||||
|
||||
// don't need securities anymore, we just need the initial
|
||||
// batch so we can filter out orders on expired securities
|
||||
unsubscribe(me->stored_sec_sub);
|
||||
// now load orders from disk, for each order check if
|
||||
// it reffers to a valid security. if so publish it.
|
||||
api::orderman::OrderRecord rec;
|
||||
std::vector<StoredOrderEvent> stored_batch;
|
||||
|
||||
// subscribe to order updates so we can store them
|
||||
subscribe<OrderStateUpdateEvent>(
|
||||
[this](auto const &ev) { this->on_order_state_update(ev); });
|
||||
LOG(persist, Info, "Loading all orders");
|
||||
auto all = me->store->retrieve_all()[orders_ns];
|
||||
LOG(persist, Info, "Finished loading all {} orders", all.size());
|
||||
|
||||
// index securities
|
||||
std::map<SecurityKey, const api::Security *> sec_idx;
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto sec_ptr = ev.security;
|
||||
sec_idx[SecurityKey(sec_ptr->id())] = sec_ptr;
|
||||
me->cutoff_time = SODPosition::get_order_cutoff_time(all);
|
||||
|
||||
for (auto const &kvp : all)
|
||||
if (utils::pb::from_string<api::orderman::OrderRecord>(
|
||||
kvp.second, &rec)) {
|
||||
auto ok = OrderKey(std::stoi(kvp.first));
|
||||
auto sk = SecurityKey(rec.def().security_key());
|
||||
auto sec = me->oracle.get_security(sk);
|
||||
|
||||
if (config::get<config::toggles::FilterOrdersWithDanglingSecurities>()
|
||||
.enabled() && sec == nullptr) {
|
||||
LOG(OrderMan, Diag,
|
||||
"Order {} refers to unresolved security {}",
|
||||
ok.value(), sk.value());
|
||||
} else {
|
||||
if (!_order_cutoff(rec))
|
||||
stored_batch.emplace_back(rec);
|
||||
me->known_orders[ok] = rec.state();
|
||||
}
|
||||
} else
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover order {} from storage. "
|
||||
"This should be investigated",
|
||||
kvp.first);
|
||||
|
||||
// now load orders from disk, for each order check if
|
||||
// it reffers to a valid security. if so publish it.
|
||||
api::orderman::OrderRecord rec;
|
||||
std::vector<StoredOrderEvent> stored_batch;
|
||||
|
||||
LOG(persist, Info, "Loading all orders");
|
||||
auto all = me->store->retrieve_all()[orders_ns];
|
||||
LOG(persist, Info, "Finished loading all {} orders", all.size());
|
||||
|
||||
me->cutoff_time = SODPosition::get_order_cutoff_time(all);
|
||||
|
||||
for (auto const &kvp : all)
|
||||
if (utils::pb::from_string<api::orderman::OrderRecord>(
|
||||
kvp.second, &rec)) {
|
||||
auto ok = OrderKey(std::stoi(kvp.first));
|
||||
auto sk = SecurityKey(rec.def().security_key());
|
||||
|
||||
if (config::get<
|
||||
config::toggles::FilterOrdersWithDanglingSecurities>()
|
||||
.enabled() &&
|
||||
sec_idx.find(sk) == sec_idx.end()) {
|
||||
LOG(OrderMan, Diag,
|
||||
"Order {} refers to unresolved security {}",
|
||||
ok.value(), sk.value());
|
||||
} else {
|
||||
if (!_order_cutoff(rec))
|
||||
stored_batch.emplace_back(rec);
|
||||
me->known_orders[ok] = rec.state();
|
||||
}
|
||||
} else
|
||||
LOG(OrderMan, Error,
|
||||
"Could not recover order {} from storage. "
|
||||
"This should be investigated",
|
||||
kvp.first);
|
||||
|
||||
me->order_stream = make_stream<StoredOrderEvent>();
|
||||
LOG(OrderMan, Info,
|
||||
"Order Store published {} orders recoved from disk out of {} "
|
||||
"total orders",
|
||||
stored_batch.size(), all.size());
|
||||
publish_batch(me->order_stream, stored_batch);
|
||||
me->state = State::READY;
|
||||
}
|
||||
}
|
||||
});
|
||||
me->order_stream = make_stream<StoredOrderEvent>();
|
||||
LOG(OrderMan, Info,
|
||||
"Order Store published {} orders recoved from disk out of {} "
|
||||
"total orders",
|
||||
stored_batch.size(), all.size());
|
||||
publish_batch(me->order_stream, stored_batch);
|
||||
}
|
||||
|
||||
void OrderStore::on_order_state_update(
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include "lib/mercury/actors/Actor.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
|
||||
namespace bts {
|
||||
@@ -15,7 +16,7 @@ class OrderStateUpdateEvent;
|
||||
class OrderStore : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
OrderStore();
|
||||
OrderStore(md::SecurityOracle);
|
||||
~OrderStore();
|
||||
void init() override;
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ OrderServiceServer::OrderServiceServer(
|
||||
bts_assert_msg(thread,
|
||||
"Could not find a configured thread for OrderStore. See "
|
||||
"the OrdermanCfg key");
|
||||
thread->executor.spawn<OrderStore>();
|
||||
thread->executor.spawn<OrderStore>(mdc.oracle());
|
||||
|
||||
// FillStore
|
||||
thread = find_thread_for_actor("FillStore", me->actor_threads);
|
||||
|
||||
Reference in new issue
Block a user