-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
272 lines (234 loc) · 6.56 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
tables = []string{
"person_static_cohort",
}
)
func init() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
viper.AutomaticEnv()
}
func main() {
cmd := &cobra.Command{
Use: "ClickHouse tools",
Short: "A simple ClickHouse admin application",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("This is a simple ClickHouse admin application")
},
}
cmd.AddCommand(&cobra.Command{
Use: "moveto",
Short: "subcommand to move all parts of a table from a disk to another disk <from_disk> <to_disk> <database> <table> as arguments",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
var (
fromDisk = args[0]
toDisk = args[1]
database = args[2]
table = args[3]
)
fmt.Printf("Moving parts to from disk %s to disk %s for table: %s.%s\n", fromDisk, toDisk, database, table)
connUS, err := connectUS()
if err != nil {
panic(err)
}
ctx := context.Background()
testConection(ctx, connUS)
moveTo(ctx, connUS, database, table, fromDisk, toDisk)
},
})
cmd.AddCommand(&cobra.Command{
Use: "drain-disk",
Short: "subcommand to move all parts of all tables from a disk to another disk <from_disk> <to_disk> as arguments",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var (
fromDisk = args[0]
toDisk = args[1]
)
fmt.Printf("Moving parts to from disk %s to disk %s\n", fromDisk, toDisk)
connUS, err := connectUS()
if err != nil {
panic(err)
}
ctx := context.Background()
testConection(ctx, connUS)
drainDisk(ctx, connUS, fromDisk, toDisk)
},
})
var (
noKafkas = false
noMatViews = false
onlyKafkas = false
onlyMatViews = false
ifNotExists = false
)
dumpSchemaCmd := &cobra.Command{
Use: "dump-schema",
Short: "dump schema to file <clickhouse_url> <file> <database> as arguments",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var (
clickhouseUrl = &args[0]
file = &args[1]
specifiedDB = &args[2]
)
conn, err := NewCHConn(clickhouseUrl)
if err != nil {
fmt.Printf("Error connecting to the database: %v\n", err)
os.Exit(1)
}
defer conn.Close()
opts := Options{
DB: conn,
Path: *file,
SpecifiedDB: *specifiedDB,
NoKafkas: noKafkas,
NoMatViews: noMatViews,
OnlyKafkas: onlyKafkas,
OnlyMatViews: onlyMatViews,
IfNotExists: ifNotExists,
}
err = Write(&opts)
if err != nil {
fmt.Printf("Error writing schema: %v\n", err)
os.Exit(1)
}
if len(opts.Path) > 0 {
fmt.Printf("Schema successfully saved to %s\n", *file)
}
},
}
dumpSchemaCmd.Flags().BoolVar(&noKafkas, "no-kafkas", false, "Don't dump Kafka tables")
dumpSchemaCmd.Flags().BoolVar(&noMatViews, "no-mat-views", false, "Don't dump materialized views")
dumpSchemaCmd.Flags().BoolVar(&onlyKafkas, "only-kafkas", false, "Dump only Kafka tables")
dumpSchemaCmd.Flags().BoolVar(&onlyMatViews, "only-mat-views", false, "Dump only materialized views")
dumpSchemaCmd.Flags().BoolVar(&ifNotExists, "if-not-exists", false, "Add IF NOT EXISTS to CREATE TABLE statements")
cmd.AddCommand(dumpSchemaCmd)
var (
tableNamesOnly = false
apply = false
)
compareSchemaCmd := &cobra.Command{
Use: "compare-schema",
Short: "compare schemas from <clickhouse_url> to <clickhouse_url> <database> ",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
var (
clickhouseUrl = &args[0]
clickhouse2Url = &args[1]
specifiedDB = &args[2]
)
conn, err := NewCHConn(clickhouseUrl)
if err != nil {
fmt.Printf("Error connecting to the database: %v\n", err)
os.Exit(1)
}
defer conn.Close()
conn2, err := NewCHConn(clickhouse2Url)
if err != nil {
fmt.Printf("Error connecting to the database: %v\n", err)
os.Exit(1)
}
defer conn2.Close()
opts := Options{
DB: conn,
DB2: conn2,
SpecifiedDB: *specifiedDB,
TableNamesOnly: tableNamesOnly,
Apply: apply,
}
err = Compare(&opts)
if err != nil {
fmt.Printf("Error comparing schemas: %v\n", err)
os.Exit(1)
}
},
}
compareSchemaCmd.Flags().BoolVar(&tableNamesOnly, "table-names-only", false, "Only return table names, not full schema")
compareSchemaCmd.Flags().BoolVar(&apply, "apply", false, "Apply changes to the second ClickHouse instance")
cmd.AddCommand(compareSchemaCmd)
cmd.AddCommand(&cobra.Command{
Use: "synctable",
Short: "subcommand to sync a table across clusters",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
connEU, err := connectEU()
if err != nil {
panic((err))
}
connCloud, err := connectCloud()
if err != nil {
panic((err))
}
ctx := context.Background()
testConection(ctx, connEU)
testConection(ctx, connCloud)
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("00:30").WaitForSchedule().Do(func() {
updateTables(connEU, connCloud)
})
updateTables(connEU, connCloud)
s.StartBlocking()
},
})
cmd.AddCommand(&cobra.Command{
Use: "replay",
Short: "Replay a portion of query history from one cluster onto another, for benchmarking. Arguments are cluster, start and stop dates.",
Args: cobra.MinimumNArgs(5),
Run: func(cmd *cobra.Command, args []string) {
var (
cluster = args[0]
clusterAConnectionString = &args[1]
clusterBConnectionString = &args[2]
startStr = args[3]
stopStr = args[4]
skipFile string
)
if len(args) > 5 {
skipFile = args[5]
}
start, err := time.Parse("2006-01-02 15:04:05", startStr)
if err != nil {
log.Errorln(err)
panic(err)
}
stop, err := time.Parse("2006-01-02 15:04:05", stopStr)
if err != nil {
log.Errorln(err)
panic(err)
}
connClusterA, err := NewCHConn(clusterAConnectionString)
if err != nil {
log.Errorln(err)
panic(err)
}
connClusterB, err := NewCHConn(clusterBConnectionString)
if err != nil {
log.Errorln(err)
panic(err)
}
ctx := context.Background()
err = replayQueryHistory(ctx, connClusterA, connClusterB, cluster, start, stop, skipFile)
if err != nil {
log.Errorln(err)
panic(err)
}
},
})
// LETS GOOOOO
cmd.Execute()
}