-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathmain.go
382 lines (352 loc) · 10.9 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
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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// smithtest is a tool to execute sqlsmith tests on cockroach demo
// instances. Failures are tracked, de-duplicated, reduced. Issues are
// prefilled for GitHub.
package main
import (
"bufio"
"bytes"
"context"
gosql "database/sql"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/internal/sqlsmith"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/google/go-github/github"
"github.com/jackc/pgx"
"github.com/lib/pq"
"github.com/pkg/browser"
"github.com/pkg/errors"
)
var (
flags = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
cockroach = flags.String("cockroach", "./cockroach", "path to cockroach binary")
reduce = flags.String("reduce", "./bin/reduce", "path to reduce binary")
num = flags.Int("num", 1, "number of parallel testers")
)
func usage() {
fmt.Fprintf(flags.Output(), "Usage of %s:\n", os.Args[0])
flags.PrintDefaults()
os.Exit(1)
}
func main() {
if err := flags.Parse(os.Args[1:]); err != nil {
usage()
}
ctx := context.Background()
setup := WorkerSetup{
cockroach: *cockroach,
reduce: *reduce,
initSQL: sqlsmith.SeedTable,
github: github.NewClient(nil),
}
rand.Seed(timeutil.Now().UnixNano())
setup.populateGitHubIssues(ctx)
fmt.Println("running...")
g := ctxgroup.WithContext(ctx)
for i := 0; i < *num; i++ {
g.GoCtx(setup.work)
}
if err := g.Wait(); err != nil {
log.Fatalf("%+v", err)
}
}
// WorkerSetup contains initialization and configuration for running smithers.
type WorkerSetup struct {
cockroach, reduce string
initSQL string
github *github.Client
}
// populateGitHubIssues populates seen with issues already in GitHub.
func (s WorkerSetup) populateGitHubIssues(ctx context.Context) {
var opts github.SearchOptions
for {
results, _, err := s.github.Search.Issues(ctx, "repo:cockroachdb/cockroach type:issue state:open label:C-bug label:O-sqlsmith", &opts)
if err != nil {
log.Fatal(err)
}
for _, issue := range results.Issues {
title := filterIssueTitle(issue.GetTitle())
seenIssues[title] = true
fmt.Println("pre populate", title)
}
if results.GetIncompleteResults() {
opts.Page++
continue
}
return
}
}
func (s WorkerSetup) work(ctx context.Context) error {
rnd := rand.New(rand.NewSource(rand.Int63()))
for {
if err := s.run(ctx, rnd); err != nil {
return err
}
}
}
var (
// lock is used to both protect the seen map from concurrent access
// and prevent overuse of system resources. When the reducer needs to
// run it gets the exclusive write lock. When normal queries are being
// smithed, they use the communal read lock. Thus, the reducer being
// executed will pause the other testing queries and prevent 2 reducers
// from running at the same time. This should greatly speed up the time
// it takes for a single reduction run.
lock syncutil.RWMutex
// seenIssues tracks the seen github issues.
seenIssues = map[string]bool{}
connRE = regexp.MustCompile(`(?m)^sql:\s*(postgresql://.*)$`)
panicRE = regexp.MustCompile(`(?m)^(panic: .*?)( \[recovered\])?$`)
stackRE = regexp.MustCompile(`panic: .*\n\ngoroutine \d+ \[running\]:\n(?s:(.*))$`)
fatalRE = regexp.MustCompile(`(?m)^(fatal error: .*?)$`)
runtimeStackRE = regexp.MustCompile(`goroutine \d+ \[running\]:\n(?s:(.*?))\n\n`)
)
// run is a single sqlsmith worker. It starts a new sqlsmither and cockroach
// demo instance. If an error is found it reduces and submits the issue. If
// an issue is successfully found, this function returns, causing the started
// cockroach instance to shut down. An error is only returned if something
// unexpected happened. That is, panics and internal errors will return
// nil, since they are expected. Something unexpected would be like the
// initialization SQL was unable to run.
func (s WorkerSetup) run(ctx context.Context, rnd *rand.Rand) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(ctx, s.cockroach,
"start-single-node",
"--port", "0",
"--http-port", "0",
"--insecure",
"--store=type=mem,size=1GB",
"--logtostderr",
)
// Look for the connection string.
var pgdb *pgx.Conn
var db *gosql.DB
var output bytes.Buffer
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return errors.Wrap(err, "start")
}
scanner := bufio.NewScanner(io.TeeReader(stderr, &output))
for scanner.Scan() {
line := scanner.Text()
if match := connRE.FindStringSubmatch(line); match != nil {
config, err := pgx.ParseURI(match[1])
if err != nil {
return errors.Wrap(err, "parse uri")
}
pgdb, err = pgx.Connect(config)
if err != nil {
return errors.Wrap(err, "connect")
}
connector, err := pq.NewConnector(match[1])
if err != nil {
return errors.Wrap(err, "connector error")
}
db = gosql.OpenDB(connector)
if err != nil {
return errors.Wrap(err, "connect")
}
fmt.Println("connected to", match[1])
break
}
}
if err := scanner.Err(); err != nil {
fmt.Println(output.String())
return errors.Wrap(err, "scanner error")
}
if db == nil {
fmt.Println(output.String())
return errors.New("no DB address found")
}
fmt.Println("worker started")
if _, err := pgdb.ExecEx(ctx, s.initSQL, nil); err != nil {
return errors.Wrap(err, "init")
}
opts := []sqlsmith.SmitherOption{
sqlsmith.DisableMutations(),
sqlsmith.DisableCRDBFns(),
}
smither, err := sqlsmith.NewSmither(db, rnd, opts...)
if err != nil {
return errors.Wrap(err, "new smither")
}
for {
// If lock is locked for writing (due to a found bug in another
// go routine), block here until it has finished reducing.
lock.RLock()
stmt := smither.Generate()
done := make(chan struct{}, 1)
go func() {
_, err = pgdb.ExecEx(ctx, stmt, nil)
done <- struct{}{}
}()
// Timeout slow statements by returning, which will cancel the
// command's context by the above defer.
select {
case <-time.After(10 * time.Second):
fmt.Printf("TIMEOUT:\n%s\n", stmt)
lock.RUnlock()
return nil
case <-done:
}
lock.RUnlock()
if err != nil {
if strings.Contains(err.Error(), "internal error") {
// Return from this function on internal
// errors. This causes the current cockroach
// instance to shut down and we start a new
// one. This is not strictly necessary, since
// internal errors don't mess up the rest of
// cockroach, but it's just easier to have a
// single logic flow in case of a found error,
// which is to shut down and start over (just
// like the panic case below).
return s.failure(ctx, stmt, err)
}
}
// If we can't ping, check if the statement caused a panic.
if err := db.PingContext(ctx); err != nil {
input := fmt.Sprintf("%s; %s;", s.initSQL, stmt)
out, _ := exec.CommandContext(ctx, s.cockroach, "demo", "--empty", "-e", input).CombinedOutput()
var pqerr pq.Error
if match := stackRE.FindStringSubmatch(string(out)); match != nil {
pqerr.Detail = strings.TrimSpace(match[1])
}
if match := panicRE.FindStringSubmatch(string(out)); match != nil {
// We found a panic as expected.
pqerr.Message = match[1]
return s.failure(ctx, stmt, &pqerr)
}
// Not a panic. Maybe a fatal?
if match := runtimeStackRE.FindStringSubmatch(string(out)); match != nil {
pqerr.Detail = strings.TrimSpace(match[1])
}
if match := fatalRE.FindStringSubmatch(string(out)); match != nil {
// A real bad non-panic error.
pqerr.Message = match[1]
return s.failure(ctx, stmt, &pqerr)
}
// A panic was not found. Shut everything down by returning an error so it can be investigated.
fmt.Printf("output:\n%s\n", out)
fmt.Printf("Ping stmt:\n%s;\n", stmt)
return err
}
}
}
// failure de-duplicates, reduces, and files errors. It generally returns nil
// indicating that this was successfully filed and we should continue looking
// for errors.
func (s WorkerSetup) failure(ctx context.Context, stmt string, err error) error {
var message, stack string
if pqerr, ok := err.(pgx.PgError); ok {
stack = pqerr.Detail
message = pqerr.Message
} else {
message = err.Error()
}
filteredMessage := filterIssueTitle(regexp.QuoteMeta(message))
message = fmt.Sprintf("sql: %s", message)
lock.Lock()
// Keep this locked for the remainder of the function so that smither
// tests won't run during the reducer, and only one reducer can run
// at once.
defer lock.Unlock()
sqlFilteredMessage := fmt.Sprintf("sql: %s", filteredMessage)
alreadySeen := seenIssues[sqlFilteredMessage]
if !alreadySeen {
seenIssues[sqlFilteredMessage] = true
}
if alreadySeen {
fmt.Println("already found", message)
return nil
}
fmt.Println("found", message)
input := fmt.Sprintf("%s\n\n%s;", s.initSQL, stmt)
fmt.Printf("SQL:\n%s\n\n", input)
// Run reducer.
cmd := exec.CommandContext(ctx, s.reduce, "-v", "-contains", filteredMessage)
cmd.Stdin = strings.NewReader(input)
cmd.Stderr = os.Stderr
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
fmt.Println(input)
return err
}
// Generate the pre-filled github issue.
makeBody := func() string {
return fmt.Sprintf("```\n%s\n```\n\n```\n%s\n```", strings.TrimSpace(out.String()), strings.TrimSpace(stack))
}
query := url.Values{
"title": []string{message},
"labels": []string{"C-bug,O-sqlsmith"},
"body": []string{makeBody()},
}
url := url.URL{
Scheme: "https",
Host: "github.com",
Path: "/cockroachdb/cockroach/issues/new",
RawQuery: query.Encode(),
}
const max = 8000
// Remove lines from the stack trace to shorten up the request so it's
// under the github limit.
for len(url.String()) > max {
last := strings.LastIndex(stack, "\n")
if last < 0 {
break
}
stack = stack[:last]
query["body"][0] = makeBody()
url.RawQuery = query.Encode()
}
if len(url.String()) > max {
fmt.Println(stmt)
return errors.New("request could not be shortened to max length")
}
if err := browser.OpenURL(url.String()); err != nil {
return err
}
return nil
}
// filterIssueTitle handles issue title where some words in the title can
// vary for identical issues. Usually things like number of bytes, IDs, or
// counts. These are converted into their regex equivalent so they can be
// correctly de-duplicated.
func filterIssueTitle(s string) string {
for _, reS := range []string{
`given: .*, expected .*`,
`Datum is .*, not .*`,
`expected .*, found .*`,
`\d+`,
`\*tree\.D\w+`,
} {
re := regexp.MustCompile(reS)
s = re.ReplaceAllString(s, reS)
}
return s
}