diff --git a/src/yb/tserver/tablet_server_main_impl.cc b/src/yb/tserver/tablet_server_main_impl.cc index 61894eb96409..485e7afc93b2 100644 --- a/src/yb/tserver/tablet_server_main_impl.cc +++ b/src/yb/tserver/tablet_server_main_impl.cc @@ -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"; diff --git a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr.template.conf b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr.template.conf index a090237753b9..b8cd21de301b 100644 --- a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr.template.conf +++ b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr.template.conf @@ -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 @@ -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%} diff --git a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_conf.cc b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_conf.cc index 965eb04a9b49..cdce6e28675a 100644 --- a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_conf.cc +++ b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_conf.cc @@ -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" @@ -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 { @@ -138,8 +141,31 @@ void YsqlConnMgrConf::AddSslConfig(std::map* 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 ysql_conn_mgr_configs = { @@ -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 ? "#" : ""}, @@ -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) { diff --git a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_stats.h b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_stats.h index 45208f0c3beb..47316805b34d 100644 --- a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_stats.h +++ b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_stats.h @@ -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 diff --git a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.cc b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.cc index d18d9d7d1a48..c799f4b06195 100644 --- a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.cc +++ b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.cc @@ -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" @@ -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, @@ -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 valid_settings = { + "log_debug", "log_config", "log_session", "log_query", "log_stats" + }; + + std::stringstream ss(value); + std::string setting; + std::unordered_set 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 { diff --git a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.h b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.h index 05901ec30a09..027e9212e354 100644 --- a/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.h +++ b/src/yb/yql/ysql_conn_mgr_wrapper/ysql_conn_mgr_wrapper.h @@ -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* ysql_conn_mgr_configs); + void UpdateLogSettings(std::string& log_settings_str); }; class YsqlConnMgrWrapper : public yb::ProcessWrapper {