diff --git a/quesma/clickhouse/connection.go b/quesma/clickhouse/connection.go index 1f2af9ccb..bd31b55fa 100644 --- a/quesma/clickhouse/connection.go +++ b/quesma/clickhouse/connection.go @@ -113,9 +113,7 @@ func InitDBConnectionPool(c *config.QuesmaConfiguration) *sql.DB { // in case of misconfigured ClickHouse connection. In the future, we might rethink how do we manage this and perhaps // move some parts to InitDBConnectionPool, but for now this should already provide some useful feedback. func RunClickHouseConnectionDoctor(c *config.QuesmaConfiguration) { - timeout := 1 * time.Second - defaultNativeProtocolPort := "9000" - defaultNativeProtocolPortEncrypted := "9440" + timeout := 5 * time.Second logger.Info().Msgf("[connection-doctor] Starting ClickHouse connection doctor") hostName, port := c.ClickHouse.Url.Hostname(), c.ClickHouse.Url.Port() @@ -124,24 +122,50 @@ func RunClickHouseConnectionDoctor(c *config.QuesmaConfiguration) { connTcp, errTcp := net.DialTimeout("tcp", address, timeout) if errTcp != nil { logger.Info().Msgf("[connection-doctor] Failed dialing with %s, err=[%v], no service listening at configured host/port, make sure to specify reachable ClickHouse address", address, errTcp) - logger.Info().Msgf("[connection-doctor] Trying default ClickHouse native ports...") - if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultNativeProtocolPort), timeout); err == nil { - logger.Info().Msgf("[connection-doctor] Default ClickHouse plaintext port is reachable, consider changing ClickHouse port to %s in Quesma configuration", defaultNativeProtocolPort) - conn.Close() - } - if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultNativeProtocolPortEncrypted), timeout); err == nil { - logger.Info().Msgf("[connection-doctor] Default ClickHouse TLS port is reachable, consider changing ClickHouse port to %s in Quesma conbfiguration", defaultNativeProtocolPortEncrypted) - conn.Close() - } + tryDefaultPorts(hostName, timeout) return } + logger.Info().Msgf("[connection-doctor] Successfully dialed host/port (%s)...", address) defer connTcp.Close() - logger.Info().Msgf("[connection-doctor] Trying to establish TLS connection with configured host/port (%s)", address) - connTls, errTls := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", address, &tls.Config{InsecureSkipVerify: true}) - if errTls != nil { - logger.Info().Msgf("[connection-doctor] Failed establishing TLS connection with %s, err=[%v], please use `clickhouse.disableTLS: true` in Quesma configuration", address, errTls) - return + + if !c.ClickHouse.DisableTLS { + logger.Info().Msgf("[connection-doctor] Trying to establish TLS connection with configured host/port (%s)", address) + connTls, errTls := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", address, &tls.Config{InsecureSkipVerify: true}) + if errTls != nil { + logger.Info().Msgf("[connection-doctor] Failed establishing TLS connection with %s, err=[%v], please use `config.disableTLS: true` in Quesma configuration of ClickHouse backend connector", address, errTls) + return + } + defer connTls.Close() + logger.Info().Msgf("[connection-doctor] TLS connection (handshake) with %s established successfully", address) + logger.Info().Msgf("[connection-doctor] Make sure you are using the correct protocol (currently: %s), correct username/password and correct database (currently: '%s')", c.ClickHouse.Url.Scheme, c.ClickHouse.Database) + } else { + logger.Info().Msgf("[connection-doctor] TLS connection is disabled in Quesma configuration (consider trying `config.disableTLS: false` in Quesma configuration of ClickHouse backend connector), skipping TLS connection tests.") + logger.Info().Msgf("[connection-doctor] Make sure you are using the correct protocol (currently: %s), correct username/password and correct database (currently: '%s')", c.ClickHouse.Url.Scheme, c.ClickHouse.Database) + tryDefaultPorts(hostName, timeout) + } +} + +func tryDefaultPorts(hostName string, timeout time.Duration) { + defaultNativeProtocolPort := "9000" + defaultNativeProtocolPortEncrypted := "9440" + defaultHttpPort := "8123" + defaultHttpsPort := "8443" + + logger.Info().Msgf("[connection-doctor] Trying default ClickHouse native ports...") + if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultNativeProtocolPort), timeout); err == nil { + logger.Info().Msgf("[connection-doctor] Default ClickHouse plaintext port is reachable, consider changing the ClickHouse URL in Quesma configuration to clickhouse://%s:%s", hostName, defaultNativeProtocolPort) + conn.Close() + } + if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultNativeProtocolPortEncrypted), timeout); err == nil { + logger.Info().Msgf("[connection-doctor] Default ClickHouse TLS port is reachable, consider changing the ClickHouse URL in Quesma configuration to clickhouse://%s:%s", hostName, defaultNativeProtocolPortEncrypted) + conn.Close() + } + if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultHttpPort), timeout); err == nil { + logger.Info().Msgf("[connection-doctor] Default ClickHouse HTTP port is reachable, consider changing the ClickHouse URL in Quesma configuration to http://%s:%s", hostName, defaultHttpPort) + conn.Close() + } + if conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", hostName, defaultHttpsPort), timeout); err == nil { + logger.Info().Msgf("[connection-doctor] Default ClickHouse HTTPS port is reachable, consider changing the ClickHouse URL in Quesma configuration to https://%s:%s", hostName, defaultHttpsPort) + conn.Close() } - defer connTls.Close() - logger.Info().Msgf("[connection-doctor] TLS connection (handshake) with %s established successfully", address) }