diff --git a/pkg/ccl/backupccl/alter_backup_schedule.go b/pkg/ccl/backupccl/alter_backup_schedule.go index 3e27fa212f82..ff0c5e5f87c3 100644 --- a/pkg/ccl/backupccl/alter_backup_schedule.go +++ b/pkg/ccl/backupccl/alter_backup_schedule.go @@ -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 } @@ -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 diff --git a/pkg/ccl/backupccl/create_scheduled_backup.go b/pkg/ccl/backupccl/create_scheduled_backup.go index a5735ca51ccf..78d40a601d4f 100644 --- a/pkg/ccl/backupccl/create_scheduled_backup.go +++ b/pkg/ccl/backupccl/create_scheduled_backup.go @@ -85,6 +85,7 @@ type scheduledBackupSpec struct { kmsURIs []string incrementalStorage []string includeAllSecondaryTenants *bool + execLoc *string } func makeScheduleDetails(opts map[string]string) (jobspb.ScheduleDetails, error) { @@ -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. @@ -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) @@ -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) diff --git a/pkg/ccl/backupccl/create_scheduled_backup_test.go b/pkg/ccl/backupccl/create_scheduled_backup_test.go index b2d39397cb42..82a95ee48dd1 100644 --- a/pkg/ccl/backupccl/create_scheduled_backup_test.go +++ b/pkg/ccl/backupccl/create_scheduled_backup_test.go @@ -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" @@ -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 @@ -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'`, diff --git a/pkg/ccl/backupccl/testdata/backup-restore/alter-schedule/backup-options b/pkg/ccl/backupccl/testdata/backup-restore/alter-schedule/backup-options index 8806eafcce83..5a65836cc939 100644 --- a/pkg/ccl/backupccl/testdata/backup-restore/alter-schedule/backup-options +++ b/pkg/ccl/backupccl/testdata/backup-restore/alter-schedule/backup-options @@ -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. @@ -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