From f4513c4718f661ff2339811a436ecc1da3d93ac0 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 14 Sep 2017 17:09:57 -0400 Subject: [PATCH] SPARK-184 make timeout value from get_bbo() more obvious. This should fix MAIN-75 for the spark branch --- src/client/sparkcmd/Program.cs | 2 +- src/lib/config/ConfigCheck.cpp | 1 + src/lib/md/Diag/MDPriceLantencyTester.cpp | 2 +- src/lib/md/MDConsumerHandle.h | 22 +++++++++++----- src/lib/md/relay/QueryServer.cpp | 12 ++++++++- src/lib/md/test/md2_tests.cpp | 32 +++++++++++------------ 6 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index bfd5dc452..3d6d25e8f 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -8,7 +8,7 @@ namespace sparkcmd static void Main(string[] args) { if (args.FirstOrDefault() == "configcheck") { - mbts.config.Configuration.configcheck(args.Length, args); + mbts.config.Configuration.configcheck(args.Length-1, args.Skip(1).ToArray()); return; } diff --git a/src/lib/config/ConfigCheck.cpp b/src/lib/config/ConfigCheck.cpp index 27b7f32d1..66fc9633e 100644 --- a/src/lib/config/ConfigCheck.cpp +++ b/src/lib/config/ConfigCheck.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace proto = google::protobuf; namespace bts { diff --git a/src/lib/md/Diag/MDPriceLantencyTester.cpp b/src/lib/md/Diag/MDPriceLantencyTester.cpp index c3781019c..e9672fef7 100644 --- a/src/lib/md/Diag/MDPriceLantencyTester.cpp +++ b/src/lib/md/Diag/MDPriceLantencyTester.cpp @@ -39,7 +39,7 @@ void run_md_price_latency_test(UserSymbol symbol) { while (true) { for(auto& entry : handles) { PriceSize bid, ask; - if (entry.handle.get_bbo(entry.time, bid, ask)) { + if (entry.handle.get_bbo(entry.time, bid, ask) == HandleReadResult::SUCCESS) { auto bid_price = bid.price(); auto ask_price = ask.price(); if (bid_price != entry.bid || ask_price != entry.ask) { diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index b808c3f17..5c60bc4ac 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -10,6 +10,12 @@ namespace md2 { BTS_EXPORT TimeStamp copy_bbo(const TimeStamp& , const SingleWriterPriceSizeStore& , const SingleWriterPriceSizeStore& , PriceSize& , PriceSize& ); + +enum class HandleReadResult { + NO_CHANGE, + BUSY, + SUCCESS +}; // Can be an invalid handle if you request a security which has not been discovered yet. class BTS_EXPORT MDConsumerHandle @@ -32,31 +38,33 @@ public: TimeStamp last_market_update() const; + - bool get_bbo(TimeStamp& time, PriceSize& bid, PriceSize& ask) const + HandleReadResult get_bbo(TimeStamp& time, PriceSize& bid, PriceSize& ask) const { - if (!is_initialized()) return false; + if (!is_initialized()) return HandleReadResult::NO_CHANGE; if (_cheader->has_depth) { auto* pdepth = hotd(); if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { TimeStamp temp = copy_bbo(pdepth->timestamp, pdepth->bid(0), pdepth->ask(0), bid, ask); - if (temp == TimeStamp::invalid()) { return false; } + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; - return true; + return HandleReadResult::SUCCESS; } } else { auto* pdepth = hot(); if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { TimeStamp temp = copy_bbo(pdepth->timestamp, pdepth->bid, pdepth->ask, bid, ask); - if (temp == TimeStamp::invalid()) { return false; } + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; - return true; + return HandleReadResult::SUCCESS; } } - return false; + return HandleReadResult::NO_CHANGE; } + // Last bbo or depth update PriceSize get_bid(size_t depth) const; diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index b9ed71e54..930b039e9 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -236,7 +236,8 @@ struct QueryServerImpl : public IQueryServer { if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DEPTH)) { PriceSize bid, ask; TimeStamp time(snapshot ? 0 : chan.time()); - if (handle.get_bbo(time, bid, ask)) { + auto result = handle.get_bbo(time, bid, ask); + if (result == HandleReadResult::SUCCESS) { auto* p = pupd->mutable_book(); convert(bid, p->add_bids()); convert(ask, p->add_asks()); @@ -253,6 +254,15 @@ struct QueryServerImpl : public IQueryServer { } } } + else if (result == HandleReadResult::BUSY) { + // 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::LAST_TRADE)) { TimeStamp time(snapshot ? 0 : chan.time()); diff --git a/src/lib/md/test/md2_tests.cpp b/src/lib/md/test/md2_tests.cpp index 10d253626..eefbd4bb0 100644 --- a/src/lib/md/test/md2_tests.cpp +++ b/src/lib/md/test/md2_tests.cpp @@ -309,7 +309,7 @@ public: PriceSize bid, ask; TimeStamp read_time; // get_boo returns true becuase this time is after the change - REQUIRE(c.get_bbo(read_time, bid, ask)); + REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); REQUIRE((read_time - TimeStamp()).seconds() > 0); REQUIRE(bid.price() == 5.0); REQUIRE(ask.price() == 5.0); @@ -321,7 +321,7 @@ public: auto read_time = TimeStamp::now(); for (const auto& c : consumers) { PriceSize bid, ask; - REQUIRE(!c.get_bbo(read_time, bid, ask)); + REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); } // update everything once more @@ -335,7 +335,7 @@ public: TimeStamp modified_time = read_time; PriceSize bid, ask; // get_bbo returns true becuase this time is after the change - REQUIRE(c.get_bbo(modified_time, bid, ask)); + REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); REQUIRE( (modified_time - read_time).seconds() > 0); REQUIRE(bid.price() == 6.55); REQUIRE(ask.price() == 6.55); @@ -414,11 +414,11 @@ public: PriceSize bid, ask; for(auto& c : consumers) { TimeStamp zerotime; - REQUIRE(!c.get_bbo(zerotime, bid, ask)); + REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); } for(auto& mc : managed_consumers) { TimeStamp zerotime; - REQUIRE(!mc->get_bbo(zerotime, bid, ask)); + REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); } assign_ids(producers.begin(), producers.end()); @@ -427,7 +427,7 @@ public: // Existing old handles remain invalid for(auto& c : consumers) { TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask)) { + if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -435,7 +435,7 @@ public: // Now, magically, the existing (managed) handles are valid for(auto& mc : managed_consumers) { TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask)) { + if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -466,11 +466,11 @@ public: PriceSize bid, ask; for(auto& c : consumers) { TimeStamp zerotime; - REQUIRE(!c.get_bbo(zerotime, bid, ask)); + REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); } for(auto& mc : managed_consumers) { TimeStamp zerotime; - REQUIRE(!mc->get_bbo(zerotime, bid, ask)); + REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); } // Only NOW create the producers (and the shared memory) @@ -488,7 +488,7 @@ public: // Existing old handles remain invalid for(auto& c : consumers) { TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask)) { + if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -496,7 +496,7 @@ public: // Now, magically, the existing (managed) handles are valid for(auto& mc : managed_consumers) { TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask)) { + if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { bbo_count++; } } @@ -653,7 +653,7 @@ public: REQUIRE(handle->is_heart_beating()); TimeStamp tzero; PriceSize bid, ask; - REQUIRE(handle->get_bbo(tzero, bid, ask)); + REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); REQUIRE(bid.price() == 4.0); REQUIRE(bid.size() == 5); REQUIRE(ask.price() == 6.0); @@ -675,9 +675,9 @@ public: src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); PriceSize bid, ask; TimeStamp start = TimeStamp::now(); - bool result = src->consumer().get_bbo(read, bid, ask); + auto result = src->consumer().get_bbo(read, bid, ask); TimeStamp end = TimeStamp::now(); - REQUIRE(result == false); + REQUIRE(result != HandleReadResult::SUCCESS); REQUIRE((end - start).seconds() < 5); } @@ -1288,7 +1288,7 @@ struct Co2PerfTests { for (auto& c : consumers) { PriceSize bid, ask; TimeStamp ts; - REQUIRE(c.get_bbo(ts, bid, ask)); + REQUIRE(c.get_bbo(ts, bid, ask) == HandleReadResult::SUCCESS); } // Start a thread which loops and writes to the producers until terminated @@ -1314,7 +1314,7 @@ struct Co2PerfTests { for (size_t x = 0; x < consumers.size(); ++x) { PriceSize bid, ask; TimeStamp ts_temp = ts; - if (consumers[x].get_bbo(ts_temp, bid, ask)) { + if (consumers[x].get_bbo(ts_temp, bid, ask) == HandleReadResult::SUCCESS) { ++read_count; } }