|
| 1 | +// Copyright 2021 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + gosql "database/sql" |
| 15 | + "fmt" |
| 16 | + "os" |
| 17 | + "path/filepath" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/cockroachdb/cockroach/pkg/internal/sqlsmith" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/util/randutil" |
| 23 | + "github.com/cockroachdb/errors" |
| 24 | +) |
| 25 | + |
| 26 | +const statementTimeout = time.Minute |
| 27 | + |
| 28 | +func registerTLP(r *testRegistry) { |
| 29 | + r.Add(testSpec{ |
| 30 | + Name: "tlp", |
| 31 | + Owner: OwnerSQLQueries, |
| 32 | + Timeout: time.Minute * 5, |
| 33 | + MinVersion: "v20.2.0", |
| 34 | + Tags: nil, |
| 35 | + Cluster: makeClusterSpec(1), |
| 36 | + Run: runTLP, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func runTLP(ctx context.Context, t *test, c *cluster) { |
| 41 | + // Set up a statement logger for easy reproduction. We only |
| 42 | + // want to log successful statements and statements that |
| 43 | + // produced a TLP error. |
| 44 | + tlpLog, err := os.Create(filepath.Join(t.artifactsDir, "tlp.log")) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("could not create tlp.log: %v", err) |
| 47 | + } |
| 48 | + defer tlpLog.Close() |
| 49 | + logStmt := func(stmt string) { |
| 50 | + stmt = strings.TrimSpace(stmt) |
| 51 | + if stmt == "" { |
| 52 | + return |
| 53 | + } |
| 54 | + fmt.Fprint(tlpLog, stmt) |
| 55 | + if !strings.HasSuffix(stmt, ";") { |
| 56 | + fmt.Fprint(tlpLog, ";") |
| 57 | + } |
| 58 | + fmt.Fprint(tlpLog, "\n\n") |
| 59 | + } |
| 60 | + |
| 61 | + conn := c.Conn(ctx, 1) |
| 62 | + |
| 63 | + rnd, seed := randutil.NewPseudoRand() |
| 64 | + c.l.Printf("seed: %d", seed) |
| 65 | + |
| 66 | + c.Put(ctx, cockroach, "./cockroach") |
| 67 | + if err := c.PutLibraries(ctx, "./lib"); err != nil { |
| 68 | + t.Fatalf("could not initialize libraries: %v", err) |
| 69 | + } |
| 70 | + c.Start(ctx, t) |
| 71 | + |
| 72 | + setup := sqlsmith.Setups["rand-tables"](rnd) |
| 73 | + |
| 74 | + t.Status("executing setup") |
| 75 | + c.l.Printf("setup:\n%s", setup) |
| 76 | + if _, err := conn.Exec(setup); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } else { |
| 79 | + logStmt(setup) |
| 80 | + } |
| 81 | + |
| 82 | + setStmtTimeout := fmt.Sprintf("SET statement_timeout='%s';", statementTimeout.String()) |
| 83 | + t.Status("setting statement_timeout") |
| 84 | + c.l.Printf("statement timeout:\n%s", setStmtTimeout) |
| 85 | + if _, err := conn.Exec(setStmtTimeout); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + logStmt(setStmtTimeout) |
| 89 | + |
| 90 | + // Initialize a smither that generates only INSERT, UPDATE, and DELETE |
| 91 | + // statements with the MutationsOnly option. Smither.GenerateTLP always |
| 92 | + // returns SELECT queries, so the MutationsOnly option is used only for |
| 93 | + // randomly mutating the database. |
| 94 | + smither, err := sqlsmith.NewSmither(conn, rnd, sqlsmith.MutationsOnly()) |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + defer smither.Close() |
| 99 | + |
| 100 | + t.Status("running TLP") |
| 101 | + until := time.After(t.spec.Timeout / 2) |
| 102 | + done := ctx.Done() |
| 103 | + for i := 1; ; i++ { |
| 104 | + select { |
| 105 | + case <-until: |
| 106 | + return |
| 107 | + case <-done: |
| 108 | + return |
| 109 | + default: |
| 110 | + } |
| 111 | + |
| 112 | + if i%1000 == 0 { |
| 113 | + t.Status("running TLP: ", i, " statements completed") |
| 114 | + } |
| 115 | + |
| 116 | + // Run 1000 mutations first so that the tables have rows. Run a mutation |
| 117 | + // for a tenth of the iterations after that to continually change the |
| 118 | + // state of the database. |
| 119 | + if i < 1000 || i%10 == 0 { |
| 120 | + runMutationStatement(conn, smither, logStmt) |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + if err := runTLPQuery(conn, smither, logStmt); err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// runMutationsStatement runs a random INSERT, UPDATE, or DELETE statement that |
| 131 | +// potentially modifies the state of the database. |
| 132 | +func runMutationStatement(conn *gosql.DB, smither *sqlsmith.Smither, logStmt func(string)) { |
| 133 | + // Ignore panics from Generate. |
| 134 | + defer func() { |
| 135 | + if r := recover(); r != nil { |
| 136 | + return |
| 137 | + } |
| 138 | + }() |
| 139 | + |
| 140 | + stmt := smither.Generate() |
| 141 | + |
| 142 | + // Ignore timeouts. |
| 143 | + _ = runWithTimeout(func() error { |
| 144 | + // Ignore errors. Log successful statements. |
| 145 | + if _, err := conn.Exec(stmt); err == nil { |
| 146 | + logStmt(stmt) |
| 147 | + } |
| 148 | + return nil |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +// runTLPQuery runs two queries to perform TLP. If the results of the query are |
| 153 | +// not equal, an error is returned. Currently GenerateTLP always returns |
| 154 | +// unpartitioned and partitioned queries of the form "SELECT count(*) ...". The |
| 155 | +// resulting counts of the queries are compared in order to verify logical |
| 156 | +// correctness. See GenerateTLP for more information on TLP and the generated |
| 157 | +// queries. |
| 158 | +func runTLPQuery(conn *gosql.DB, smither *sqlsmith.Smither, logStmt func(string)) error { |
| 159 | + // Ignore panics from GenerateTLP. |
| 160 | + defer func() { |
| 161 | + if r := recover(); r != nil { |
| 162 | + return |
| 163 | + } |
| 164 | + }() |
| 165 | + |
| 166 | + unpartitioned, partitioned := smither.GenerateTLP() |
| 167 | + |
| 168 | + return runWithTimeout(func() error { |
| 169 | + var unpartitionedCount int |
| 170 | + row := conn.QueryRow(unpartitioned) |
| 171 | + if err := row.Scan(&unpartitionedCount); err != nil { |
| 172 | + // Ignore errors. |
| 173 | + //nolint:returnerrcheck |
| 174 | + return nil |
| 175 | + } |
| 176 | + |
| 177 | + var partitionedCount int |
| 178 | + row = conn.QueryRow(partitioned) |
| 179 | + if err := row.Scan(&partitionedCount); err != nil { |
| 180 | + // Ignore errors. |
| 181 | + //nolint:returnerrcheck |
| 182 | + return nil |
| 183 | + } |
| 184 | + |
| 185 | + if unpartitionedCount != partitionedCount { |
| 186 | + logStmt(unpartitioned) |
| 187 | + logStmt(partitioned) |
| 188 | + return errors.Newf( |
| 189 | + "expected unpartitioned count %d to equal partitioned count %d\nsql: %s\n%s", |
| 190 | + unpartitionedCount, partitionedCount, unpartitioned, partitioned) |
| 191 | + } |
| 192 | + |
| 193 | + return nil |
| 194 | + }) |
| 195 | +} |
| 196 | + |
| 197 | +func runWithTimeout(f func() error) error { |
| 198 | + done := make(chan error, 1) |
| 199 | + go func() { |
| 200 | + err := f() |
| 201 | + done <- err |
| 202 | + }() |
| 203 | + select { |
| 204 | + case <-time.After(statementTimeout + time.Second*5): |
| 205 | + // Ignore timeouts. |
| 206 | + return nil |
| 207 | + case err := <-done: |
| 208 | + return err |
| 209 | + } |
| 210 | +} |
0 commit comments