Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spanconfigsqltranslator: translate spanconfigs for offline dbs #91173

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_test(
"//pkg/spanconfig/spanconfigtestutils",
"//pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster",
"//pkg/sql",
"//pkg/sql/catalog/dbdesc",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/descs",
"//pkg/sql/catalog/tabledesc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigtestutils"
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
Expand Down Expand Up @@ -248,6 +249,20 @@ func TestDataDriven(t *testing.T) {
mutable.SetPublic()
})

case "mark-database-offline":
var dbName string
d.ScanArgs(t, "database", &dbName)
tenant.WithMutableDatabaseDescriptor(ctx, dbName, func(mutable *dbdesc.Mutable) {
mutable.SetOffline("for testing")
})

case "mark-database-public":
var dbName string
d.ScanArgs(t, "database", &dbName)
tenant.WithMutableDatabaseDescriptor(ctx, dbName, func(mutable *dbdesc.Mutable) {
mutable.SetPublic()
})

case "protect":
var recordID string
var protectTS int
Expand Down
49 changes: 43 additions & 6 deletions pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/misc
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,66 @@ translate database=db
translate id=106
----

# Mark table t2 as offline, we should still be able to generate a span
subtest offline-descriptors

exec-sql
ALTER DATABASE db CONFIGURE ZONE USING gc.ttlseconds=10;
----

# Mark the database as offline, we should still be able to generate a span
# configuration for it.
mark-database-offline database=db
----

translate database=db
----
/Table/10{7-8} ttl_seconds=10

# Delete the bespoke zone config on db. t2 should fallback to the RANGE DEFAULT
# zone config even though all descriptors are offline.
exec-sql
DELETE FROM system.zones WHERE id = 104;
----

translate database=db
----
/Table/10{7-8} range default

translate database=db table=t2
----
/Table/10{7-8} range default

mark-database-public database=db
----

exec-sql
ALTER DATABASE db CONFIGURE ZONE USING gc.ttlseconds=11;
----

# Mark table db.t2 as offline, we should still be able to generate a span
# configuration for it.
mark-table-offline database=db table=t2
----

# Should work for both when we start from the table and when we start from the
# table.
# database.
translate database=db table=t2
----
/Table/10{7-8} range default
/Table/10{7-8} ttl_seconds=11

translate database=db
----
/Table/10{7-8} range default

/Table/10{7-8} ttl_seconds=11

# Mark the table as public again.
mark-table-public database=db table=t2
----

translate database=db table=t2
----
/Table/10{7-8} range default
/Table/10{7-8} ttl_seconds=11

subtest end

# Test schemas/types don't generate a span configuration.
exec-sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ ALTER DATABASE db CONFIGURE ZONE USING num_replicas=7;
ALTER TABLE db.t1 CONFIGURE ZONE USING num_voters=5;
----

# Mark the database as offline.
mark-database-offline database=db
----

# Write a protected timestamp on the offline db.
protect record-id=9 ts=9
descs 104
----

# We should still see the protectedts translate onto the tables in the offline db.
translate database=db
----
/Table/10{6-7} num_replicas=7 num_voters=5 protection_policies=[{ts: 9}]
/Table/10{7-8} num_replicas=7 protection_policies=[{ts: 9}]

release record-id=9
----

mark-database-public database=db
----

translate database=db
----
/Table/10{6-7} num_replicas=7 num_voters=5
/Table/10{7-8} num_replicas=7


# Write a protected timestamp on t1.
protect record-id=1 ts=1
descs 106
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,66 @@ translate database=db
translate id=56
----

# Mark table t2 as offline, we should still be able to generate a span
subtest offline-descriptors

exec-sql
ALTER DATABASE db CONFIGURE ZONE USING gc.ttlseconds=10;
----

# Mark the database as offline, we should still be able to generate a span
# configuration for it.
mark-database-offline database=db
----

translate database=db
----
/Tenant/10/Table/10{7-8} ttl_seconds=10

# Delete the bespoke zone config on db. t2 should fallback to the RANGE DEFAULT
# zone config even though all descriptors are offline.
exec-sql
DELETE FROM system.zones WHERE id = 104;
----

translate database=db
----
/Tenant/10/Table/10{7-8} range default

translate database=db table=t2
----
/Tenant/10/Table/10{7-8} range default

mark-database-public database=db
----

exec-sql
ALTER DATABASE db CONFIGURE ZONE USING gc.ttlseconds=11;
----

# Mark table db.t2 as offline, we should still be able to generate a span
# configuration for it.
mark-table-offline database=db table=t2
----

# Should work for both when we start from the table and when we start from the
# table.
# database.
translate database=db table=t2
----
/Tenant/10/Table/10{7-8} range default
/Tenant/10/Table/10{7-8} ttl_seconds=11

translate database=db
----
/Tenant/10/Table/10{7-8} range default

/Tenant/10/Table/10{7-8} ttl_seconds=11

# Mark the table as public again.
mark-table-public database=db table=t2
----

translate database=db table=t2
----
/Tenant/10/Table/10{7-8} range default
/Tenant/10/Table/10{7-8} ttl_seconds=11

subtest end

# Test schemas/types don't generate a span configuration.
exec-sql
Expand All @@ -59,7 +96,7 @@ CREATE TYPE db.typ AS ENUM();

translate database=db
----
/Tenant/10/Table/10{7-8} range default
/Tenant/10/Table/10{7-8} ttl_seconds=11

# Schema.
translate id=58
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ ALTER DATABASE db CONFIGURE ZONE USING num_replicas=7;
ALTER TABLE db.t1 CONFIGURE ZONE USING num_voters=5;
----

# Mark the database as offline.
mark-database-offline database=db
----

# Write a protected timestamp on the offline db.
protect record-id=9 ts=9
descs 104
----

# We should still see the protectedts translate onto the offline db.
translate database=db
----
/Tenant/10/Table/10{6-7} num_replicas=7 num_voters=5 protection_policies=[{ts: 9}]
/Tenant/10/Table/10{7-8} num_replicas=7 protection_policies=[{ts: 9}]

release record-id=9
----

mark-database-public database=db
----

translate database=db
----
/Tenant/10/Table/10{6-7} num_replicas=7 num_voters=5
/Tenant/10/Table/10{7-8} num_replicas=7

# Write a protected timestamp on t1.
protect record-id=1 ts=1
descs 106
Expand Down
5 changes: 2 additions & 3 deletions pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,8 @@ func (s *SQLTranslator) findDescendantLeafIDsForDescriptor(
return nil, nil
}

// There's nothing for us to do if the descriptor is offline or has been
// dropped.
if db.Offline() || db.Dropped() {
// There's nothing for us to do if the descriptor has been dropped.
if db.Dropped() {
return nil, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/spanconfig/spanconfigtestutils",
"//pkg/sql",
"//pkg/sql/catalog",
"//pkg/sql/catalog/dbdesc",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/descs",
"//pkg/sql/catalog/tabledesc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigtestutils"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
Expand Down Expand Up @@ -139,6 +140,33 @@ func (s *Tenant) KVAccessorRecorder() *spanconfigtestutils.KVAccessorRecorder {
return s.recorder
}

// WithMutableDatabaseDescriptor invokes the provided callback with a mutable
// database descriptor, changes to which are then committed back to the system.
// The callback needs to be idempotent.
func (s *Tenant) WithMutableDatabaseDescriptor(
ctx context.Context, dbName string, f func(*dbdesc.Mutable),
) {
execCfg := s.ExecutorConfig().(sql.ExecutorConfig)
require.NoError(s.t, sql.DescsTxn(ctx, &execCfg, func(
ctx context.Context, txn *kv.Txn, descsCol *descs.Collection,
) error {
desc, err := descsCol.GetMutableDatabaseByName(
ctx,
txn,
dbName,
tree.DatabaseLookupFlags{
Required: true,
IncludeOffline: true,
},
)
if err != nil {
return err
}
f(desc)
return descsCol.WriteDesc(ctx, false, desc, txn)
}))
}

// WithMutableTableDescriptor invokes the provided callback with a mutable table
// descriptor, changes to which are then committed back to the system. The
// callback needs to be idempotent.
Expand Down