6060#include < Poco/StringTokenizer.h>
6161#include < Poco/Timestamp.h>
6262#include < Poco/Util/HelpFormatter.h>
63+ #include < Poco/Util/LayeredConfiguration.h>
6364#include < Server/BgStorageInit.h>
6465#include < Server/Bootstrap.h>
6566#include < Server/CertificateReloader.h>
@@ -282,31 +283,33 @@ struct TiFlashProxyConfig
282283 args.push_back (iter->second .data ());
283284 }
284285
285- explicit TiFlashProxyConfig (Poco::Util::LayeredConfiguration & config, bool has_s3_config)
286+ // Try to parse start args from `config`.
287+ // Return true if proxy need to be started, and `val_map` will be filled with the
288+ // proxy start params.
289+ // Return false if proxy is not need.
290+ bool tryParseFromConfig (const Poco::Util::LayeredConfiguration & config, bool has_s3_config, const LoggerPtr & log)
286291 {
287- auto disaggregated_mode = getDisaggregatedMode (config);
288-
289292 // tiflash_compute doesn't need proxy.
293+ auto disaggregated_mode = getDisaggregatedMode (config);
290294 if (disaggregated_mode == DisaggregatedMode::Compute && useAutoScaler (config))
291295 {
292- LOG_INFO (
293- Logger::get (),
294- " TiFlash Proxy will not start because AutoScale Disaggregated Compute Mode is specified." );
295- return ;
296+ LOG_INFO (log, " TiFlash Proxy will not start because AutoScale Disaggregated Compute Mode is specified." );
297+ return false ;
296298 }
297299
298300 Poco::Util::AbstractConfiguration::Keys keys;
299301 config.keys (" flash.proxy" , keys);
300302 if (!config.has (" raft.pd_addr" ))
301303 {
302- LOG_WARNING (Logger::get () , " TiFlash Proxy will not start because `raft.pd_addr` is not configured." );
304+ LOG_WARNING (log , " TiFlash Proxy will not start because `raft.pd_addr` is not configured." );
303305 if (!keys.empty ())
304- LOG_WARNING (Logger::get () , " `flash.proxy.*` is ignored because TiFlash Proxy will not start." );
306+ LOG_WARNING (log , " `flash.proxy.*` is ignored because TiFlash Proxy will not start." );
305307
306- return ;
308+ return false ;
307309 }
308310
309311 {
312+ // config items start from `flash.proxy.`
310313 std::unordered_map<std::string, std::string> args_map;
311314 for (const auto & key : keys)
312315 args_map[key] = config.getString (" flash.proxy." + key);
@@ -325,14 +328,54 @@ struct TiFlashProxyConfig
325328 for (auto && [k, v] : args_map)
326329 val_map.emplace (" --" + k, std::move (v));
327330 }
331+ return true ;
332+ }
333+
334+ TiFlashProxyConfig (
335+ Poco::Util::LayeredConfiguration & config,
336+ bool has_s3_config,
337+ const StorageFormatVersion & format_version,
338+ const Settings & settings,
339+ const LoggerPtr & log)
340+ {
341+ is_proxy_runnable = tryParseFromConfig (config, has_s3_config, log);
328342
329343 args.push_back (" TiFlash Proxy" );
330344 for (const auto & v : val_map)
331345 {
332346 args.push_back (v.first .data ());
333347 args.push_back (v.second .data ());
334348 }
335- is_proxy_runnable = true ;
349+
350+ // Enable unips according to `format_version`
351+ if (format_version.page == PageFormat::V4)
352+ {
353+ LOG_INFO (log, " Using UniPS for proxy" );
354+ addExtraArgs (" unips-enabled" , " 1" );
355+ }
356+
357+ // Set the proxy's memory by size or ratio
358+ std::visit (
359+ [&](auto && arg) {
360+ using T = std::decay_t <decltype (arg)>;
361+ if constexpr (std::is_same_v<T, UInt64>)
362+ {
363+ if (arg != 0 )
364+ {
365+ LOG_INFO (log, " Limit proxy's memory, size={}" , arg);
366+ addExtraArgs (" memory-limit-size" , std::to_string (arg));
367+ }
368+ }
369+ else if constexpr (std::is_same_v<T, double >)
370+ {
371+ if (arg > 0 && arg <= 1.0 )
372+ {
373+ LOG_INFO (log, " Limit proxy's memory, ratio={}" , arg);
374+ addExtraArgs (" memory-limit-ratio" , std::to_string (arg));
375+ }
376+ }
377+ },
378+ settings.max_memory_usage_for_all_queries .get ());
336379 }
337380};
338381
@@ -516,7 +559,7 @@ struct RaftStoreProxyRunner : boost::noncopyable
516559 pthread_attr_t attribute;
517560 pthread_attr_init (&attribute);
518561 pthread_attr_setstacksize (&attribute, parms.stack_size );
519- LOG_INFO (log, " start raft store proxy" );
562+ LOG_INFO (log, " Start raft store proxy. Args: {} " , parms. conf . args );
520563 pthread_create (&thread, &attribute, runRaftStoreProxyFFI, &parms);
521564 pthread_attr_destroy (&attribute);
522565 }
@@ -1028,27 +1071,40 @@ int Server::main(const std::vector<std::string> & /*args*/)
10281071 // Set whether to use safe point v2.
10291072 PDClientHelper::enable_safepoint_v2 = config ().getBool (" enable_safe_point_v2" , false );
10301073
1074+ /* * Context contains all that query execution is dependent:
1075+ * settings, available functions, data types, aggregate functions, databases...
1076+ */
1077+ global_context = Context::createGlobal ();
1078+ // / Initialize users config reloader.
1079+ auto users_config_reloader = UserConfig::parseSettings (config (), config_path, global_context, log);
1080+
1081+ // / Load global settings from default_profile and system_profile.
1082+ // / It internally depends on UserConfig::parseSettings.
1083+ // TODO: Parse the settings from config file at the program beginning
1084+ global_context->setDefaultProfiles (config ());
1085+ LOG_INFO (
1086+ log,
1087+ " Loaded global settings from default_profile and system_profile, changed configs: {{{}}}" ,
1088+ global_context->getSettingsRef ().toString ());
1089+ Settings & settings = global_context->getSettingsRef ();
1090+
10311091 // Init Proxy's config
1032- TiFlashProxyConfig proxy_conf (config (), storage_config.s3_config .isS3Enabled ());
1092+ TiFlashProxyConfig proxy_conf ( //
1093+ config (),
1094+ storage_config.s3_config .isS3Enabled (),
1095+ STORAGE_FORMAT_CURRENT,
1096+ settings,
1097+ log);
10331098 EngineStoreServerWrap tiflash_instance_wrap{};
10341099 auto helper = GetEngineStoreServerHelper (&tiflash_instance_wrap);
10351100
1036- if (STORAGE_FORMAT_CURRENT.page == PageFormat::V4)
1037- {
1038- LOG_INFO (log, " Using UniPS for proxy" );
1039- proxy_conf.addExtraArgs (" unips-enabled" , " 1" );
1040- }
1041- else
1042- {
1043- LOG_INFO (log, " UniPS is not enabled for proxy, page_version={}" , STORAGE_FORMAT_CURRENT.page );
1044- }
1045-
10461101#ifdef USE_JEMALLOC
10471102 LOG_INFO (log, " Using Jemalloc for TiFlash" );
10481103#else
10491104 LOG_INFO (log, " Not using Jemalloc for TiFlash" );
10501105#endif
10511106
1107+
10521108 RaftStoreProxyRunner proxy_runner (RaftStoreProxyRunner::RunRaftStoreProxyParms{&helper, proxy_conf}, log);
10531109
10541110 if (proxy_conf.is_proxy_runnable )
@@ -1092,10 +1148,6 @@ int Server::main(const std::vector<std::string> & /*args*/)
10921148 gpr_set_log_verbosity (GPR_LOG_SEVERITY_DEBUG);
10931149 gpr_set_log_function (&printGRPCLog);
10941150
1095- /* * Context contains all that query execution is dependent:
1096- * settings, available functions, data types, aggregate functions, databases...
1097- */
1098- global_context = Context::createGlobal ();
10991151 SCOPE_EXIT ({
11001152 if (!proxy_conf.is_proxy_runnable )
11011153 return ;
@@ -1296,24 +1348,11 @@ int Server::main(const std::vector<std::string> & /*args*/)
12961348 // / Init TiFlash metrics.
12971349 global_context->initializeTiFlashMetrics ();
12981350
1299- // / Initialize users config reloader.
1300- auto users_config_reloader = UserConfig::parseSettings (config (), config_path, global_context, log);
1301-
1302- // / Load global settings from default_profile and system_profile.
1303- // / It internally depends on UserConfig::parseSettings.
1304- // TODO: Parse the settings from config file at the program beginning
1305- global_context->setDefaultProfiles (config ());
1306- LOG_INFO (
1307- log,
1308- " Loaded global settings from default_profile and system_profile, changed configs: {{{}}}" ,
1309- global_context->getSettingsRef ().toString ());
1310-
13111351 // /
13121352 // / The config value in global settings can only be used from here because we just loaded it from config file.
13131353 // /
13141354
13151355 // / Initialize the background & blockable background thread pool.
1316- Settings & settings = global_context->getSettingsRef ();
13171356 LOG_INFO (log, " Background & Blockable Background pool size: {}" , settings.background_pool_size );
13181357 auto & bg_pool = global_context->initializeBackgroundPool (settings.background_pool_size );
13191358 auto & blockable_bg_pool = global_context->initializeBlockableBackgroundPool (settings.background_pool_size );
0 commit comments