Commit 9b507562 authored by craig[bot]'s avatar craig[bot]

Merge #49784

49784: config: introduce pseudo "tenants" zone r=nvanbenschoten a=nvanbenschoten

Fixes #49318.
Fixes #49445.
Progress towards #48123.
Informs #48774.

This commit introduces a new pseudo object ID in the system tenant's namespace called "tenants". Like "liveness" and "timeseries" before it, the pseudo object allows zone configurations to be applied to pseudo-objects that do not live directly in the system tenant's SQL keyspace. In this case, the "tenants" zone allows zone configurations to be set by the system tenant and applied to all other tenants in the system. There may come a time when we want secondary tenants to have more control over their zone configurations, but that will require a much larger change to the zone config structure and UX as a whole.

While making this change, we rationalize the rest of zone configuration handling and how it relates to multi-tenancy. Now that secondary tenant ranges have a zone config to call their own, we can make sense of calls from SQL into the zone configuration infrastructure. We gate off calls that don't make sense for secondary tenants and clean up hooks in SQL that handle zone config manipulation.

All of this works towards a good cause - we eliminate the remaining uses of `keys.TODOSQLCodec` from `pkg/sql/...` and `pkg/config/...`, bringing us a big step closer towards being able to remove the placeholder and close #48123.

This work also reveals that in order to address #48774, we need to be able to determine split points from the SystemConfig. This makes it very difficult to split on secondary tenant object (e.g. table) boundaries. However, it makes it straightforward to split on secondary tenant keysapce boundaries. This is already what we were thinking (see #47907), so out both convenience and desire, I expect that we'll follow this up with a PR that splits Ranges only at secondary tenant boundaries - placing the overhead of an otherwise empty tenant at only a single Range and a few KBs of data.
Co-authored-by: default avatarNathan VanBenschoten <[email protected]>
parents c817d37b 69531820
......@@ -1228,7 +1228,7 @@ func TestBackupRestoreControlJob(t *testing.T) {
if err != nil {
t.Fatal(err)
}
last := uint32(v.ValueInt())
last := config.SystemTenantObjectID(v.ValueInt())
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.RangeMaxBytes = proto.Int64(5000)
config.TestingSetZoneConfig(last+1, zoneConfig)
......
......@@ -55,7 +55,7 @@ func setupExportableBank(t *testing.T, nodes, rows int) (*sqlutils.SQLRunner, st
if err != nil {
t.Fatal(err)
}
last := uint32(v.ValueInt())
last := config.SystemTenantObjectID(v.ValueInt())
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.RangeMaxBytes = proto.Int64(5000)
config.TestingSetZoneConfig(last+1, zoneConfig)
......
......@@ -1406,7 +1406,7 @@ ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (y)
// Get the zone config corresponding to the table.
table := sqlbase.GetTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "t")
kv, err := kvDB.Get(ctx, config.MakeZoneKey(uint32(table.ID)))
kv, err := kvDB.Get(ctx, config.MakeZoneKey(config.SystemTenantObjectID(table.ID)))
if err != nil {
t.Fatal(err)
}
......
......@@ -54,7 +54,7 @@ requesting heap files for node 1... writing: debug/nodes/1/heapprof.err.txt
requesting goroutine files for node 1... writing: debug/nodes/1/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/1/ranges/1.json
writing: debug/nodes/1/ranges/2.json
writing: debug/nodes/1/ranges/3.json
......@@ -88,6 +88,7 @@ writing: debug/nodes/1/ranges/30.json
writing: debug/nodes/1/ranges/31.json
writing: debug/nodes/1/ranges/32.json
writing: debug/nodes/1/ranges/33.json
writing: debug/nodes/1/ranges/34.json
writing: debug/nodes/2/status.json
using SQL connection URL for node 2: postgresql://...
retrieving SQL data for crdb_internal.feature_usage... writing: debug/nodes/2/crdb_internal.feature_usage.txt
......@@ -179,7 +180,7 @@ requesting heap files for node 3... writing: debug/nodes/3/heapprof.err.txt
requesting goroutine files for node 3... writing: debug/nodes/3/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/3/ranges/1.json
writing: debug/nodes/3/ranges/2.json
writing: debug/nodes/3/ranges/3.json
......@@ -213,6 +214,7 @@ writing: debug/nodes/3/ranges/30.json
writing: debug/nodes/3/ranges/31.json
writing: debug/nodes/3/ranges/32.json
writing: debug/nodes/3/ranges/33.json
writing: debug/nodes/3/ranges/34.json
requesting list of SQL databases... 3 found
requesting database details for defaultdb... writing: debug/schema/[email protected]
0 tables found
......
......@@ -54,7 +54,7 @@ requesting heap files for node 1... writing: debug/nodes/1/heapprof.err.txt
requesting goroutine files for node 1... writing: debug/nodes/1/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/1/ranges/1.json
writing: debug/nodes/1/ranges/2.json
writing: debug/nodes/1/ranges/3.json
......@@ -88,6 +88,7 @@ writing: debug/nodes/1/ranges/30.json
writing: debug/nodes/1/ranges/31.json
writing: debug/nodes/1/ranges/32.json
writing: debug/nodes/1/ranges/33.json
writing: debug/nodes/1/ranges/34.json
writing: debug/nodes/2.skipped
writing: debug/nodes/3/status.json
using SQL connection URL for node 3: postgresql://...
......@@ -116,7 +117,7 @@ requesting heap files for node 3... writing: debug/nodes/3/heapprof.err.txt
requesting goroutine files for node 3... writing: debug/nodes/3/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/3/ranges/1.json
writing: debug/nodes/3/ranges/2.json
writing: debug/nodes/3/ranges/3.json
......@@ -150,6 +151,7 @@ writing: debug/nodes/3/ranges/30.json
writing: debug/nodes/3/ranges/31.json
writing: debug/nodes/3/ranges/32.json
writing: debug/nodes/3/ranges/33.json
writing: debug/nodes/3/ranges/34.json
requesting list of SQL databases... 3 found
requesting database details for defaultdb... writing: debug/schema/[email protected]
0 tables found
......
......@@ -54,7 +54,7 @@ requesting heap files for node 1... writing: debug/nodes/1/heapprof.err.txt
requesting goroutine files for node 1... writing: debug/nodes/1/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/1/ranges/1.json
writing: debug/nodes/1/ranges/2.json
writing: debug/nodes/1/ranges/3.json
......@@ -88,6 +88,7 @@ writing: debug/nodes/1/ranges/30.json
writing: debug/nodes/1/ranges/31.json
writing: debug/nodes/1/ranges/32.json
writing: debug/nodes/1/ranges/33.json
writing: debug/nodes/1/ranges/34.json
writing: debug/nodes/3/status.json
using SQL connection URL for node 3: postgresql://...
retrieving SQL data for crdb_internal.feature_usage... writing: debug/nodes/3/crdb_internal.feature_usage.txt
......@@ -115,7 +116,7 @@ requesting heap files for node 3... writing: debug/nodes/3/heapprof.err.txt
requesting goroutine files for node 3... writing: debug/nodes/3/goroutines.err.txt
^- resulted in ...
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/3/ranges/1.json
writing: debug/nodes/3/ranges/2.json
writing: debug/nodes/3/ranges/3.json
......@@ -149,6 +150,7 @@ writing: debug/nodes/3/ranges/30.json
writing: debug/nodes/3/ranges/31.json
writing: debug/nodes/3/ranges/32.json
writing: debug/nodes/3/ranges/33.json
writing: debug/nodes/3/ranges/34.json
requesting list of SQL databases... 3 found
requesting database details for defaultdb... writing: debug/schema/[email protected]
0 tables found
......
......@@ -52,7 +52,7 @@ requesting heap profile for node 1... writing: debug/nodes/1/heap.pprof
requesting heap files for node 1... ? found
requesting goroutine files for node 1... 0 found
requesting log file ...
requesting ranges... 33 found
requesting ranges... 34 found
writing: debug/nodes/1/ranges/1.json
writing: debug/nodes/1/ranges/2.json
writing: debug/nodes/1/ranges/3.json
......@@ -86,6 +86,7 @@ writing: debug/nodes/1/ranges/30.json
writing: debug/nodes/1/ranges/31.json
writing: debug/nodes/1/ranges/32.json
writing: debug/nodes/1/ranges/33.json
writing: debug/nodes/1/ranges/34.json
requesting list of SQL databases... 3 found
requesting database details for defaultdb... writing: debug/schema/[email protected]
0 tables found
......
......@@ -17,19 +17,20 @@ import (
// MakeZoneKeyPrefix returns the key prefix for id's row in the system.zones
// table.
func MakeZoneKeyPrefix(id uint32) roachpb.Key {
return keys.SystemSQLCodec.ZoneKeyPrefix(id)
func MakeZoneKeyPrefix(id SystemTenantObjectID) roachpb.Key {
return keys.SystemSQLCodec.ZoneKeyPrefix(uint32(id))
}
// MakeZoneKey returns the key for id's entry in the system.zones table.
func MakeZoneKey(id uint32) roachpb.Key {
return keys.SystemSQLCodec.ZoneKey(id)
func MakeZoneKey(id SystemTenantObjectID) roachpb.Key {
return keys.SystemSQLCodec.ZoneKey(uint32(id))
}
// DecodeObjectID decodes the object ID from the front of key. It returns the
// decoded object ID, the remainder of the key, and whether the result is valid
// (i.e., whether the key was within the structured key space).
func DecodeObjectID(key roachpb.RKey) (uint32, []byte, bool) {
rem, id, err := keys.TODOSQLCodec.DecodeTablePrefix(key.AsRawKey())
return id, rem, err == nil
// DecodeSystemTenantObjectID decodes the object ID for the system-tenant from
// the front of key. It returns the decoded object ID, the remainder of the key,
// and whether the result is valid (i.e., whether the key was within the system
// tenant's structured key space).
func DecodeSystemTenantObjectID(key roachpb.RKey) (SystemTenantObjectID, []byte, bool) {
rem, id, err := keys.SystemSQLCodec.DecodeTablePrefix(key.AsRawKey())
return SystemTenantObjectID(id), rem, err == nil
}
......@@ -21,14 +21,14 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
)
func TestDecodeObjectID(t *testing.T) {
func TestDecodeSystemTenantObjectID(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
key roachpb.RKey
keySuffix []byte
success bool
id uint32
id config.SystemTenantObjectID
}{
// Before the structured span.
{roachpb.RKeyMin, nil, false, 0},
......@@ -43,7 +43,7 @@ func TestDecodeObjectID(t *testing.T) {
}
for tcNum, tc := range testCases {
id, keySuffix, success := config.DecodeObjectID(tc.key)
id, keySuffix, success := config.DecodeSystemTenantObjectID(tc.key)
if success != tc.success {
t.Errorf("#%d: expected success=%t", tcNum, tc.success)
continue
......
This diff is collapsed.
......@@ -11,6 +11,7 @@
package config_test
import (
"math"
"sort"
"testing"
......@@ -39,16 +40,24 @@ func tkey(tableID uint32, chunks ...string) []byte {
return key
}
func sqlKV(tableID uint32, indexID, descriptorID uint64) roachpb.KeyValue {
func tenantTkey(tenantID uint64, tableID uint32, chunks ...string) []byte {
key := keys.MakeSQLCodec(roachpb.MakeTenantID(tenantID)).TablePrefix(tableID)
for _, c := range chunks {
key = append(key, []byte(c)...)
}
return key
}
func sqlKV(tableID uint32, indexID, descID uint64) roachpb.KeyValue {
k := tkey(tableID)
k = encoding.EncodeUvarintAscending(k, indexID)
k = encoding.EncodeUvarintAscending(k, descriptorID)
k = encoding.EncodeUvarintAscending(k, descID)
k = encoding.EncodeUvarintAscending(k, 12345) // Column ID, but could be anything.
return kv(k, nil)
}
func descriptor(descriptorID uint64) roachpb.KeyValue {
k := sqlbase.MakeDescMetadataKey(keys.SystemSQLCodec, sqlbase.ID(descriptorID))
func descriptor(descID uint64) roachpb.KeyValue {
k := sqlbase.MakeDescMetadataKey(keys.SystemSQLCodec, sqlbase.ID(descID))
v := sqlbase.WrapDescriptor(&sqlbase.TableDescriptor{})
kv := roachpb.KeyValue{Key: k}
if err := kv.Value.SetProto(v); err != nil {
......@@ -57,9 +66,9 @@ func descriptor(descriptorID uint64) roachpb.KeyValue {
return kv
}
func zoneConfig(descriptorID uint32, spans ...zonepb.SubzoneSpan) roachpb.KeyValue {
func zoneConfig(descID config.SystemTenantObjectID, spans ...zonepb.SubzoneSpan) roachpb.KeyValue {
kv := roachpb.KeyValue{
Key: config.MakeZoneKey(descriptorID),
Key: config.MakeZoneKey(descID),
}
if err := kv.Value.SetProto(&zonepb.ZoneConfig{SubzoneSpans: spans}); err != nil {
panic(err)
......@@ -126,28 +135,42 @@ func TestGetLargestID(t *testing.T) {
defer leaktest.AfterTest(t)()
type testCase struct {
values []roachpb.KeyValue
largest uint32
maxID uint32
errStr string
values []roachpb.KeyValue
largest config.SystemTenantObjectID
maxID config.SystemTenantObjectID
pseudoIDs []uint32
errStr string
}
testCases := []testCase{
// No data.
{nil, 0, 0, "descriptor table not found"},
{nil, 0, 0, nil, "descriptor table not found"},
// Some data, but not from the system span.
{[]roachpb.KeyValue{plainKV("a", "b")}, 0, 0, "descriptor table not found"},
{[]roachpb.KeyValue{plainKV("a", "b")}, 0, 0, nil, "descriptor table not found"},
// Some real data, but no descriptors.
{[]roachpb.KeyValue{
sqlKV(keys.NamespaceTableID, 1, 1),
sqlKV(keys.NamespaceTableID, 1, 2),
sqlKV(keys.UsersTableID, 1, 3),
}, 0, 0, "descriptor table not found"},
}, 0, 0, nil, "descriptor table not found"},
// Decoding error, unbounded max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 1, math.MaxUint64),
}, 0, 0, nil, "descriptor ID 18446744073709551615 exceeds uint32 bounds"},
// Decoding error, bounded max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 1, math.MaxUint64),
sqlKV(keys.DescriptorTableID, 2, 1),
}, 0, 5, nil, "descriptor ID 18446744073709551615 exceeds uint32 bounds"},
// Single correct descriptor entry.
{[]roachpb.KeyValue{sqlKV(keys.DescriptorTableID, 1, 1)}, 1, 0, ""},
{[]roachpb.KeyValue{sqlKV(keys.DescriptorTableID, 1, 1)}, 1, 0, nil, ""},
// Surrounded by other data.
{[]roachpb.KeyValue{
......@@ -155,7 +178,7 @@ func TestGetLargestID(t *testing.T) {
sqlKV(keys.NamespaceTableID, 1, 30),
sqlKV(keys.DescriptorTableID, 1, 8),
sqlKV(keys.ZonesTableID, 1, 40),
}, 8, 0, ""},
}, 8, 0, nil, ""},
// Descriptors with holes. Index ID does not matter.
{[]roachpb.KeyValue{
......@@ -163,15 +186,20 @@ func TestGetLargestID(t *testing.T) {
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 12, 0, ""},
}, 12, 0, nil, ""},
// Real SQL layout.
func() testCase {
ms := sqlbase.MakeMetadataSchema(keys.SystemSQLCodec, zonepb.DefaultZoneConfigRef(), zonepb.DefaultSystemZoneConfigRef())
descIDs := ms.DescriptorIDs()
maxDescID := descIDs[len(descIDs)-1]
maxDescID := config.SystemTenantObjectID(descIDs[len(descIDs)-1])
kvs, _ /* splits */ := ms.GetInitialValues()
return testCase{kvs, uint32(maxDescID), 0, ""}
pseudoIDs := keys.PseudoTableIDs
const pseudoIDIsMax = true // NOTE: will change as new system objects are added
if pseudoIDIsMax {
maxDescID = config.SystemTenantObjectID(keys.MaxPseudoTableID)
}
return testCase{kvs, maxDescID, 0, pseudoIDs, ""}
}(),
// Test non-zero max.
......@@ -180,7 +208,7 @@ func TestGetLargestID(t *testing.T) {
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 8, 8, ""},
}, 8, 8, nil, ""},
// Test non-zero max.
{[]roachpb.KeyValue{
......@@ -188,13 +216,35 @@ func TestGetLargestID(t *testing.T) {
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 5, 7, ""},
}, 5, 7, nil, ""},
// Test pseudo ID (MetaRangesID = 16), exact.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 4, 12),
sqlKV(keys.DescriptorTableID, 4, 19),
sqlKV(keys.DescriptorTableID, 4, 22),
}, 16, 16, []uint32{16, 17, 18}, ""},
// Test pseudo ID (TimeseriesRangesID = 18), above.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 4, 12),
sqlKV(keys.DescriptorTableID, 4, 21),
sqlKV(keys.DescriptorTableID, 4, 22),
}, 18, 20, []uint32{16, 17, 18}, ""},
// Test pseudo ID (TimeseriesRangesID = 18), largest.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 18, 0, []uint32{16, 17, 18}, ""},
}
cfg := config.NewSystemConfig(zonepb.DefaultZoneConfigRef())
for tcNum, tc := range testCases {
cfg.Values = tc.values
ret, err := cfg.GetLargestObjectID(tc.maxID)
ret, err := cfg.GetLargestObjectID(tc.maxID, tc.pseudoIDs)
if !testutils.IsError(err, tc.errStr) {
t.Errorf("#%d: expected err=%q, got %v", tcNum, tc.errStr, err)
continue
......@@ -393,7 +443,7 @@ func TestGetZoneConfigForKey(t *testing.T) {
testCases := []struct {
key roachpb.RKey
expectedID uint32
expectedID config.SystemTenantObjectID
}{
{roachpb.RKeyMin, keys.MetaRangesID},
{roachpb.RKey(keys.Meta1Prefix), keys.MetaRangesID},
......@@ -435,6 +485,12 @@ func TestGetZoneConfigForKey(t *testing.T) {
{tkey(keys.MinUserDescID), keys.MinUserDescID},
{tkey(keys.MinUserDescID + 22), keys.MinUserDescID + 22},
{roachpb.RKeyMax, keys.RootNamespaceID},
// Secondary tenant tables should refer to the TenantsRangesID.
{tenantTkey(5, keys.MinUserDescID), keys.TenantsRangesID},
{tenantTkey(5, keys.MinUserDescID+22), keys.TenantsRangesID},
{tenantTkey(10, keys.MinUserDescID), keys.TenantsRangesID},
{tenantTkey(10, keys.MinUserDescID+22), keys.TenantsRangesID},
}
originalZoneConfigHook := config.ZoneConfigHook
......@@ -450,9 +506,9 @@ func TestGetZoneConfigForKey(t *testing.T) {
Values: kvs,
}
for tcNum, tc := range testCases {
var objectID uint32
var objectID config.SystemTenantObjectID
config.ZoneConfigHook = func(
_ *config.SystemConfig, id uint32,
_ *config.SystemConfig, id config.SystemTenantObjectID,
) (*zonepb.ZoneConfig, *zonepb.ZoneConfig, bool, error) {
objectID = id
return &zonepb.ZoneConfig{}, nil, false, nil
......
......@@ -16,7 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
type zoneConfigMap map[uint32]zonepb.ZoneConfig
type zoneConfigMap map[SystemTenantObjectID]zonepb.ZoneConfig
var (
testingZoneConfig zoneConfigMap
......@@ -40,7 +40,7 @@ func TestingSetupZoneConfigHook(stopper *stop.Stopper) {
testingZoneConfig = make(zoneConfigMap)
testingPreviousHook = ZoneConfigHook
ZoneConfigHook = testingZoneConfigHook
testingLargestIDHook = func(maxID uint32) (max uint32) {
testingLargestIDHook = func(maxID SystemTenantObjectID) (max SystemTenantObjectID) {
testingLock.Lock()
defer testingLock.Unlock()
for id := range testingZoneConfig {
......@@ -70,14 +70,14 @@ func testingResetZoneConfigHook() {
// TestingSetZoneConfig sets the zone config entry for object 'id'
// in the testing map.
func TestingSetZoneConfig(id uint32, zone zonepb.ZoneConfig) {
func TestingSetZoneConfig(id SystemTenantObjectID, zone zonepb.ZoneConfig) {
testingLock.Lock()
defer testingLock.Unlock()
testingZoneConfig[id] = zone
}
func testingZoneConfigHook(
_ *SystemConfig, id uint32,
_ *SystemConfig, id SystemTenantObjectID,
) (*zonepb.ZoneConfig, *zonepb.ZoneConfig, bool, error) {
testingLock.Lock()
defer testingLock.Unlock()
......
......@@ -35,6 +35,7 @@ const (
MetaZoneName = "meta"
SystemZoneName = "system"
TimeseriesZoneName = "timeseries"
TenantsZoneName = "tenants"
)
// NamedZones maps named zones to their pseudo-table ID that can be used to
......@@ -45,6 +46,7 @@ var NamedZones = map[string]uint32{
MetaZoneName: keys.MetaRangesID,
SystemZoneName: keys.SystemRangesID,
TimeseriesZoneName: keys.TimeseriesRangesID,
TenantsZoneName: keys.TenantsRangesID,
}
// NamedZonesByID is the inverse of NamedZones: it maps pseudo-table IDs to
......
This diff was suppressed by a .gitattributes entry.
......@@ -26,11 +26,11 @@ message GCPolicy {
// TTLSeconds specifies the maximum age of a value before it's
// garbage collected. Only older versions of values are garbage
// collected. Specifying <=0 mean older versions are never GC'd.
// collected. Specifying <= 0 mean older versions are never GC'd.
optional int32 ttl_seconds = 1 [(gogoproto.nullable) = false, (gogoproto.customname) = "TTLSeconds"];
}
// Constraint constrains the stores a replica can be stored on.
// Constraint constrains the stores that a replica can be stored on.
message Constraint {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;
......@@ -82,7 +82,7 @@ message LeasePreference {
// ZoneConfig holds configuration that applies to one or more ranges.
//
// Note: when adding.removing fields here, be sure to update
// Note: when adding/removing fields here, be sure to update
// supportedZoneConfigOptions in the sql package, to synchronize
// with the code for ALTER ... CONFIGURE ZONE.
message ZoneConfig {
......@@ -92,11 +92,14 @@ message ZoneConfig {
reserved 1;
optional int64 range_min_bytes = 2 [(gogoproto.moretags) = "yaml:\"range_min_bytes\""];
optional int64 range_max_bytes = 3 [(gogoproto.moretags) = "yaml:\"range_max_bytes\""];
// If GC policy is not set, uses the next highest, non-null policy
// in the zone config hierarchy, up to the default policy if necessary.
optional GCPolicy gc = 4 [(gogoproto.customname) = "GC"];
// NumReplicas specifies the desired number of replicas
optional int32 num_replicas = 5 [(gogoproto.moretags) = "yaml:\"num_replicas\""];
// Constraints constrains which stores the replicas can be stored on. The
// order in which the constraints are stored is arbitrary and may change.
// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20160706_expressive_zone_config.md#constraint-system
......
......@@ -353,34 +353,31 @@ const (
RangeEventTableID = 13
UITableID = 14
JobsTableID = 15
MetaRangesID = 16
SystemRangesID = 17
TimeseriesRangesID = 18
MetaRangesID = 16 // pseudo
SystemRangesID = 17 // pseudo
TimeseriesRangesID = 18 // pseudo
WebSessionsTableID = 19
TableStatisticsTableID = 20
LocationsTableID = 21
LivenessRangesID = 22
LivenessRangesID = 22 // pseudo
RoleMembersTableID = 23
CommentsTableID = 24
ReplicationConstraintStatsTableID = 25
ReplicationCriticalLocalitiesTableID = 26
ReplicationStatsTableID = 27
ReportsMetaTableID = 28
PublicSchemaID = 29
PublicSchemaID = 29 // pseudo
// New NamespaceTableID for cluster version >= 20.1
// Ensures that NamespaceTable does not get gossiped again
NamespaceTableID = 30
ProtectedTimestampsMetaTableID = 31
ProtectedTimestampsRecordsTableID = 32
RoleOptionsTableID = 33
NamespaceTableID = 30
ProtectedTimestampsMetaTableID = 31
ProtectedTimestampsRecordsTableID = 32
RoleOptionsTableID = 33
StatementBundleChunksTableID = 34
StatementDiagnosticsRequestsTableID = 35
StatementDiagnosticsTableID = 36
ScheduledJobsTableID = 37
ScheduledJobsTableID = 37
TenantsRangesID = 38 // pseudo
// CommentType is type for system.comments
DatabaseCommentType = 0
......@@ -402,4 +399,22 @@ const (
// there's no table descriptor). They're grouped here because the cluster
// bootstrap process needs to create splits for them; splits for the tables
// happen separately.
var PseudoTableIDs = []uint32{MetaRangesID, SystemRangesID, TimeseriesRangesID, LivenessRangesID, PublicSchemaID}
var PseudoTableIDs = []uint32{
MetaRangesID,
SystemRangesID,
TimeseriesRangesID,
LivenessRangesID,
PublicSchemaID,
TenantsRangesID,
}
// MaxPseudoTableID is the largest ID in PseudoTableIDs.
var MaxPseudoTableID = func() uint32 {
var max uint32
for _, id := range PseudoTableIDs {
if max < id {
max = id
}
}
return max
}()
......@@ -12,6 +12,7 @@ package keys
import (
"bytes"
"math"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
......@@ -217,7 +218,7 @@ func (d sqlDecoder) DecodeIndexPrefix(key roachpb.Key) ([]byte, uint32, uint32,
}
// DecodeDescMetadataID decodes a descriptor ID from a descriptor metadata key.
func (d sqlDecoder) DecodeDescMetadataID(key roachpb.Key) (uint64, error) {
func (d sqlDecoder) DecodeDescMetadataID(key roachpb.Key) (uint32, error) {
// Extract table and index ID from key.
remaining, tableID, _, err := d.DecodeIndexPrefix(key)
if err != nil {
......@@ -231,5 +232,8 @@ func (d sqlDecoder) DecodeDescMetadataID(key roachpb.Key) (uint64, error) {
if err != nil {
return 0, err
}
return id, nil
if id > math.MaxUint32 {
return 0, errors.Errorf("descriptor ID %d exceeds uint32 bounds", id)
}
return uint32(id), nil
}
......@@ -1017,7 +1017,7 @@ func TestStoreZoneUpdateAndRangeSplit(t *testing.T) {
descID := uint32(keys.MinUserDescID)
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.RangeMaxBytes = proto.Int64(maxBytes)
config.TestingSetZoneConfig(descID, zoneConfig)
config.TestingSetZoneConfig(config.SystemTenantObjectID(descID), zoneConfig)
// Trigger gossip callback.
if err := store.Gossip().AddInfoProto(gossip.KeySystemConfig, &config.SystemConfigEntries{}, 0); err != nil {
......@@ -1079,7 +1079,7 @@ func TestStoreRangeSplitWithMaxBytesUpdate(t *testing.T) {
descID := uint32(keys.MinUserDescID)
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.RangeMaxBytes = proto.Int64(maxBytes)
config.TestingSetZoneConfig(descID, zoneConfig)
config.TestingSetZoneConfig(config.SystemTenantObjectID(descID), zoneConfig)
// Trigger gossip callback.
if err := store.Gossip().AddInfoProto(gossip.KeySystemConfig, &config.SystemConfigEntries{}, 0); err != nil {
......@@ -1325,6 +1325,9 @@ func TestStoreRangeSystemSplits(t *testing.T) {
}
ids := schema.DescriptorIDs()
maxID := uint32(ids[len(ids)-1])
if maxPseudo := keys.MaxPseudoTableID; maxID < maxPseudo {
maxID = maxPseudo
}
for i := uint32(keys.MaxSystemConfigDescID + 1); i <= maxID; i++ {
expKeys = append(expKeys,
testutils.MakeKey(keys.Meta2Prefix, keys.SystemSQLCodec.TablePrefix(i)),
......
......@@ -19,6 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
......@@ -182,7 +183,7 @@ func (r *replicationConstraintStatsReportSaver) loadPreviousVersion(
r.previousVersion = make(ConstraintReport, len(rows))
for _, row := range rows {
key := ConstraintStatusKey{}
key.ZoneID = (uint32)(*row[0].(*tree.DInt))
key.ZoneID = (config.SystemTenantObjectID)(*row[0].(*tree.DInt))
key.SubzoneID = base.SubzoneID((*row[1].(*tree.DInt)))
key.ViolationType = (ConstraintType)(*row[2].(*tree.DString))
key.Constraint = (ConstraintRepr)(*row[3].(*tree.DString))
......@@ -389,11 +390,12 @@ func (v *constraintConformanceVisitor) reset(ctx context.Context) {
// zones that have constraints. Otherwise, just iterating through the ranges
// wouldn't create entries for constraints that aren't violated, and
// definitely not for zones that don't apply to any ranges.
maxObjectID, err := v.cfg.GetLargestObjectID(0 /* maxID - return the largest ID in the config */)
maxObjectID, err := v.cfg.GetLargestObjectID(
0 /* maxID - return the largest ID in the config */, keys.PseudoTableIDs)
if err != nil {
log.Fatalf(ctx, "unexpected failure to compute max object id: %s", err)
}
for i := uint32(1); i <= maxObjectID; i++ {
for i := config.SystemTenantObjectID(1); i <= maxObjectID; i++ {
zone, err := getZoneByID(i, v.cfg)
if err != nil {
log.Fatalf(ctx, "unexpected failure to compute max object id: %s", err)
......
......@@ -1011,13 +1011,13 @@ func (b *systemConfigBuilder) setDefaultZoneConfig(cfg zonepb.ZoneConfig) error