From f12cc0b4e2edef96ca6272819fcdf34b721e27da Mon Sep 17 00:00:00 2001 From: Anshul Khandelwal <12948312+k-anshul@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:38:48 +0530 Subject: [PATCH] switch to http port if connection with native port failed --- runtime/drivers/clickhouse/clickhouse.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/runtime/drivers/clickhouse/clickhouse.go b/runtime/drivers/clickhouse/clickhouse.go index dccbf273ef9..1e02c21e527 100644 --- a/runtime/drivers/clickhouse/clickhouse.go +++ b/runtime/drivers/clickhouse/clickhouse.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "strings" "github.com/ClickHouse/clickhouse-go/v2" "github.com/XSAM/otelsql" @@ -187,6 +188,24 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien } db := sqlx.NewDb(otelsql.OpenDB(clickhouse.Connector(opts)), "clickhouse") + err = db.Ping() + if err != nil { + if !strings.Contains(err.Error(), "unexpected packet") && !strings.Contains(err.Error(), "i/o timeout") { + return nil, err + } + if conf.DSN != "" { + return nil, err + } + // may be the port is http, also try with http protocol if DSN is not provided + opts.Protocol = clickhouse.HTTP + db = sqlx.NewDb(otelsql.OpenDB(clickhouse.Connector(opts)), "clickhouse") + err := db.Ping() + if err != nil { + return nil, err + } + // connection with http protocol is successful + logger.Warn("clickHouse connection is established with HTTP protocol. Use native port for better performance") + } // very roughly approximating num queries required for a typical page load // TODO: copied from druid reevaluate db.SetMaxOpenConns(maxOpenConnections) @@ -196,11 +215,6 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien return nil, fmt.Errorf("registering db stats metrics: %w", err) } - err = db.Ping() - if err != nil { - return nil, fmt.Errorf("connection: %w", err) - } - // group by positional args are supported post 22.7 and we use them heavily in our queries row := db.QueryRow(` WITH