Skip to content

Commit 9ff0d60

Browse files
Set proxy's memory limit by TiFlash (#9753) and update tiflash proxy (#9776)
ref #4982, close #9745 Signed-off-by: CalvinNeo <[email protected]> Signed-off-by: Calvin Neo <[email protected]> Signed-off-by: JaySon-Huang <[email protected]> Co-authored-by: JaySon <[email protected]>
1 parent 7275a8c commit 9ff0d60

File tree

3 files changed

+184
-45
lines changed

3 files changed

+184
-45
lines changed

dbms/src/Server/Server.cpp

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
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);

metrics/grafana/tiflash_summary.json

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"gnetId": null,
5353
"graphTooltip": 1,
5454
"id": null,
55-
"iteration": 1728897014230,
55+
"iteration": 1735826311826,
5656
"links": [],
5757
"panels": [
5858
{
@@ -8736,7 +8736,7 @@
87368736
"alertThreshold": true
87378737
},
87388738
"percentage": false,
8739-
"pluginVersion": "7.5.17",
8739+
"pluginVersion": "7.5.11",
87408740
"pointradius": 5,
87418741
"points": false,
87428742
"renderer": "flot",
@@ -9513,7 +9513,7 @@
95139513
"alertThreshold": true
95149514
},
95159515
"percentage": false,
9516-
"pluginVersion": "7.5.17",
9516+
"pluginVersion": "7.5.11",
95179517
"pointradius": 2,
95189518
"points": false,
95199519
"renderer": "flot",
@@ -9612,7 +9612,7 @@
96129612
"alertThreshold": true
96139613
},
96149614
"percentage": false,
9615-
"pluginVersion": "7.5.17",
9615+
"pluginVersion": "7.5.11",
96169616
"pointradius": 2,
96179617
"points": false,
96189618
"renderer": "flot",
@@ -15003,6 +15003,106 @@
1500315003
"align": false,
1500415004
"alignLevel": null
1500515005
}
15006+
},
15007+
{
15008+
"aliasColors": {},
15009+
"bars": false,
15010+
"dashLength": 10,
15011+
"dashes": false,
15012+
"datasource": "${DS_TEST-CLUSTER}",
15013+
"fieldConfig": {
15014+
"defaults": {},
15015+
"overrides": []
15016+
},
15017+
"fill": 0,
15018+
"fillGradient": 0,
15019+
"gridPos": {
15020+
"h": 7,
15021+
"w": 12,
15022+
"x": 0,
15023+
"y": 156
15024+
},
15025+
"hiddenSeries": false,
15026+
"id": 296,
15027+
"legend": {
15028+
"alignAsTable": false,
15029+
"avg": false,
15030+
"current": false,
15031+
"max": false,
15032+
"min": false,
15033+
"rightSide": false,
15034+
"show": true,
15035+
"total": false,
15036+
"values": false
15037+
},
15038+
"lines": true,
15039+
"linewidth": 1,
15040+
"links": [],
15041+
"nullPointMode": "null as zero",
15042+
"options": {
15043+
"alertThreshold": true
15044+
},
15045+
"percentage": false,
15046+
"pluginVersion": "7.5.11",
15047+
"pointradius": 5,
15048+
"points": false,
15049+
"renderer": "flot",
15050+
"seriesOverrides": [],
15051+
"spaceLength": 10,
15052+
"stack": false,
15053+
"steppedLine": false,
15054+
"targets": [
15055+
{
15056+
"exemplar": true,
15057+
"expr": "sum(rate(tiflash_proxy_tikv_server_raft_append_rejects{}[1m])) by (instance)",
15058+
"format": "time_series",
15059+
"interval": "",
15060+
"intervalFactor": 1,
15061+
"legendFormat": "{{instance}}",
15062+
"refId": "A"
15063+
}
15064+
],
15065+
"thresholds": [],
15066+
"timeFrom": null,
15067+
"timeRegions": [],
15068+
"timeShift": null,
15069+
"title": "Log Replication Rejected",
15070+
"tooltip": {
15071+
"shared": true,
15072+
"sort": 0,
15073+
"value_type": "individual"
15074+
},
15075+
"type": "graph",
15076+
"xaxis": {
15077+
"buckets": null,
15078+
"mode": "time",
15079+
"name": null,
15080+
"show": true,
15081+
"values": []
15082+
},
15083+
"yaxes": [
15084+
{
15085+
"decimals": null,
15086+
"format": "ops",
15087+
"label": null,
15088+
"logBase": 1,
15089+
"max": null,
15090+
"min": "0",
15091+
"show": true
15092+
},
15093+
{
15094+
"format": "none",
15095+
"label": null,
15096+
"logBase": 1,
15097+
"max": null,
15098+
"min": null,
15099+
"show": true
15100+
}
15101+
],
15102+
"yaxis": {
15103+
"align": false,
15104+
"alignLevel": null
15105+
}
1500615106
}
1500715107
],
1500815108
"repeat": null,

0 commit comments

Comments
 (0)