-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slstorage: migrate system.sqlliveness to an rbr table
Migrate the sqlliveness table to a format that is compatible with regional by row tables. Multi region serverless is the motiviation for this change. When a sql server starts up, it must write its session to the sqlliveness table. The remote session write can add ~400ms to a servers startup time. Part of #85736 Release note: None
- Loading branch information
1 parent
dc21074
commit 9707e5e
Showing
19 changed files
with
387 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
pkg/ccl/multiregionccl/multiregion_system_table_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2022 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 multiregionccl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cockroachdb/apd/v3" | ||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/ccl/multiregionccl/multiregionccltestutils" | ||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/enum" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slstorage" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/timeutil" | ||
"github.com/cockroachdb/cockroach/pkg/util/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createSqllivenessTable( | ||
t *testing.T, db *sqlutils.SQLRunner, dbName string, | ||
) (tableID descpb.ID) { | ||
t.Helper() | ||
db.Exec(t, fmt.Sprintf(` | ||
CREATE DATABASE IF NOT EXISTS "%s" | ||
WITH PRIMARY REGION "us-east1" | ||
REGIONS "us-east1", "us-east2", "us-east3" | ||
`, dbName)) | ||
|
||
// expiration needs to be column 2. slstorage.Table assumes the column id. | ||
// session_uuid and crdb_region are identified by their location in the | ||
// primary key. | ||
db.Exec(t, fmt.Sprintf(` | ||
CREATE TABLE "%s".sqlliveness ( | ||
session_uuid BYTES NOT NULL, | ||
expiration DECIMAL NOT NULL, | ||
crdb_region "%s".public.crdb_internal_region, | ||
PRIMARY KEY(crdb_region, session_uuid) | ||
) LOCALITY REGIONAL BY ROW; | ||
`, dbName, dbName)) | ||
db.QueryRow(t, ` | ||
select u.id | ||
from system.namespace t | ||
join system.namespace u | ||
on t.id = u."parentID" | ||
where t.name = $1 and u.name = $2`, | ||
dbName, "sqlliveness").Scan(&tableID) | ||
return tableID | ||
} | ||
|
||
func TestRbrSqllivenessTable(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
|
||
cluster, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster(t, 3, base.TestingKnobs{}) | ||
defer cleanup() | ||
settings := cluster.Servers[0].Cfg.Settings | ||
kvDB := cluster.Servers[0].DB() | ||
|
||
tDB := sqlutils.MakeSQLRunner(sqlDB) | ||
|
||
t0 := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) | ||
timeSource := timeutil.NewManualTime(t0) | ||
clock := hlc.NewClock(timeSource, base.DefaultMaxClockOffset) | ||
|
||
setup := func(t *testing.T) slstorage.Table { | ||
dbName := t.Name() | ||
tableID := createSqllivenessTable(t, tDB, dbName) | ||
return slstorage.MakeTestTable(settings, keys.SystemSQLCodec, tableID, 1, 2) | ||
} | ||
|
||
t.Run("SqlRead", func(t *testing.T) { | ||
table := setup(t) | ||
|
||
initialUUID := uuid.MakeV4() | ||
session, err := slstorage.MakeSessionID(enum.One, initialUUID) | ||
require.NoError(t, err) | ||
|
||
writeExpiration := clock.Now().Add(10, 00) | ||
require.NoError(t, kvDB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { | ||
return table.SetExpiration(ctx, txn, session, writeExpiration) | ||
})) | ||
|
||
var sessionUUID string | ||
var crdbRegion string | ||
var rawExpiration apd.Decimal | ||
|
||
row := tDB.QueryRow(t, fmt.Sprintf(`SELECT crdb_region, session_uuid, expiration FROM "%s".sqlliveness`, t.Name())) | ||
row.Scan(&crdbRegion, &sessionUUID, &rawExpiration) | ||
|
||
require.Contains(t, []string{"us-east1", "us-east2", "us-east3"}, crdbRegion) | ||
require.Equal(t, sessionUUID, string(initialUUID.GetBytes())) | ||
|
||
readExpiration, err := hlc.DecimalToHLC(&rawExpiration) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, writeExpiration, readExpiration) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.