From 034aaa5b98e82f2c87eb315dad0b9e53a934bf36 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 18 Dec 2018 14:26:19 -0500 Subject: [PATCH] SPARK-1934 - Settlement will fail to send to client when first request is not entitled --- src/lib/md/Diag/ShmDumper.cpp | 1 + src/lib/md/protos/co2.proto | 5 +- src/lib/md/relay/QueryServer.cpp | 137 ++++++++++++++++--------------- src/lib/md/relay/RelayClient.cpp | 1 + src/lib/md/test/md2_tests.cpp | 1 + 5 files changed, 76 insertions(+), 69 deletions(-) diff --git a/src/lib/md/Diag/ShmDumper.cpp b/src/lib/md/Diag/ShmDumper.cpp index 66eccfe24..a0b4272d0 100644 --- a/src/lib/md/Diag/ShmDumper.cpp +++ b/src/lib/md/Diag/ShmDumper.cpp @@ -153,6 +153,7 @@ string dump_entry(const ColdEntry& entry) { if (has_value(entry.last_trade)) res << "'last_trade': " << dump(entry.last_trade) << ", "; if (entry.total_volume > 0) res << "'total_volume': " << entry.total_volume << ", "; if (entry.last_trade_time > 0) res << "'last_trade_time_ll': " << entry.last_trade_time << ", "; + // Reminder: settlement price is in hot if (entry.misc.settlement_date.year > 0) res << "'settlement_date': " << dump(entry.misc.settlement_date) << ", "; if (!(entry.misc.previous_settlement_price.is_nan())) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price.price() << ", "; if (entry.misc.previous_settlement_date.year > 0) res << "'previous_settlement_date': " << dump(entry.misc.previous_settlement_date) << ", "; diff --git a/src/lib/md/protos/co2.proto b/src/lib/md/protos/co2.proto index a93fc1505..b05794f9c 100644 --- a/src/lib/md/protos/co2.proto +++ b/src/lib/md/protos/co2.proto @@ -46,6 +46,7 @@ message SecurityChannel { int64 trade_cursor = 6; int64 rfq_cursor = 7; uint64 segment_id = 8; + bool entitled = 9; } message MDRequest { @@ -62,8 +63,8 @@ message MDRequest { IMPLIED = 0x40; } string host = 2; -// bytes token = 3; - reserved 3; + // bytes token = 3; + reserved 3; } message ChannelUpdate { diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 53d7886d5..dea02db98 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -230,6 +230,7 @@ public: reply->mutable_channel()->CopyFrom(chan); reply->set_platform(group.platform); reply->mutable_channel()->set_time(TimeStamp::now().to_uint64()); + reply->mutable_channel()->set_entitled(entitled); auto flags = (MDRequest::Flags)chan.flags(); // Get a copy of handles. If recovery happens during this call @@ -252,78 +253,81 @@ public: snapshot = true; } if (entitled) { - if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { - std::array bids, asks; + // If we are entitled now but this is the first request where we are ensure we send all values + if(!chan.entitled()) snapshot = true; + + if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { + std::array bids, asks; + TimeStamp time(snapshot ? 0 : chan.time()); + size_t copy_depth = HASFLAG(flags, MDRequest::DIRECT) ? COCONUT_MAX_DEPTH : 1; + auto result = handle.get_direct_book(time, copy_depth, bids.data(), asks.data()); + if (result == HandleReadResult::SUCCESS) { + auto* p = pupd->mutable_direct(); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + PriceSize b = bids[x]; + if (b != NO_MARKET) { + convert(b, p->add_bids()); + } + PriceSize a = asks[x]; + if (a != NO_MARKET) { + convert(a, p->add_asks()); + } + } + } + else if (result == HandleReadResult::BUSY) { + LOG(md,Info, "Busy read on handle {}. Retry on next request", instrument.handle.key()); + // If we get a busy reading BBO (which happens 1-2x per day for a single relay client) + // We move the timestamp back so the next request will try to read this value again. + // ALTERNATIVE: We could spin until the value was not busy but it could be that the writer + // was context switched out and we will have to wait multiple ms to unblock the request. + // This fix assumes it is better to send the update now with 1 stale value than to block the call + // and allow all the values to be stale until this one can be read. + reply->mutable_channel()->set_time(chan.time()); + } + } + if ((HASFLAG(flags, MDRequest::IMPLIED) || HASFLAG(flags, MDRequest::BBO)) && !handle.is_synthetic()) { + std::array bids, asks; TimeStamp time(snapshot ? 0 : chan.time()); - size_t copy_depth = HASFLAG(flags, MDRequest::DIRECT) ? COCONUT_MAX_DEPTH : 1; - auto result = handle.get_direct_book(time, copy_depth, bids.data(), asks.data()); - if (result == HandleReadResult::SUCCESS) { - auto* p = pupd->mutable_direct(); - for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - PriceSize b = bids[x]; - if (b != NO_MARKET) { - convert(b, p->add_bids()); - } - PriceSize a = asks[x]; - if (a != NO_MARKET) { - convert(a, p->add_asks()); + size_t copy_depth = HASFLAG(flags, MDRequest::IMPLIED) ? COCONUT_MAX_IMPLIED_DEPTH : 1; + auto result = handle.get_implied_book(time, copy_depth, bids.data(), asks.data()); + if (result == HandleReadResult::SUCCESS) { + auto* p = pupd->mutable_implied(); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + PriceSize b = bids[x]; + if (b != NO_MARKET) { + convert(b, p->add_bids()); + } + PriceSize a = asks[x]; + if (a != NO_MARKET) { + convert(a, p->add_asks()); + } } } - } - else if (result == HandleReadResult::BUSY) { - LOG(md,Info, "Busy read on handle {}. Retry on next request", instrument.handle.key()); - // If we get a busy reading BBO (which happens 1-2x per day for a single relay client) - // We move the timestamp back so the next request will try to read this value again. - // ALTERNATIVE: We could spin until the value was not busy but it could be that the writer - // was context switched out and we will have to wait multiple ms to unblock the request. - // This fix assumes it is better to send the update now with 1 stale value than to block the call - // and allow all the values to be stale until this one can be read. - reply->mutable_channel()->set_time(chan.time()); - } - } - if ((HASFLAG(flags, MDRequest::IMPLIED) || HASFLAG(flags, MDRequest::BBO)) && !handle.is_synthetic()) { - std::array bids, asks; - TimeStamp time(snapshot ? 0 : chan.time()); - size_t copy_depth = HASFLAG(flags, MDRequest::IMPLIED) ? COCONUT_MAX_IMPLIED_DEPTH : 1; - auto result = handle.get_implied_book(time, copy_depth, bids.data(), asks.data()); - if (result == HandleReadResult::SUCCESS) { - auto* p = pupd->mutable_implied(); - for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { - PriceSize b = bids[x]; - if (b != NO_MARKET) { - convert(b, p->add_bids()); - } - PriceSize a = asks[x]; - if (a != NO_MARKET) { - convert(a, p->add_asks()); - } + else if (result == HandleReadResult::BUSY) { + LOG(md,Info, "Busy read on handle (implied) {}. Retry on next request", instrument.handle.key()); + reply->mutable_channel()->set_time(chan.time()); } } - else if (result == HandleReadResult::BUSY) { - LOG(md,Info, "Busy read on handle (implied) {}. Retry on next request", instrument.handle.key()); - reply->mutable_channel()->set_time(chan.time()); + if (HASFLAG(flags, MDRequest::LAST_TRADE)) { + TimeStamp time(snapshot ? 0 : chan.time()); + PriceSize price; + datetime::LLDateTime trade_time; + size_t volume; + if (handle.get_last_trade(time, price, trade_time, &volume)) { + auto* trade = pupd->mutable_last_trade(); + trade->set_key(handle.key()); + trade->set_time(trade_time); + trade->set_total_volume(volume); + set_price(price, trade); + } } - } - if (HASFLAG(flags, MDRequest::LAST_TRADE)) { - TimeStamp time(snapshot ? 0 : chan.time()); - PriceSize price; - datetime::LLDateTime trade_time; - size_t volume; - if (handle.get_last_trade(time, price, trade_time, &volume)) { - auto* trade = pupd->mutable_last_trade(); - trade->set_key(handle.key()); - trade->set_time(trade_time); - trade->set_total_volume(volume); - set_price(price, trade); + if (HASFLAG(flags, MDRequest::MISC)) { + TimeStamp time(snapshot ? 0 : chan.time()); + if (handle.misc_timestamp() > time) { + copy_misc(handle.settlement_price(), handle.misc(), pupd->mutable_misc()); + } } } - if (HASFLAG(flags, MDRequest::MISC)) { - TimeStamp time(snapshot ? 0 : chan.time()); - if (handle.misc_timestamp() > time) { - copy_misc(handle.settlement_price(), handle.misc(), pupd->mutable_misc()); - } - } - } if (pupd->has_direct() || pupd->has_implied() || pupd->has_last_trade() || @@ -359,9 +363,8 @@ public: if (groupiter != _groups.end()) { auto& group = groupiter->second; bool is_entitled = bypass_entitlement || - /*ch.exchange() == api::VENUE_SIM || */ - ch.exchange() == api::VENUE_BTS || - (entitlements && entitlements->count(ch.symbol())); + ch.exchange() == api::VENUE_BTS || + (entitlements && entitlements->count(ch.symbol())); fill(ch, *group, reply->add_channels(), is_entitled); } } diff --git a/src/lib/md/relay/RelayClient.cpp b/src/lib/md/relay/RelayClient.cpp index ca9bf5030..ae073dda8 100644 --- a/src/lib/md/relay/RelayClient.cpp +++ b/src/lib/md/relay/RelayClient.cpp @@ -269,6 +269,7 @@ public: req.exchange() == reply.channel().exchange() && req.symbol() == reply.channel().symbol()) { + req.set_entitled(reply.channel().entitled()); req.set_time(reply.channel().time()); req.set_trade_cursor(reply.channel().trade_cursor()); req.set_rfq_cursor(reply.channel().rfq_cursor()); diff --git a/src/lib/md/test/md2_tests.cpp b/src/lib/md/test/md2_tests.cpp index c78efa58b..0ec5f24a2 100644 --- a/src/lib/md/test/md2_tests.cpp +++ b/src/lib/md/test/md2_tests.cpp @@ -1298,6 +1298,7 @@ struct Co2RelayTests { chan->set_symbol(symbol); chan->set_exchange(VENUE_BTS); chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); + chan->set_entitled(true); } // This request should include everything