forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_sql_test.go
511 lines (441 loc) · 16.9 KB
/
server_sql_test.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2020 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package serverccl
import (
"context"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/ccl"
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl/licenseccl"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/isql"
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance/instancestorage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/errors"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
)
// TestSQLServer starts up a semi-dedicated SQL server and runs some smoke test
// queries.
func TestSQLServer(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
ts, db, _ := serverutils.StartServer(t, base.TestServerArgs{
// This test is specific to secondary tenants; no need to run it
// using the system tenant.
DefaultTestTenant: base.TestTenantAlwaysEnabled,
})
defer ts.Stopper().Stop(ctx)
r := sqlutils.MakeSQLRunner(db)
r.QueryStr(t, `SELECT 1`)
r.Exec(t, `CREATE DATABASE foo`)
r.Exec(t, `CREATE TABLE foo.kv (k STRING PRIMARY KEY, v STRING)`)
r.Exec(t, `INSERT INTO foo.kv VALUES('foo', 'bar')`)
// Cause an index backfill operation.
r.Exec(t, `CREATE INDEX ON foo.kv (v)`)
t.Log(sqlutils.MatrixToStr(r.QueryStr(t, `SET distsql=off; SELECT * FROM foo.kv`)))
t.Log(sqlutils.MatrixToStr(r.QueryStr(t, `SET distsql=auto; SELECT * FROM foo.kv`)))
}
func TestTenantCannotSetClusterSetting(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc := serverutils.StartCluster(t, 1, base.TestClusterArgs{ServerArgs: base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
}})
defer tc.Stopper().Stop(ctx)
// StartTenant with the default permissions to
_, db := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: serverutils.TestTenantID()})
defer db.Close()
_, err := db.Exec(`SET CLUSTER SETTING sql.defaults.vectorize=off`)
require.NoError(t, err)
_, err = db.Exec(`SET CLUSTER SETTING kv.snapshot_rebalance.max_rate = '2MiB';`)
var pqErr *pq.Error
ok := errors.As(err, &pqErr)
require.True(t, ok, "expected err to be a *pq.Error but is of type %T. error is: %v", err)
if !strings.Contains(pqErr.Message, "unknown cluster setting") {
t.Errorf("unexpected error: %v", err)
}
}
// TestTenantCanUseEnterpriseFeaturesWithEnvVar verifies that tenants
// can get a license from the env variable.
func TestTenantCanUseEnterpriseFeaturesWithEnvVar(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
license, _ := (&licenseccl.License{
Type: licenseccl.License_Enterprise,
}).Encode()
defer ccl.TestingDisableEnterprise()()
defer envutil.TestSetEnv(t, "COCKROACH_TENANT_LICENSE", license)()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(context.Background())
_, db := serverutils.StartTenant(t, s, base.TestTenantArgs{TenantID: serverutils.TestTenantID()})
defer db.Close()
_, err := db.Exec(`BACKUP INTO 'userfile:///backup'`)
require.NoError(t, err)
_, err = db.Exec(`BACKUP INTO LATEST IN 'userfile:///backup'`)
require.NoError(t, err)
}
// TestTenantCanUseEnterpriseFeatures verifies that tenants can get a license
// from the cluster setting.
func TestTenantCanUseEnterpriseFeaturesWithSetting(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
license, _ := (&licenseccl.License{
Type: licenseccl.License_Enterprise,
OrganizationName: "mytest",
}).Encode()
defer ccl.TestingDisableEnterprise()()
ctx := context.Background()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
ie := s.SystemLayer().InternalExecutor().(isql.Executor)
_, err := ie.Exec(ctx, "set-license", nil, "SET CLUSTER SETTING cluster.organization = 'mytest'")
require.NoError(t, err)
_, err = ie.Exec(ctx, "set-license", nil, "SET CLUSTER SETTING enterprise.license = $1", license)
require.NoError(t, err)
_, db := serverutils.StartTenant(t, s, base.TestTenantArgs{TenantID: serverutils.TestTenantID()})
defer db.Close()
_, err = db.Exec(`BACKUP INTO 'userfile:///backup'`)
require.NoError(t, err)
_, err = db.Exec(`BACKUP INTO LATEST IN 'userfile:///backup'`)
require.NoError(t, err)
}
func TestTenantUnauthenticatedAccess(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
_, err := s.TenantController().StartTenant(ctx,
base.TestTenantArgs{
TenantID: roachpb.MustMakeTenantID(securitytest.EmbeddedTenantIDs()[0]),
TestingKnobs: base.TestingKnobs{
TenantTestingKnobs: &sql.TenantTestingKnobs{
// Configure the SQL server to access the wrong tenant keyspace.
TenantIDCodecOverride: roachpb.MustMakeTenantID(securitytest.EmbeddedTenantIDs()[1]),
},
},
})
require.Error(t, err)
require.Regexp(t, `requested key .* not fully contained in tenant keyspace /Tenant/1{0-1}.*Unauthenticated`, err)
}
// TestTenantHTTP verifies that SQL tenant servers expose metrics and debugging endpoints.
func TestTenantHTTP(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
// This test is specific to secondary tenants; no need to run it
// using the system tenant.
DefaultTestTenant: base.TestIsForStuffThatShouldWorkWithSharedProcessModeButDoesntYet(
base.TestTenantAlwaysEnabled, 113187,
),
})
defer s.Stopper().Stop(ctx)
ts := s.ApplicationLayer()
t.Run("prometheus", func(t *testing.T) {
httpClient, err := ts.GetUnauthenticatedHTTPClient()
require.NoError(t, err)
defer httpClient.CloseIdleConnections()
resp, err := httpClient.Get(ts.AdminURL().WithPath("/_status/vars").String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "sql_ddl_started_count_internal")
})
t.Run("pprof", func(t *testing.T) {
httpClient, err := ts.GetAdminHTTPClient()
require.NoError(t, err)
defer httpClient.CloseIdleConnections()
u := ts.AdminURL().WithPath("/debug/pprof/goroutine")
q := u.Query()
q.Set("debug", "2")
u.RawQuery = q.Encode()
resp, err := httpClient.Get(u.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "goroutine")
})
}
// TestTenantProcessDebugging verifies that in-process SQL tenant servers gate
// process debugging behind capabilities.
func TestTenantProcessDebugging(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
// External service tenants are allowed to debug their own processes without
// capabilities and shared service tenants implicitly have all capabilities,
// so we currently never expect a tenant hitting their admin server -- whether
// that is their own process or the system one.
const expectDebugToRequireCap = false
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
tenant, _, err := s.TenantController().StartSharedProcessTenant(ctx,
base.TestSharedProcessTenantArgs{
TenantID: serverutils.TestTenantID(),
TenantName: "processdebug",
})
require.NoError(t, err)
defer tenant.AppStopper().Stop(ctx)
t.Run("system tenant pprof", func(t *testing.T) {
httpClient, err := s.GetAdminHTTPClient()
require.NoError(t, err)
defer httpClient.CloseIdleConnections()
url := s.AdminURL().WithPath("/debug/pprof/goroutine")
q := url.Query()
q.Add("debug", "2")
url.RawQuery = q.Encode()
resp, err := httpClient.Get(url.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Contains(t, string(body), "goroutine")
})
t.Run("pprof", func(t *testing.T) {
httpClient, err := tenant.GetAdminHTTPClient()
require.NoError(t, err)
defer httpClient.CloseIdleConnections()
url := tenant.AdminURL().WithPath("/debug/pprof/")
q := url.Query()
q.Add("debug", "2")
url.RawQuery = q.Encode()
if expectDebugToRequireCap {
resp, err := httpClient.Get(url.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusForbidden, resp.StatusCode)
require.Contains(t, string(body), "tenant does not have capability to debug the running process")
_, err = db.Exec(`ALTER TENANT processdebug GRANT CAPABILITY can_debug_process=true`)
require.NoError(t, err)
serverutils.WaitForTenantCapabilities(t, s, serverutils.TestTenantID(), map[tenantcapabilities.ID]string{
tenantcapabilities.CanDebugProcess: "true",
}, "")
}
resp, err := httpClient.Get(url.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Contains(t, string(body), "goroutine")
_, err = db.Exec(`ALTER TENANT processdebug REVOKE CAPABILITY can_debug_process`)
require.NoError(t, err)
serverutils.WaitForTenantCapabilities(t, s, serverutils.TestTenantID(), map[tenantcapabilities.ID]string{
tenantcapabilities.CanDebugProcess: "false",
}, "")
})
t.Run("vmodule", func(t *testing.T) {
httpClient, err := tenant.GetAdminHTTPClient()
require.NoError(t, err)
defer httpClient.CloseIdleConnections()
url := tenant.AdminURL().WithPath("/debug/vmodule")
q := url.Query()
q.Add("duration", "-1s")
q.Add("vmodule", "exec_log=3")
url.RawQuery = q.Encode()
if expectDebugToRequireCap {
resp, err := httpClient.Get(url.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusForbidden, resp.StatusCode)
require.Contains(t, string(body), "tenant does not have capability to debug the running process")
_, err = db.Exec(`ALTER TENANT processdebug GRANT CAPABILITY can_debug_process=true`)
require.NoError(t, err)
serverutils.WaitForTenantCapabilities(t, s, serverutils.TestTenantID(), map[tenantcapabilities.ID]string{
tenantcapabilities.CanDebugProcess: "true",
}, "")
}
resp, err := httpClient.Get(url.String())
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Contains(t, string(body), "previous vmodule configuration: \nnew vmodule configuration: exec_log=3\n")
_, err = db.Exec(`ALTER TENANT processdebug REVOKE CAPABILITY can_debug_process`)
require.NoError(t, err)
serverutils.WaitForTenantCapabilities(t, s, serverutils.TestTenantID(), map[tenantcapabilities.ID]string{
tenantcapabilities.CanDebugProcess: "false",
}, "")
})
}
func TestNonExistentTenant(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
_, err := s.TenantController().StartTenant(ctx,
base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
DisableCreateTenant: true,
SkipTenantCheck: true,
SkipWaitForTenantCache: true,
TestingKnobs: base.TestingKnobs{
Server: &server.TestingKnobs{
ShutdownTenantConnectorEarlyIfNoRecordPresent: true,
},
},
})
require.True(t, errors.Is(err, &kvpb.MissingRecordError{}))
}
// TestTenantRowIDs confirms `unique_rowid()` works as expected in a
// multi-tenant setup.
func TestTenantRowIDs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{
DefaultTestTenant: base.TestTenantAlwaysEnabled,
})
defer s.Stopper().Stop(ctx)
const numRows = 10
sqlDB := sqlutils.MakeSQLRunner(db)
sqlDB.Exec(t, `CREATE TABLE foo(key INT PRIMARY KEY DEFAULT unique_rowid(), val INT)`)
sqlDB.Exec(t, fmt.Sprintf("INSERT INTO foo (val) SELECT * FROM generate_series(1, %d)", numRows))
// Verify that the rows are inserted successfully and that the row ids
// are based on the SQL instance ID.
rows := sqlDB.Query(t, "SELECT key FROM foo")
defer rows.Close()
rowCount := 0
instanceID := int(s.ApplicationLayer().SQLInstanceID())
for rows.Next() {
var key int
if err := rows.Scan(&key); err != nil {
t.Fatal(err)
}
require.Equal(t, instanceID, key&instanceID)
rowCount++
}
require.Equal(t, numRows, rowCount)
}
// TestTenantInstanceIDReclaimLoop confirms that the sql_instances reclaim loop
// has been started.
func TestTenantInstanceIDReclaimLoop(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
clusterSettings := func() *cluster.Settings {
cs := cluster.MakeTestingClusterSettings()
instancestorage.ReclaimLoopInterval.Override(ctx, &cs.SV, 250*time.Millisecond)
instancestorage.PreallocatedCount.Override(ctx, &cs.SV, 5)
return cs
}
s := serverutils.StartServerOnly(t, base.TestServerArgs{
Settings: clusterSettings(),
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
_, db := serverutils.StartTenant(
t, s, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
Settings: clusterSettings(),
},
)
defer db.Close()
sqlDB := sqlutils.MakeSQLRunner(db)
var rowCount int64
testutils.SucceedsSoon(t, func() error {
sqlDB.QueryRow(t, `SELECT count(*) FROM system.sql_instances WHERE addr IS NULL`).Scan(&rowCount)
// We set PreallocatedCount to 5. When the tenant gets started, it drops
// to 4. Eventually this will be 5 if the reclaim loop runs.
if rowCount == 5 {
return nil
}
return fmt.Errorf("waiting for preallocated rows")
})
}
// TestStartTenantWithStaleInstance covers the following scenario:
// - a sql server starts up and is assigned port 'a'
// - the sql server shuts down and releases port 'a'
// - something else starts up and claims port 'a'. In the test that is the
// listener. This is important because the listener causes connections to 'a' to
// hang instead of responding with a RESET packet.
// - a different server with stale instance information schedules a distsql
// flow and attempts to dial port 'a'.
func TestStartTenantWithStaleInstance(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s := serverutils.StartServerOnly(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)
var listener net.Listener
// In rare cases under stress net.Listen call can result in an error that
// the address is already in use (because the stopped tenant hasn't released
// the socket); thus, we allow for some retries to go around that issue.
testutils.SucceedsSoon(t, func() error {
rpcAddr := func() string {
tenantStopper := stop.NewStopper()
defer tenantStopper.Stop(ctx)
server, db := serverutils.StartTenant(t, s, base.TestTenantArgs{
Stopper: tenantStopper,
TenantID: serverutils.TestTenantID(),
},
)
defer db.Close()
return server.RPCAddr()
}()
var err error
listener, err = net.Listen("tcp", rpcAddr)
return err
})
defer func() {
_ = listener.Close()
}()
_, db := serverutils.StartTenant(t, s, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
})
defer func() {
_ = db.Close()
}()
// Query a table to make sure the tenant is healthy, doesn't really matter
// which table.
_, err := db.Exec("SELECT count(*) FROM system.sqlliveness")
require.NoError(t, err)
}