From 5bdaab05813c3fc635bdd93ab426d0a38a1a524e Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 28 Aug 2019 12:26:03 -0400 Subject: [PATCH 1/8] Add BTS.init() to init_spark lib and provide args --- src/client/spark/App.xaml.cs | 2 +- src/client/spark_api/utils/BTS.cs | 22 +++++++++++++++++++ .../spark_api/utils/BTSNativeMethods.cs | 2 +- src/client/spark_api/utils/LLDateTime.cs | 1 + src/client/spark_core/config/config.cs | 4 +--- src/client/spark_sdk/samples/SDKMain.cs | 1 + src/client/sparkcmd/Program.cs | 2 +- 7 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/client/spark_api/utils/BTS.cs diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index ea2b1de78..7d4600944 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -31,6 +31,7 @@ namespace mbts.ui.shell if (!IsInDesigner()) { _mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); } + BTS.init(); } private static bool IsInDesigner() { @@ -70,7 +71,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/utils/BTS.cs b/src/client/spark_api/utils/BTS.cs new file mode 100644 index 000000000..ddb7143fd --- /dev/null +++ b/src/client/spark_api/utils/BTS.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace bts +{ + public static class BTS + { + static bool _initialized = false; + public static void init() { + if (!_initialized) { + _initialized = true; + utils.BTSNativeMethods.init_spark_lib(); + var args = Environment.GetCommandLineArgs(); + utils.BTSNativeMethods.provide_command_line(args, args.Length); + AppDomain.CurrentDomain.ProcessExit += (o, e) => { + utils.BTSNativeMethods.finit_spark_lib(); + }; + } + } + } +} diff --git a/src/client/spark_api/utils/BTSNativeMethods.cs b/src/client/spark_api/utils/BTSNativeMethods.cs index cef374bba..cfefceb65 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 { 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..52133f0d5 100644 --- a/src/client/spark_core/config/config.cs +++ b/src/client/spark_core/config/config.cs @@ -33,8 +33,7 @@ namespace mbts.config public static void initialize() { if (_state == State.OK) return; - string[] args = Environment.GetCommandLineArgs(); - utils.BTSNativeMethods.provide_command_line(args, args.Length); + bts.BTS.init(); // 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. @@ -44,7 +43,6 @@ namespace mbts.config throw new ApplicationException(error); } _state = State.OK; - utils.BTSNativeMethods.init_spark_lib(); } public static void initialize_for_test() { 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/Program.cs b/src/client/sparkcmd/Program.cs index c35332f50..a5184ae50 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -23,7 +23,7 @@ namespace sparkcmd { // return 3; // } static int Main(string[] args) { - mbts.utils.BTSNativeMethods.provide_command_line(args, args.Length); + BTS.init(); // note: this can be made to infer the options type list from the list of delegates return Parser.Default .ParseArguments< From 2ee0f0be2cdbbe4fadc5934b2d352b2e7183c228 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 28 Aug 2019 12:26:41 -0400 Subject: [PATCH 2/8] buildspark.cmd should touch CMakeLists so it doesn't get lonely --- tools/buildspark.cmd | 1 + 1 file changed, 1 insertion(+) 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 From e030518d601900c09b087d1b846d8f92af71d5bd Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 28 Aug 2019 14:25:37 -0400 Subject: [PATCH 3/8] Config bug: if configserver is down but we proceed (for unit tests or something) we don't get the right values --- src/lib/config/ConfigurationState.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index dce50e8c6..f43c735cb 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() throws + initialize_builtins(); + initialize_macros(); + _deferred_queue->process(); + ); + load(settings, fallback, log); } void ConfigurationState::reset() From d5445119937ecac72bcc503b64f22ea610730b4d Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 28 Aug 2019 15:32:17 -0400 Subject: [PATCH 4/8] Additional managed init cleanup --- src/client/spark/App.xaml.cs | 1 - src/client/spark_api/api/ServiceContext.cs | 1 + src/client/spark_api/utils/BTS.cs | 36 +++++++++++++---- .../spark_api/utils/BTSNativeMethods.cs | 3 ++ src/client/spark_core/config/config.cs | 40 +++++++++++-------- src/client/sparkcmd/Cmd/TestCommand.cs | 7 ++++ src/client/sparkcmd/Program.cs | 9 ++++- src/lib/config/ConfigurationState.cpp | 2 +- src/lib/config/config.cpp | 5 ++- src/lib/config/config.h | 3 +- src/lib/config/defaults/logging.conf | 22 +++++----- 11 files changed, 87 insertions(+), 42 deletions(-) diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index 7d4600944..0883bc861 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -31,7 +31,6 @@ namespace mbts.ui.shell if (!IsInDesigner()) { _mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); } - BTS.init(); } private static bool IsInDesigner() { 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 index ddb7143fd..e34f8b65a 100644 --- a/src/client/spark_api/utils/BTS.cs +++ b/src/client/spark_api/utils/BTS.cs @@ -6,16 +6,36 @@ namespace bts { public static class BTS { - static bool _initialized = false; + static bool _needs_init_spark_lib = true; public static void init() { - if (!_initialized) { - _initialized = true; + 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 (_needs_init_spark_lib) { + _needs_init_spark_lib = false; utils.BTSNativeMethods.init_spark_lib(); - var args = Environment.GetCommandLineArgs(); - utils.BTSNativeMethods.provide_command_line(args, args.Length); - AppDomain.CurrentDomain.ProcessExit += (o, e) => { - utils.BTSNativeMethods.finit_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 cfefceb65..66a562c39 100644 --- a/src/client/spark_api/utils/BTSNativeMethods.cs +++ b/src/client/spark_api/utils/BTSNativeMethods.cs @@ -25,5 +25,8 @@ namespace bts.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_core/config/config.cs b/src/client/spark_core/config/config.cs index 52133f0d5..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,36 +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; - bts.BTS.init(); // 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; } - 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; } //////////// @@ -65,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/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 a5184ae50..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) { - BTS.init(); + // 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/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index f43c735cb..b7eabbb88 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -350,7 +350,7 @@ void ConfigurationState::initialize_settings() std::string settings = settings_path(); std::string fallback = get_command_argument("-ConfigFallback"); - DEFER(// Do this even if load() throws + DEFER(// Do this even if load() fails initialize_builtins(); initialize_macros(); _deferred_queue->process(); 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..b97013877 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: 32 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 From 64d43be34a50a05bf00c486adf9b969b89d1a728 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 28 Aug 2019 15:56:27 -0400 Subject: [PATCH 5/8] Add error checking to order sample --- src/client/spark_sdk/samples/OrderSample.cs | 4 ++++ 1 file changed, 4 insertions(+) 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) { From f7b5eeb04cecbc45c7f278e6b81f0bdf7e3cc409 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 29 Aug 2019 10:49:38 -0400 Subject: [PATCH 6/8] Increase default message queue size on windows --- src/lib/config/defaults/logging.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/config/defaults/logging.conf b/src/lib/config/defaults/logging.conf index b97013877..a09a08e1b 100644 --- a/src/lib/config/defaults/logging.conf +++ b/src/lib/config/defaults/logging.conf @@ -4,7 +4,7 @@ Logging { console { default_level: "Error" } syslog { default_level: "Info" } file { default_level: "Info" } - initial_queue_size: 32 + initial_queue_size: 128 max_queue_growth: 100 } } From d12200e9777e9a5bc9ec3beca8fbf358e3ddbf29 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 29 Aug 2019 10:50:00 -0400 Subject: [PATCH 7/8] Allow a larger timeout for rpc connect in bts api --- src/lib/api/core/ApiClientInterop.cpp | 27 ++++++++++++++++++------ src/lib/config/protos/quickbus_cfg.proto | 3 +++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 487b2eabb..811c8acea 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 accumulated_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); //accumulated_delay ? 0 : 1); + if (!connected && accumulated_delay < rpc_connect_timeout_ms()) { + // 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, accumulated_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, 0, extra); +} CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) { return pclient->qbus.on_qb_thread(); 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 From ce0beafac35e0baeab105bb11ed6e5fff67d981c Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Thu, 29 Aug 2019 16:43:43 -0400 Subject: [PATCH 8/8] CR feedback --- src/client/spark_api/utils/BTS.cs | 6 +++--- src/lib/api/core/ApiClientInterop.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client/spark_api/utils/BTS.cs b/src/client/spark_api/utils/BTS.cs index e34f8b65a..856365559 100644 --- a/src/client/spark_api/utils/BTS.cs +++ b/src/client/spark_api/utils/BTS.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading; namespace bts { public static class BTS { - static bool _needs_init_spark_lib = true; + static int _needs_init_spark_lib = 0; public static void init() { var args = Environment.GetCommandLineArgs(); utils.BTSNativeMethods.provide_command_line(args, args.Length); @@ -17,8 +18,7 @@ namespace bts throw new ApplicationException(buffer.ToString()); } - if (_needs_init_spark_lib) { - _needs_init_spark_lib = false; + if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) { utils.BTSNativeMethods.init_spark_lib(); } diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 811c8acea..6c29be4b7 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -144,14 +144,14 @@ 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 accumulated_delay, void* extra) { +void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t delay, void* extra) { bts_assert(pclient); - bool connected = pclient->rpc.wait_until_connected(0); //accumulated_delay ? 0 : 1); - if (!connected && accumulated_delay < rpc_connect_timeout_ms()) { + 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, accumulated_delay + 100, extra); + api_client_send_internal(pclient, std::move(request), cb, delay - 100, extra); }, 100); return; } @@ -178,7 +178,7 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len 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, 0, extra); + api_client_send_internal(pclient, std::move(request), cb, rpc_connect_timeout_ms(), extra); } CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) {