diff --git a/programs/client/Client.cpp b/programs/client/Client.cpp index a3f5c0ab1c7..3ca75ed98c0 100644 --- a/programs/client/Client.cpp +++ b/programs/client/Client.cpp @@ -496,7 +496,7 @@ void Client::connect() if (hosts_and_ports.empty()) { String host = config().getString("host", "localhost"); - UInt16 port = static_cast(ConnectionParameters::getPortFromConfig(config())); + UInt16 port = ConnectionParameters::getPortFromConfig(config()); hosts_and_ports.emplace_back(HostAndPort{host, port}); } @@ -1058,7 +1058,7 @@ void Client::addOptions(OptionsDescription & options_description) "Example of usage: '--host host1 --host host2 --port port2 --host host3 ...'" "Each '--port port' will be attached to the last seen host that doesn't have a port yet," "if there is no such host, the port will be attached to the next first host or to default host.") - ("port", po::value()->default_value(DBMS_DEFAULT_PORT), "server ports") + ("port", po::value(), "server ports") ; } @@ -1106,8 +1106,11 @@ void Client::processOptions(const OptionsDescription & options_description, = po::command_line_parser(hosts_and_ports_argument).options(options_description.hosts_and_ports_description.value()).run(); po::variables_map host_and_port_options; po::store(parsed_hosts_and_ports, host_and_port_options); - hosts_and_ports.emplace_back( - HostAndPort{host_and_port_options["host"].as(), host_and_port_options["port"].as()}); + std::string host = host_and_port_options["host"].as(); + std::optional port = !host_and_port_options["port"].empty() + ? std::make_optional(host_and_port_options["port"].as()) + : std::nullopt; + hosts_and_ports.emplace_back(HostAndPort{host, port}); } send_external_tables = true; diff --git a/src/Client/ClientBase.h b/src/Client/ClientBase.h index e625d4a5c63..4df4377d91f 100644 --- a/src/Client/ClientBase.h +++ b/src/Client/ClientBase.h @@ -256,7 +256,7 @@ protected: struct HostAndPort { String host; - UInt16 port; + std::optional port; }; std::vector hosts_and_ports{}; diff --git a/src/Client/ConnectionParameters.cpp b/src/Client/ConnectionParameters.cpp index 55569f080f6..3101176101b 100644 --- a/src/Client/ConnectionParameters.cpp +++ b/src/Client/ConnectionParameters.cpp @@ -25,7 +25,9 @@ namespace ErrorCodes ConnectionParameters::ConnectionParameters(const Poco::Util::AbstractConfiguration & config, std::string connection_host, - int connection_port) : host(connection_host), port(connection_port) + std::optional connection_port) + : host(connection_host) + , port(connection_port.value_or(getPortFromConfig(config))) { bool is_secure = config.getBool("secure", false); security = is_secure ? Protocol::Secure::Enable : Protocol::Secure::Disable; @@ -73,7 +75,7 @@ ConnectionParameters::ConnectionParameters(const Poco::Util::AbstractConfigurati { } -int ConnectionParameters::getPortFromConfig(const Poco::Util::AbstractConfiguration & config) +UInt16 ConnectionParameters::getPortFromConfig(const Poco::Util::AbstractConfiguration & config) { bool is_secure = config.getBool("secure", false); return config.getInt("port", diff --git a/src/Client/ConnectionParameters.h b/src/Client/ConnectionParameters.h index dc509049c83..7e4bbfb102a 100644 --- a/src/Client/ConnectionParameters.h +++ b/src/Client/ConnectionParameters.h @@ -24,9 +24,9 @@ struct ConnectionParameters ConnectionParameters() {} ConnectionParameters(const Poco::Util::AbstractConfiguration & config); - ConnectionParameters(const Poco::Util::AbstractConfiguration & config, std::string host, int port); + ConnectionParameters(const Poco::Util::AbstractConfiguration & config, std::string host, std::optional port); - static int getPortFromConfig(const Poco::Util::AbstractConfiguration & config); + static UInt16 getPortFromConfig(const Poco::Util::AbstractConfiguration & config); }; } diff --git a/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.reference b/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.reference index 40d20c08279..ec41ff681af 100644 --- a/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.reference +++ b/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.reference @@ -13,6 +13,10 @@ 1 1 1 +1 +1 +1 +1 === Code 210 with ipv6 1 1 @@ -23,6 +27,18 @@ === Values form config 1 1 +1 +1 +1 +1 +1 +1 +=== Values form config 2 +1 +1 +1 +1 +1 === 1 1 diff --git a/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.sh b/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.sh index 168b9be00c6..bbcc0128353 100755 --- a/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.sh +++ b/tests/queries/0_stateless/02100_multiple_hosts_command_line_set.sh @@ -32,6 +32,14 @@ error="$(${CLICKHOUSE_CLIENT} --host "${CLICKHOUSE_HOST}" --port "${not_alive_po echo "${error}" | grep -Fc "Code: 210" echo "${error}" | grep -Fc "${CLICKHOUSE_HOST}:${not_alive_port}" +error="$(${CLICKHOUSE_CLIENT} --host "${not_alive_host}" --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "${not_alive_host}:9000" + +error="$(${CLICKHOUSE_CLIENT} --secure --host "${not_alive_host}" --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "${not_alive_host}:9440" + ${CLICKHOUSE_CLIENT} --host "${CLICKHOUSE_HOST}" --port "${not_alive_port}" --host "${CLICKHOUSE_HOST}" --query "SELECT 1"; ${CLICKHOUSE_CLIENT} --host "${CLICKHOUSE_HOST}" --port "${CLICKHOUSE_PORT_TCP}" --port "${not_alive_port}" --query "SELECT 1"; @@ -67,6 +75,39 @@ EOF error="$(${CLICKHOUSE_CLIENT} --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" echo "${error}" | grep -Fc "DB::NetException" echo "${error}" | grep -Fc "${not_alive_host}:${not_alive_port}" + +error="$(${CLICKHOUSE_CLIENT} --secure --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "${not_alive_host}:${not_alive_port}" + +error="$(${CLICKHOUSE_CLIENT} --host localhost --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "localhost:${not_alive_port}" + +error="$(${CLICKHOUSE_CLIENT} --secure --host localhost --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "localhost:${not_alive_port}" + +rm -f ${CUSTOM_CONFIG} + +echo '=== Values form config 2' + +cat << EOF > ${CUSTOM_CONFIG} + + ${not_alive_host} + +EOF + +error="$(${CLICKHOUSE_CLIENT} --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "${not_alive_host}:9000" + +error="$(${CLICKHOUSE_CLIENT} --secure --config ${CUSTOM_CONFIG} --query "SELECT 1" 2>&1 > /dev/null)" +echo "${error}" | grep -Fc "DB::NetException" +echo "${error}" | grep -Fc "${not_alive_host}:9440" + +${CLICKHOUSE_CLIENT} --host "${CLICKHOUSE_HOST}" --config ${CUSTOM_CONFIG} --query "SELECT 1" + rm -f ${CUSTOM_CONFIG} echo '==='