Skip to content

Commit

Permalink
api: set the job submission during job reversion (#17097)
Browse files Browse the repository at this point in the history
* api: set the job submission during job reversion

This PR fixes a bug where the job submission would always be nil when
a job goes through a reversion to a previous version. Basically we need
to detect when this happens, lookup the submission of the job version
being reverted to, and set that as the submission of the new job being
created.

* e2e: add e2e test for job submissions during reversion

This e2e test ensures a reverted job inherits the job submission
associated with the version of the job being reverted to.
  • Loading branch information
shoenig authored May 8, 2023
1 parent c2dc1c5 commit 8dff1f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
42 changes: 42 additions & 0 deletions e2e/jobsubmissions/jobsubapi_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jobsubmissions

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -23,6 +24,7 @@ func TestJobSubmissionAPI(t *testing.T) {
t.Run("testRunCLIVarFlags", testRunCLIVarFlags)
t.Run("testSubmissionACL", testSubmissionACL)
t.Run("testMaxSize", testMaxSize)
t.Run("testReversion", testReversion)
}

func testParseAPI(t *testing.T) {
Expand Down Expand Up @@ -275,3 +277,43 @@ func testMaxSize(t *testing.T) {
must.ErrorContains(t, err, "job source not found")
must.Nil(t, sub)
}

func testReversion(t *testing.T) {
nomad := e2eutil.NomadClient(t)

jobID := "job-sub-reversion-" + uuid.Short()
jobIDs := []string{jobID}
t.Cleanup(e2eutil.MaybeCleanupJobsAndGC(&jobIDs))

// register job 3 times
for i := 0; i < 3; i++ {
yVar := fmt.Sprintf("-var=Y=%d", i)

// register the job
err := e2eutil.RegisterWithArgs(jobID, "input/xyz.hcl", "-var=X=hello", yVar, "-var=Z=false")
must.NoError(t, err)

// find our alloc id
allocID := e2eutil.SingleAllocID(t, jobID, "", i)

// wait for alloc to complete
_ = e2eutil.WaitForAllocStopped(t, nomad, allocID)
}

// revert the job back to version 1

err := e2eutil.Revert(jobID, "input/xyz.hcl", 1)
must.NoError(t, err)

// there should be a submission for version 3, and it should
// contain Y=1 as did the version 1 of the job
expectY := []string{"0", "1", "2", "1"}
for version := 0; version < 4; version++ {
sub, _, err := nomad.Jobs().Submission(jobID, version, &api.QueryOptions{
Region: "global",
Namespace: "default",
})
must.NoError(t, err)
must.Eq(t, expectY[version], sub.VariableFlags["Y"])
}
}
5 changes: 5 additions & 0 deletions nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,11 @@ func (s *StateStore) upsertJobImpl(index uint64, sub *structs.JobSubmission, job
if !keepVersion {
job.JobModifyIndex = index
if job.Version <= existingJob.Version {
if sub == nil {
// in the reversion case we must set the submission to be
// that of the job version we are reverting to
sub, _ = s.jobSubmission(nil, job.Namespace, job.ID, job.Version, txn)
}
job.Version = existingJob.Version + 1
}
}
Expand Down

0 comments on commit 8dff1f7

Please sign in to comment.