github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/roachtest/scrub.go (about)

     1  // Copyright 2018 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  
    11  package main
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"time"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
    19  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    20  )
    21  
    22  func registerScrubIndexOnlyTPCC(r *testRegistry) {
    23  	// numScrubRuns is set assuming a single SCRUB run (index only) takes ~1 min
    24  	r.Add(makeScrubTPCCTest(5, 100, 30*time.Minute, "index-only", 20))
    25  }
    26  
    27  func registerScrubAllChecksTPCC(r *testRegistry) {
    28  	// numScrubRuns is set assuming a single SCRUB run (all checks) takes ~2 min
    29  	r.Add(makeScrubTPCCTest(5, 100, 30*time.Minute, "all-checks", 10))
    30  }
    31  
    32  func makeScrubTPCCTest(
    33  	numNodes, warehouses int, length time.Duration, optionName string, numScrubRuns int,
    34  ) testSpec {
    35  	var stmtOptions string
    36  	// SCRUB checks are run at -1m to avoid contention with TPCC traffic.
    37  	// By the time the SCRUB queries start, the tables will have been loaded for
    38  	// some time (since it takes some time to run the TPCC consistency checks),
    39  	// so using a timestamp in the past is fine.
    40  	switch optionName {
    41  	case "index-only":
    42  		stmtOptions = `AS OF SYSTEM TIME '-1m' WITH OPTIONS INDEX ALL`
    43  	case "all-checks":
    44  		stmtOptions = `AS OF SYSTEM TIME '-1m'`
    45  	default:
    46  		panic(fmt.Sprintf("Not a valid option: %s", optionName))
    47  	}
    48  
    49  	return testSpec{
    50  		Name:    fmt.Sprintf("scrub/%s/tpcc/w=%d", optionName, warehouses),
    51  		Owner:   OwnerSQLExec,
    52  		Cluster: makeClusterSpec(numNodes),
    53  		Run: func(ctx context.Context, t *test, c *cluster) {
    54  			runTPCC(ctx, t, c, tpccOptions{
    55  				Warehouses: warehouses,
    56  				Extra:      "--wait=false --tolerate-errors",
    57  				During: func(ctx context.Context) error {
    58  					if !c.isLocal() {
    59  						// Wait until tpcc has been running for a few minutes to start SCRUB checks
    60  						sleepInterval := time.Minute * 10
    61  						maxSleep := length / 2
    62  						if sleepInterval > maxSleep {
    63  							sleepInterval = maxSleep
    64  						}
    65  						time.Sleep(sleepInterval)
    66  					}
    67  
    68  					conn := c.Conn(ctx, 1)
    69  					defer conn.Close()
    70  
    71  					c.l.Printf("Starting %d SCRUB checks", numScrubRuns)
    72  					for i := 0; i < numScrubRuns; i++ {
    73  						c.l.Printf("Running SCRUB check %d\n", i+1)
    74  						before := timeutil.Now()
    75  						err := sqlutils.RunScrubWithOptions(conn, "tpcc", "order", stmtOptions)
    76  						c.l.Printf("SCRUB check %d took %v\n", i+1, timeutil.Since(before))
    77  
    78  						if err != nil {
    79  							t.Fatal(err)
    80  						}
    81  					}
    82  					return nil
    83  				},
    84  				Duration: length,
    85  			})
    86  		},
    87  		MinVersion: "v19.1.0",
    88  	}
    89  }