Skip to content

Commit

Permalink
sql: support tenant creation and deletion, track in system.tenants table
Browse files Browse the repository at this point in the history
Most of cockroachdb#47904.

First commit from cockroachdb#48504.
Second and third commit from cockroachdb#48773.

This commit implements the initial structure for tenant creation and
deletion. It does so by introducing a new system table to track tenants
in a multi-tenant cluster and two new builtin functions to manipulate
this table and the overall multi-tenant state.

The change starts by introducing a new `system.tenants` table with the
following schema:
```
CREATE TABLE system.tenants (
    id     INT8 NOT NULL PRIMARY KEY,
    active BOOL NOT NULL DEFAULT true,
    info   BYTES
);
```

The `id` column is self-explanatory. The `active` column is used to
coordinate tenant destruction in an asynchronous job. The `info` column
is an opaque byte slice to allow users to associate arbitrary
information with specific tenants. I don't know exactly how this third
field will be used (mapping tenants back to CockroachCloud user IDs?),
but it seems like a good idea to add some flexibility since we do intend
to eventually expose this externally.

The table is given an ID of 8, which means that it falls within the
"system config span". I believe this is ok, because the entire concept
of the "system config span" should be going away with cockroachdb#47150. The table
is also only exposed to the system-tenant and is never created for
secondary tenants.

The change then introduces two new builtin functions:
`crdb_internal.create_tenant` and `crdb_internal.destroy_tenant`. These
do what you would expect - creating and destroying tenant keyspaces,
along with updating metadata in system.tenants.
  • Loading branch information
nvanbenschoten committed May 14, 2020
1 parent 51f231f commit 4272248
Show file tree
Hide file tree
Showing 31 changed files with 988 additions and 220 deletions.
13 changes: 13 additions & 0 deletions docs/generated/sql/functions.md
Expand Up @@ -1114,6 +1114,19 @@ has no relationship with the commit order of concurrent transactions.</p>
</span></td></tr></tbody>
</table>

### Multi-tenancy functions

<table>
<thead><tr><th>Function &rarr; Returns</th><th>Description</th></tr></thead>
<tbody>
<tr><td><a name="crdb_internal.create_tenant"></a><code>crdb_internal.create_tenant(id: <a href="int.html">int</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Creates a new tenant with the provided ID. Must be run by the System tenant.</p>
</span></td></tr>
<tr><td><a name="crdb_internal.create_tenant"></a><code>crdb_internal.create_tenant(id: <a href="int.html">int</a>, info: <a href="bytes.html">bytes</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Creates a new tenant with the provided ID. Must be run by the System tenant.</p>
</span></td></tr>
<tr><td><a name="crdb_internal.destroy_tenant"></a><code>crdb_internal.destroy_tenant(id: <a href="int.html">int</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Destroys a tenant with the provided ID. Must be run by the System tenant.</p>
</span></td></tr></tbody>
</table>

### Sequence functions

<table>
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/testdata/zip/partial1
Expand Up @@ -217,7 +217,7 @@ requesting database details for defaultdb... writing: debug/schema/defaultdb@det
requesting database details for postgres... writing: debug/schema/postgres@details.json
0 tables found
requesting database details for system... writing: debug/schema/system@details.json
26 tables found
27 tables found
requesting table details for system.comments... writing: debug/schema/system/comments.json
requesting table details for system.descriptor... writing: debug/schema/system/descriptor.json
requesting table details for system.eventlog... writing: debug/schema/system/eventlog.json
Expand All @@ -240,6 +240,7 @@ requesting table details for system.statement_bundle_chunks... writing: debug/sc
requesting table details for system.statement_diagnostics... writing: debug/schema/system/statement_diagnostics.json
requesting table details for system.statement_diagnostics_requests... writing: debug/schema/system/statement_diagnostics_requests.json
requesting table details for system.table_statistics... writing: debug/schema/system/table_statistics.json
requesting table details for system.tenants... writing: debug/schema/system/tenants.json
requesting table details for system.ui... writing: debug/schema/system/ui.json
requesting table details for system.users... writing: debug/schema/system/users.json
requesting table details for system.web_sessions... writing: debug/schema/system/web_sessions.json
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/testdata/zip/partial1_excluded
Expand Up @@ -154,7 +154,7 @@ requesting database details for defaultdb... writing: debug/schema/defaultdb@det
requesting database details for postgres... writing: debug/schema/postgres@details.json
0 tables found
requesting database details for system... writing: debug/schema/system@details.json
26 tables found
27 tables found
requesting table details for system.comments... writing: debug/schema/system/comments.json
requesting table details for system.descriptor... writing: debug/schema/system/descriptor.json
requesting table details for system.eventlog... writing: debug/schema/system/eventlog.json
Expand All @@ -177,6 +177,7 @@ requesting table details for system.statement_bundle_chunks... writing: debug/sc
requesting table details for system.statement_diagnostics... writing: debug/schema/system/statement_diagnostics.json
requesting table details for system.statement_diagnostics_requests... writing: debug/schema/system/statement_diagnostics_requests.json
requesting table details for system.table_statistics... writing: debug/schema/system/table_statistics.json
requesting table details for system.tenants... writing: debug/schema/system/tenants.json
requesting table details for system.ui... writing: debug/schema/system/ui.json
requesting table details for system.users... writing: debug/schema/system/users.json
requesting table details for system.web_sessions... writing: debug/schema/system/web_sessions.json
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/testdata/zip/partial2
Expand Up @@ -153,7 +153,7 @@ requesting database details for defaultdb... writing: debug/schema/defaultdb@det
requesting database details for postgres... writing: debug/schema/postgres@details.json
0 tables found
requesting database details for system... writing: debug/schema/system@details.json
26 tables found
27 tables found
requesting table details for system.comments... writing: debug/schema/system/comments.json
requesting table details for system.descriptor... writing: debug/schema/system/descriptor.json
requesting table details for system.eventlog... writing: debug/schema/system/eventlog.json
Expand All @@ -176,6 +176,7 @@ requesting table details for system.statement_bundle_chunks... writing: debug/sc
requesting table details for system.statement_diagnostics... writing: debug/schema/system/statement_diagnostics.json
requesting table details for system.statement_diagnostics_requests... writing: debug/schema/system/statement_diagnostics_requests.json
requesting table details for system.table_statistics... writing: debug/schema/system/table_statistics.json
requesting table details for system.tenants... writing: debug/schema/system/tenants.json
requesting table details for system.ui... writing: debug/schema/system/ui.json
requesting table details for system.users... writing: debug/schema/system/users.json
requesting table details for system.web_sessions... writing: debug/schema/system/web_sessions.json
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/testdata/zip/specialnames
Expand Up @@ -21,7 +21,7 @@ requesting table details for defaultdb.pg_catalog.pg_class... writing: debug/sch
requesting database details for postgres... writing: debug/schema/postgres@details.json
0 tables found
requesting database details for system... writing: debug/schema/system-1@details.json
26 tables found
27 tables found
requesting table details for system.comments... writing: debug/schema/system-1/comments.json
requesting table details for system.descriptor... writing: debug/schema/system-1/descriptor.json
requesting table details for system.eventlog... writing: debug/schema/system-1/eventlog.json
Expand All @@ -44,6 +44,7 @@ requesting table details for system.statement_bundle_chunks... writing: debug/sc
requesting table details for system.statement_diagnostics... writing: debug/schema/system-1/statement_diagnostics.json
requesting table details for system.statement_diagnostics_requests... writing: debug/schema/system-1/statement_diagnostics_requests.json
requesting table details for system.table_statistics... writing: debug/schema/system-1/table_statistics.json
requesting table details for system.tenants... writing: debug/schema/system-1/tenants.json
requesting table details for system.ui... writing: debug/schema/system-1/ui.json
requesting table details for system.users... writing: debug/schema/system-1/users.json
requesting table details for system.web_sessions... writing: debug/schema/system-1/web_sessions.json
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/testdata/zip/testzip
Expand Up @@ -91,7 +91,7 @@ requesting database details for defaultdb... writing: debug/schema/defaultdb@det
requesting database details for postgres... writing: debug/schema/postgres@details.json
0 tables found
requesting database details for system... writing: debug/schema/system@details.json
26 tables found
27 tables found
requesting table details for system.comments... writing: debug/schema/system/comments.json
requesting table details for system.descriptor... writing: debug/schema/system/descriptor.json
requesting table details for system.eventlog... writing: debug/schema/system/eventlog.json
Expand All @@ -114,6 +114,7 @@ requesting table details for system.statement_bundle_chunks... writing: debug/sc
requesting table details for system.statement_diagnostics... writing: debug/schema/system/statement_diagnostics.json
requesting table details for system.statement_diagnostics_requests... writing: debug/schema/system/statement_diagnostics_requests.json
requesting table details for system.table_statistics... writing: debug/schema/system/table_statistics.json
requesting table details for system.tenants... writing: debug/schema/system/tenants.json
requesting table details for system.ui... writing: debug/schema/system/ui.json
requesting table details for system.users... writing: debug/schema/system/users.json
requesting table details for system.web_sessions... writing: debug/schema/system/web_sessions.json
Expand Down
1 change: 1 addition & 0 deletions pkg/keys/constants.go
Expand Up @@ -332,6 +332,7 @@ const (
ZonesTableID = 5
SettingsTableID = 6
DescIDSequenceID = 7
TenantsTableID = 8

// IDs for the important columns and indexes in the zones table live here to
// avoid introducing a dependency on sql/sqlbase throughout the codebase.
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/conn_executor.go
Expand Up @@ -1938,11 +1938,12 @@ func (ex *connExecutor) initEvalCtx(ctx context.Context, evalCtx *extendedEvalCo
*evalCtx = extendedEvalContext{
EvalContext: tree.EvalContext{
Planner: p,
Sequence: p,
SessionData: ex.sessionData,
SessionAccessor: p,
PrivilegedAccessor: p,
SessionAccessor: p,
ClientNoticeSender: p,
Sequence: p,
Tenant: p,
SessionData: ex.sessionData,
Settings: ex.server.cfg.Settings,
TestingKnobs: ex.server.cfg.EvalContextTestingKnobs,
ClusterID: ex.server.cfg.ClusterID(),
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/distsql/server.go
Expand Up @@ -316,10 +316,11 @@ func (ds *ServerImpl) setupFlow(
// ProcessorBase. StartInternal().
Context: ctx,
Planner: &sqlbase.DummyEvalPlanner{},
SessionAccessor: &sqlbase.DummySessionAccessor{},
PrivilegedAccessor: &sqlbase.DummyPrivilegedAccessor{},
Sequence: &sqlbase.DummySequenceOperators{},
SessionAccessor: &sqlbase.DummySessionAccessor{},
ClientNoticeSender: &sqlbase.DummyClientNoticeSender{},
Sequence: &sqlbase.DummySequenceOperators{},
Tenant: &sqlbase.DummyTenantOperator{},
InternalExecutor: ie,
Txn: leafTxn,
}
Expand Down

0 comments on commit 4272248

Please sign in to comment.