diff --git a/.gitignore b/.gitignore index d864b520d..652408787 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ obj/ msbuild.log *.orig vcxproj.user +launchSettings.json diff --git a/src/client/spark/Spark.csproj b/src/client/spark/Spark.csproj index 09dcb3022..b9f5cc22a 100644 --- a/src/client/spark/Spark.csproj +++ b/src/client/spark/Spark.csproj @@ -16,6 +16,8 @@ bts 7.1 + true + diff --git a/src/client/spark_api/config/config.cs b/src/client/spark_api/config/config.cs index 2c2c10346..6d2dd10e8 100644 --- a/src/client/spark_api/config/config.cs +++ b/src/client/spark_api/config/config.cs @@ -69,6 +69,10 @@ namespace mbts.config private static extern void reset(); + [DllImport("spark_lib")] + internal static extern void configcheck(int count, string[] args); + + private static object config_lock = new object(); enum State { diff --git a/src/client/spark_api/spark_api.csproj b/src/client/spark_api/spark_api.csproj index 39ae944db..605deabb3 100644 --- a/src/client/spark_api/spark_api.csproj +++ b/src/client/spark_api/spark_api.csproj @@ -9,6 +9,8 @@ true x64 AnyCPU + true + diff --git a/src/client/spark_core/spark_core.csproj b/src/client/spark_core/spark_core.csproj index 1c2d33c83..d4799d5e6 100644 --- a/src/client/spark_core/spark_core.csproj +++ b/src/client/spark_core/spark_core.csproj @@ -5,6 +5,8 @@ true x64 AnyCPU + true + diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index c6a9b5056..3d6d25e8f 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace sparkcmd { @@ -6,6 +7,11 @@ namespace sparkcmd { static void Main(string[] args) { + if (args.FirstOrDefault() == "configcheck") { + mbts.config.Configuration.configcheck(args.Length-1, args.Skip(1).ToArray()); + return; + } + mbts.config.Configuration.initialize(); //mbts.utils.BTSNativeMethods.init_spark_lib(); var c = mbts.config.Configuration.get(); diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index d4ce21abb..4f2338da0 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -75,6 +75,7 @@ set (SparkLib_Sources config/ConfigurationState.cpp config/ConfigValueQueue.cpp config/PreprocessInputStream.cpp + config/ConfigCheck.cpp config/text_format/text_format.cpp config/tests/config_ref_tests.cpp config/tests/HTTPServer.cpp diff --git a/src/lib/config/ConfigCheck.cpp b/src/lib/config/ConfigCheck.cpp new file mode 100644 index 000000000..a15be7d1e --- /dev/null +++ b/src/lib/config/ConfigCheck.cpp @@ -0,0 +1,110 @@ +#include "config.h" +#include "ConfigCheck.h" +#include "lib/utils/bts_export.h" +#include "lib/utils/TimeStamp.h" + +#include +#include +#include +#include +#include +#include + +namespace proto = google::protobuf; +namespace bts { +namespace config { +namespace interop { + std::string get_macro_string(const char* name); +} } } +using namespace bts; + +void fill_in_defaults(google::protobuf::Message* msg) { + const proto::Descriptor* d = msg->GetDescriptor(); + const proto::Reflection* r = msg->GetReflection(); + for (int x = 0; x < d->field_count(); ++x) { + auto* fd = d->field(x); + auto type = fd->cpp_type(); + if (!fd->is_repeated()) { + if (type == proto::FieldDescriptor::CPPTYPE_MESSAGE) { + fill_in_defaults(r->MutableMessage(msg, fd)); + } + else if (fd->has_default_value() && !r->HasField(*msg, fd)) { + switch (type) { + case proto::FieldDescriptor::CPPTYPE_INT32: + r->SetInt32(msg, fd, fd->default_value_int32()); + break; + case proto::FieldDescriptor::CPPTYPE_INT64: + r->SetInt64(msg, fd, fd->default_value_int64()); + break; + case proto::FieldDescriptor::CPPTYPE_UINT32: + r->SetUInt32(msg, fd, fd->default_value_uint32()); + break; + case proto::FieldDescriptor::CPPTYPE_UINT64: + r->SetUInt64(msg, fd, fd->default_value_uint64()); + break; + case proto::FieldDescriptor::CPPTYPE_DOUBLE: + r->SetDouble(msg, fd, fd->default_value_double()); + break; + case proto::FieldDescriptor::CPPTYPE_FLOAT: + r->SetFloat(msg, fd, fd->default_value_float()); + break; + case proto::FieldDescriptor::CPPTYPE_BOOL: + r->SetBool(msg, fd, fd->default_value_bool()); + break; + case proto::FieldDescriptor::CPPTYPE_ENUM: + r->SetEnum(msg, fd, fd->default_value_enum()); + break; + case proto::FieldDescriptor::CPPTYPE_STRING: + r->SetString(msg, fd, fd->default_value_string()); + break; + default: + break; + } + } + } + } +} + +extern "C" BTS_EXPORT void configcheck(int argc, const char** argv) { + std::vector args(argv, argv+argc); + char buffer[4096] = {0}; + utils::TimeStamp start = utils::TimeStamp::now(); + bool success = config::initialize_nothrow(buffer, sizeof(buffer)); + double time = (utils::TimeStamp::now() - start).seconds(); + + if (success) { + success = config::internal::check_rules(buffer, sizeof(buffer)); + } + + if (!success) { + std::cout << buffer << std::endl; + } + std::cout << "Load time> " << std::floor(time * 1000) << " ms" << std::endl; + + for (size_t x = 0; x < args.size(); x++) + { + std::string key = args[x]; + if (key[0] == '$') { + // macro + std::string result = config::interop::get_macro_string(key.c_str()); + std::cout << key << ": " << result; + } + else { + const google::protobuf::Message* message = config::interop::get_named(key.c_str()); + if (message) + { + google::protobuf::Message* mut = message->New(); + mut->CopyFrom(*message); + if (args.size() > 1) + std::cout << key << ":" << std::endl; + fill_in_defaults(mut); + mut->PrintDebugString(); + } + else + { + std::cout << "Cannot find a config key by the name: " << key; + } + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/src/lib/config/ConfigCheck.h b/src/lib/config/ConfigCheck.h new file mode 100644 index 000000000..b79431a0b --- /dev/null +++ b/src/lib/config/ConfigCheck.h @@ -0,0 +1,3 @@ +#pragma once +#include "lib/utils/bts_export.h" +extern "C" BTS_EXPORT void configcheck(int argc, const char** argv); \ No newline at end of file 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..958b70ce1 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,16 @@ struct QueryServerImpl : public IQueryServer { } } } + else if (result == HandleReadResult::BUSY) { + LOG(MarketData,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::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; } } diff --git a/src/lib/md/platforms/mdp3_onixs/CMESecurityIndex.h b/src/server/sparkd/md/platforms/mdp3_onixs/CMESecurityIndex.h similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/CMESecurityIndex.h rename to src/server/sparkd/md/platforms/mdp3_onixs/CMESecurityIndex.h diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Listener.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Listener.cpp rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Listener.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Listener.h rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Main.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Main.cpp rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.cpp diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Main.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Main.h rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Main.h diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Utils.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Utils.cpp similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Utils.cpp rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Utils.cpp diff --git a/src/lib/md/platforms/mdp3_onixs/MDP3Utils.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Utils.h similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/MDP3Utils.h rename to src/server/sparkd/md/platforms/mdp3_onixs/MDP3Utils.h diff --git a/src/lib/md/platforms/mdp3_onixs/licenses/Onixs.BTS.lic b/src/server/sparkd/md/platforms/mdp3_onixs/licenses/Onixs.BTS.lic similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/licenses/Onixs.BTS.lic rename to src/server/sparkd/md/platforms/mdp3_onixs/licenses/Onixs.BTS.lic diff --git a/src/lib/md/platforms/mdp3_onixs/onixs.h b/src/server/sparkd/md/platforms/mdp3_onixs/onixs.h similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/onixs.h rename to src/server/sparkd/md/platforms/mdp3_onixs/onixs.h diff --git a/src/lib/md/platforms/mdp3_onixs/tests/mdp3_tests.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/tests/mdp3_tests.cpp similarity index 100% rename from src/lib/md/platforms/mdp3_onixs/tests/mdp3_tests.cpp rename to src/server/sparkd/md/platforms/mdp3_onixs/tests/mdp3_tests.cpp