-
Notifications
You must be signed in to change notification settings - Fork 226
/
clickhouse.go
394 lines (368 loc) · 11.3 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
package main
import (
"strconv"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/jmoiron/sqlx"
_ "github.com/kshvakov/clickhouse"
)
// ClickHouse - provide info and freeze tables
type ClickHouse struct {
DryRun bool
Config *ClickHouseConfig
conn *sqlx.DB
uid *int
gid *int
}
// Table - Clickhouse table struct
type Table struct {
Database string `db:"database"`
Name string `db:"name"`
DataPath string `db:"data_path"`
MetadataPath string `db:"metadata_path"`
IsTemporary bool `db:"is_temporary"`
Skip bool
}
// BackupPartition - struct representing Clickhouse partition
type BackupPartition struct {
Name string
Path string
}
// BackupTable - struct to store additional information on partitions
type BackupTable struct {
Database string
Name string
Partitions []BackupPartition
Path string
}
// RestoreTable - struct to store information needed during restore
type RestoreTable struct {
Database string
Table string
Query string
}
// Connect - connect to clickhouse
func (ch *ClickHouse) Connect() error {
connectionString := fmt.Sprintf("tcp://%v:%v?username=%v&password=%v&database=system&compress=true",
ch.Config.Host, ch.Config.Port, ch.Config.Username, ch.Config.Password)
var err error
if ch.conn, err = sqlx.Open("clickhouse", connectionString); err != nil {
return err
}
return ch.conn.Ping()
}
// ConnectDatabase - connect to clickhouse to specified database
func (ch *ClickHouse) ConnectDatabase(database string) error {
if database == "" {
database = "default"
}
connectionString := fmt.Sprintf("tcp://%v:%v?username=%v&password=%v&database=%v&compress=true",
ch.Config.Host, ch.Config.Port, ch.Config.Username, ch.Config.Password, database)
var err error
if ch.conn, err = sqlx.Open("clickhouse", connectionString); err != nil {
return err
}
return ch.conn.Ping()
}
// GetDataPath - return clickhouse data_path
func (ch *ClickHouse) GetDataPath() (string, error) {
if ch.Config.DataPath != "" {
return ch.Config.DataPath, nil
}
var result []struct {
MetadataPath string `db:"metadata_path"`
}
if err := ch.conn.Select(&result, "SELECT metadata_path FROM system.tables WHERE database == 'system' LIMIT 1;"); err != nil {
return "/var/lib/clickhouse", err
}
metadataPath := result[0].MetadataPath
dataPathArray := strings.Split(metadataPath, "/")
clickhouseData := path.Join(dataPathArray[:len(dataPathArray)-3]...)
return path.Join("/", clickhouseData), nil
}
// Close - close connection to clickhouse
func (ch *ClickHouse) Close() error {
return ch.conn.Close()
}
// GetTables - get all tables info
func (ch *ClickHouse) GetTables() ([]Table, error) {
var tables []Table
if err := ch.conn.Select(&tables, "SELECT database, name, is_temporary, data_path, metadata_path FROM system.tables WHERE data_path != '' AND is_temporary = 0;"); err != nil {
return nil, err
}
for i, t := range tables {
for _, filter := range ch.Config.SkipTables {
if matched, _ := filepath.Match(filter, fmt.Sprintf("%s.%s", t.Database, t.Name)); matched {
t.Skip = true
tables[i] = t
break
}
}
}
return tables, nil
}
func (ch *ClickHouse) getVersion() (int, error) {
var result []string
q := fmt.Sprintf("SELECT value FROM `system`.`build_options` where name='VERSION_INTEGER'")
if err := ch.conn.Select(&result, q); err != nil {
return 0, fmt.Errorf("can't get ClickHouse version with %v", err)
}
if len(result) == 0 {
return 0, nil
}
return strconv.Atoi(result[0])
}
// FreezeTableOldWay - freeze table for versions below 19.1
func (ch *ClickHouse) FreezeTableOldWay(table Table) error {
var partitions []struct {
PartitionID string `db:"partition_id"`
}
q := fmt.Sprintf("SELECT DISTINCT partition_id FROM `system`.`parts` WHERE database='%v' AND table='%v'", table.Database, table.Name)
if err := ch.conn.Select(&partitions, q); err != nil {
return fmt.Errorf("can't get partitions for \"%s.%s\" with %v", table.Database, table.Name, err)
}
log.Printf("Freeze '%v.%v'", table.Database, table.Name)
for _, item := range partitions {
if ch.DryRun {
log.Printf(" partition '%v' ...skip because dry-run", item.PartitionID)
continue
}
log.Printf(" partition '%v'", item.PartitionID)
query := fmt.Sprintf(
"ALTER TABLE `%v`.`%v` FREEZE PARTITION ID '%v';",
table.Database,
table.Name,
item.PartitionID)
if item.PartitionID == "all" {
query = fmt.Sprintf(
"ALTER TABLE `%v`.`%v` FREEZE PARTITION tuple();",
table.Database,
table.Name)
}
if _, err := ch.conn.Exec(query); err != nil {
return fmt.Errorf("can't freeze partition '%s' on '%s.%s' with: %v", item.PartitionID, table.Database, table.Name, err)
}
}
return nil
}
// FreezeTable - freeze all partitions for table
func (ch *ClickHouse) FreezeTable(table Table) error {
version, err := ch.getVersion()
if err != nil {
return err
}
if version < 19001005 {
return ch.FreezeTableOldWay(table)
}
log.Printf("Freeze '%v.%v'", table.Database, table.Name)
query := fmt.Sprintf("ALTER TABLE `%v`.`%v` FREEZE;", table.Database, table.Name)
if _, err := ch.conn.Exec(query); err != nil {
return fmt.Errorf("can't freeze '%s.%s' with: %v", table.Database, table.Name, err)
}
return nil
}
// GetBackupTables - return list of backups of tables that can be restored
func (ch *ClickHouse) GetBackupTables(backupName string) (map[string]BackupTable, error) {
dataPath, err := ch.GetDataPath()
if err != nil {
return nil, err
}
backupShadowPath := filepath.Join(dataPath, "backup", backupName, "shadow")
dbNum := 0
tableNum := 1
partNum := 2
totalNum := 3
if isClickhouseShadow(backupShadowPath) {
dbNum = 2
tableNum = 3
partNum = 4
totalNum = 5
}
fi, err := os.Stat(backupShadowPath)
if err != nil {
return nil, fmt.Errorf("can't get tables, %v", err)
}
if !fi.IsDir() {
return nil, fmt.Errorf("can't get tables, %s is not a dir", backupShadowPath)
}
result := make(map[string]BackupTable)
if err := filepath.Walk(backupShadowPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
filePath = filepath.ToSlash(filePath) // fix fucking Windows slashes
relativePath := strings.Trim(strings.TrimPrefix(filePath, backupShadowPath), "/")
parts := strings.Split(relativePath, "/")
if len(parts) != totalNum {
return nil
}
partition := BackupPartition{
Name: parts[partNum],
Path: filePath,
}
table := BackupTable{
Database: parts[dbNum],
Name: parts[tableNum],
Partitions: []BackupPartition{partition},
}
fullTableName := fmt.Sprintf("%s.%s", table.Database, table.Name)
if t, ok := result[fullTableName]; ok {
t.Partitions = append(t.Partitions, partition)
result[fullTableName] = t
return nil
}
result[fullTableName] = table
return nil
}
return nil
}); err != nil {
return nil, err
}
return result, nil
}
// Chown - set owner and group to clickhouse user
func (ch *ClickHouse) Chown(name string) error {
var (
dataPath string
err error
)
if ch.uid == nil || ch.gid == nil {
if dataPath, err = ch.GetDataPath(); err != nil {
return err
}
info, err := os.Stat(path.Join(dataPath, "data"))
if err != nil {
return err
}
stat := info.Sys().(*syscall.Stat_t)
uid := int(stat.Uid)
gid := int(stat.Gid)
ch.uid = &uid
ch.gid = &gid
}
return os.Chown(name, *ch.uid, *ch.gid)
}
// CopyData - copy partitions for specific table to detached folder
func (ch *ClickHouse) CopyData(table BackupTable) error {
if ch.DryRun {
log.Printf("Prepare data for restoring '%s.%s' ...skip dry-run", table.Database, table.Name)
return nil
}
log.Printf("Prepare data for restoring '%s.%s'", table.Database, table.Name)
dataPath, err := ch.GetDataPath()
if err != nil {
return err
}
detachedParentDir := filepath.Join(dataPath, "data", table.Database, table.Name, "detached")
os.MkdirAll(detachedParentDir, 0750)
ch.Chown(detachedParentDir)
for _, partition := range table.Partitions {
detachedPath := filepath.Join(detachedParentDir, partition.Name)
info, err := os.Stat(detachedPath)
if err != nil {
if os.IsNotExist(err) {
// partition dir does not exist, creating
os.MkdirAll(detachedPath, 0750)
} else {
return err
}
} else if !info.IsDir() {
return fmt.Errorf("'%s' should be directory or absent", detachedPath)
}
ch.Chown(detachedPath)
if err := filepath.Walk(partition.Path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filePath = filepath.ToSlash(filePath) // fix Windows slashes
filename := strings.Trim(strings.TrimPrefix(filePath, partition.Path), "/")
dstFilePath := filepath.Join(detachedPath, filename)
if info.IsDir() {
os.MkdirAll(dstFilePath, 0750)
return ch.Chown(dstFilePath)
}
if !info.Mode().IsRegular() {
log.Printf("'%s' is not a regular file, skipping.", filePath)
return nil
}
if err := os.Link(filePath, dstFilePath); err != nil {
return fmt.Errorf("Failed to crete hard link %s -> %s with %v", filePath, dstFilePath, err)
}
return ch.Chown(dstFilePath)
}); err != nil {
return fmt.Errorf("Error during filepath.Walk for partition %s: %v", partition.Path, err)
}
}
return nil
}
func convertPartition(detachedTableFolder string) string {
parts := strings.Split(detachedTableFolder, "_")
if parts[0] == "all" {
// table is not partitioned at all
// ENGINE = MergeTree ORDER BY id
return "tuple()"
}
if len(parts) == 5 {
// legacy partitioning based on month: toYYYYMM(date_column)
// in this case we return YYYYMM
// ENGINE = MergeTree(Date, (TimeStamp, Log), 8192)
return parts[0][:6]
}
// in case a custom partitioning key is used this is a partition name
// same as in system.parts table, it may be used in ALTER TABLE queries
// https://clickhouse.yandex/docs/en/operations/table_engines/custom_partitioning_key/
return fmt.Sprintf("ID '%s'", parts[0])
}
// AttachPatritions - execute ATTACH command for specific table
func (ch *ClickHouse) AttachPatritions(table BackupTable) error {
attachedParts := make(map[string]struct{})
for _, partition := range table.Partitions {
partName := convertPartition(partition.Name)
if _, ok := attachedParts[partName]; ok {
continue
}
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` ATTACH PARTITION %s", table.Database, table.Name, partName)
attachedParts[partName] = struct{}{}
log.Println(query)
if ch.DryRun {
continue
}
if _, err := ch.conn.Exec(query); err != nil {
return err
}
}
return nil
}
// CreateDatabase - create specific database from metadata in backup folder
func (ch *ClickHouse) CreateDatabase(database string) error {
createQuery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", database)
if ch.DryRun {
log.Printf("DRY-RUN: %s", createQuery)
return nil
}
if _, err := ch.conn.Exec(createQuery); err != nil {
return err
}
return nil
}
// CreateTable - create specific table from metadata in backup folder
func (ch *ClickHouse) CreateTable(table RestoreTable) error {
if ch.DryRun {
log.Printf("DRY-RUN: Create table '%s.%s'", table.Database, table.Table)
return nil
}
if _, err := ch.conn.Exec(fmt.Sprintf("USE `%s`", table.Database)); err != nil {
return err
}
log.Printf("Create table '%s.%s'", table.Database, table.Table)
if _, err := ch.conn.Exec(table.Query); err != nil {
return err
}
return nil
}