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: replace the ie in Registry.createJobsInBatchWithTxn #91679

Merged
merged 1 commit into from
Nov 11, 2022

Conversation

ZhouXing19
Copy link
Collaborator

@ZhouXing19 ZhouXing19 commented Nov 10, 2022

When creating a job, we should use an internal executor bound to the outer txn and related metadata.

We also found that in this case, if a user changes the timezone and then triggers a schema change job, the job's creation time (i.e. value createdcolumn in the system.jobs) will automatically switch to the timestamp in the updated timezone, rather than UTC. This is because we have the created using now() in the SQL statement. We now change it to be inserted always with UTC time.

Informs: #91004
Informs: #91718

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 force-pushed the create-job-with-txn branch 7 times, most recently from 97c175c to 82f2c6e Compare November 11, 2022 13:34
@ZhouXing19 ZhouXing19 marked this pull request as ready for review November 11, 2022 13:41
@ZhouXing19 ZhouXing19 requested review from a team as code owners November 11, 2022 13:41
@ZhouXing19 ZhouXing19 requested review from a team and rhu713 and removed request for a team November 11, 2022 13:41
// If the job has failed, and the dump mode is set to anything
// except noDump, then we should dump the trace.
// The string comparison is unfortunate but is used to differentiate a job
// that has failed from a job that has been canceled.
if jobErr != nil && !HasErrJobCanceled(jobErr) && resumerCtx.Err() == nil {
r.td.Dump(dumpCtx, strconv.Itoa(int(jobID)), traceID, r.ex)

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra empty line

Comment on lines 131 to 128
// NextRunClause calculates the next execution time of a job with exponential backoff delay, calculated
// using last_run and num_runs values.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: This comment belongs to the query below I believe.

nit: Add comment for this newly added query?

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.

Instead of this approach, can we leave the query exactly as it is and change the way the value which gets generated comes to be by not using the default value? Consider:

diff --git a/pkg/jobs/registry.go b/pkg/jobs/registry.go
index 24a96699ad1..55b7dfed541 100644
--- a/pkg/jobs/registry.go
+++ b/pkg/jobs/registry.go
@@ -379,7 +379,7 @@ func (r *Registry) batchJobInsertStmt(
 ) (string, []interface{}, []jobspb.JobID, error) {
 	instanceID := r.ID()
 	const numColumns = 6
-	columns := [numColumns]string{`id`, `status`, `payload`, `progress`, `claim_session_id`, `claim_instance_id`}
+	columns := [numColumns]string{`id`, `created`, `status`, `payload`, `progress`, `claim_session_id`, `claim_instance_id`}
 	marshalPanic := func(m protoutil.Message) []byte {
 		data, err := protoutil.Marshal(m)
 		if err != nil {
@@ -387,8 +387,13 @@ func (r *Registry) batchJobInsertStmt(
 		}
 		return data
 	}
+	created, err := tree.MakeDTimestamp(timeutil.FromUnixMicros(modifiedMicros), time.Microsecond)
+	if err != nil {
+		return "", nil, nil, errors.NewAssertionErrorWithWrappedErrf(err, "failed to make timestamp")
+	}
 	valueFns := map[string]func(*Record) interface{}{
 		`id`:                func(rec *Record) interface{} { return rec.JobID },
+		`created`:           func(record *Record) interface{} { return created },
 		`status`:            func(rec *Record) interface{} { return StatusRunning },
 		`claim_session_id`:  func(rec *Record) interface{} { return sessionID.UnsafeBytes() },
 		`claim_instance_id`: func(rec *Record) interface{} { return instanceID },

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


pkg/jobs/adopt.go line 135 at r1 (raw file):

	PossibleDelay = `args.initial_delay * (power(2, least(62, COALESCE(num_runs, 0))) - 1)::FLOAT`
	Piece1        = `COALESCE(last_run, created)`
	Piece2        = `least(

nit: don't export these

Code quote:

	PossibleDelay = `args.initial_delay * (power(2, least(62, COALESCE(num_runs, 0))) - 1)::FLOAT`
	Piece1        = `COALESCE(last_run, created)`
	Piece2        = `least(

Copy link
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

change the way the value which gets generated comes to be by not using the default value

Oh I did something similar in the second commit already: 82f2c6e
But I was with r.clock.Now().GoTime().Format(time.RFC3339Nano). Do you think that would work too?

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

Copy link
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

can we leave the query exactly as it is

Removed the first commit.

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


pkg/jobs/adopt.go line 85 at r1 (raw file):

Previously, Xiang-Gu (Xiang Gu) wrote…

nit: extra empty line

Done.


pkg/jobs/adopt.go line 132 at r1 (raw file):

Previously, Xiang-Gu (Xiang Gu) wrote…

nit: This comment belongs to the query below I believe.

nit: Add comment for this newly added query?

Those are queries that I used for debugging and forgot to remove them in that commit 😅 Have them deleted now.

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: mod the nit

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rhu713, @Xiang-Gu, and @ZhouXing19)


pkg/jobs/registry.go line 397 at r3 (raw file):

	valueFns := map[string]func(*Record) interface{}{
		`id`:                func(rec *Record) interface{} { return rec.JobID },
		`created`:           func(rec *Record) interface{} { return r.clock.Now().GoTime().Format(time.RFC3339Nano) },

I think this should use modifiedMicros. It'll evaluate to the same value as now() which it relative to the transaction timestamp. I also have a mild preference for constructing the datum as opposed to passing the string literal to the sql layer and making it parse it back. It also alleviates the need to ask the question whether RFC3339Nano is a format that sql likes.

When creating a job, we should use an internal executor bound to the outer txn
and related metadata.

We also found that in this case, if a user changes the timezone and then triggers
a schema change job, the job's creation time (i.e. value createdcolumn in the
system.jobs) will automatically switch to the timestamp in the updated timezone,
rather than UTC. This is because we have the created using now() in the SQL
statement. We now change it to be inserted always with UTC time.

Informs: cockroachdb#91004
Informs: cockroachdb#91718

Release note: None
@ZhouXing19
Copy link
Collaborator Author

pkg/jobs/registry.go line 397 at r3 (raw file):

Previously, ajwerner wrote…

I think this should use modifiedMicros. It'll evaluate to the same value as now() which it relative to the transaction timestamp. I also have a mild preference for constructing the datum as opposed to passing the string literal to the sql layer and making it parse it back. It also alleviates the need to ask the question whether RFC3339Nano is a format that sql likes.

That makes sense, thanks for explaining!
Done.

@ZhouXing19
Copy link
Collaborator Author

TFTR!
bors r=ajwerner

@craig
Copy link
Contributor

craig bot commented Nov 11, 2022

Build succeeded:

@craig craig bot merged commit 5c76966 into cockroachdb:master Nov 11, 2022
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.

4 participants