Skip to content

Commit

Permalink
updates based on review.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Sep 28, 2023
1 parent c26431b commit 729421a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
33 changes: 22 additions & 11 deletions nomad/plan_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,22 +808,33 @@ func isValidForDisconnectedNode(plan *structs.Plan, nodeID string) bool {
// ensure this, and will return an error when a duplicate is found.
func evaluatePlanAllocIndexes(stateSnap *state.StateSnapshot, plan *structs.Plan) error {

switch plan.Job.Type {
case structs.JobTypeSystem, structs.JobTypeSysBatch:
// system: all allocation indexes are zero, so we cannot perform this
// check.
//
// sysbatch: allocation indexes are unique across each set of node
// allocations. They are not unique across a version of a job,
// therefore this will require new functionality and testing.
// TODO(jrasell): add this.
return nil
default:
}

// In the case there are no node allocations, the plan will only be
// destructive actions which will not result in new alloc indexes being
// generated. Therefore, exit early.
if len(plan.NodeAllocation) == 0 {
return nil
}

// This map will be used to track what allocation names we have. The
// allocation name is formed using the jobID, taskgroup name, and alloc
// index. We can therefore use this to identify alloc index duplicates and
// save the computational overhead of extracting the index from the name.
var allocIndexMap map[string]string

// If the plan includes node allocations, instantiate the set. In the case
// there are no node allocations, the plan will only be destructive actions
// which will not result in new alloc indexes being generated.
if len(plan.NodeAllocation) > 0 {
allocIndexMap = make(map[string]string)
} else {
return nil
}
allocIndexMap := make(map[string]string)

stateAllocs, err := stateSnap.AllocsByJob(nil, plan.Job.Namespace, plan.Job.ID, false)
stateAllocs, err := stateSnap.AllocsByJob(nil, plan.Job.Namespace, plan.Job.ID, true)
if err != nil {
return err
}
Expand Down
50 changes: 50 additions & 0 deletions nomad/plan_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,4 +1559,54 @@ func Test_evaluatePlanAllocIndexes(t *testing.T) {
evaluatePlanAllocIndexes(testStateSnapshotFn(t, testState), &testPlan),
duplicateAllocIndexErrorString)
})

// All allocations within a job of type system have zero index. Therefore,
// this check is not run.
t.Run("system job", func(t *testing.T) {
testPlan := structs.Plan{
Job: &structs.Job{
ID: testAllocs[0].JobID,
Namespace: testAllocs[0].Namespace,
Version: 0,
Type: structs.JobTypeSystem,
},
NodeUpdate: nil,
NodeAllocation: map[string][]*structs.Allocation{
"8bdb21db-9445-3650-ca9d-0d7883cc8a73": {
{Name: "example.cache[0]", ID: "bd8cf1bc-5ded-5942-5b39-078878e74e15"},
},
"52b3508a-c88e-fc83-74c9-829d5ef1103a": {
{Name: "example.cache[0]", ID: "2a8243d9-c54b-ec78-8b51-e99ff3579d72"},
},
},
}
must.NoError(t, evaluatePlanAllocIndexes(testStateSnapshotFn(t, testState), &testPlan))
})

// Allocation indexes are unique across each set of node allocations. They
// are not unique across a version of a job, therefore this will require
// new functionality and testing. Once the additional functionality is
// added, this test should be removed/updated.
t.Run("sysbatch job", func(t *testing.T) {
testPlan := structs.Plan{
Job: &structs.Job{
ID: testAllocs[0].JobID,
Namespace: testAllocs[0].Namespace,
Version: 0,
Type: structs.JobTypeSysBatch,
},
NodeUpdate: nil,
NodeAllocation: map[string][]*structs.Allocation{
"8bdb21db-9445-3650-ca9d-0d7883cc8a73": {
{Name: "example.cache[0]", ID: "bd8cf1bc-5ded-5942-5b39-078878e74e15"},
{Name: "example.cache[1]", ID: "1097ce2b-f131-4065-9ed1-86795b738e7a"},
},
"52b3508a-c88e-fc83-74c9-829d5ef1103a": {
{Name: "example.cache[0]", ID: "2a8243d9-c54b-ec78-8b51-e99ff3579d72"},
{Name: "example.cache[1]", ID: "cc7460e0-a02d-47ef-9041-7b0e681f37fd"},
},
},
}
must.NoError(t, evaluatePlanAllocIndexes(testStateSnapshotFn(t, testState), &testPlan))
})
}

0 comments on commit 729421a

Please sign in to comment.