-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhouse.go
211 lines (189 loc) · 5.15 KB
/
clickhouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func testConection(ctx context.Context, conn driver.Conn) {
rows, err := conn.Query(ctx, "SELECT hostname(), version()")
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var (
hostname, version string
)
if err := rows.Scan(
&hostname,
&version,
); err != nil {
log.Fatal(err)
}
log.Printf("Connected to %s with server version of %s", hostname, version)
}
}
func connectCloud() (driver.Conn, error) {
addr := fmt.Sprintf("%s:%d", viper.GetString("CLICKHOUSE_CLOUD_HOSTNAME"), viper.GetInt("CLICKHOUSE_CLOUD_PORT"))
log.Infoln("Connecting to ClickHouse Cloud at ", addr)
var (
ctx = context.Background()
conn, err = clickhouse.Open(&clickhouse.Options{
Addr: []string{addr},
Auth: clickhouse.Auth{
Database: viper.GetString("CLICKHOUSE_CLOUD_DATABASE"),
Username: viper.GetString("CLICKHOUSE_CLOUD_USERNAME"),
Password: viper.GetString("CLICKHOUSE_CLOUD_PASSWORD"),
},
ClientInfo: clickhouse.ClientInfo{
Products: []struct {
Name string
Version string
}{
{Name: "synch benchmark", Version: "0.1"},
},
},
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v)
},
Settings: clickhouse.Settings{
// "move_all_conditions_to_prewhere": 1,
// "enable_multiple_prewhere_read_steps": 0,
// "use_hedged_requests": 0,
// "allow_experimental_parallel_reading_from_replicas": 1,
// "max_parallel_replicas": 6,
// "parallel_replicas_single_task_marks_count_multiplier": 0.125,
},
TLS: &tls.Config{
InsecureSkipVerify: true,
},
})
)
if err != nil {
log.Errorln("Error connecting to ClickHouse Cloud")
return nil, err
}
if err := conn.Ping(ctx); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
log.Errorf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
}
log.Errorln("Error Pinging ClickHouse Cloud")
return nil, err
}
return conn, nil
}
func connectEU() (driver.Conn, error) {
addr := fmt.Sprintf("%s:%d", viper.GetString("CLICKHOUSE_EU_HOSTNAME"), viper.GetInt("CLICKHOUSE_EU_PORT"))
var (
ctx = context.Background()
conn, err = clickhouse.Open(&clickhouse.Options{
Addr: []string{addr},
Auth: clickhouse.Auth{
Database: viper.GetString("CLICKHOUSE_EU_DATABASE"),
Username: viper.GetString("CLICKHOUSE_EU_USERNAME"),
Password: viper.GetString("CLICKHOUSE_EU_PASSWORD"),
},
ClientInfo: clickhouse.ClientInfo{
Products: []struct {
Name string
Version string
}{
{Name: "an-example-go-client", Version: "0.1"},
},
},
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v)
},
TLS: &tls.Config{
InsecureSkipVerify: true,
},
})
)
if err != nil {
return nil, err
}
if err := conn.Ping(ctx); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
}
return nil, err
}
return conn, nil
}
func connectUS() (driver.Conn, error) {
addr := fmt.Sprintf("%s:%d", viper.GetString("CLICKHOUSE_US_HOSTNAME"), viper.GetInt("CLICKHOUSE_US_PORT"))
var (
ctx = context.Background()
conn, err = clickhouse.Open(&clickhouse.Options{
Addr: []string{addr},
Auth: clickhouse.Auth{
Database: viper.GetString("CLICKHOUSE_US_DATABASE"),
Username: viper.GetString("CLICKHOUSE_US_USERNAME"),
Password: viper.GetString("CLICKHOUSE_US_PASSWORD"),
},
ClientInfo: clickhouse.ClientInfo{
Products: []struct {
Name string
Version string
}{
{Name: "an-example-go-client", Version: "0.1"},
},
},
ReadTimeout: 300 * time.Minute,
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v)
},
TLS: &tls.Config{
InsecureSkipVerify: true,
},
})
)
if err != nil {
return nil, err
}
if err := conn.Ping(ctx); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
}
return nil, err
}
return conn, nil
}
func NewCHConn(url *string) (*sql.DB, error) {
db, err := sql.Open("clickhouse", *url)
if err != nil {
return nil, fmt.Errorf("getting clickhouse connection: %v", err)
}
err = retry(3, time.Second, func() error {
if _, err := db.Exec("SELECT 1"); err != nil {
return fmt.Errorf("trying to ping clickhouse: %s", err)
}
return nil
})
if err != nil {
return nil, err
}
return db, nil
}
func retry(attempts int, sleep time.Duration, f func() error) error {
if err := f(); err != nil {
if s, ok := err.(stop); ok {
// Return the original error for later checking
return s.error
}
if attempts--; attempts > 0 {
time.Sleep(sleep)
return retry(attempts, 2*sleep, f)
}
return err
}
return nil
}
type stop struct {
error
}