Skip to content

Commit 4b208ca

Browse files
craig[bot]jbowensknz
committed
108304: roachtest: tolerate errors in clearrange workload r=srosenberg a=jbowens During the clearrange roachtest, a concurrent kv0 workload runs in the background. Previously, this background was not configured to `--tolerate-errors`. Epic: none Informs cockroachdb#108244. Release note: None 108442: testserver,testtenant: fix `SQLConn()` r=stevendanna a=knz Fixes cockroachdb#107859. Epic: CRDB-18499 Prior to this patch, the (recently introduced) `SQLConn()` method was blind to the concept of a shared SQL port where connections get redirected to a secondary tenant based on the `cluster` URL option. Because of this, two bad things were happening: - `.SQLConn()` on the `ApplicationLayerInterface` of a secondary tenant with a shared SQL port would fail to connect to the right tenant. - `.SystemLayer().SQLConn()` would fail if the cluster setting `server.controller.default_tenant` was set. This patch fixes it. Release note: None Co-authored-by: Jackson Owens <[email protected]> Co-authored-by: Raphael 'kena' Poss <[email protected]>
3 parents 66c9e4f + e3e9f63 + e4d2288 commit 4b208ca

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

pkg/cmd/roachtest/tests/clearrange.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ ORDER BY raw_start_key ASC LIMIT 1`,
147147
m := c.NewMonitor(ctx)
148148
m.Go(func(ctx context.Context) error {
149149
c.Run(ctx, c.Node(1), `./cockroach workload init kv`)
150-
c.Run(ctx, c.All(), `./cockroach workload run kv --concurrency=32 --duration=1h`)
150+
c.Run(ctx, c.All(), `./cockroach workload run kv --concurrency=32 --duration=1h --tolerate-errors`)
151151
return nil
152152
})
153153
m.Go(func(ctx context.Context) error {

pkg/server/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ go_test(
491491
"//pkg/kv/kvserver/kvserverpb",
492492
"//pkg/kv/kvserver/kvstorage",
493493
"//pkg/kv/kvserver/liveness/livenesspb",
494+
"//pkg/multitenant",
494495
"//pkg/roachpb",
495496
"//pkg/rpc",
496497
"//pkg/security",

pkg/server/server_controller_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import (
1515
"testing"
1616

1717
"github.com/cockroachdb/cockroach/pkg/base"
18+
"github.com/cockroachdb/cockroach/pkg/multitenant"
19+
"github.com/cockroachdb/cockroach/pkg/roachpb"
1820
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
21+
"github.com/cockroachdb/cockroach/pkg/sql/isql"
1922
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
2023
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
2124
"github.com/cockroachdb/cockroach/pkg/util/log"
25+
"github.com/stretchr/testify/assert"
2226
"github.com/stretchr/testify/require"
2327
)
2428

@@ -81,6 +85,7 @@ func TestSQLErrorUponInvalidTenant(t *testing.T) {
8185
require.NoError(t, err)
8286

8387
err = db.Ping()
88+
require.NotNil(t, err)
8489
require.Regexp(t, `service unavailable for target tenant \(nonexistent\)`, err.Error())
8590
}
8691

@@ -109,3 +114,57 @@ func TestSharedProcessServerInheritsTempStorageLimit(t *testing.T) {
109114
tss := ts.(*testTenant)
110115
require.Equal(t, int64(specialSize), tss.SQLCfg.TempStorageConfig.Mon.Limit())
111116
}
117+
118+
// TestServerSQLConn checks that the SQLConn() method on the
119+
// SystemLayer() of TestServerInterface works even when non-specific
120+
// SQL connection requests are redirected to a secondary tenant.
121+
func TestServerSQLConn(t *testing.T) {
122+
defer leaktest.AfterTest(t)()
123+
defer log.Scope(t).Close(t)
124+
125+
ctx := context.Background()
126+
s := serverutils.StartServerOnly(t, base.TestServerArgs{
127+
DefaultTestTenant: base.TestControlsTenantsExplicitly,
128+
})
129+
defer s.Stopper().Stop(ctx)
130+
systemTenant := s.SystemLayer()
131+
132+
// Start some secondary tenant servers.
133+
secondaryTenantExtNoName, err := s.StartTenant(ctx, base.TestTenantArgs{
134+
TenantID: roachpb.MustMakeTenantID(2),
135+
})
136+
require.NoError(t, err)
137+
138+
secondaryTenantExtNamed, err := s.StartTenant(ctx, base.TestTenantArgs{
139+
TenantName: "hello",
140+
TenantID: roachpb.MustMakeTenantID(10),
141+
})
142+
require.NoError(t, err)
143+
144+
secondaryTenantSh, _, err := s.StartSharedProcessTenant(ctx, base.TestSharedProcessTenantArgs{
145+
TenantName: "world",
146+
})
147+
require.NoError(t, err)
148+
multitenant.DefaultTenantSelect.Override(ctx, &systemTenant.ClusterSettings().SV, "world")
149+
150+
for _, tc := range []struct {
151+
testName string
152+
tbName string
153+
sqlInterface serverutils.ApplicationLayerInterface
154+
}{
155+
{"system", "foo", systemTenant},
156+
{"secondary-external-noname", "bar", secondaryTenantExtNoName},
157+
{"secondary-external-named", "baz", secondaryTenantExtNamed},
158+
{"secondary-shared", "qux", secondaryTenantSh},
159+
} {
160+
t.Run(tc.testName, func(t *testing.T) {
161+
_, err = tc.sqlInterface.InternalExecutor().(isql.Executor).
162+
Exec(ctx, "create-table", nil, "CREATE TABLE defaultdb."+tc.tbName+" (i INT)")
163+
require.NoError(t, err)
164+
165+
conn := tc.sqlInterface.SQLConn(t, "defaultdb")
166+
var unused int
167+
assert.NoError(t, conn.QueryRowContext(ctx, "SELECT count(*) FROM "+tc.tbName).Scan(&unused))
168+
})
169+
}
170+
}

pkg/server/testserver.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"github.com/cockroachdb/cockroach/pkg/sql/deprecatedshowranges"
5555
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
5656
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
57+
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
5758
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
5859
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
5960
"github.com/cockroachdb/cockroach/pkg/storage"
@@ -495,7 +496,9 @@ func (ts *testServer) SQLConnE(dbName string) (*gosql.DB, error) {
495496

496497
// SQLConnForUserE is part of the serverutils.ApplicationLayerInterface.
497498
func (ts *testServer) SQLConnForUserE(userName string, dbName string) (*gosql.DB, error) {
498-
return openTestSQLConn(userName, dbName, ts.Stopper(),
499+
return openTestSQLConn(
500+
userName, dbName, catconstants.SystemTenantName,
501+
ts.Stopper(),
499502
ts.topLevelServer.loopbackPgL,
500503
ts.cfg.SQLAdvertiseAddr,
501504
ts.cfg.Insecure,
@@ -787,7 +790,15 @@ func (t *testTenant) SQLConnE(dbName string) (*gosql.DB, error) {
787790

788791
// SQLConnForUserE is part of the serverutils.ApplicationLayerInterface.
789792
func (t *testTenant) SQLConnForUserE(userName string, dbName string) (*gosql.DB, error) {
790-
return openTestSQLConn(userName, dbName, t.Stopper(),
793+
tenantName := t.t.tenantName
794+
if !t.Cfg.DisableSQLListener {
795+
// This tenant server has its own SQL listener. It will not accept
796+
// a "cluster" connection parameter.
797+
tenantName = ""
798+
}
799+
return openTestSQLConn(
800+
userName, dbName, tenantName,
801+
t.Stopper(),
791802
t.pgL,
792803
t.Cfg.SQLAdvertiseAddr,
793804
t.Cfg.Insecure,

pkg/server/testserver_sqlconn.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ package server
1313
import (
1414
"context"
1515
gosql "database/sql"
16+
"fmt"
1617
"net"
1718
"net/url"
19+
"strings"
1820
"time"
1921

22+
"github.com/cockroachdb/cockroach/pkg/roachpb"
2023
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
2124
"github.com/cockroachdb/cockroach/pkg/util/log"
2225
"github.com/cockroachdb/cockroach/pkg/util/netutil"
@@ -43,6 +46,7 @@ const useLoopbackListener = false
4346
// of serverutils.ApplicationLayerInterface.
4447
func openTestSQLConn(
4548
userName, dbName string,
49+
tenantName roachpb.TenantName,
4650
stopper *stop.Stopper,
4751
// When useLoopbackListener is set, only this is used:
4852
pgL *netutil.LoopbackListener,
@@ -53,13 +57,21 @@ func openTestSQLConn(
5357
cleanupFn := func() {}
5458
var goDB *gosql.DB
5559

60+
opts := url.Values{}
61+
if tenantName != "" && !strings.HasPrefix(dbName, "cluster:") {
62+
opts.Add("options", fmt.Sprintf("-ccluster=%s", tenantName))
63+
}
64+
if insecure || useLoopbackListener {
65+
opts.Add("sslmode", "disable")
66+
}
67+
5668
if useLoopbackListener {
5769
pgurl := url.URL{
5870
Scheme: "postgres",
5971
User: url.User(userName),
6072
Host: "unused",
6173
Path: dbName,
62-
RawQuery: "sslmode=disable",
74+
RawQuery: opts.Encode(),
6375
}
6476
// TODO(sql): consider using pgx for tests instead of lib/pq.
6577
connector, err := pq.NewConnector(pgurl.String())
@@ -76,9 +88,16 @@ func openTestSQLConn(
7688
return nil, err
7789
}
7890
pgURL.Path = dbName
79-
if insecure {
80-
pgURL.RawQuery = "sslmode=disable"
91+
92+
// Add the common query options decided above to those prepared by
93+
// PGUrlE().
94+
qv := pgURL.Query()
95+
for k, v := range opts {
96+
qv[k] = v
8197
}
98+
pgURL.RawQuery = qv.Encode()
99+
100+
// Open the connection.
82101
goDB, err = gosql.Open("postgres", pgURL.String())
83102
if err != nil {
84103
cleanupFn()

0 commit comments

Comments
 (0)