Skip to content

Commit

Permalink
spanconfigsqltranslator: translate spanconfigs for offline dbs
Browse files Browse the repository at this point in the history
Previously, we would ignore sql updates for databases
that are offline. As explained in  cockroachdb#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: cockroachdb#91149

Release note: None
  • Loading branch information
adityamaru committed Nov 3, 2022
1 parent 2c6b3c0 commit 9150a84
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 16 deletions.
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 @@ -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
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

0 comments on commit 9150a84

Please sign in to comment.