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

jobs: don't reset next_run when resuming active schedules #69571

Merged
merged 1 commit into from
Sep 2, 2021
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
18 changes: 18 additions & 0 deletions pkg/jobs/schedule_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -75,6 +76,23 @@ func TestScheduleControl(t *testing.T) {
require.True(t, th.loadSchedule(t, scheduleID).IsPaused())
})

t.Run("pause-active-schedule", func(t *testing.T) {
schedule := th.newScheduledJob(t, "test schedule", "select 42")
require.NoError(t, schedule.SetSchedule("@weekly"))
// Datums only store up until microseconds.
ms := time.Microsecond
firstRunTime := timeutil.Now().Add(10 * time.Second).Truncate(ms)
schedule.SetNextRun(firstRunTime)
require.NoError(t, schedule.Create(ctx, th.cfg.InternalExecutor, nil))
scheduleID := schedule.ScheduleID()
require.Equal(t, schedule.NextRun(), firstRunTime)
th.sqlDB.Exec(t, "RESUME SCHEDULE $1", scheduleID)

afterSchedule := th.loadSchedule(t, scheduleID)
require.False(t, afterSchedule.IsPaused())
require.Equal(t, afterSchedule.NextRun(), firstRunTime)
})

t.Run("cannot-resume-one-off-schedule", func(t *testing.T) {
schedule := th.newScheduledJob(t, "test schedule", "select 42")
require.NoError(t, schedule.Create(ctx, th.cfg.InternalExecutor, nil))
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/control_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func loadSchedule(params runParams, scheduleID tree.Datum) (*jobs.ScheduledJob,
"load-schedule",
params.EvalContext().Txn, sessiondata.InternalExecutorOverride{User: security.RootUserName()},
fmt.Sprintf(
"SELECT schedule_id, schedule_expr, executor_type, execution_args FROM %s WHERE schedule_id = $1",
"SELECT schedule_id, next_run, schedule_expr, executor_type, execution_args FROM %s WHERE schedule_id = $1",
env.ScheduledJobsTableName(),
),
scheduleID)
Expand Down Expand Up @@ -138,9 +138,13 @@ func (n *controlSchedulesNode) startExec(params runParams) error {
schedule.Pause()
err = updateSchedule(params, schedule)
case tree.ResumeSchedule:
err = schedule.ScheduleNextRun()
if err == nil {
err = updateSchedule(params, schedule)
// Only schedule the next run time on PAUSED schedules, since ACTIVE schedules may
// have a custom next run time set by first_run.
if schedule.IsPaused() {
err = schedule.ScheduleNextRun()
if err == nil {
err = updateSchedule(params, schedule)
}
}
case tree.DropSchedule:
var ex jobs.ScheduledJobExecutor
Expand Down