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

sql,jobs,ccl: add ability to specify a REASON string with PAUSE JOB #68909

Merged
merged 3 commits into from
Aug 18, 2021

Conversation

rhu713
Copy link
Contributor

@rhu713 rhu713 commented Aug 13, 2021

Add ability to specify a REASON string with PAUSE JOB, persist this string in the job's payload. See individual commits.

@rhu713 rhu713 requested a review from a team August 13, 2021 19:17
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rhu713 rhu713 removed the request for review from a team August 13, 2021 19:17
Copy link

@sajjadrizvi sajjadrizvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in jobs package look good to me.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got some build issues. Otherwise, change looks good to me mod the call for the defensive check.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rhu713)


pkg/jobs/jobs_test.go, line 3181 at r3 (raw file):

Quoted 8 lines of code…
		var payloadBytes []byte
		var payload jobspb.Payload
		var status string
		tdb.QueryRow(t, fmt.Sprintf("SELECT status, payload FROM system.jobs where id = %d", jobID)).Scan(
			&status, &payloadBytes)
		require.Equal(t, "paused", status)
		require.NoError(t, protoutil.Unmarshal(payloadBytes, &payload))
		require.Equal(t, "for testing", payload.PauseReason)

nit: make yourself some closures for this. Also pass the ID as a parameter to the query:

getStatusAndPayload := func(t *testing.T, id jobspb.ID) (status string, payload jobspb.Payload) {
    var payloadBytes []byte
    tdb.QueryRow(
        t, "SELECT status, payload FROM system.jobs where id = $1", jobID,
    ).Scan(&status, &payloadBytes)
    require.NoError(t, protoutil.Unmarshal(payloadBytes, &payload))
    return status, payload
}
checkStatusAndPauseReason := func(t *testing.T, id jobspb.ID, expStatus, expPauseReason string). {
    status, payload := getStatusAndPayload(t, id)
    require.Equal(t, expStatus, status, "status")
    require.Equal(t, expPauseReason, payload.PauseReason, "pause reason")
}

Then these will look like:

	{
		// Next, pause the job with a reason. Wait for pause and make sure the pause reason is set.
		require.NoError(t, registry.PauseRequested(ctx, nil, jobID, "for testing"))
		tdb.CheckQueryResultsRetry(t, q, [][]string{{"paused"}})
                checkStatusAndPauseReason("paused", "for testing")
        }

pkg/jobs/jobs_test.go, line 3197 at r3 (raw file):

		require.NoError(t, protoutil.Unmarshal(payloadBytes, &payload))
		require.Equal(t, "for testing", payload.PauseReason)
		fmt.Println(payload)

remove


pkg/sql/control_jobs.go, line 65 at r3 (raw file):

		}
	}

How do you feel about adding an assertion that reason is empty unless the desiredStatus is paused? I know the grammar doesn't allow it but we've been hurt in the past when we change the grammar and then don't realize that assumptions have been violated.

@rhu713 rhu713 requested a review from dt August 16, 2021 14:31
@rhu713 rhu713 requested a review from a team as a code owner August 16, 2021 20:17
Copy link
Contributor Author

@rhu713 rhu713 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner and @dt)


pkg/jobs/jobs_test.go, line 3181 at r3 (raw file):

Previously, ajwerner wrote…
		var payloadBytes []byte
		var payload jobspb.Payload
		var status string
		tdb.QueryRow(t, fmt.Sprintf("SELECT status, payload FROM system.jobs where id = %d", jobID)).Scan(
			&status, &payloadBytes)
		require.Equal(t, "paused", status)
		require.NoError(t, protoutil.Unmarshal(payloadBytes, &payload))
		require.Equal(t, "for testing", payload.PauseReason)

nit: make yourself some closures for this. Also pass the ID as a parameter to the query:

getStatusAndPayload := func(t *testing.T, id jobspb.ID) (status string, payload jobspb.Payload) {
    var payloadBytes []byte
    tdb.QueryRow(
        t, "SELECT status, payload FROM system.jobs where id = $1", jobID,
    ).Scan(&status, &payloadBytes)
    require.NoError(t, protoutil.Unmarshal(payloadBytes, &payload))
    return status, payload
}
checkStatusAndPauseReason := func(t *testing.T, id jobspb.ID, expStatus, expPauseReason string). {
    status, payload := getStatusAndPayload(t, id)
    require.Equal(t, expStatus, status, "status")
    require.Equal(t, expPauseReason, payload.PauseReason, "pause reason")
}

Then these will look like:

	{
		// Next, pause the job with a reason. Wait for pause and make sure the pause reason is set.
		require.NoError(t, registry.PauseRequested(ctx, nil, jobID, "for testing"))
		tdb.CheckQueryResultsRetry(t, q, [][]string{{"paused"}})
                checkStatusAndPauseReason("paused", "for testing")
        }

Done, thanks!


pkg/jobs/jobs_test.go, line 3197 at r3 (raw file):

Previously, ajwerner wrote…

remove

Done


pkg/sql/control_jobs.go, line 65 at r3 (raw file):

Previously, ajwerner wrote…

How do you feel about adding an assertion that reason is empty unless the desiredStatus is paused? I know the grammar doesn't allow it but we've been hurt in the past when we change the grammar and then don't realize that assumptions have been violated.

Okay, added this this defensive check. Let me know what you think.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 15 files at r8, 2 of 6 files at r9.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dt)

@rhu713 rhu713 force-pushed the pause-reason branch 2 times, most recently from 0debb91 to b73dc9d Compare August 17, 2021 14:02
@rhu713 rhu713 removed request for a team August 17, 2021 14:14
@rhu713 rhu713 force-pushed the pause-reason branch 3 times, most recently from 1c59bf0 to fc9bb9e Compare August 18, 2021 17:30
Rui Hu added 3 commits August 18, 2021 16:14
Currently there's not a way to attach a user-specified reason message to a
PAUSE JOB command. The reason message is useful to gain visibility into why a
job was paused by a user or by the job itself. To address this, an optional
WITH REASON = <reason> was added to PAUSE JOB(s) so that a reason can be
attached to any PAUSE statement. This commit only adds the WITH REASON
syntax to the SQL grammar, without any implementation.

Release note (enterprise change): Adds WITH REASON = <reason> to PAUSE JOB
statement to gain visibility into why a job was paused. This commit only
adds the syntax to the grammar with no implementation.
Previously a WITH REASON clause was added to the PAUSE JOB command to allow
a reason string to be attached to a pause command. This change allows for
this string to be propagated all the way into the hander for pause requests
in the jobs code.

Release note: None
Previously a WITH REASON clause was added to the PAUSE JOB command to allow
a reason string to be attached to a pause command. This change persists this
pause reason string into the payload of the job every time a pause is
requested.

Release note (enterprise change): Adds WITH REASON = <reason> to PAUSE JOB
statement to gain visibility into why a job was paused by allowing pauses to
be attached to a reason string. This reason is then persisted in the payload of
the job and can be queried later.
@rhu713
Copy link
Contributor Author

rhu713 commented Aug 18, 2021

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 18, 2021

Build succeeded:

@craig craig bot merged commit 5506d0b into cockroachdb:master Aug 18, 2021
@@ -6060,11 +6060,25 @@ pause_jobs_stmt:
Command: tree.PauseJob,
}
}
| PAUSE JOB a_expr WITH REASON '=' string_or_placeholder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late comment, but I'm curious what the motivation was to include the = token? Was PAUSE JOB a_expr WITH REASON string_or_placeholder an ambiguous syntax?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants