diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index ea2b1de78..0883bc861 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -70,7 +70,6 @@ namespace mbts.ui.shell { Log.Gen.info("Application.OnExit"); if (_runtime != null) _runtime.Dispose(); - utils.BTSNativeMethods.finit_spark_lib(); base.OnExit(e); Environment.Exit(0); } diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index eb30f7726..be8bf6c0a 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -13,6 +13,7 @@ namespace bts.api.core { AuthToken _token; public ServiceContext() { + BTS.init(); _qbus = ApiNativeMethods.api_create_quickbus(null, 0); } diff --git a/src/client/spark_api/utils/BTS.cs b/src/client/spark_api/utils/BTS.cs new file mode 100644 index 000000000..856365559 --- /dev/null +++ b/src/client/spark_api/utils/BTS.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace bts +{ + public static class BTS + { + static int _needs_init_spark_lib = 0; + public static void init() { + var args = Environment.GetCommandLineArgs(); + utils.BTSNativeMethods.provide_command_line(args, args.Length); + + var buffer = new StringBuilder(4096); + bool config_ok = utils.BTSNativeMethods.initialize_nothrow(buffer, buffer.Capacity); + if (!config_ok) { + throw new ApplicationException(buffer.ToString()); + } + + if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) { + utils.BTSNativeMethods.init_spark_lib(); + } + + // This seems like a good idea but it crashes due to thread shutdown order + // AppDomain.CurrentDomain.ProcessExit += (o, e) => { + // utils.BTSNativeMethods.finit_spark_lib(); + // }; + } + + internal static IEnumerable remove_config_arg(string[] args) { + int skip = 0; + foreach (var arg in args) { + if (arg.ToLower() == "-configserver") { + skip = 2; + } + if (skip > 0) { skip -= 1; } + else { yield return arg; } + } + } + } +} diff --git a/src/client/spark_api/utils/BTSNativeMethods.cs b/src/client/spark_api/utils/BTSNativeMethods.cs index cef374bba..66a562c39 100644 --- a/src/client/spark_api/utils/BTSNativeMethods.cs +++ b/src/client/spark_api/utils/BTSNativeMethods.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; -namespace mbts.utils +namespace bts.utils { internal static class BTSNativeMethods { @@ -25,5 +25,8 @@ namespace mbts.utils [DllImport("kernel32.dll")] public static extern bool SetConsoleTitle(string lpConsoleTitle); + + [DllImport("libspark")] + public static extern bool initialize_nothrow(StringBuilder buffer, int length); } } diff --git a/src/client/spark_api/utils/LLDateTime.cs b/src/client/spark_api/utils/LLDateTime.cs index 968a1a9eb..0ed577a58 100644 --- a/src/client/spark_api/utils/LLDateTime.cs +++ b/src/client/spark_api/utils/LLDateTime.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using bts.utils; namespace mbts.utils { diff --git a/src/client/spark_core/config/config.cs b/src/client/spark_core/config/config.cs index abf97b268..81d232b60 100644 --- a/src/client/spark_core/config/config.cs +++ b/src/client/spark_core/config/config.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Threading; using System.Linq; +using bts.utils; namespace mbts.config { @@ -24,38 +25,37 @@ namespace mbts.config return ProtoBuf.Serializer.Deserialize(stream); } - private static bool initialize(out string error) { - StringBuilder buffer = new StringBuilder(4096); - bool success = initialize_nothrow(buffer, buffer.Capacity); - error = buffer.ToString(); - return success; - } - public static void initialize() { if (_state == State.OK) return; - string[] args = Environment.GetCommandLineArgs(); - utils.BTSNativeMethods.provide_command_line(args, args.Length); // Considered doing this async on a background thread // but it wouldn't do any good. The next guy down the line to ask for config // will just block until this completes. - string error; - if (!initialize(out error)) { + try { + bts.BTS.init(); + } catch(ApplicationException) { _state = State.Error; - throw new ApplicationException(error); + throw; } _state = State.OK; - utils.BTSNativeMethods.init_spark_lib(); } - public static void initialize_for_test() { - reset(); - initialize(); + public static void initialize_for_test(string config="") { + raw_initialize_for_test(config); + // still need init_spark_lib + bts.BTS.init(); + _state = State.OK; } - public static bool try_initialize(out string error) - { + public static bool try_initialize(out string error) { reset(); - return initialize(out error); + try { + bts.BTS.init(); + }catch(ApplicationException e) { + error = e.Message; + return false; + } + error = string.Empty; + return true; } //////////// @@ -67,6 +67,10 @@ namespace mbts.config [DllImport("libspark")] private static extern bool initialize_nothrow(StringBuilder buffer, int length); + + [DllImport("libspark", EntryPoint="initialize_for_test")] + private static extern void raw_initialize_for_test(string buffer); + [DllImport("libspark")] private static extern void reset(); diff --git a/src/client/spark_sdk/samples/OrderSample.cs b/src/client/spark_sdk/samples/OrderSample.cs index d6df99265..aec01460e 100644 --- a/src/client/spark_sdk/samples/OrderSample.cs +++ b/src/client/spark_sdk/samples/OrderSample.cs @@ -89,6 +89,10 @@ namespace bts.sdk cr.NewOrder.SubType = OrderSubType.None; cr.NewOrder.TimeInForce = TimeInForce.Day; var admin = await _admin.Value.get_admin_data_async(new VoidMessage()); + if (!admin.success) { + Console.Error.WriteLine(admin.error_message); + return; + } BTSUser user = admin.value.Users.FirstOrDefault( x=> username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true ); if (user != null) { diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs index d23ef3fbc..f0fb158d8 100644 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ b/src/client/spark_sdk/samples/SDKMain.cs @@ -26,6 +26,7 @@ namespace bts.sdk } public async static Task Main(string[] args) { + BTS.init(); Log.ClientSdk.info("Welcome to the BTS Client Side SDK example"); string u = args.FirstOrDefault(x => x.Contains("--user=")); diff --git a/src/client/sparkcmd/Cmd/TestCommand.cs b/src/client/sparkcmd/Cmd/TestCommand.cs index b5b919f0a..9b071eb7a 100644 --- a/src/client/sparkcmd/Cmd/TestCommand.cs +++ b/src/client/sparkcmd/Cmd/TestCommand.cs @@ -9,6 +9,7 @@ using mbts.config; using System.Linq; using CommandLine; using Newtonsoft.Json; +using bts; namespace sparkcmd { @@ -41,6 +42,12 @@ namespace sparkcmd { } public static int Run(TestCommandOptions o) { + if (o.type == TestType.Unit) { + // Skip loading any local config + Configuration.initialize_for_test(); + } + else Configuration.initialize(); + Console.WriteLine(JsonConvert.SerializeObject(o)); if (o.list) { diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index c35332f50..8999b8bcc 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -9,6 +9,7 @@ using bts; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; +using bts.utils; namespace sparkcmd { class Program { @@ -23,7 +24,11 @@ namespace sparkcmd { // return 3; // } static int Main(string[] args) { - mbts.utils.BTSNativeMethods.provide_command_line(args, args.Length); + // Initializing config is up to each individual command + // We call provide_command_line here in case config is first + // initialized from c++ instead of c# + BTSNativeMethods.provide_command_line(args, args.Length); + // note: this can be made to infer the options type list from the list of delegates return Parser.Default .ParseArguments< @@ -35,7 +40,7 @@ namespace sparkcmd { TestCommandOptions, OrderStressOptions, MBOCommandOptions - >(args) + >(BTS.remove_config_arg(args)) .MapResult< ConfigcheckOptions, ScriptClientOptions, diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 487b2eabb..6c29be4b7 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -140,14 +140,23 @@ void reply_error(ApiError error, raw_response_callback cb, void* extra) { } -CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { +int32_t rpc_connect_timeout_ms() { + static int32_t t = config::get().rpc_connect_timeout_seconds() * 1000; + return t; +} +void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t delay, void* extra) { bts_assert(pclient); - std::string strbuf(buffer, buffer_length); - qbus::RPCRequest request(move(strbuf)); - pclient->rpc.wait_until_connected(1); - LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + bool connected = pclient->rpc.wait_until_connected(0); + if (!connected && delay > 0) { + // This branch is the uncommon 'slow path' where connection is taking longer than we expect + LOG(Api,Diag,"rpc call delay waiting for connection {}",((int64_t)request.value().c_str())); + pclient->qbus.post([=, request{std::move(request)}]() mutable { + api_client_send_internal(pclient, std::move(request), cb, delay - 100, extra); + }, 100); + return; + } pclient->rpc.send(std::move(request), [=](qbus::RPCError e, const qbus::RPCReplyPtr& reply) { - LOG(Api,Diag,"rpc reply {}",((int64_t)buffer)); + LOG(Api,Diag,"rpc reply {}",((int64_t)request.value().c_str())); switch (e) { case qbus::RPCError::RPC_OK: safe_call_fn([&]() { @@ -165,6 +174,12 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len } }); } +CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) { + LOG(Api,Diag,"send rpc {}",((int64_t)buffer)); + std::string strbuf(buffer, buffer_length); + qbus::RPCRequest request(move(strbuf)); + api_client_send_internal(pclient, std::move(request), cb, rpc_connect_timeout_ms(), extra); +} CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) { return pclient->qbus.on_qb_thread(); diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index dce50e8c6..b7eabbb88 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -11,6 +11,7 @@ #include "lib/utils/bts_assert.h" #include "lib/utils/is_unittest_environment.h" #include "lib/utils/bts_array_size.h" +#include "lib/utils/defer.h" #include "config.h" #include "lib/config/protos/system.pb.h" @@ -348,11 +349,13 @@ void ConfigurationState::initialize_settings() std::string settings = settings_path(); std::string fallback = get_command_argument("-ConfigFallback"); - load(settings, fallback, log); - initialize_builtins(); - initialize_macros(); - _deferred_queue->process(); + DEFER(// Do this even if load() fails + initialize_builtins(); + initialize_macros(); + _deferred_queue->process(); + ); + load(settings, fallback, log); } void ConfigurationState::reset() diff --git a/src/lib/config/config.cpp b/src/lib/config/config.cpp index be514ea26..38455c881 100755 --- a/src/lib/config/config.cpp +++ b/src/lib/config/config.cpp @@ -89,7 +89,10 @@ namespace internal { // Functions used in testing ////////////////////////////////////////// -extern "C" BTS_EXPORT void initialize_for_test(const std::string& values, bool print_errors) +extern "C" BTS_EXPORT void initialize_for_test(const char* config) { + initialize_for_test(std::string(config), false); +} +void initialize_for_test(const std::string& values, bool print_errors) { config_state.reset(); config_state.load_string(values, print_errors); diff --git a/src/lib/config/config.h b/src/lib/config/config.h index 4cf0dc3bb..46f388312 100755 --- a/src/lib/config/config.h +++ b/src/lib/config/config.h @@ -56,7 +56,8 @@ namespace config { { // Initialize configuration state from a string instead of from the filesystem. defaults.conf will still be loaded. // Calling this a second time clears all values from previous initialization - extern "C" BTS_EXPORT void initialize_for_test(const std::string& configuration, bool print_errors=true); + extern "C" BTS_EXPORT void initialize_for_test(const char* config); + void initialize_for_test(const std::string& configuration, bool print_errors=true); // Releases all resources and returns config to factory defaults extern "C" BTS_EXPORT void reset(); diff --git a/src/lib/config/defaults/logging.conf b/src/lib/config/defaults/logging.conf index 44a2d58d6..a09a08e1b 100644 --- a/src/lib/config/defaults/logging.conf +++ b/src/lib/config/defaults/logging.conf @@ -1,24 +1,24 @@ -Logging { - rule: "System.build_type == 'debug' && !('testorderman' in System.cmd_args) && !('dump' in System.cmd_args) && !('sparkcmd' in System.cmd_args) && !('dotnet' in System.cmd_args)" - value - { - console { default_level: "Info" } - syslog { default_level: "Info" } - file { default_level: "Info" } - } -} - Logging { rule: "System.platform == 'windows'" value { console { default_level: "Error" } syslog { default_level: "Info" } file { default_level: "Info" } - initial_queue_size: 1024 + initial_queue_size: 128 max_queue_growth: 100 } } +Logging { + rule: "System.build_type = 'debug'" + value + { + console { default_level: "Info" } + syslog { default_level: "Info" } + file { default_level: "Info" } + } +} + Logging { value diff --git a/src/lib/config/protos/quickbus_cfg.proto b/src/lib/config/protos/quickbus_cfg.proto index c779a2f77..bed2302ee 100644 --- a/src/lib/config/protos/quickbus_cfg.proto +++ b/src/lib/config/protos/quickbus_cfg.proto @@ -18,3 +18,6 @@ message QuickbusCfg { optional uint32 max_missed_hbs = 6 [default=3]; } +message RPCConfig { + optional uint32 rpc_connect_timeout_seconds = 1 [default = 10]; +} \ No newline at end of file diff --git a/tools/buildspark.cmd b/tools/buildspark.cmd index 25ddc7079..afa7df151 100644 --- a/tools/buildspark.cmd +++ b/tools/buildspark.cmd @@ -4,4 +4,5 @@ setlocal cd /d %~dp0/../ setlocal set path=%spark_build_python_path%;%path% +touch src\lib\CMakeLists.txt msbuild /m /nologo /p:debugtype=portable /p:platform=x64 /p:GenerateFullPaths=true /clp:verbosity=minimal /clp:summary %* spark_client.sln