More tests, more fixes.
This commit is contained in:
1 parent
1cd1a23c80
commit
1fddbf1cca
7 files changed
+86
-33
No files matched your search
@@ -4,20 +4,23 @@ namespace bts::spartan::mdp3 {
|
||||
|
||||
struct InstrumentSnapshot::PImpl {
|
||||
PImpl(InstrumentDefinition const &def)
|
||||
: books(), implied_books(), orders(), fills(), high_limit_price(),
|
||||
low_limit_price(), max_price_variation(), cleared_volume(),
|
||||
electronic_volume(), fixing_price(), open_interest(), open_price(),
|
||||
high_bid(), low_offer(), settlement_price(), high_trade_price(),
|
||||
low_trade_price(), definition(def) {}
|
||||
: security_id(def.security_id()), books(), implied_books(), orders(),
|
||||
fills(), high_limit_price(), low_limit_price(), max_price_variation(),
|
||||
cleared_volume(), electronic_volume(), fixing_price(), open_interest(),
|
||||
open_price(), high_bid(), low_offer(), settlement_price(),
|
||||
high_trade_price(), low_trade_price(), definition(def),
|
||||
trading_status(definition.trading_status()) {}
|
||||
|
||||
PImpl(InstrumentDefinition &&def)
|
||||
: books(), implied_books(), orders(), fills(), high_limit_price(),
|
||||
low_limit_price(), max_price_variation(), cleared_volume(),
|
||||
electronic_volume(), fixing_price(), open_interest(), open_price(),
|
||||
high_bid(), low_offer(), settlement_price(), high_trade_price(),
|
||||
low_trade_price(), definition(std::move(def)) {}
|
||||
PImpl(InstrumentDefinition &&def)
|
||||
: security_id(def.security_id()), books(), implied_books(), orders(),
|
||||
fills(), high_limit_price(), low_limit_price(), max_price_variation(),
|
||||
cleared_volume(), electronic_volume(), fixing_price(), open_interest(),
|
||||
open_price(), high_bid(), low_offer(), settlement_price(),
|
||||
high_trade_price(), low_trade_price(), definition(std::move(def)),
|
||||
trading_status(definition.trading_status()) {}
|
||||
|
||||
// Books
|
||||
const MDP3SecurityID security_id;
|
||||
std::array<Book, 2> books;
|
||||
std::array<ImpliedBook, 2> implied_books;
|
||||
// Orders
|
||||
@@ -41,7 +44,6 @@ struct InstrumentSnapshot::PImpl {
|
||||
md::IntPrice low_trade_price;
|
||||
InstrumentDefinition definition;
|
||||
|
||||
const MDP3SecurityID security_id;
|
||||
::mdp3::SecurityTradingStatus::Value trading_status;
|
||||
};
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ class MDP3Packet {
|
||||
public:
|
||||
MDP3Packet() : _size(0) {}
|
||||
|
||||
MDP3Packet(MDP3Packet const&other) = default;
|
||||
MDP3Packet(MDP3Packet const &other) = default;
|
||||
|
||||
MDP3Packet(std::string_view const &bytes) {
|
||||
bts_assert(_backing_store.size() >= bytes.size());
|
||||
@@ -143,9 +143,9 @@ class MDP3Packet {
|
||||
_size = bytes.size();
|
||||
}
|
||||
|
||||
MDP3Packet(MDP3PacketView const&v) : MDP3Packet (v.data()) {}
|
||||
MDP3Packet(MDP3PacketView const &v) : MDP3Packet(v.data()) {}
|
||||
|
||||
MDP3Packet&operator=(MDP3Packet const&other) = default;
|
||||
MDP3Packet &operator=(MDP3Packet const &other) = default;
|
||||
|
||||
MDP3PacketView view() const {
|
||||
if (_size)
|
||||
@@ -183,10 +183,12 @@ using AllMDTypes = meta::list<
|
||||
|
||||
template <class Handler, class... Msg> class ParserImpl {
|
||||
public:
|
||||
static_assert (sizeof...(Msg) > 1);
|
||||
static_assert(
|
||||
sizeof...(Msg) > 1,
|
||||
"The handler used for this parser does not handle any message types");
|
||||
ParserImpl(Handler &h) : handler(h) {}
|
||||
|
||||
void on_packet(MDP3PacketView const &packet) {
|
||||
void on_packet(MDP3PacketView const &packet) {
|
||||
handler.on_begin_packet(packet);
|
||||
for (auto const &msg : packet)
|
||||
(void)(try_handle<Msg>(msg) || ...);
|
||||
@@ -207,9 +209,7 @@ template <class Handler, class... Msg> class ParserImpl {
|
||||
};
|
||||
|
||||
template <class Handler> class Parser {
|
||||
template <class Msg>
|
||||
using is_handled =
|
||||
std::is_invocable<Handler,Msg&>;
|
||||
template <class Msg> using is_handled = std::is_invocable<Handler, Msg &>;
|
||||
using HandledMsgs = typename meta::filter<is_handled, AllMDTypes>::type;
|
||||
using TypeListArgs = typename meta::cons<Handler, HandledMsgs>::type;
|
||||
using RealParser =
|
||||
@@ -287,9 +287,8 @@ class MDP3MsgWrapper {
|
||||
|
||||
template <class M> class MDP3MsgBox {
|
||||
public:
|
||||
MDP3MsgBox(M const &m)
|
||||
: _store(m.buffer(), m.sbePosition()) {}
|
||||
M unbox() const {return M((char*)_store.data(),_store.size());}
|
||||
MDP3MsgBox(M const &m) : _store(m.buffer(), m.sbePosition()) {}
|
||||
M unbox() const { return M((char *)_store.data(), _store.size()); }
|
||||
|
||||
private:
|
||||
std::string _store;
|
||||
|
||||
@@ -12,6 +12,7 @@ enum class IncrementalQueue::State {
|
||||
};
|
||||
|
||||
struct IncrementalQueue::PImpl {
|
||||
size_t last_sn = 0;
|
||||
bool processing_event = false;
|
||||
bool processing_packet = false;
|
||||
bool is_first_message_in_event = false;
|
||||
@@ -58,10 +59,13 @@ std::vector<MDP3Packet> IncrementalQueue::get_queued_packets_and_reset() {
|
||||
|
||||
void IncrementalQueue::on_begin_packet(MDP3PacketView const &v) {
|
||||
bts_assert(!me->processing_packet);
|
||||
auto previous_last = me->last_sn;
|
||||
me->last_sn = v.seq_num();
|
||||
me->processing_packet = true;
|
||||
me->is_first_message_in_event = true;
|
||||
me->last_message_was_event_end = false;
|
||||
me->queue.emplace_back(v);
|
||||
bts_assert(previous_last == 0 || previous_last+1 == v.seq_num());
|
||||
}
|
||||
|
||||
void IncrementalQueue::on_end_packet() {
|
||||
|
||||
@@ -38,6 +38,9 @@ class IncrementalQueue {
|
||||
void reset();
|
||||
std::vector<MDP3Packet> get_queued_packets_and_reset();
|
||||
|
||||
|
||||
// This class expects for no loss to ever be observed. Packet loss
|
||||
// and recovery should be managed by the caller.
|
||||
void on_begin_packet(MDP3PacketView const &);
|
||||
void on_end_packet();
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ RecoveryHandler::MaybeSnapshot RecoveryHandler::try_build_snapshot() {
|
||||
for (auto &d : definition_snaps) {
|
||||
auto instrument_snapshot =
|
||||
std::make_unique<InstrumentSnapshot>(std::move(d));
|
||||
auto this_book_snap = book_snaps_idx[d.security_id()];
|
||||
auto this_book_snap = book_snaps_idx[instrument_snapshot->security_id()];
|
||||
uint32_t last_processed_sn = 0;
|
||||
if (this_book_snap) {
|
||||
last_processed_sn = this_book_snap->book.unbox().lastMsgSeqNumProcessed();
|
||||
@@ -295,7 +295,7 @@ void RecoveryHandler::update_book_in_snapshot(
|
||||
r.noMDEntries().forEach([&snap](auto &md_entry) {
|
||||
switch (md_entry.mDEntryType()) {
|
||||
case ::mdp3::MDEntryType::Bid:
|
||||
case ::mdp3::MDEntryType::Offer: {
|
||||
case ::mdp3::MDEntryType::Offer:
|
||||
snap.update_book_level(
|
||||
convert_side(md_entry.mDEntryType()), md_entry.mDPriceLevel(),
|
||||
BookLevel{md::IntPrice{md_entry.mDEntryPx().mantissa(),
|
||||
@@ -303,9 +303,8 @@ void RecoveryHandler::update_book_in_snapshot(
|
||||
MDP3Quantity{md_entry.mDEntrySize()},
|
||||
md_entry.numberOfOrders()});
|
||||
break;
|
||||
}
|
||||
case ::mdp3::MDEntryType::ImpliedBid:
|
||||
case ::mdp3::MDEntryType::ImpliedOffer: {
|
||||
case ::mdp3::MDEntryType::ImpliedOffer:
|
||||
snap.update_implied_book_level(
|
||||
convert_side(md_entry.mDEntryType()), md_entry.mDPriceLevel(),
|
||||
BookLevel{md::IntPrice{md_entry.mDEntryPx().mantissa(),
|
||||
@@ -313,16 +312,12 @@ void RecoveryHandler::update_book_in_snapshot(
|
||||
MDP3Quantity{md_entry.mDEntrySize()},
|
||||
md_entry.numberOfOrders()});
|
||||
break;
|
||||
}
|
||||
|
||||
case ::mdp3::MDEntryType::Trade:
|
||||
break;
|
||||
|
||||
case ::mdp3::MDEntryType::OpenPrice:
|
||||
snap.set_open_price(md::IntPrice{md_entry.mDEntryPx().mantissa(),
|
||||
md_entry.mDEntryPx().exponent()});
|
||||
break;
|
||||
|
||||
case ::mdp3::MDEntryType::SettlementPrice:
|
||||
snap.set_settlement_price(md::IntPrice{
|
||||
md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()});
|
||||
|
||||
@@ -77,6 +77,10 @@ class RecoveryHandler {
|
||||
};
|
||||
SnapshotFeedHandler &on_snapshot_feed;
|
||||
|
||||
|
||||
// No loss on the incremental feed should be observed by this
|
||||
// class. Packet loss for the incremental feed should be managed by
|
||||
// the caller.
|
||||
class IncrementalFeedHandler {
|
||||
public:
|
||||
void on_begin_packet(MDP3PacketView const &);
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/BookSnapshot.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/DefinitionSnapshot.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/IncrementalQueue.h"
|
||||
#include "server/sparkd/cmds/spartan/md/mdp3/recovery/RecoveryHandler.h"
|
||||
|
||||
|
||||
#include "server/sparkd/cmds/spartan/util/PCapReader.h"
|
||||
|
||||
@@ -72,7 +74,7 @@ TEST_CASE("spartan_smoke", "[spartan_smoke]") {
|
||||
|
||||
ChannelHandler handler(obs, ctrl);
|
||||
|
||||
SECTION("can_generate_snapshot") {
|
||||
SECTION("can_bootstrap") {
|
||||
reader.on_packet() = [&](auto const &info, auto const &payload) {
|
||||
if ((info.dst_ip == IA_ip && info.dst_port == IA_port)) {
|
||||
buffers[0].i = MDP3Packet{payload};
|
||||
@@ -166,4 +168,48 @@ TEST_CASE("spartan_inc_queue", "[spartan_inc_queue]") {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::spartan::mdp3::tests
|
||||
TEST_CASE("spartan_recovery", "[spartan_recovery]") {
|
||||
|
||||
auto reader = PCapReader{"/home/ppinto/es_md/es_all.pcap"};
|
||||
std::array<PacketBuffers, 2> buffers;
|
||||
RecoveryHandler recovery;
|
||||
AB_Arbitrageur ab;
|
||||
SECTION("can_build_snapshot") {
|
||||
|
||||
reader.on_packet() = [&](auto const &info, auto const &payload) {
|
||||
|
||||
if ((info.dst_ip == IA_ip && info.dst_port == IA_port)) {
|
||||
buffers[0].i = MDP3Packet{payload};
|
||||
} else if ((info.dst_ip == IB_ip && info.dst_port == IB_port)) {
|
||||
buffers[1].i = MDP3Packet{payload};
|
||||
} else if ((info.dst_ip == NA_ip && info.dst_port == NA_port)) {
|
||||
parse_packet(MDP3PacketView{payload},recovery.on_instrument_feed);
|
||||
return true;
|
||||
} else if ((info.dst_ip == SAMBO_ip && info.dst_port == SAMBO_port)) {
|
||||
parse_packet(MDP3PacketView{payload},recovery.on_snapshot_feed);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
|
||||
auto action = ab(buffers[0].i.view(), buffers[1].i.view());
|
||||
switch (action.action) {
|
||||
case AB_Arbitrageur::APPLY:
|
||||
parse_packet(action.packet,recovery.on_incremental_feed);
|
||||
break;
|
||||
case AB_Arbitrageur::IGNORE:
|
||||
break;
|
||||
case AB_Arbitrageur::RECOVER:
|
||||
REQUIRE(false);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
while (reader.read_next())
|
||||
;
|
||||
REQUIRE(recovery.try_build_snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user