-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhouse.go
218 lines (186 loc) · 5.19 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
package main
/* vim: set ts=2 sw=2 sts=2 ff=unix ft=go noet: */
import (
"context"
"database/sql"
"fmt"
"math"
"regexp"
"strings"
"time"
"github.com/kshvakov/clickhouse"
)
// need update by request DDL from clickhouse for table values
var clickhouseMetricCount = 12
var clickhouseMetrics = make(map[string]struct{})
var clickhouseDb *sql.DB
var clickhouseCtx = context.Background()
const clickhouseMetricType = "Nullable(Float32)"
func clickhouseCheckFieldName(name string) (result bool) {
re := regexp.MustCompile(`^[^a-zA-Z0-9:\-_]+$`)
result = re.MatchString(name)
return !result
}
func clickhouseAddMetric(fieldName, fieldType string) {
if fieldType == clickhouseMetricType {
clickhouseMetrics[fieldName] = struct{}{}
clickhouseMetricCount = len(clickhouseMetrics)
log.Info("Metric init ", fieldName)
} else {
verbosePrint("Ignore [" + fieldName + "] cuz fieldType = " + fieldType)
}
}
// Get clickhouseMetricCount from real batabase table structure
func clickhouseInitMetrics() {
rows, err := clickhouseDb.QueryContext(clickhouseCtx, "DESCRIBE TABLE `metrics`")
if err != nil {
log.Fatal(err)
}
defer func() {
e := rows.Close()
if e != nil {
log.Fatal(e)
}
}()
cols, err := rows.Columns()
if err != nil {
log.Fatal("ClickHouse get columns ERR:", err)
}
colCount := len(cols)
verbosePrint(fmt.Sprintf("DESCRIBE Cols: %d", colCount))
vals := make([]string, colCount)
dist := make([]interface{}, colCount)
for i := range cols {
dist[i] = &vals[i]
}
for rows.Next() {
if err := rows.Scan(dist...); err != nil {
log.Fatal(err)
}
if dist[0] != nil && dist[1] != nil {
clickhouseAddMetric(vals[0], vals[1])
}
}
verbosePrint(fmt.Sprintf("clickhouseMetricCount = %d", clickhouseMetricCount))
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
func clickhouseConnect() {
dburl := fmt.Sprintf("tcp://%s:%d?username=%s&password=%s&database=%s&read_timeout=10&write_timeout=20&debug=true",
config.ClickHouse.Host,
config.ClickHouse.Port,
config.ClickHouse.User,
config.ClickHouse.Pass,
config.ClickHouse.Name,
)
var err error
clickhouseDb, err = sql.Open("clickhouse", dburl)
if err != nil {
log.Fatal(err)
}
//defer clickhouseDb.Close()
clickhouseInitMetrics()
}
func clickhousePing() bool {
if err := clickhouseDb.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
log.Errorf("clickhousePing ERR: [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
log.Error("clickhousePing ERR:", err)
}
return false
}
return true
}
func clickhouseWaitConnection() {
var delay = 500
var factor = 1.5
var maxsleep = 10000
for !clickhousePing() {
// TODO save rquest for feature send it to database
time.Sleep(time.Duration(delay) * time.Millisecond)
delay = int(math.Ceil(float64(delay) * factor))
if delay > maxsleep {
delay = maxsleep
}
}
}
func clickhouseCreateMetric(name string) (ok bool) {
if ok := clickhouseCheckFieldName(name); ok {
_, err := clickhouseDb.Exec("ALTER TABLE `metrics` ADD COLUMN `$1` "+clickhouseMetricType, name)
if err != nil {
log.Errorf("Cant ADD COLUMN [%s] to database: %v", name, err)
return false
}
// TODO check error inside clickhouseAddMetric
clickhouseAddMetric(name, clickhouseMetricType)
} else {
log.Warnf("Invalid COLUMN name [%s]", name)
return false
}
return true
}
func clickhouseMetricInsert(timestamp int64, row map[string]float64) {
clickhouseWaitConnection()
// result
sqlFields := make([]string, 0, clickhouseMetricCount+1)
sqlNames := make([]string, 0, clickhouseMetricCount+1)
vals := []interface{}{timestamp}
for key, val := range row {
// check key name column exist in our ckickhouse DDL
if _, ok := clickhouseMetrics[key]; !ok {
// if not exist - create it by ALTER TABLE values ADD key float8
if ok := clickhouseCreateMetric(key); !ok {
// TODO save request for feature send it to database (and return)
continue
}
}
log.Debugf("ROW [%s] %.3f", key, val)
//create insert here like this
sqlFields = append(sqlFields, fmt.Sprintf("`%s`", key))
sqlNames = append(sqlNames, "?")
vals = append(vals, val)
}
if len(sqlNames) > 0 {
/*
year, month, day := now.Date()
weekday := now.Weekday()
if weekday == 0 { weekday = 7 }
log.Debug(fmt.Sprintf("""
INSERT INTO `values` SET
`ctime` = %d,
`year` = %d,
`month` = %d,
`day` = %d,
`weekday` = %d,
`hour` = %d,
`minute` = %d,
%s""",timestamp, year, month, day, weekday, now.Hour(), now.Minute(), strings.Join(sql, ", ")))
*/
strSQL := "INSERT INTO `metrics`" +
" (`ctime`, " + strings.Join(sqlFields, ", ") + ")" +
" VALUES (?," + strings.Join(sqlNames, ",") + ")"
tx, err := clickhouseDb.Begin()
if err != nil {
log.Errorf("Cant Begin clickhouse tx: %v", err)
}
stmt, err := tx.Prepare(strSQL) // nolint: safesql
if err != nil {
log.Errorf("Cant Prepare clickhouse stmt: %v", err)
}
//defer tx.Rollback()
defer func() {
err := stmt.Close()
if err != nil {
log.Fatal(err)
}
}()
if _, err := stmt.Exec(vals...); err != nil {
log.Fatal(err)
}
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}
}