From 9150a84f6b40d03992b7e4221e43d7457a8de862 Mon Sep 17 00:00:00 2001 From: adityamaru Date: Wed, 2 Nov 2022 17:29:08 -0400 Subject: [PATCH] spanconfigsqltranslator: translate spanconfigs for offline dbs Previously, we would ignore sql updates for databases that are offline. As explained in #91148 in some situations we require writing a protected timestamp to an offline database during a restore. This will require the spanconfig machinery to reconcile the PTS record so that the GC queue can observe and respect it. We already reconcile sql udpates for offline tables so it seems reasonable to do the same for offline databases. Fixes: #91149 Release note: None --- .../spanconfigsqltranslatorccl/BUILD.bazel | 1 + .../datadriven_test.go | 15 ++++++ .../spanconfigsqltranslatorccl/testdata/misc | 49 +++++++++++++++--- .../testdata/tenant/misc | 51 ++++++++++++++++--- .../spanconfigsqltranslator/sqltranslator.go | 5 +- .../spanconfigtestcluster/BUILD.bazel | 1 + .../spanconfigtestcluster/tenant_state.go | 28 ++++++++++ 7 files changed, 134 insertions(+), 16 deletions(-) diff --git a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/BUILD.bazel b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/BUILD.bazel index e25f09562445..7e64bcc79c86 100644 --- a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/BUILD.bazel +++ b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/BUILD.bazel @@ -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", diff --git a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/datadriven_test.go b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/datadriven_test.go index 7b102b8469d9..ac147ff34fab 100644 --- a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/datadriven_test.go +++ b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/datadriven_test.go @@ -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" @@ -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 diff --git a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/misc b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/misc index 6135456f9e78..bd0439271ac2 100644 --- a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/misc +++ b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/misc @@ -38,21 +38,56 @@ 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 @@ -60,7 +95,9 @@ 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 diff --git a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/tenant/misc b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/tenant/misc index a31fd5e9eba0..6c52d59137b7 100644 --- a/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/tenant/misc +++ b/pkg/ccl/spanconfigccl/spanconfigsqltranslatorccl/testdata/tenant/misc @@ -27,21 +27,56 @@ 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 @@ -49,7 +84,9 @@ 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 @@ -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 diff --git a/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go b/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go index 8b6fe8719dec..2db46ff6fb26 100644 --- a/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go +++ b/pkg/spanconfig/spanconfigsqltranslator/sqltranslator.go @@ -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 } diff --git a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/BUILD.bazel b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/BUILD.bazel index 4dcf99e53f01..888df890cc7d 100644 --- a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/BUILD.bazel +++ b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/BUILD.bazel @@ -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", diff --git a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go index d8eb4080d1c9..2aa3b1a96287 100644 --- a/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go +++ b/pkg/spanconfig/spanconfigtestutils/spanconfigtestcluster/tenant_state.go @@ -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" @@ -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.