-
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.
69381: sql: enable auto-rehoming of REGIONAL BY ROW tables r=arulajmani a=pawalt Previously, REGIONAL BY ROW tables never had their rows rehomed unless the user manually specified to do so. This PR adds a cluster setting, `sql.defaults.auto_rehoming.enabled` and a session setting, `enable_auto_rehoming` that will rehoming rows to the user's gateway region on UPDATE. Release justification: low-risk change on existing feature, hidden behind a cluster setting. Release note (sql change): The cluster setting `sql.defaults.auto_rehoming.enabled` and session setting `enable_auto_rehoming` were added to enable auto-rehoming on UPDATE for REGIONAL BY ROW tables. 69406: sql: version gate DateStyle/IntervalStyle_enabled r=rafiss a=otan Release justification: fix to new functionality Release note: None Co-authored-by: Peyton Walters <peyton.walters@cockroachlabs.com> Co-authored-by: Oliver Tan <otan@cockroachlabs.com>
- Loading branch information
Showing
19 changed files
with
482 additions
and
125 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
45 changes: 45 additions & 0 deletions
45
pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming_mixed
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,45 @@ | ||
# LogicTest: local-mixed-21.1-21.2 | ||
|
||
statement ok | ||
SET CLUSTER SETTING sql.defaults.experimental_auto_rehoming.enabled = true; | ||
|
||
statement ok | ||
SET experimental_enable_auto_rehoming = true; | ||
|
||
statement ok | ||
CREATE DATABASE db PRIMARY REGION "test"; | ||
|
||
statement ok | ||
CREATE TABLE db.rbr ( | ||
p INT PRIMARY KEY, | ||
s STRING, | ||
FAMILY "primary" (p, s, crdb_region) | ||
) LOCALITY REGIONAL BY ROW | ||
|
||
query TT | ||
SHOW CREATE TABLE db.rbr | ||
---- | ||
db.public.rbr CREATE TABLE public.rbr ( | ||
p INT8 NOT NULL, | ||
s STRING NULL, | ||
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, | ||
CONSTRAINT "primary" PRIMARY KEY (p ASC), | ||
FAMILY "primary" (p, s, crdb_region) | ||
) LOCALITY REGIONAL BY ROW | ||
|
||
|
||
statement ok | ||
CREATE TABLE db.rbr_altered (p INT PRIMARY KEY) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION | ||
|
||
statement ok | ||
ALTER TABLE db.rbr_altered SET LOCALITY REGIONAL BY ROW | ||
|
||
query TT | ||
SHOW CREATE TABLE db.rbr_altered | ||
---- | ||
db.public.rbr_altered CREATE TABLE public.rbr_altered ( | ||
p INT8 NOT NULL, | ||
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region, | ||
CONSTRAINT "primary" PRIMARY KEY (p ASC), | ||
FAMILY "primary" (p, crdb_region) | ||
) LOCALITY REGIONAL BY ROW |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2021 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 ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/ccl/multiregionccl/multiregionccltestutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
func TestAutoHoming(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
tests := []struct { | ||
name string | ||
createStatement string | ||
expectedLocality string | ||
}{ | ||
{ | ||
name: "auto homing", | ||
createStatement: ` | ||
SET CLUSTER SETTING sql.defaults.experimental_auto_rehoming.enabled = true; | ||
SET experimental_enable_auto_rehoming = true; | ||
CREATE DATABASE test PRIMARY REGION "us-east2" REGIONS "us-east1", "us-east3"; | ||
CREATE TABLE test.rbr (p INT PRIMARY KEY, s STRING) LOCALITY REGIONAL BY ROW`, | ||
expectedLocality: "us-east3", | ||
}, | ||
{ | ||
name: "auto homing with altered table", | ||
createStatement: ` | ||
SET CLUSTER SETTING sql.defaults.experimental_auto_rehoming.enabled = true; | ||
SET experimental_enable_auto_rehoming = true; | ||
CREATE DATABASE test PRIMARY REGION "us-east2" REGIONS "us-east1", "us-east3"; | ||
CREATE TABLE test.rbr (p INT PRIMARY KEY, s STRING) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION; | ||
ALTER TABLE test.rbr SET LOCALITY REGIONAL BY ROW`, | ||
expectedLocality: "us-east3", | ||
}, | ||
{ | ||
name: "auto homing disabled", | ||
createStatement: ` | ||
CREATE DATABASE test PRIMARY REGION "us-east2" REGIONS "us-east1", "us-east3"; | ||
CREATE TABLE test.rbr (p INT PRIMARY KEY, s STRING) LOCALITY REGIONAL BY ROW`, | ||
expectedLocality: "us-east1", | ||
}, | ||
{ | ||
name: "auto homing outside region", | ||
createStatement: ` | ||
SET CLUSTER SETTING sql.defaults.experimental_auto_rehoming.enabled = true; | ||
SET experimental_enable_auto_rehoming = true; | ||
CREATE DATABASE test PRIMARY REGION "us-east2" REGIONS "us-east1"; | ||
CREATE TABLE test.rbr (p INT PRIMARY KEY, s STRING) LOCALITY REGIONAL BY ROW`, | ||
expectedLocality: "us-east2", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
updateTypes := []struct { | ||
name string | ||
stmt string | ||
}{ | ||
{ | ||
name: "update", | ||
stmt: `UPDATE rbr SET (s) = ('whaddup') WHERE p = 1`, | ||
}, | ||
{ | ||
name: "upsert", | ||
stmt: `UPSERT INTO rbr (p, s) VALUES (1, 'whaddup')`, | ||
}, | ||
} | ||
for _, updateType := range updateTypes { | ||
t.Run(test.name+"-"+updateType.name, func(t *testing.T) { | ||
tc, _, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster( | ||
t, | ||
3, | ||
base.TestingKnobs{}, | ||
) | ||
defer cleanup() | ||
|
||
// Create a separate bootstrapping connection so new connections have the | ||
// cluster setting applied if necessary. | ||
bootstrapCon := sqlutils.MakeSQLRunner(tc.Conns[0]) | ||
bootstrapCon.Exec(t, test.createStatement) | ||
|
||
sql0 := sqlutils.MakeSQLRunner(tc.Conns[0]) | ||
sql2 := sqlutils.MakeSQLRunner(tc.Conns[2]) | ||
|
||
sql0.Exec(t, "USE test") | ||
sql2.Exec(t, "USE test") | ||
|
||
var crdbRegion string | ||
|
||
sql0.Exec(t, ` | ||
INSERT INTO rbr (p, s) VALUES (1, 'hi'); | ||
`) | ||
|
||
row := sql0.QueryRow(t, ` | ||
SELECT crdb_region FROM rbr WHERE p = 1`) | ||
row.Scan(&crdbRegion) | ||
|
||
if crdbRegion != "us-east1" { | ||
t.Fatalf( | ||
"expected initial crdbRegion to be us-east1 but got %s", | ||
crdbRegion, | ||
) | ||
} | ||
|
||
sql2.Exec(t, updateType.stmt) | ||
|
||
row = sql0.QueryRow(t, ` | ||
SELECT crdb_region FROM rbr WHERE p = 1`) | ||
row.Scan(&crdbRegion) | ||
|
||
if crdbRegion != test.expectedLocality { | ||
t.Fatalf( | ||
"expected crdbRegion after update to be %s but got %s", | ||
test.expectedLocality, | ||
crdbRegion, | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.