forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_http.go
573 lines (517 loc) · 14.8 KB
/
conn_http.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package clickhouse
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"context"
"database/sql/driver"
"fmt"
"io"
"log"
"mime/multipart"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/ClickHouse/clickhouse-go/v2/resources"
"github.com/ClickHouse/ch-go/compress"
chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"github.com/andybalholm/brotli"
"github.com/pkg/errors"
)
const (
quotaKeyParamName = "quota_key"
queryIDParamName = "query_id"
)
type Pool[T any] struct {
pool *sync.Pool
}
func NewPool[T any](fn func() T) Pool[T] {
return Pool[T]{
pool: &sync.Pool{New: func() any { return fn() }},
}
}
func (p *Pool[T]) Get() T {
return p.pool.Get().(T)
}
func (p *Pool[T]) Put(x T) {
p.pool.Put(x)
}
type HTTPReaderWriter struct {
reader io.Reader
writer io.WriteCloser
err error
method CompressionMethod
}
// NewReader will return a reader that will decompress data if needed.
func (rw *HTTPReaderWriter) NewReader(res *http.Response) (io.Reader, error) {
enc := res.Header.Get("Content-Encoding")
if !res.Uncompressed && rw.method.String() == enc {
switch rw.method {
case CompressionGZIP:
reader := rw.reader.(*gzip.Reader)
if err := reader.Reset(res.Body); err != nil {
return nil, err
}
return reader, nil
case CompressionDeflate:
reader := rw.reader
if err := reader.(flate.Resetter).Reset(res.Body, nil); err != nil {
return nil, err
}
return reader, nil
case CompressionBrotli:
reader := rw.reader.(*brotli.Reader)
if err := reader.Reset(res.Body); err != nil {
return nil, err
}
return reader, nil
}
}
return res.Body, nil
}
func (rw *HTTPReaderWriter) reset(pw *io.PipeWriter) io.WriteCloser {
switch rw.method {
case CompressionGZIP:
rw.writer.(*gzip.Writer).Reset(pw)
return rw.writer
case CompressionDeflate:
rw.writer.(*zlib.Writer).Reset(pw)
return rw.writer
case CompressionBrotli:
rw.writer.(*brotli.Writer).Reset(pw)
return rw.writer
default:
return pw
}
}
func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpConnect, error) {
var debugf = func(format string, v ...any) {}
if opt.Debug {
if opt.Debugf != nil {
debugf = func(format string, v ...any) {
opt.Debugf(
"[clickhouse][conn=%d][%s] "+format,
append([]interface{}{num, addr}, v...)...,
)
}
} else {
debugf = log.New(os.Stdout, fmt.Sprintf("[clickhouse][conn=%d][%s]", num, addr), 0).Printf
}
}
if opt.scheme == "" {
switch opt.Protocol {
case HTTP:
opt.scheme = opt.Protocol.String()
if opt.TLS != nil {
opt.scheme = fmt.Sprintf("%ss", opt.scheme)
}
default:
return nil, errors.New("invalid interface type for http")
}
}
u := &url.URL{
Scheme: opt.scheme,
Host: addr,
Path: opt.HttpUrlPath,
}
headers := make(map[string]string)
for k, v := range opt.HttpHeaders {
headers[k] = v
}
if opt.TLS == nil && len(opt.Auth.Username) > 0 {
if len(opt.Auth.Password) > 0 {
u.User = url.UserPassword(opt.Auth.Username, opt.Auth.Password)
} else {
u.User = url.User(opt.Auth.Username)
}
} else if opt.TLS != nil && len(opt.Auth.Username) > 0 {
headers["X-ClickHouse-User"] = opt.Auth.Username
if len(opt.Auth.Password) > 0 {
headers["X-ClickHouse-Key"] = opt.Auth.Password
headers["X-ClickHouse-SSL-Certificate-Auth"] = "off"
} else {
headers["X-ClickHouse-SSL-Certificate-Auth"] = "on"
}
}
headers["User-Agent"] = opt.ClientInfo.String()
query := u.Query()
if len(opt.Auth.Database) > 0 {
query.Set("database", opt.Auth.Database)
}
if opt.Compression == nil {
opt.Compression = &Compression{
Method: CompressionNone,
}
}
compressionPool, err := createCompressionPool(opt.Compression)
if err != nil {
return nil, err
}
for k, v := range opt.Settings {
if cv, ok := v.(CustomSetting); ok {
v = cv.Value
}
query.Set(k, fmt.Sprint(v))
}
query.Set("default_format", "Native")
u.RawQuery = query.Encode()
httpProxy := http.ProxyFromEnvironment
if opt.HTTPProxyURL != nil {
httpProxy = http.ProxyURL(opt.HTTPProxyURL)
}
t := &http.Transport{
Proxy: httpProxy,
DialContext: (&net.Dialer{
Timeout: opt.DialTimeout,
}).DialContext,
MaxIdleConns: 1,
IdleConnTimeout: opt.ConnMaxLifetime,
ResponseHeaderTimeout: opt.ReadTimeout,
TLSClientConfig: opt.TLS,
}
if opt.DialContext != nil {
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return opt.DialContext(ctx, addr)
}
}
conn := &httpConnect{
client: &http.Client{
Transport: t,
},
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
blockBufferSize: opt.BlockBufferSize,
headers: headers,
}
location, err := conn.readTimeZone(ctx)
if err != nil {
return nil, err
}
if num == 1 {
version, err := conn.readVersion(ctx)
if err != nil {
return nil, err
}
if !resources.ClientMeta.IsSupportedClickHouseVersion(version) {
debugf("WARNING: version %v of ClickHouse is not supported by this client\n", version)
}
}
return &httpConnect{
client: &http.Client{
Transport: t,
},
url: u,
buffer: new(chproto.Buffer),
compression: opt.Compression.Method,
blockCompressor: compress.NewWriter(),
compressionPool: compressionPool,
location: location,
blockBufferSize: opt.BlockBufferSize,
headers: headers,
}, nil
}
type httpConnect struct {
url *url.URL
client *http.Client
location *time.Location
buffer *chproto.Buffer
compression CompressionMethod
blockCompressor *compress.Writer
compressionPool Pool[HTTPReaderWriter]
blockBufferSize uint8
headers map[string]string
}
func (h *httpConnect) isBad() bool {
return h.client == nil
}
func (h *httpConnect) readTimeZone(ctx context.Context) (*time.Location, error) {
rows, err := h.query(Context(ctx, ignoreExternalTables()), func(*connect, error) {}, "SELECT timezone()")
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, errors.New("unable to determine server timezone")
}
var serverLocation string
if err := rows.Scan(&serverLocation); err != nil {
return nil, err
}
location, err := time.LoadLocation(serverLocation)
if err != nil {
return nil, err
}
return location, nil
}
func (h *httpConnect) readVersion(ctx context.Context) (proto.Version, error) {
rows, err := h.query(Context(ctx, ignoreExternalTables()), func(*connect, error) {}, "SELECT version()")
if err != nil {
return proto.Version{}, err
}
if !rows.Next() {
return proto.Version{}, errors.New("unable to determine version")
}
var v string
if err := rows.Scan(&v); err != nil {
return proto.Version{}, err
}
version := proto.ParseVersion(v)
return version, nil
}
func createCompressionPool(compression *Compression) (Pool[HTTPReaderWriter], error) {
pool := NewPool(func() HTTPReaderWriter {
switch compression.Method {
case CompressionGZIP:
// trick so we can init the reader to something to Reset when we reuse
writer, err := gzip.NewWriterLevel(io.Discard, compression.Level)
if err != nil {
return HTTPReaderWriter{err: err}
}
b := new(bytes.Buffer)
writer.Reset(b)
writer.Flush()
writer.Close()
reader, err := gzip.NewReader(bytes.NewReader(b.Bytes()))
return HTTPReaderWriter{writer: writer, reader: reader, err: err, method: compression.Method}
case CompressionDeflate:
writer, err := zlib.NewWriterLevel(io.Discard, compression.Level)
if err != nil {
return HTTPReaderWriter{err: err}
}
b := new(bytes.Buffer)
writer.Reset(b)
writer.Flush()
writer.Close()
reader, err := zlib.NewReader(bytes.NewReader(b.Bytes()))
if err != nil {
return HTTPReaderWriter{err: err}
}
return HTTPReaderWriter{writer: writer, reader: reader, method: compression.Method}
case CompressionBrotli:
writer := brotli.NewWriterLevel(io.Discard, compression.Level)
b := new(bytes.Buffer)
writer.Reset(b)
writer.Flush()
writer.Close()
reader := brotli.NewReader(bytes.NewReader(b.Bytes()))
return HTTPReaderWriter{writer: writer, reader: reader, method: compression.Method}
default:
return HTTPReaderWriter{method: CompressionNone}
}
})
err := pool.Get().err
if err != nil {
return pool, err
}
return pool, nil
}
func (h *httpConnect) writeData(block *proto.Block) error {
// Saving offset of compressible data
start := len(h.buffer.Buf)
if err := block.Encode(h.buffer, 0); err != nil {
return err
}
if h.compression == CompressionLZ4 || h.compression == CompressionZSTD {
// Performing compression. Supported and requires
data := h.buffer.Buf[start:]
if err := h.blockCompressor.Compress(compress.Method(h.compression), data); err != nil {
return errors.Wrap(err, "compress")
}
h.buffer.Buf = append(h.buffer.Buf[:start], h.blockCompressor.Data...)
}
return nil
}
func (h *httpConnect) readData(reader *chproto.Reader, timezone *time.Location) (*proto.Block, error) {
location := h.location
if timezone != nil {
location = timezone
}
block := proto.Block{Timezone: location}
if h.compression == CompressionLZ4 || h.compression == CompressionZSTD {
reader.EnableCompression()
defer reader.DisableCompression()
}
if err := block.Decode(reader, 0); err != nil {
return nil, err
}
return &block, nil
}
func (h *httpConnect) sendStreamQuery(ctx context.Context, r io.Reader, options *QueryOptions, headers map[string]string) (*http.Response, error) {
req, err := h.createRequest(ctx, h.url.String(), r, options, headers)
if err != nil {
return nil, err
}
res, err := h.executeRequest(req)
if err != nil {
return nil, err
}
return res, nil
}
func (h *httpConnect) sendQuery(ctx context.Context, query string, options *QueryOptions, headers map[string]string) (*http.Response, error) {
req, err := h.prepareRequest(ctx, query, options, headers)
if err != nil {
return nil, err
}
res, err := h.executeRequest(req)
if err != nil {
return nil, err
}
return res, nil
}
func (h *httpConnect) readRawResponse(response *http.Response) (body []byte, err error) {
rw := h.compressionPool.Get()
defer h.compressionPool.Put(rw)
reader, err := rw.NewReader(response)
if err != nil {
return nil, err
}
if h.compression == CompressionLZ4 || h.compression == CompressionZSTD {
chReader := chproto.NewReader(reader)
chReader.EnableCompression()
reader = chReader
}
body, err = io.ReadAll(reader)
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
return body, nil
}
func (h *httpConnect) createRequest(ctx context.Context, requestUrl string, reader io.Reader, options *QueryOptions, headers map[string]string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestUrl, reader)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Add(k, v)
}
var query url.Values
if options != nil {
query = req.URL.Query()
if options.queryID != "" {
query.Set(queryIDParamName, options.queryID)
}
if options.quotaKey != "" {
query.Set(quotaKeyParamName, options.quotaKey)
}
for key, value := range options.settings {
// check that query doesn't change format
if key == "default_format" {
continue
}
if cv, ok := value.(CustomSetting); ok {
value = cv.Value
}
query.Set(key, fmt.Sprint(value))
}
for key, value := range options.parameters {
query.Set(fmt.Sprintf("param_%s", key), value)
}
req.URL.RawQuery = query.Encode()
}
return req, nil
}
func (h *httpConnect) prepareRequest(ctx context.Context, query string, options *QueryOptions, headers map[string]string) (*http.Request, error) {
if options == nil || len(options.external) == 0 {
return h.createRequest(ctx, h.url.String(), strings.NewReader(query), options, headers)
}
return h.createRequestWithExternalTables(ctx, query, options, headers)
}
func (h *httpConnect) createRequestWithExternalTables(ctx context.Context, query string, options *QueryOptions, headers map[string]string) (*http.Request, error) {
payload := &bytes.Buffer{}
w := multipart.NewWriter(payload)
currentUrl := new(url.URL)
*currentUrl = *h.url
queryValues := currentUrl.Query()
buf := &chproto.Buffer{}
for _, table := range options.external {
tableName := table.Name()
queryValues.Set(fmt.Sprintf("%v_format", tableName), "Native")
queryValues.Set(fmt.Sprintf("%v_structure", tableName), table.Structure())
partWriter, err := w.CreateFormFile(tableName, "")
if err != nil {
return nil, err
}
buf.Reset()
err = table.Block().Encode(buf, 0)
if err != nil {
return nil, err
}
_, err = partWriter.Write(buf.Buf)
if err != nil {
return nil, err
}
}
currentUrl.RawQuery = queryValues.Encode()
err := w.WriteField("query", query)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
headers["Content-Type"] = w.FormDataContentType()
return h.createRequest(ctx, currentUrl.String(), bytes.NewReader(payload.Bytes()), options, headers)
}
func (h *httpConnect) executeRequest(req *http.Request) (*http.Response, error) {
if h.client == nil {
return nil, driver.ErrBadConn
}
resp, err := h.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
msg, err := h.readRawResponse(resp)
if err != nil {
return nil, fmt.Errorf("clickhouse [execute]:: %d code: failed to read the response: %w", resp.StatusCode, err)
}
return nil, fmt.Errorf("clickhouse [execute]:: %d code: %s", resp.StatusCode, string(msg))
}
return resp, nil
}
func (h *httpConnect) ping(ctx context.Context) error {
rows, err := h.query(Context(ctx, ignoreExternalTables()), nil, "SELECT 1")
if err != nil {
return err
}
column := rows.Columns()
// check that we got column 1
if len(column) == 1 && column[0] == "1" {
return nil
}
return errors.New("clickhouse [ping]:: cannot ping clickhouse")
}
func (h *httpConnect) close() error {
if h.client == nil {
return nil
}
h.client.CloseIdleConnections()
h.client = nil
return nil
}