-
Notifications
You must be signed in to change notification settings - Fork 0
/
disks.go
138 lines (132 loc) · 3.47 KB
/
disks.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)
func moveTo(ctx context.Context, conn driver.Conn, database, table, fromDisk, toDisk string) error {
fmt.Printf("Moving parts for table: %s.%s from disk %s to disk %s\n", database, table, fromDisk, toDisk)
rows, err := conn.Query(
ctx,
"select name from system.parts where active and disk_name = {fromDisk:String} and database = {database:String} and table = {table:String} group by name;",
clickhouse.Named("fromDisk", fromDisk),
clickhouse.Named("database", database),
clickhouse.Named("table", table))
if err != nil {
log.Fatal(err)
return err
}
for rows.Next() {
var name string
if err := rows.Scan(
&name,
); err != nil {
log.Fatal(err)
return err
}
fmt.Printf("Moving part: %s for table %s.%s to disk %s\n", name, database, table, toDisk)
fmtQuery := fmt.Sprintf("ALTER TABLE {database:Identifier}.{table:Identifier} move part '%s' to disk '%s'", name, toDisk)
done := make(chan bool)
go func() {
pollconn, err := connectUS()
if err != nil {
panic(err)
}
defer pollconn.Close()
for {
select {
case <-done:
return
default:
rows, err := pollconn.Query(
ctx,
"select database, table, elapsed, target_disk_name, target_disk_path, part_name, part_size, thread_id from system.moves",
)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var (
database string
table string
elapsed float64
targetDiskName string
targetDiskPath string
partName string
partSize uint64
threadID uint64
)
if err := rows.Scan(
&database,
&table,
&elapsed,
&targetDiskName,
&targetDiskPath,
&partName,
&partSize,
&threadID,
); err != nil {
log.Fatal(err)
}
fmt.Printf("Moving part %s for table %s.%s to disk %s (%s) [elapsed: %f, size: %d, thread: %d]\n", partName, database, table, targetDiskName, targetDiskPath, elapsed, partSize, threadID)
}
time.Sleep(2 * time.Second)
}
}
}()
err = conn.Exec(
ctx,
fmtQuery,
clickhouse.Named("database", database),
clickhouse.Named("table", table),
clickhouse.Named("part_name", name))
if err != nil {
log.Fatal(err)
done <- true
return err
}
done <- true
}
return nil
}
func drainDisk(ctx context.Context, conn driver.Conn, disk, toDisk string) error {
fmt.Printf("Draining disk: %s\n", disk)
query := "select database, table, disk_name, sum(bytes) b, formatReadableSize(sum(bytes)) size, count(1) parts " +
"from system.parts where disk_name = {disk_name:String} group by database, table, disk_name order by disk_name desc;"
rows, err := conn.Query(ctx, query, clickhouse.Named("disk_name", disk))
if err != nil {
log.Fatal(err)
return err
}
for rows.Next() {
var (
database string
table string
diskName string
bytes uint64
size string
parts uint64
)
if err := rows.Scan(
&database,
&table,
&diskName,
&bytes,
&size,
&parts,
); err != nil {
log.Fatal(err)
return err
}
fmt.Printf("Moving Table: %s.%s, Disk: %s, Parts: %d, Size: %s To Disk: %s\n", database, table, diskName, parts, size, toDisk)
err := moveTo(ctx, conn, database, table, disk, toDisk)
if err != nil {
log.Fatal(err)
return err
}
}
return nil
}