Skip to content

Commit

Permalink
[YSQL][#24551][#24550][#23838][#22954][#24657] Connection manager wor…
Browse files Browse the repository at this point in the history
…k items

Summary:
The PR will address the following:
  - Set the max Pool connection as 10000
  - Set the stats_interval default as 1 sec.
  - Attempt to bind the PG backend on the port 6433 first if connection manager and PG backends are in conflict. If binding on 6433 is not working, then bind on a random port.
  - Provide support to set the server lifetime i.e, a period after which backend postgres server is closed. It is supported through the gflag `ysql_conn_mgr_server_lifetime`. The value has to be provided in secs.
  - Provide gflag support to set the log settings for connection manager. The gflag `ysql_conn_mgr_log_settings` is used for this purpose. This is a comma separated string which takes any or all of the values in `log_debug,log_config,log_session,log_query,log_stats`. They can be passed in any combination.

Test Plan: Jenkins: enable connection manager, all tests

Reviewers: skumar, rbarigidad, mkumar, devansh.saxena, stiwary

Reviewed By: rbarigidad

Subscribers: ybase, yql

Differential Revision: https://phorge.dev.yugabyte.com/D39363
  • Loading branch information
vpatibandla-yb committed Oct 29, 2024
1 parent 8b96ace commit 9086ea7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/yb/tserver/tablet_server_main_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ void SetProxyAddresses() {
<< "Bad pgsql_proxy_bind_address " << FLAGS_pgsql_proxy_bind_address << ": " << status;
for (const auto& addr : bind_addresses) {
if (addr.port() == PgProcessConf::kDefaultPort && addr.port() == FLAGS_ysql_conn_mgr_port) {
uint16_t preferred_port = 6433;
if (addr.port() != preferred_port) {
LOG(INFO) << "The connection manager and backend db ports are conflicting on "
<< addr.port() << ". Trying port " << preferred_port << " for backend db.";
Endpoint preferred_sock_addr(boost::asio::ip::address_v4::loopback(), preferred_port);
Socket preferred_sock;
Status preferred_status = preferred_sock.Init(0);
if (preferred_status.ok() && preferred_sock.Bind(preferred_sock_addr, false).ok()) {
LOG(INFO) << "Successfully bound to port " << preferred_port << " for backend db.";
SetProxyAddress(&FLAGS_pgsql_proxy_bind_address, "YSQL", preferred_port, true);
break;
}
}

LOG(INFO) << "The connection manager and backend db ports are conflicting on "
<< addr.port() << ". Assigning a random available port: " << freeport <<
" to backend db and " << PgProcessConf::kDefaultPort << " to the connection manager";
Expand Down
12 changes: 6 additions & 6 deletions src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr.template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ log_syslog_ident "ysqlconnmgr"
log_syslog_facility "daemon"

#TODO JA: "log_debug" needs to be in sync with the tserver log levels.
log_debug yes
log_debug {%log_debug%}

log_config no
log_session no
log_query yes
log_stats no
log_config {%log_config%}
log_session {%log_session%}
log_query {%log_query%}
log_stats {%log_stats%}
stats_interval {%stats_interval%}
log_general_stats_prom no
log_route_stats_prom no
Expand Down Expand Up @@ -104,7 +104,7 @@ database default {
pool_client_idle_timeout 3600
application_name_add_host no
reserve_session_server_connection yes
server_lifetime 3600
server_lifetime {%server_lifetime%}
quantiles "{%quantiles%}"
client_fwd_error yes
min_pool_size {%min_pool_size%}
Expand Down
34 changes: 31 additions & 3 deletions src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "yb/util/env_util.h"
#include "yb/util/path_util.h"
#include "yb/util/net/net_util.h"
#include "yb/util/string_trim.h"
#include "yb/util/string_util.h"
#include "yb/util/pg_util.h"

Expand All @@ -40,6 +41,8 @@ DECLARE_uint32(ysql_conn_mgr_num_workers);
DECLARE_uint32(ysql_conn_mgr_stats_interval);
DECLARE_uint32(ysql_conn_mgr_min_conns_per_db);
DECLARE_int32(ysql_max_connections);
DECLARE_string(ysql_conn_mgr_log_settings);
DECLARE_uint32(ysql_conn_mgr_server_lifetime);

namespace yb {
namespace ysql_conn_mgr_wrapper {
Expand Down Expand Up @@ -138,8 +141,31 @@ void YsqlConnMgrConf::AddSslConfig(std::map<std::string, std::string>* ysql_conn
(*ysql_conn_mgr_configs)["{%tls_cert_file%}"] = tls_cert_file;
}

void YsqlConnMgrConf::UpdateLogSettings(std::string& log_settings_str) {
std::stringstream ss(log_settings_str);
std::string setting;

while (std::getline(ss, setting, ',')) {
util::TrimStr(setting);
if (!setting.empty()) {
if (setting == "log_debug") {
log_debug_ = true;
} else if (setting == "log_config") {
log_config_ = true;
} else if (setting == "log_session") {
log_session_ = true;
} else if (setting == "log_query") {
log_query_ = true;
} else if (setting == "log_stats") {
log_stats_ = true;
}
}
}
}

std::string YsqlConnMgrConf::CreateYsqlConnMgrConfigAndGetPath() {
const auto conf_file_path = JoinPathSegments(data_dir_, conf_file_name_);
UpdateLogSettings(FLAGS_ysql_conn_mgr_log_settings);

// Config map
std::map<std::string, std::string> ysql_conn_mgr_configs = {
Expand All @@ -158,8 +184,13 @@ std::string YsqlConnMgrConf::CreateYsqlConnMgrConfigAndGetPath() {
std::to_string(FLAGS_ysql_conn_mgr_max_client_connections)},
{"{%ysql_port%}", std::to_string(postgres_address_.port())},
{"{%log_debug%}", BoolToString(log_debug_)},
{"{%log_config%}", BoolToString(log_config_)},
{"{%log_session%}", BoolToString(log_session_)},
{"{%log_query%}", BoolToString(log_query_)},
{"{%log_stats%}", BoolToString(log_stats_)},
{"{%logtostderr%}", FLAGS_logtostderr ? "yes" : "no"},
{"{%stats_interval%}", std::to_string(FLAGS_ysql_conn_mgr_stats_interval)},
{"{%server_lifetime%}", std::to_string(FLAGS_ysql_conn_mgr_server_lifetime)},
{"{%min_pool_size%}", std::to_string(FLAGS_ysql_conn_mgr_min_conns_per_db)},
{"{%yb_use_unix_socket%}", FLAGS_ysql_conn_mgr_use_unix_conn ? "" : "#"},
{"{%yb_use_tcp_socket%}", FLAGS_ysql_conn_mgr_use_unix_conn ? "#" : ""},
Expand Down Expand Up @@ -227,9 +258,6 @@ void YsqlConnMgrConf::UpdateConfigFromGFlags() {

CHECK_OK(postgres_address_.ParseString(
FLAGS_pgsql_proxy_bind_address, pgwrapper::PgProcessConf().kDefaultPort));

// Use the log level of tserver flag `minloglevel`.
log_debug_ = (GetAtomicFlag(&FLAGS_minloglevel) <= 2) ? true : false;
}

YsqlConnMgrConf::YsqlConnMgrConf(const std::string& data_path) {
Expand Down
2 changes: 1 addition & 1 deletion src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define YSQL_CONN_MGR_SHMEM_KEY_ENV_NAME "YSQL_CONN_MGR_STATS_KEY"
#define YSQL_CONN_MGR_WARMUP_DB "YB_YSQL_CONN_MGR_WARMUP_DB"
#define YSQL_CONN_MGR_MAX_POOLS 100
#define YSQL_CONN_MGR_MAX_POOLS 10000
#define DB_NAME_MAX_LEN 64
#define USER_NAME_MAX_LEN DB_NAME_MAX_LEN

Expand Down
44 changes: 43 additions & 1 deletion src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "yb/util/env_util.h"
#include "yb/util/flag_validators.h"
#include "yb/util/net/net_util.h"
#include "yb/util/string_trim.h"
#include "yb/util/path_util.h"
#include "yb/util/pg_util.h"

Expand Down Expand Up @@ -63,7 +64,7 @@ DEFINE_NON_RUNTIME_bool(ysql_conn_mgr_dowarmup, false,
"Enable precreation of server connections in Ysql Connection Manager. If set false, "
"the server connections are created lazily (on-demand) in Ysql Connection Manager.");

DEFINE_NON_RUNTIME_uint32(ysql_conn_mgr_stats_interval, 10,
DEFINE_NON_RUNTIME_uint32(ysql_conn_mgr_stats_interval, 1,
"Interval (in secs) at which the stats for Ysql Connection Manager will be updated.");

DEFINE_NON_RUNTIME_uint32(ysql_conn_mgr_min_conns_per_db, 1,
Expand All @@ -76,6 +77,47 @@ DEFINE_NON_RUNTIME_bool(ysql_conn_mgr_use_unix_conn, true,
"'local all yugabyte trust' in hba.conf (set ysql_hba_conf_csv as 'local all yugabyte trust')."
);

DEFINE_NON_RUNTIME_uint32(ysql_conn_mgr_server_lifetime, 3600,
"Specifies the maximum duration (in seconds) that a backend PostgreSQL connection "
"managed by Ysql Connection Manager can remain open after its creation. Once this time "
"is reached, the connection is automatically closed, regardless of activity, ensuring that "
"fresh backend connections are regularly maintained.");

DEFINE_NON_RUNTIME_string(ysql_conn_mgr_log_settings, "",
"Comma-separated list of log settings for Ysql Connection Manger, which may include "
"'log_debug', 'log_config', 'log_session', 'log_query', and 'log_stats'. Only the "
"log settings present in this string will be enabled. Omitted settings will remain disabled.");
namespace {

bool ValidateLogSettings(const char* flag_name, const std::string& value) {
const std::unordered_set<std::string> valid_settings = {
"log_debug", "log_config", "log_session", "log_query", "log_stats"
};

std::stringstream ss(value);
std::string setting;
std::unordered_set<std::string> seen_settings;

while (std::getline(ss, setting, ',')) {
setting = yb::util::TrimStr(setting);

if (setting.empty()) {
continue;
}
if (valid_settings.find(setting) == valid_settings.end()) {
LOG_FLAG_VALIDATION_ERROR(flag_name, value)
<< "Invalid log setting '" << setting << "'. Valid options are: "
<< "'log_debug', 'log_config', 'log_session', 'log_query', and 'log_stats'.";
return false;
}
}
return true;
}

} // namespace

DEFINE_validator(ysql_conn_mgr_log_settings, &ValidateLogSettings);

namespace yb {
namespace ysql_conn_mgr_wrapper {

Expand Down
5 changes: 5 additions & 0 deletions src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ class YsqlConnMgrConf : public yb::ProcessWrapperCommonConfig {
uint num_resolver_threads_ = 1;

bool log_debug_ = false;
bool log_config_ = false;
bool log_session_ = false;
bool log_query_ = false;
bool log_stats_ = false;

void UpdateConfigFromGFlags();
std::string GetBindAddress();
void AddSslConfig(std::map<std::string, std::string>* ysql_conn_mgr_configs);
void UpdateLogSettings(std::string& log_settings_str);
};

class YsqlConnMgrWrapper : public yb::ProcessWrapper {
Expand Down

0 comments on commit 9086ea7

Please sign in to comment.