/
clickhouse.go
563 lines (500 loc) · 14.6 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
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
// PromHouse
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package storages
import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
"sync"
"time"
_ "github.com/kshvakov/clickhouse" // register SQL driver
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/sirupsen/logrus"
"github.com/Percona-Lab/PromHouse/prompb"
)
const (
namespace = "promhouse"
subsystem = "clickhouse"
sampleRowSize = 2 + 8 + 8 + 8
)
// ClickHouse implements storage interface for the ClickHouse.
type ClickHouse struct {
db *sql.DB
l *logrus.Entry
database string
timeSeriesRW sync.RWMutex
timeSeries map[uint64][]*prompb.Label
mTimeSeriesCurrent prometheus.Gauge
mSamplesCurrent prometheus.Gauge
mSamplesCurrentBytes prometheus.Gauge
mSamplesCurrentVirtualBytes prometheus.Gauge
mReads prometheus.Summary
mReadErrors *prometheus.CounterVec
mWrites prometheus.Summary
mWriteErrors *prometheus.CounterVec
mWrittenTimeSeries prometheus.Counter
mWrittenSamples prometheus.Counter
}
func NewClickHouse(dsn string, database string, drop bool) (*ClickHouse, error) {
l := logrus.WithField("component", "clickhouse")
var queries []string
if drop {
queries = append(queries, fmt.Sprintf(`DROP DATABASE IF EXISTS %s`, database))
}
queries = append(queries, fmt.Sprintf(`CREATE DATABASE IF NOT EXISTS %s`, database))
// TODO use GraphiteMergeTree?
// change sampleRowSize is you change this table
queries = append(queries, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s.samples (
date Date,
fingerprint UInt64,
timestamp_ms Int64,
value Float64
)
ENGINE = MergeTree(date, (fingerprint, timestamp_ms), 8192)`, database))
queries = append(queries, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s.time_series (
date Date,
fingerprint UInt64,
labels String
)
ENGINE = ReplacingMergeTree(date, fingerprint, 8192)`, database))
// we can't use database in DSN if it doesn't yet exist, so handle that in a special way
dsnURL, err := url.Parse(dsn)
if err != nil {
return nil, err
}
if dsnURL.Query().Get("database") != "" {
return nil, fmt.Errorf("database should no be set in ClickHouse dsn")
}
// init schema
initDB, err := sql.Open("clickhouse", dsn)
if err != nil {
return nil, err
}
defer initDB.Close()
for _, q := range queries {
l.Infof("Executing: %s", q)
if _, err = initDB.Exec(q); err != nil {
return nil, err
}
}
// reconnect to created database
q := dsnURL.Query()
q.Set("database", database)
dsnURL.RawQuery = q.Encode()
dsn = dsnURL.String()
db, err := sql.Open("clickhouse", dsn)
if err != nil {
return nil, err
}
ch := &ClickHouse{
db: db,
l: l,
database: database,
timeSeries: make(map[uint64][]*prompb.Label, 8192),
mTimeSeriesCurrent: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "time_series_current",
Help: "Current number of stored time series (rows).",
}),
mSamplesCurrent: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "samples_current",
Help: "Current number of stored samples.",
}),
mSamplesCurrentBytes: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "samples_current_bytes",
Help: "Current number of stored samples (bytes).",
}),
mSamplesCurrentVirtualBytes: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "samples_current_virtual_bytes",
Help: "Current number of stored samples (virtual uncompressed bytes).",
}),
mReads: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "reads",
Help: "Durations of successful reads.",
}),
mReadErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "read_errors",
Help: "Number of read errors by type: canceled, other.",
}, []string{"type"}),
mWrites: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "writes",
Help: "Durations of committed writes.",
}),
mWriteErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "write_errors",
Help: "Number of write errors by type: canceled, other.",
}, []string{"type"}),
mWrittenTimeSeries: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "written_time_series",
Help: "Number of written time series.",
}),
mWrittenSamples: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "written_samples",
Help: "Number of written samples.",
}),
}
go ch.runTimeSeriesReloader(context.TODO())
ch.mReadErrors.WithLabelValues("canceled").Set(0)
ch.mReadErrors.WithLabelValues("other").Set(0)
ch.mWriteErrors.WithLabelValues("canceled").Set(0)
ch.mWriteErrors.WithLabelValues("other").Set(0)
return ch, nil
}
func (ch *ClickHouse) runTimeSeriesReloader(ctx context.Context) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
q := fmt.Sprintf(`SELECT DISTINCT fingerprint, labels FROM %s.time_series`, ch.database)
for {
ch.timeSeriesRW.RLock()
timeSeries := make(map[uint64][]*prompb.Label, len(ch.timeSeries))
ch.timeSeriesRW.RUnlock()
err := func() error {
ch.l.Debug(q)
rows, err := ch.db.Query(q)
if err != nil {
return err
}
defer rows.Close()
var f uint64
var b []byte
for rows.Next() {
if err = rows.Scan(&f, &b); err != nil {
return err
}
if timeSeries[f], err = unmarshalLabels(b); err != nil {
return err
}
}
return rows.Err()
}()
if err == nil {
ch.timeSeriesRW.Lock()
n := len(timeSeries) - len(ch.timeSeries)
for f, m := range timeSeries {
ch.timeSeries[f] = m
}
ch.timeSeriesRW.Unlock()
ch.l.Debugf("Loaded %d existing time series, %d were unknown to this instance.", len(timeSeries), n)
} else {
ch.l.Error(err)
}
select {
case <-ctx.Done():
ch.l.Warn(ctx.Err)
return
case <-ticker.C:
}
}
}
func (ch *ClickHouse) Describe(c chan<- *prometheus.Desc) {
ch.mTimeSeriesCurrent.Describe(c)
ch.mSamplesCurrent.Describe(c)
ch.mSamplesCurrentBytes.Describe(c)
ch.mSamplesCurrentVirtualBytes.Describe(c)
ch.mReads.Describe(c)
ch.mReadErrors.Describe(c)
ch.mWrites.Describe(c)
ch.mWriteErrors.Describe(c)
ch.mWrittenTimeSeries.Describe(c)
ch.mWrittenSamples.Describe(c)
}
func (ch *ClickHouse) Collect(c chan<- prometheus.Metric) {
// TODO remove this when https://github.com/f1yegor/clickhouse_exporter/pull/13 is merged
// 'SELECT COUNT(*) FROM samples' is slow
query := `
SELECT table, sum(rows) AS rows, sum(bytes) AS bytes, (? * rows) AS virtual_bytes
FROM system.parts
WHERE database = ? AND active
GROUP BY table`
ch.l.Debugf("%s [%v, %v]", query, sampleRowSize, ch.database)
rows, err := ch.db.Query(query, sampleRowSize, ch.database)
if err != nil {
ch.l.Error(err)
return
}
defer rows.Close()
var table string
var r, b, vb uint64
for rows.Next() {
if err = rows.Scan(&table, &r, &b, &vb); err != nil {
ch.l.Error(err)
return
}
switch table {
case "time_series":
ch.mTimeSeriesCurrent.Set(float64(r))
// ignore b and vb
case "samples":
ch.mSamplesCurrent.Set(float64(r))
ch.mSamplesCurrentBytes.Set(float64(b))
ch.mSamplesCurrentVirtualBytes.Set(float64(vb))
default:
ch.l.Errorf("unexpected table %q", table)
}
}
if err = rows.Err(); err != nil {
ch.l.Error(err)
}
ch.mTimeSeriesCurrent.Collect(c)
ch.mSamplesCurrent.Collect(c)
ch.mSamplesCurrentBytes.Collect(c)
ch.mSamplesCurrentVirtualBytes.Collect(c)
ch.mReads.Collect(c)
ch.mReadErrors.Collect(c)
ch.mWrites.Collect(c)
ch.mWriteErrors.Collect(c)
ch.mWrittenTimeSeries.Collect(c)
ch.mWrittenSamples.Collect(c)
}
func (ch *ClickHouse) Read(ctx context.Context, queries []Query) (res *prompb.ReadResponse, err error) {
// track time and response status
start := time.Now()
defer func() {
if err == nil {
ch.mReads.Observe(time.Since(start).Seconds())
return
}
t := "other"
if err == context.Canceled {
t = "canceled"
}
ch.mReadErrors.WithLabelValues(t).Inc()
}()
// special case for {{job="rawsql", query="SELECT …"}} (start is ignored)
if len(queries) == 1 && len(queries[0].Matchers) == 2 {
var query string
var hasJob bool
for _, m := range queries[0].Matchers {
if m.Type == MatchEqual && m.Name == "job" && m.Value == "rawsql" {
hasJob = true
}
if m.Type == MatchEqual && m.Name == "query" {
query = m.Value
}
}
if hasJob && query != "" {
return ch.readRawSQL(ctx, query, int64(queries[0].End))
}
}
res = &prompb.ReadResponse{
Results: make([]*prompb.QueryResult, len(queries)),
}
for i, q := range queries {
res.Results[i] = new(prompb.QueryResult)
// find matching time series
fingerprints := make(map[uint64]struct{}, 64)
ch.timeSeriesRW.RLock()
for f, labels := range ch.timeSeries {
if q.Matchers.MatchLabels(labels) {
fingerprints[f] = struct{}{}
}
}
ch.timeSeriesRW.RUnlock()
if len(fingerprints) == 0 {
continue
}
placeholders := strings.Repeat("?, ", len(fingerprints))
query := fmt.Sprintf(`
SELECT fingerprint, timestamp_ms, value
FROM %s.samples
WHERE fingerprint IN (%s) AND timestamp_ms >= ? AND timestamp_ms <= ?
ORDER BY fingerprint, timestamp_ms`,
ch.database, placeholders[:len(placeholders)-2], // cut last ", "
)
args := make([]interface{}, 0, len(fingerprints)+2)
for f := range fingerprints {
args = append(args, f)
}
args = append(args, int64(q.Start))
args = append(args, int64(q.End))
err = func() error {
ch.l.Debugf("%s %v", query, args)
rows, err := ch.db.Query(query, args...)
if err != nil {
return errors.WithStack(err)
}
defer rows.Close()
var ts *prompb.TimeSeries
var fingerprint, prevFingerprint uint64
var timestampMs int64
var value float64
for rows.Next() {
if err = rows.Scan(&fingerprint, ×tampMs, &value); err != nil {
return errors.WithStack(err)
}
if fingerprint != prevFingerprint {
prevFingerprint = fingerprint
if ts != nil {
res.Results[i].TimeSeries = append(res.Results[i].TimeSeries, ts)
}
ch.timeSeriesRW.RLock()
labels := ch.timeSeries[fingerprint]
ch.timeSeriesRW.RUnlock()
ts = &prompb.TimeSeries{
Labels: labels,
}
}
ts.Samples = append(ts.Samples, &prompb.Sample{
TimestampMs: timestampMs,
Value: value,
})
}
if ts != nil {
res.Results[i].TimeSeries = append(res.Results[i].TimeSeries, ts)
}
return errors.WithStack(rows.Err())
}()
if err != nil {
return
}
}
return
}
func inTransaction(ctx context.Context, db *sql.DB, f func(*sql.Tx) error) (err error) {
var tx *sql.Tx
if tx, err = db.BeginTx(ctx, nil); err != nil {
return
}
defer func() {
if err == nil {
err = tx.Commit()
} else {
tx.Rollback()
}
}()
err = f(tx)
return
}
func (ch *ClickHouse) Write(ctx context.Context, data *prompb.WriteRequest) (err error) {
// track time and response status
start := time.Now()
defer func() {
if err == nil {
ch.mWrites.Observe(time.Since(start).Seconds())
return
}
t := "other"
if err == context.Canceled {
t = "canceled"
}
ch.mWriteErrors.WithLabelValues(t).Inc()
}()
// calculate fingerprints, map them to time series
fingerprints := make([]uint64, len(data.TimeSeries))
timeSeries := make(map[uint64][]*prompb.Label, len(data.TimeSeries))
for i, ts := range data.TimeSeries {
sortLabels(ts.Labels)
f := fingerprint(ts.Labels)
fingerprints[i] = f
timeSeries[f] = ts.Labels
}
if len(fingerprints) != len(timeSeries) {
ch.l.Debugf("got %d fingerprints, but only %d of them were unique time series", len(fingerprints), len(timeSeries))
}
// find new time series
var newTimeSeries []uint64
ch.timeSeriesRW.Lock()
for f, m := range timeSeries {
_, ok := ch.timeSeries[f]
if !ok {
newTimeSeries = append(newTimeSeries, f)
ch.timeSeries[f] = m
}
}
ch.timeSeriesRW.Unlock()
// write new time series
if len(newTimeSeries) > 0 {
err = inTransaction(ctx, ch.db, func(tx *sql.Tx) error {
query := fmt.Sprintf(`INSERT INTO %s.time_series (date, fingerprint, labels) VALUES (?, ?, ?)`, ch.database)
var stmt *sql.Stmt
if stmt, err = tx.PrepareContext(ctx, query); err != nil {
return errors.WithStack(err)
}
args := make([]interface{}, 3)
args[0] = model.Now().Time()
for _, f := range newTimeSeries {
args[1] = f
args[2] = marshalLabels(timeSeries[f], make([]byte, 0, 128)) // TODO use pool?
ch.l.Debugf("%s %v", query, args)
if _, err = stmt.ExecContext(ctx, args...); err != nil {
return errors.WithStack(err)
}
}
return stmt.Close()
})
if err != nil {
return
}
}
// write samples
var samples int
err = inTransaction(ctx, ch.db, func(tx *sql.Tx) error {
query := fmt.Sprintf(`INSERT INTO %s.samples (date, fingerprint, timestamp_ms, value) VALUES (?, ?, ?, ?)`, ch.database)
var stmt *sql.Stmt
if stmt, err = tx.PrepareContext(ctx, query); err != nil {
return errors.WithStack(err)
}
args := make([]interface{}, 4)
for i, ts := range data.TimeSeries {
args[1] = fingerprints[i]
for _, s := range ts.Samples {
args[0] = model.Time(s.TimestampMs).Time()
args[2] = s.TimestampMs
args[3] = s.Value
ch.l.Debugf("%s %v", query, args)
if _, err = stmt.ExecContext(ctx, args...); err != nil {
return errors.WithStack(err)
}
samples++
}
}
return stmt.Close()
})
if err != nil {
return
}
ch.mWrittenTimeSeries.Add(float64(len(newTimeSeries)))
ch.mWrittenSamples.Add(float64(samples))
ch.l.Debugf("Wrote %d new time series, %d samples.", len(newTimeSeries), samples)
return
}
// check interface
var _ Storage = (*ClickHouse)(nil)