github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/show_trace_replica_test.go (about)

     1  // Copyright 2017 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 sql_test
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"reflect"
    17  	"testing"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/base"
    20  	"github.com/cockroachdb/cockroach/pkg/config/zonepb"
    21  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    22  	"github.com/cockroachdb/cockroach/pkg/server"
    23  	"github.com/cockroachdb/cockroach/pkg/testutils"
    24  	"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
    25  	"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
    26  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    27  	"github.com/cockroachdb/errors"
    28  	"github.com/gogo/protobuf/proto"
    29  )
    30  
    31  func TestShowTraceReplica(t *testing.T) {
    32  	defer leaktest.AfterTest(t)()
    33  
    34  	t.Skip("https://github.com/cockroachdb/cockroach/issues/34213")
    35  
    36  	const numNodes = 4
    37  
    38  	zoneConfig := zonepb.DefaultZoneConfig()
    39  	zoneConfig.NumReplicas = proto.Int32(1)
    40  
    41  	ctx := context.Background()
    42  	tsArgs := func(node string) base.TestServerArgs {
    43  		return base.TestServerArgs{
    44  			Knobs: base.TestingKnobs{
    45  				Server: &server.TestingKnobs{
    46  					DefaultZoneConfigOverride:       &zoneConfig,
    47  					DefaultSystemZoneConfigOverride: &zoneConfig,
    48  				},
    49  			},
    50  			StoreSpecs: []base.StoreSpec{{InMemory: true, Attributes: roachpb.Attributes{Attrs: []string{node}}}},
    51  		}
    52  	}
    53  	tcArgs := base.TestClusterArgs{ServerArgsPerNode: map[int]base.TestServerArgs{
    54  		0: tsArgs(`n1`),
    55  		1: tsArgs(`n2`),
    56  		2: tsArgs(`n3`),
    57  		3: tsArgs(`n4`),
    58  	}}
    59  	tc := testcluster.StartTestCluster(t, numNodes, tcArgs)
    60  	defer tc.Stopper().Stop(ctx)
    61  
    62  	sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])
    63  	sqlDB.Exec(t, `ALTER RANGE "default" CONFIGURE ZONE USING constraints = '[+n4]'`)
    64  	sqlDB.Exec(t, `ALTER DATABASE system CONFIGURE ZONE USING constraints = '[+n4]'`)
    65  	sqlDB.Exec(t, `CREATE DATABASE d`)
    66  	sqlDB.Exec(t, `CREATE TABLE d.t1 (a INT PRIMARY KEY)`)
    67  	sqlDB.Exec(t, `CREATE TABLE d.t2 (a INT PRIMARY KEY)`)
    68  	sqlDB.Exec(t, `CREATE TABLE d.t3 (a INT PRIMARY KEY)`)
    69  	sqlDB.Exec(t, `ALTER TABLE d.t1 CONFIGURE ZONE USING constraints = '[+n1]'`)
    70  	sqlDB.Exec(t, `ALTER TABLE d.t2 CONFIGURE ZONE USING constraints = '[+n2]'`)
    71  	sqlDB.Exec(t, `ALTER TABLE d.t3 CONFIGURE ZONE USING constraints = '[+n3]'`)
    72  
    73  	tests := []struct {
    74  		query    string
    75  		expected [][]string
    76  		distinct bool
    77  	}{
    78  		{
    79  			// Read-only
    80  			query:    `SELECT * FROM d.t1`,
    81  			expected: [][]string{{`1`, `1`}},
    82  		},
    83  		{
    84  			// Write-only
    85  			query:    `UPSERT INTO d.t2 VALUES (1)`,
    86  			expected: [][]string{{`2`, `2`}},
    87  		},
    88  		{
    89  			// A write to delete the row.
    90  			query:    `DELETE FROM d.t2`,
    91  			expected: [][]string{{`2`, `2`}},
    92  		},
    93  		{
    94  			// Admin command. We use distinct because the ALTER statement is
    95  			// DDL and cause event log / job ranges to be touched too.
    96  			query:    `ALTER TABLE d.t3 SCATTER`,
    97  			expected: [][]string{{`4`, `4`}, {`3`, `3`}},
    98  			distinct: true,
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		t.Run(test.query, func(t *testing.T) {
   104  			testutils.SucceedsSoon(t, func() error {
   105  				_ = sqlDB.Exec(t, fmt.Sprintf(`SET tracing = on; %s; SET tracing = off`, test.query))
   106  
   107  				distinct := ""
   108  				if test.distinct {
   109  					distinct = "DISTINCT"
   110  				}
   111  				actual := sqlDB.QueryStr(t,
   112  					fmt.Sprintf(`SELECT %s node_id, store_id FROM [SHOW EXPERIMENTAL_REPLICA TRACE FOR SESSION]`, distinct),
   113  				)
   114  				if !reflect.DeepEqual(actual, test.expected) {
   115  					return errors.Errorf(`%s: got %v expected %v`, test.query, actual, test.expected)
   116  				}
   117  				return nil
   118  			})
   119  		})
   120  	}
   121  }