forked from PingCAP-QE/clustered-index-rand-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
393 lines (367 loc) · 9.54 KB
/
cmd.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
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"sync/atomic"
"time"
"github.com/PingCAP-QE/clustered-index-rand-test/sqlgen"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/zyguan/sqlz"
"github.com/zyguan/sqlz/resultset"
"golang.org/x/sync/errgroup"
)
type global struct {
storeDSN string
store Store
}
func rootCmd() *cobra.Command {
var g global
cmd := &cobra.Command{
Use: "clustered index random abtest",
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
g.store, err = NewStore(g.storeDSN)
return
},
}
cmd.PersistentFlags().StringVar(&g.storeDSN, "store", "", "mysql dsn of test store")
cmd.AddCommand(initCmd(&g))
cmd.AddCommand(clearCmd(&g))
cmd.AddCommand(genTestCmd(&g))
cmd.AddCommand(runTestCmd(&g))
cmd.AddCommand(whyTestCmd(&g))
cmd.AddCommand(interactCmd())
cmd.AddCommand(printCmd())
return cmd
}
func initCmd(g *global) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize test store",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return g.store.Init()
},
}
return cmd
}
func clearCmd(g *global) *cobra.Command {
cmd := &cobra.Command{
Use: "clear",
Short: "Clear test store",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return g.store.Clear()
},
}
return cmd
}
func printCmd() *cobra.Command {
var count int
cmd := &cobra.Command{
Use: "print",
Short: "Print SQL statements",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
state := sqlgen.NewState()
gen := sqlgen.NewGenerator(state)
for i := 0; i < count; i++ {
fmt.Printf("%s\n", gen())
}
return nil
},
}
cmd.Flags().IntVar(&count, "count", 1, "number of SQLs")
return cmd
}
func interactCmd() *cobra.Command {
var (
stmtCount int
dryrun bool
dsn1 string
dsn2 string
)
cmd := &cobra.Command{
Use: "interact",
Short: "Run tests interactively",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
state := sqlgen.NewState()
gen := sqlgen.NewGenerator(state)
for i := 0; i < stmtCount; i++ {
if dryrun {
fmt.Printf("%s\n", gen())
} else {
prepare := func(dsn string) (db *sql.DB, dbName string, err error) {
db, err = sql.Open("mysql", dsn)
if err != nil {
return
}
dbName = "db" + strings.ReplaceAll(uuid.New().String(), "-", "_")
var conn *sql.Conn
conn, err = sqlz.Connect(ctx, db)
if err != nil {
return
}
_, err = conn.ExecContext(ctx, "create database "+dbName)
if err != nil {
return
}
_, err = conn.ExecContext(ctx, "use "+dbName)
if err != nil {
return
}
return
}
db1, db1Name, err := prepare(dsn1)
if err != nil {
return err
}
db2, db2Name, err := prepare(dsn2)
if err != nil {
return err
}
err = sqlgen.RunInteractTest(context.Background(), db1, db2, state, gen())
if err != nil {
return err
}
hs1, err1 := checkTables(ctx, db1, db1Name)
if err1 != nil {
return err1
}
hs2, err2 := checkTables(ctx, db2, db2Name)
if err2 != nil {
return err2
}
for t := range hs2 {
if hs1[t] != hs2[t] {
return fmt.Errorf("data mismatch: %s != %s @%s", hs1[t], hs2[t], t)
}
}
}
}
return nil
},
}
cmd.Flags().IntVar(&stmtCount, "count", 1, "number of statements to run")
cmd.Flags().BoolVar(&dryrun, "dry-run", false, "dry run")
cmd.Flags().StringVar(&dsn1, "dsn1", "", "dsn for 1st database")
cmd.Flags().StringVar(&dsn2, "dsn2", "", "dsn for 2nd database")
return cmd
}
func genTestCmd(g *global) *cobra.Command {
var (
opts genTestOptions
tests int
dryrun bool
)
cmd := &cobra.Command{
Use: "gen <input file>",
Short: "Generate tests",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
for i := 0; i < tests; i++ {
t, err := genTest(opts)
if err != nil {
return err
}
if dryrun {
for _, stmt := range t.InitSQL {
fmt.Println(stmt + ";")
}
for _, txn := range t.Steps {
for _, stmt := range txn {
fmt.Println(stmt.Stmt+"; -- query:", naiveQueryDetect(stmt.Stmt))
}
}
} else {
if err := g.store.AddTest(t); err != nil {
return err
}
log.Printf("test #%d added", i)
}
}
return nil
},
}
cmd.Flags().IntVar(&tests, "test", 1, "number of test to generate")
cmd.Flags().BoolVar(&dryrun, "dry-run", false, "dry run")
cmd.Flags().StringVar(&opts.InitRoot, "init-root", "init", "entry rule of initialization sql")
cmd.Flags().StringVar(&opts.TxnRoot, "txn-root", "txn", "entry rule of transaction")
cmd.Flags().IntVar(&opts.RecurLimit, "recur-limit", 15, "max recursion level for sql generation")
cmd.Flags().IntVar(&opts.NumTxn, "txn", 5, "number of transactions per test")
cmd.Flags().BoolVar(&opts.Debug, "debug", false, "enable debug option of generator")
cmd.Flags().BoolVar(&opts.TiFlash, "tiflash", false, "enable test TiFlash (default false)")
return cmd
}
func runTestCmd(g *global) *cobra.Command {
var (
opts runABTestOptions
dsn1 string
dsn2 string
test uint32
)
cmd := &cobra.Command{
Use: "run",
Short: "Run generated tests",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
opts.Store = g.store
if opts.Threads <= 0 {
opts.Threads = 1
}
if opts.DB1, err = sql.Open("mysql", dsn1); err != nil {
return
}
if opts.DB2, err = sql.Open("mysql", dsn2); err != nil {
return
}
if test > 0 {
var cnt uint32
opts.Continue = func() bool {
return atomic.AddUint32(&cnt, 1) <= test
}
}
var g errgroup.Group
failed := make(chan struct{})
for i := 0; i < opts.Threads; i++ {
g.Go(func() error { return runABTest(context.Background(), failed, opts) })
}
return g.Wait()
},
}
cmd.Flags().Uint32Var(&test, "test", 0, "number of tests to run")
cmd.Flags().StringVar(&dsn1, "dsn1", "", "dsn for 1st database")
cmd.Flags().StringVar(&dsn2, "dsn2", "", "dsn for 2nd database")
cmd.Flags().StringVar(&opts.Tag1, "tag1", "A", "tag of 1st database")
cmd.Flags().StringVar(&opts.Tag2, "tag2", "B", "tag of 2nd database")
cmd.Flags().IntVar(&opts.Threads, "thread", 1, "number of worker threads")
cmd.Flags().IntVar(&opts.QueryTimeout, "query-timeout", 30, "timeout in seconds for a singe query")
return cmd
}
func whyTestCmd(g *global) *cobra.Command {
cmd := &cobra.Command{
Use: "why <test id>",
Short: "Explain a test",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
db, err := sql.Open("mysql", g.storeDSN)
if err != nil {
return err
}
return PrintWhy(args[0], db)
},
}
return cmd
}
func PrintWhy(id string, db *sql.DB) error {
var (
t Test
initRaw []byte
)
row := db.QueryRow("select status, started_at, finished_at, message, init_sql from test where id = ?", id)
if err := row.Scan(&t.Status, &t.StartedAt, &t.FinishedAt, &t.Message, &initRaw); err != nil {
return err
}
if err := json.Unmarshal(initRaw, &t.InitSQL); err != nil {
return err
}
t1 := time.Unix(t.StartedAt, 0)
t2 := t1
if t.FinishedAt > t.StartedAt {
t2 = time.Unix(t.FinishedAt, 0)
}
fmt.Printf("# [%s] %s (%s,%s)\n", t.Status, id, t1.Format(time.RFC3339), t2.Sub(t1))
if len(t.Message) > 0 {
fmt.Println("\n> " + t.Message)
}
if t.Status != TestFailed {
return nil
}
dumpRes := func(tag string, raw []byte, err string) {
fmt.Println("\n**" + tag + "**")
if len(err) > 0 {
fmt.Println("Error: " + err)
return
}
var rs resultset.ResultSet
if e := rs.Decode(raw); e != nil {
fmt.Println("oops: " + e.Error())
return
}
rs.PrettyPrint(os.Stdout)
}
dumpStmts := func(seq int) {
var (
stmt Stmt
)
for _, stmt := range t.InitSQL {
fmt.Println(stmt + ";")
}
rows, err := db.Query("select stmt, txn from stmt where test_id = ? and seq <= ? order by seq", id, seq)
if err != nil {
fmt.Println("oops: " + err.Error())
return
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&stmt.Stmt, &stmt.Txn); err != nil {
fmt.Println("oops: " + err.Error())
return
}
fmt.Println(stmt.Stmt + ";")
}
if err := rows.Err(); err != nil {
fmt.Println("oops: " + err.Error())
}
}
var (
seq int
stmt string
)
if err := db.QueryRow("select seq from stmt_result where test_id = ? order by seq desc", id).Scan(&seq); err != nil {
return err
}
if err := db.QueryRow("select stmt from stmt where test_id = ? and seq = ?", id, seq).Scan(&stmt); err != nil {
return err
}
fmt.Println("\n## last query")
fmt.Printf("\n%d: %s\n", seq, stmt)
fmt.Println("\n```")
rows, err := db.Query("select tag, result, errmsg from stmt_result where test_id = ? and seq = ? order by tag", id, seq)
if err != nil {
fmt.Println("oops: " + err.Error())
} else {
defer rows.Close()
for rows.Next() {
var (
tag string
err string
raw []byte
)
if rows.Scan(&tag, &raw, &err) == nil {
dumpRes(tag, raw, err)
}
}
}
fmt.Println("\n```")
fmt.Println("\n## history")
fmt.Println("\n```sql")
dumpStmts(seq)
fmt.Println("```")
return nil
}