-
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.
sql: allow ALTER DATABASE PRIMARY REGION to work on system tenants
Previously, on a multi-region setup the system database could not be modified to be multi-region and it was blocked from being made multi-region aware. To address this, we are now allowing ALTER DATABASE PRIMARY REGION to work on system tenants. Fixes: #63365 Epic: CRDB-33032 Release note (sql change): Previously, we added support for settings reegion on the system database, which was limited to tenants only. We lifted this limitation to allow ALTER DATABASE PRIMARY REGION to work on system tenants. To support preview status, we created a cluster setting called sql.multiregion.preview_multiregion_system_database that will give users the option to set up their system database as multi-region for Cockroach dedicated (this cluster setting is not enabled by default). Note that after adding non-primary regions, we recommend that users do a rolling restart to propogate region information.
- Loading branch information
1 parent
49173f4
commit ef0fde6
Showing
7 changed files
with
238 additions
and
3 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
128 changes: 128 additions & 0 deletions
128
pkg/cmd/roachtest/tests/multi_region_system_database.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,128 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/spec" | ||
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test" | ||
"github.com/cockroachdb/cockroach/pkg/roachprod/install" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func registerMultiRegionSystemDatabase(r registry.Registry) { | ||
clusterSpec := r.MakeClusterSpec(3, spec.Geo(), spec.GatherCores(), spec.GCEZones("us-east1-b,us-west1-b,us-central1-b")) | ||
r.Add(registry.TestSpec{ | ||
Name: "multi-region-system-database", | ||
Owner: registry.OwnerSQLFoundations, | ||
Timeout: time.Hour * 1, | ||
RequiresLicense: true, | ||
Cluster: clusterSpec, | ||
CompatibleClouds: registry.OnlyGCE, | ||
Suites: registry.Suites(registry.Weekly), | ||
Run: func(ctx context.Context, t test.Test, c cluster.Cluster) { | ||
nodes := c.Spec().NodeCount | ||
regions := strings.Split(c.Spec().GCE.Zones, ",") | ||
regionOnly := func(regionAndZone string) string { | ||
r := strings.Split(regionAndZone, "-") | ||
return r[0] + "-" + r[1] | ||
} | ||
t.Status("starting cluster") | ||
c.Start(ctx, t.L(), option.DefaultStartOpts(), install.MakeClusterSettings(install.SecureOption(false))) | ||
conn := c.Conn(ctx, t.L(), 1) | ||
defer conn.Close() | ||
|
||
_, err := conn.ExecContext(ctx, "SET CLUSTER SETTING sql.multiregion.preview_multiregion_system_database.enabled = true") | ||
require.NoError(t, err) | ||
|
||
_, err = conn.ExecContext(ctx, | ||
fmt.Sprintf(`ALTER DATABASE system SET PRIMARY REGION '%s'`, regionOnly(regions[0]))) | ||
require.NoError(t, err) | ||
|
||
_, err = conn.ExecContext(ctx, | ||
fmt.Sprintf(`ALTER DATABASE system ADD REGION '%s'`, regionOnly(regions[1]))) | ||
require.NoError(t, err) | ||
|
||
_, err = conn.ExecContext(ctx, | ||
fmt.Sprintf(`ALTER DATABASE system ADD REGION '%s'`, regionOnly(regions[2]))) | ||
require.NoError(t, err) | ||
|
||
//Perform rolling restart to propagate region information to non-primary nodes | ||
for i := 2; i <= nodes; i++ { | ||
t.WorkerStatus("stop") | ||
err := c.StopCockroachGracefullyOnNode(ctx, t.L(), i) | ||
if err != nil { | ||
return | ||
} | ||
t.WorkerStatus("start") | ||
startOpts := option.DefaultStartOpts() | ||
c.Start(ctx, t.L(), startOpts, install.MakeClusterSettings(install.SecureOption(false)), c.Node(i)) | ||
} | ||
|
||
//Check system.lease table to ensure that region information for each node is correct | ||
rows, err := conn.Query("SELECT DISTINCT sql_instance_id, crdb_region FROM system.lease") | ||
require.NoError(t, err) | ||
|
||
nodeToRegionName := make(map[int]string) | ||
for rows.Next() { | ||
var sqlInstanceID int | ||
var crdbRegion string | ||
require.NoError(t, rows.Scan(&sqlInstanceID, &crdbRegion)) | ||
nodeToRegionName[sqlInstanceID] = crdbRegion | ||
} | ||
|
||
for node, regionName := range nodeToRegionName { | ||
require.Equal(t, regionOnly(regions[node-1]), regionName) | ||
} | ||
|
||
//Intentionally tear down nodes and ensure that everything is still working | ||
chaosTest := func() { | ||
//Random operations on user-created table | ||
_, err := conn.Exec(`CREATE TABLE foo (key INT PRIMARY KEY)`) | ||
if err != nil { | ||
return | ||
} | ||
defer func() { | ||
_, err := conn.Exec(`DROP TABLE foo`) | ||
if err != nil { | ||
return | ||
} | ||
}() | ||
_, err = conn.Exec(`INSERT INTO foo VALUES (1), (2), (3)`) | ||
require.NoError(t, err) | ||
row := conn.QueryRow(`SELECT * FROM foo LIMIT 1`) | ||
var rowPK int | ||
require.NoError(t, row.Scan(&rowPK)) | ||
require.Equal(t, 1, rowPK) | ||
} | ||
|
||
for i := 2; i <= nodes; i++ { | ||
t.WorkerStatus("stop") | ||
stopOpts := option.DefaultStopOpts() | ||
c.Stop(ctx, t.L(), stopOpts, c.Node(i)) | ||
|
||
t.WorkerStatus("chaos testing") | ||
chaosTest() | ||
|
||
t.WorkerStatus("start") | ||
startOpts := option.DefaultStartOpts() | ||
c.Start(ctx, t.L(), startOpts, install.MakeClusterSettings(install.SecureOption(false)), c.Node(i)) | ||
} | ||
}, | ||
}) | ||
} |
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