Skip to content

Commit

Permalink
Merge #69381 #69406
Browse files Browse the repository at this point in the history
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
3 people committed Aug 27, 2021
3 parents 8cae60f + 4a70b3d + 3d2e834 commit 2b8d42d
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 125 deletions.
3 changes: 2 additions & 1 deletion docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ sql.defaults.disallow_full_table_scans.enabled boolean false setting to true rej
sql.defaults.distsql enumeration auto default distributed SQL execution mode [off = 0, auto = 1, on = 2]
sql.defaults.drop_enum_value.enabled boolean false default value for enable_drop_enum_value; allows for dropping enum values
sql.defaults.experimental_alter_column_type.enabled boolean false default value for experimental_alter_column_type session setting; enables the use of ALTER COLUMN TYPE for general conversions
sql.defaults.experimental_auto_rehoming.enabled boolean false default value for experimental_enable_auto_rehoming; allows for rows in REGIONAL BY ROW tables to be auto-rehomed on UPDATE
sql.defaults.experimental_distsql_planning enumeration off default experimental_distsql_planning mode; enables experimental opt-driven DistSQL planning [off = 0, on = 1]
sql.defaults.experimental_enable_unique_without_index_constraints.enabled boolean false default value for experimental_enable_unique_without_index_constraints session setting;disables unique without index constraints by default
sql.defaults.experimental_hash_sharded_indexes.enabled boolean false default value for experimental_enable_hash_sharded_indexes; allows for creation of hash sharded indexes by default
Expand Down Expand Up @@ -161,4 +162,4 @@ trace.datadog.project string CockroachDB the project under which traces will be
trace.debug.enable boolean false if set, traces for recent requests can be seen at https://<ui>/debug/requests
trace.lightstep.token string if set, traces go to Lightstep using this token
trace.zipkin.collector string if set, traces go to the given Zipkin instance (example: '127.0.0.1:9411'). Only one tracer can be configured at a time.
version version 21.1-158 set the active cluster version in the format '<major>.<minor>'
version version 21.1-160 set the active cluster version in the format '<major>.<minor>'
3 changes: 2 additions & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<tr><td><code>sql.defaults.distsql</code></td><td>enumeration</td><td><code>auto</code></td><td>default distributed SQL execution mode [off = 0, auto = 1, on = 2]</td></tr>
<tr><td><code>sql.defaults.drop_enum_value.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for enable_drop_enum_value; allows for dropping enum values</td></tr>
<tr><td><code>sql.defaults.experimental_alter_column_type.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for experimental_alter_column_type session setting; enables the use of ALTER COLUMN TYPE for general conversions</td></tr>
<tr><td><code>sql.defaults.experimental_auto_rehoming.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for experimental_enable_auto_rehoming; allows for rows in REGIONAL BY ROW tables to be auto-rehomed on UPDATE</td></tr>
<tr><td><code>sql.defaults.experimental_distsql_planning</code></td><td>enumeration</td><td><code>off</code></td><td>default experimental_distsql_planning mode; enables experimental opt-driven DistSQL planning [off = 0, on = 1]</td></tr>
<tr><td><code>sql.defaults.experimental_enable_unique_without_index_constraints.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for experimental_enable_unique_without_index_constraints session setting;disables unique without index constraints by default</td></tr>
<tr><td><code>sql.defaults.experimental_hash_sharded_indexes.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for experimental_enable_hash_sharded_indexes; allows for creation of hash sharded indexes by default</td></tr>
Expand Down Expand Up @@ -165,6 +166,6 @@
<tr><td><code>trace.debug.enable</code></td><td>boolean</td><td><code>false</code></td><td>if set, traces for recent requests can be seen at https://<ui>/debug/requests</td></tr>
<tr><td><code>trace.lightstep.token</code></td><td>string</td><td><code></code></td><td>if set, traces go to Lightstep using this token</td></tr>
<tr><td><code>trace.zipkin.collector</code></td><td>string</td><td><code></code></td><td>if set, traces go to the given Zipkin instance (example: '127.0.0.1:9411'). Only one tracer can be configured at a time.</td></tr>
<tr><td><code>version</code></td><td>version</td><td><code>21.1-158</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
<tr><td><code>version</code></td><td>version</td><td><code>21.1-160</code></td><td>set the active cluster version in the format '<major>.<minor>'</td></tr>
</tbody>
</table>
3 changes: 3 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,9 @@ This will error if the current database is not a multi-region database.</p>
</span></td></tr>
<tr><td><a name="gateway_region"></a><code>gateway_region() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the region of the connection’s current node as defined by
the locality flag on node startup. Returns an error if no region is set.</p>
</span></td></tr>
<tr><td><a name="rehome_row"></a><code>rehome_row() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the region of the connection’s current node as defined by
the locality flag on node startup. Returns an error if no region is set.</p>
</span></td></tr></tbody>
</table>

Expand Down
45 changes: 45 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/auto_rehoming_mixed
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
2 changes: 2 additions & 0 deletions pkg/ccl/multiregionccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_test(
name = "multiregionccl_test",
size = "enormous",
srcs = [
"auto_rehoming_test.go",
"datadriven_test.go",
"main_test.go",
"multiregion_test.go",
Expand All @@ -31,6 +32,7 @@ go_test(
"show_test.go",
],
data = glob(["testdata/**"]),
embed = [":multiregionccl"],
deps = [
"//pkg/base",
"//pkg/ccl",
Expand Down
132 changes: 132 additions & 0 deletions pkg/ccl/multiregionccl/auto_rehoming_test.go
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,
)
}
})
}
}
}
8 changes: 6 additions & 2 deletions pkg/clusterversion/cockroach_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ const (
// compaction on cluster startup and ensures that there is only one entry for
// the schedule.
SQLStatsCompactionScheduledJob

// DateAndIntervalStyle enables DateStyle and IntervalStyle to be changed.
DateAndIntervalStyle
// Step (1): Add new versions here.
)

Expand Down Expand Up @@ -555,7 +556,10 @@ var versionsSingleton = keyedVersions{
Key: SQLStatsCompactionScheduledJob,
Version: roachpb.Version{Major: 21, Minor: 1, Internal: 158},
},

{
Key: DateAndIntervalStyle,
Version: roachpb.Version{Major: 21, Minor: 1, Internal: 160},
},
// Step (2): Add new versions here.
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/clusterversion/key_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/sql/alter_table_locality.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (n *alterTableSetLocalityNode) alterTableLocalityToRegionalByRow(
ColumnDef: regionalByRowDefaultColDef(
enumOID,
regionalByRowRegionDefaultExpr(enumOID, tree.Name(primaryRegion)),
maybeRegionalByRowOnUpdateExpr(params.EvalContext(), enumOID),
),
}
tn, err := params.p.getQualifiedTableName(params.ctx, n.tableDesc)
Expand Down
28 changes: 26 additions & 2 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,11 @@ func NewTableDesc(
oid := typedesc.TypeIDToOID(regionConfig.RegionEnumID())
n.Defs = append(
n.Defs,
regionalByRowDefaultColDef(oid, regionalByRowGatewayRegionDefaultExpr(oid)),
regionalByRowDefaultColDef(
oid,
regionalByRowGatewayRegionDefaultExpr(oid),
maybeRegionalByRowOnUpdateExpr(evalCtx, oid),
),
)
columnDefaultExprs = append(columnDefaultExprs, nil)
}
Expand Down Expand Up @@ -2792,14 +2796,34 @@ func regionalByRowGatewayRegionDefaultExpr(oid oid.Oid) tree.Expr {
}
}

func regionalByRowDefaultColDef(oid oid.Oid, defaultExpr tree.Expr) *tree.ColumnTableDef {
// maybeRegionalByRowOnUpdateExpr returns a gateway region default statement if
// the auto rehoming session setting is enabled, nil otherwise.
func maybeRegionalByRowOnUpdateExpr(evalCtx *tree.EvalContext, enumOid oid.Oid) tree.Expr {
if evalCtx.SessionData().AutoRehomingEnabled &&
evalCtx.Settings.Version.IsActive(evalCtx.Ctx(), clusterversion.OnUpdateExpressions) {
return &tree.CastExpr{
Expr: &tree.FuncExpr{
Func: tree.WrapFunction(builtins.RehomeRowBuiltinName),
},
Type: &tree.OIDTypeReference{OID: enumOid},
SyntaxMode: tree.CastShort,
}
}
return nil
}

func regionalByRowDefaultColDef(
oid oid.Oid, defaultExpr tree.Expr, onUpdateExpr tree.Expr,
) *tree.ColumnTableDef {
c := &tree.ColumnTableDef{
Name: tree.RegionalByRowRegionDefaultColName,
Type: &tree.OIDTypeReference{OID: oid},
Hidden: true,
}
c.Nullable.Nullability = tree.NotNull
c.DefaultExpr.Expr = defaultExpr
c.OnUpdateExpr.Expr = onUpdateExpr

return c
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ var placementEnabledClusterMode = settings.RegisterBoolSetting(
false,
)

var autoRehomingEnabledClusterMode = settings.RegisterBoolSetting(
"sql.defaults.experimental_auto_rehoming.enabled",
"default value for experimental_enable_auto_rehoming;"+
" allows for rows in REGIONAL BY ROW tables to be auto-rehomed on UPDATE",
false,
).WithPublic()

var temporaryTablesEnabledClusterMode = settings.RegisterBoolSetting(
"sql.defaults.experimental_temporary_tables.enabled",
"default value for experimental_enable_temp_tables; allows for use of temporary tables by default",
Expand Down Expand Up @@ -2751,6 +2758,10 @@ func (m *sessionDataMutator) SetPlacementEnabled(val bool) {
m.data.PlacementEnabled = val
}

func (m *sessionDataMutator) SetAutoRehomingEnabled(val bool) {
m.data.AutoRehomingEnabled = val
}

func (m *sessionDataMutator) SetTempTablesEnabled(val bool) {
m.data.TempTablesEnabled = val
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -4612,6 +4612,7 @@ enable_seqscan on
enable_zigzag_join on
escape_string_warning on
experimental_computed_column_rewrites ·
experimental_enable_auto_rehoming off
experimental_enable_hash_sharded_indexes off
experimental_enable_implicit_column_partitioning off
experimental_enable_temp_tables off
Expand Down
Loading

0 comments on commit 2b8d42d

Please sign in to comment.