Skip to content

Commit

Permalink
sql: split ranges synchronously when creating new tenant
Browse files Browse the repository at this point in the history
The previous commit taught the split and merge queues about tenant
boundaries, which means that split points will be created asynchronously
when a new tenant is created. However, it is valuable from an isolation
perspective to split off tenant ranges synchronously upon the creation
of a new tenant. This commit adds in such synchronous range splitting to
the `crdb_internal.create_tenant` builtin.
  • Loading branch information
nvanbenschoten committed Jun 5, 2020
1 parent 3a7a51b commit de038a3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 68 deletions.
20 changes: 18 additions & 2 deletions pkg/sql/sqlbase/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,24 @@ func (ms MetadataSchema) GetInitialValues() ([]roachpb.KeyValue, []roachpb.RKey)
addDescriptor(sysObj.parentID, sysObj.desc)
}

for _, id := range ms.otherSplitIDs {
splits = append(splits, roachpb.RKey(ms.codec.TablePrefix(id)))
// The splits slice currently has a split point for each of the object
// descriptors in ms.descs. If we're fetching the initial values for the
// system tenant, add any additional split point, which correspond to
// "pseudo" tables that don't have real descriptors.
//
// If we're fetching the initial values for a secondary tenant, things are
// different. Secondary tenants do not enforce split points at table
// boundaries. In fact, if we tried to split at table boundaries, those
// splits would quickly be merged away. The only enforced split points are
// between secondary tenants (e.g. between /tenant/<id> and /tenant/<id+1>).
// So we drop all descriptor split points and replace it with a single split
// point at the beginning of this tenant's keyspace.
if ms.codec.ForSystemTenant() {
for _, id := range ms.otherSplitIDs {
splits = append(splits, roachpb.RKey(ms.codec.TablePrefix(id)))
}
} else {
splits = []roachpb.RKey{roachpb.RKey(ms.codec.TenantPrefix())}
}

// Other key/value generation that doesn't fit into databases and
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/sqlbase/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,6 @@ func addSystemDatabaseToSchema(
defaultSystemZoneConfig *zonepb.ZoneConfig,
) {
addSystemDescriptorsToSchema(target)
// TODO(nvanbenschoten): only do this for the system tenant. Tracked in #48774.
addSplitIDs(target)
addZoneConfigKVsToSchema(target, defaultZoneConfig, defaultSystemZoneConfig)
}
Expand Down
26 changes: 19 additions & 7 deletions pkg/sql/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package sql

import (
"context"
"time"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand Down Expand Up @@ -94,16 +95,27 @@ func (p *planner) CreateTenant(ctx context.Context, tenID uint64, tenInfo []byte
return err
}

// TODO(nvanbenschoten): we currently neither split ranges between a single
// tenant's tables nor split ranges between different tenant keyspaces. We
// should do both. Performing the splits here won't have the desired effect
// until we also teach SystemConfig.ComputeSplitKey about tenant tables and
// tenant boundaries. Tracked in #48774.
_ = splits
// Create initial splits for the new tenant. This is performed
// non-transactionally, so the range splits will remain even if the
// statement's transaction is rolled back. In this case, the manual splits
// can and will be merged away after its 1h expiration elapses.
//
// If the statement's transaction commits and updates the system.tenants
// table, the manual splits' expirations will no longer be necessary to
// prevent the split points from being merged away. Likewise, if the
// transaction did happen to take long enough that the manual splits'
// expirations did elapse and the splits were merged away, they would
// quickly (but asynchronously) be recreated once the KV layer notices the
// updated system.tenants table in the gossipped SystemConfig.
expTime := p.ExecCfg().Clock.Now().Add(time.Hour.Nanoseconds(), 0)
for _, key := range splits {
if err := p.ExecCfg().DB.AdminSplit(ctx, key, expTime); err != nil {
return err
}
}

// Tenant creation complete! Note that sqlmigrations have not been run yet.
// They will be run when a sqlServer bound to this tenant is first launched.

return nil
}

Expand Down
62 changes: 4 additions & 58 deletions pkg/sql/tests/testdata/initial_keys
Original file line number Diff line number Diff line change
Expand Up @@ -159,35 +159,8 @@ initial-keys tenant=5
/Tenant/5/NamespaceTable/30/1/1/29/"ui"/4/1
/Tenant/5/NamespaceTable/30/1/1/29/"users"/4/1
/Tenant/5/NamespaceTable/30/1/1/29/"web_sessions"/4/1
28 splits:
/Tenant/5/Table/11
/Tenant/5/Table/12
/Tenant/5/Table/13
/Tenant/5/Table/14
/Tenant/5/Table/15
/Tenant/5/Table/16
/Tenant/5/Table/17
/Tenant/5/Table/18
/Tenant/5/Table/19
/Tenant/5/Table/20
/Tenant/5/Table/21
/Tenant/5/Table/22
/Tenant/5/Table/23
/Tenant/5/Table/24
/Tenant/5/Table/25
/Tenant/5/Table/26
/Tenant/5/Table/27
/Tenant/5/Table/28
/Tenant/5/Table/29
/Tenant/5/NamespaceTable/30
/Tenant/5/NamespaceTable/Max
/Tenant/5/Table/32
/Tenant/5/Table/33
/Tenant/5/Table/34
/Tenant/5/Table/35
/Tenant/5/Table/36
/Tenant/5/Table/37
/Tenant/5/Table/38
1 splits:
/Tenant/5

initial-keys tenant=999
----
Expand Down Expand Up @@ -250,32 +223,5 @@ initial-keys tenant=999
/Tenant/999/NamespaceTable/30/1/1/29/"ui"/4/1
/Tenant/999/NamespaceTable/30/1/1/29/"users"/4/1
/Tenant/999/NamespaceTable/30/1/1/29/"web_sessions"/4/1
28 splits:
/Tenant/999/Table/11
/Tenant/999/Table/12
/Tenant/999/Table/13
/Tenant/999/Table/14
/Tenant/999/Table/15
/Tenant/999/Table/16
/Tenant/999/Table/17
/Tenant/999/Table/18
/Tenant/999/Table/19
/Tenant/999/Table/20
/Tenant/999/Table/21
/Tenant/999/Table/22
/Tenant/999/Table/23
/Tenant/999/Table/24
/Tenant/999/Table/25
/Tenant/999/Table/26
/Tenant/999/Table/27
/Tenant/999/Table/28
/Tenant/999/Table/29
/Tenant/999/NamespaceTable/30
/Tenant/999/NamespaceTable/Max
/Tenant/999/Table/32
/Tenant/999/Table/33
/Tenant/999/Table/34
/Tenant/999/Table/35
/Tenant/999/Table/36
/Tenant/999/Table/37
/Tenant/999/Table/38
1 splits:
/Tenant/999

0 comments on commit de038a3

Please sign in to comment.