Skip to content

Commit

Permalink
backupccl: support 'execution locality' option in scheduled backups
Browse files Browse the repository at this point in the history
Release note: none.
Epic: CRDB-9547.
  • Loading branch information
dt committed Mar 22, 2023
1 parent 469df64 commit 85eb8d9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
11 changes: 11 additions & 0 deletions pkg/ccl/backupccl/alter_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ func processOptionsForArgs(inOpts tree.BackupOptions, outOpts *tree.BackupOption
outOpts.CaptureRevisionHistory = inOpts.CaptureRevisionHistory
}

if inOpts.ExecutionLocality != nil {
outOpts.ExecutionLocality = inOpts.ExecutionLocality
}

if inOpts.IncludeAllSecondaryTenants != nil {
outOpts.IncludeAllSecondaryTenants = inOpts.IncludeAllSecondaryTenants
}
Expand All @@ -352,6 +356,13 @@ func processOptionsForArgs(inOpts tree.BackupOptions, outOpts *tree.BackupOption
outOpts.EncryptionPassphrase = inOpts.EncryptionPassphrase
}
}
if inOpts.ExecutionLocality != nil {
if tree.AsStringWithFlags(inOpts.ExecutionLocality, tree.FmtBareStrings) == "" {
outOpts.ExecutionLocality = nil
} else {
outOpts.ExecutionLocality = inOpts.ExecutionLocality
}
}
if inOpts.EncryptionKMSURI != nil {
if tree.AsStringWithFlags(&inOpts.EncryptionKMSURI, tree.FmtBareStrings) == "" {
outOpts.EncryptionKMSURI = nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/ccl/backupccl/create_scheduled_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type scheduledBackupSpec struct {
kmsURIs []string
incrementalStorage []string
includeAllSecondaryTenants *bool
execLoc *string
}

func makeScheduleDetails(opts map[string]string) (jobspb.ScheduleDetails, error) {
Expand Down Expand Up @@ -234,6 +235,10 @@ func doCreateBackupSchedules(
}
}

if eval.execLoc != nil && *eval.execLoc != "" {
backupNode.Options.ExecutionLocality = tree.NewStrVal(*eval.execLoc)
}

// Evaluate encryption KMS URIs if set.
// Only one of encryption passphrase and KMS URI should be set, but this check
// is done during backup planning so we do not need to worry about it here.
Expand Down Expand Up @@ -688,6 +693,17 @@ func makeScheduledBackupSpec(
}
spec.captureRevisionHistory = &capture
}

if schedule.BackupOptions.ExecutionLocality != nil {
loc, err := exprEval.String(
ctx, schedule.BackupOptions.ExecutionLocality,
)
if err != nil {
return nil, err
}
spec.execLoc = &loc
}

if schedule.BackupOptions.IncludeAllSecondaryTenants != nil {
includeSecondary, err := exprEval.Bool(ctx,
schedule.BackupOptions.IncludeAllSecondaryTenants)
Expand Down Expand Up @@ -761,6 +777,7 @@ func createBackupScheduleTypeCheck(
schedule.ScheduleLabelSpec.Label,
schedule.Recurrence,
schedule.BackupOptions.EncryptionPassphrase,
schedule.BackupOptions.ExecutionLocality,
}
if schedule.FullBackup != nil {
stringExprs = append(stringExprs, schedule.FullBackup.Recurrence)
Expand Down
23 changes: 23 additions & 0 deletions pkg/ccl/backupccl/create_scheduled_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/jobs/jobstest"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs/schedulebase"
"github.com/cockroachdb/cockroach/pkg/security/username"
Expand Down Expand Up @@ -97,6 +98,7 @@ func newTestHelper(t *testing.T) (*testHelper, func()) {
}

args := base.TestServerArgs{
Locality: roachpb.Locality{Tiers: []roachpb.Tier{{Key: "region", Value: "of-france"}}},
Settings: cluster.MakeClusterSettings(),
ExternalIODir: dir,
// Some scheduled backup tests fail when run within a tenant. More
Expand Down Expand Up @@ -639,6 +641,27 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
},
},
},
{
name: "exec-loc",
user: enterpriseUser,
query: `
CREATE SCHEDULE FOR BACKUP DATABASE system
INTO 'nodelocal://0/backup'
WITH revision_history, execution locality = 'region=of-france'
RECURRING '1 2 * * *'
FULL BACKUP ALWAYS
WITH SCHEDULE OPTIONS first_run=$1
`,
queryArgs: []interface{}{th.env.Now().Add(time.Minute)},
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP DATABASE system INTO 'nodelocal://0/backup' " +
"WITH revision_history = true, detached, execution locality = 'region=of-france'",
period: 24 * time.Hour,
},
},
},
{
name: "missing-destination-placeholder",
query: `CREATE SCHEDULE FOR BACKUP TABLE system.public.jobs INTO $1 RECURRING '@hourly'`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
new-cluster name=s1 allow-implicit-access
new-cluster name=s1 allow-implicit-access localities=us-east-1
----

# Create test schedules.
Expand Down Expand Up @@ -35,16 +35,20 @@ exec-sql
alter backup schedule $incID set with revision_history = false;
----

exec-sql
alter backup schedule $incID set with execution locality = 'region=us-east-1'
----

query-sql
with schedules as (show schedules) select id, command->'backup_statement' from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID "BACKUP INTO 'nodelocal://1/example-schedule' WITH revision_history = false, detached"
$incID "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH revision_history = false, detached"
$fullID "BACKUP INTO 'nodelocal://1/example-schedule' WITH revision_history = false, detached, execution locality = 'region=us-east-1'"
$incID "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH revision_history = false, detached, execution locality = 'region=us-east-1'"

# Change an option and set another.

exec-sql
alter backup schedule $incID set with revision_history = true, set with encryption_passphrase = 'abc';
alter backup schedule $incID set with revision_history = true, set with execution locality = '', set with encryption_passphrase = 'abc';
----

query-sql
Expand Down

0 comments on commit 85eb8d9

Please sign in to comment.