-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Job revert #2575
Job revert #2575
Changes from 7 commits
b8b18db
daeabb9
99e9f2a
6a9af5a
c818602
694d9c4
e0af3d7
8d01a4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,8 +286,8 @@ func (j *Job) Summary(args *structs.JobSummaryRequest, | |
} | ||
|
||
// Validate validates a job | ||
func (j *Job) Validate(args *structs.JobValidateRequest, | ||
reply *structs.JobValidateResponse) error { | ||
func (j *Job) Validate(args *structs.JobValidateRequest, reply *structs.JobValidateResponse) error { | ||
defer metrics.MeasureSince([]string{"nomad", "job", "validate"}, time.Now()) | ||
|
||
if err := validateJob(args.Job); err != nil { | ||
if merr, ok := err.(*multierror.Error); ok { | ||
|
@@ -300,10 +300,70 @@ func (j *Job) Validate(args *structs.JobValidateRequest, | |
reply.Error = err.Error() | ||
} | ||
} | ||
|
||
reply.DriverConfigValidated = true | ||
return nil | ||
} | ||
|
||
// Revert is used to revert the job to a prior version | ||
func (j *Job) Revert(args *structs.JobRevertRequest, reply *structs.JobRegisterResponse) error { | ||
if done, err := j.srv.forward("Job.Revert", args, args, reply); done { | ||
return err | ||
} | ||
defer metrics.MeasureSince([]string{"nomad", "job", "revert"}, time.Now()) | ||
|
||
// Validate the arguments | ||
if args.JobID == "" { | ||
return fmt.Errorf("missing job ID for evaluation") | ||
} | ||
|
||
// Lookup the job by version | ||
snap, err := j.srv.fsm.State().Snapshot() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ws := memdb.NewWatchSet() | ||
jobV, err := snap.JobByIDAndVersion(ws, args.JobID, args.JobVersion) | ||
if err != nil { | ||
return err | ||
} | ||
if jobV == nil { | ||
return fmt.Errorf("job %q at version %d not found", args.JobID, args.JobVersion) | ||
} | ||
|
||
cur, err := snap.JobByID(ws, args.JobID) | ||
if err != nil { | ||
return err | ||
} | ||
if cur == nil { | ||
return fmt.Errorf("job %q not found", args.JobID) | ||
} | ||
|
||
if args.JobVersion == cur.Version { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call! Done. |
||
return fmt.Errorf("can't revert to current version") | ||
} | ||
|
||
// Build the register request | ||
reg := &structs.JobRegisterRequest{ | ||
Job: jobV.Copy(), | ||
WriteRequest: args.WriteRequest, | ||
} | ||
|
||
// If the request is enforcing the existing version do a check. | ||
if args.EnforcePriorVersion != nil { | ||
if cur.Version != *args.EnforcePriorVersion { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is racy, and probably should be evaluated inside the FSM. Effectively this is a check-and-set, but multiple concurrent calls to this API will be allowed to make progress depending on the timing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I realized that when implementing this. The register endpoint should push the logic from the RPC layer down into the FSM. This will fix this issue for both this and the register. Just out of scope for this PR. I will put a comment on that part of the code though |
||
return fmt.Errorf("Current job has version %d; enforcing version %d", cur.Version, *args.EnforcePriorVersion) | ||
} | ||
|
||
reg.EnforceIndex = true | ||
reg.JobModifyIndex = cur.JobModifyIndex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The modify index will get overwritten at Upsert time right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those two are the way to do the CAS operation on the register |
||
} | ||
|
||
// Register the version. | ||
return j.Register(reg, reply) | ||
} | ||
|
||
// Evaluate is used to force a job for re-evaluation | ||
func (j *Job) Evaluate(args *structs.JobEvaluateRequest, reply *structs.JobRegisterResponse) error { | ||
if done, err := j.srv.forward("Job.Evaluate", args, args, reply); done { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check this first, since you expect to get the "job not found" error before the "job at version" not found error.