Skip to content

Commit dd7c0b4

Browse files
committed
merged the code from the pullrequest hashicorp#15097
1 parent 6aff34d commit dd7c0b4

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

nomad/core_sched.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,18 @@ func (c *CoreScheduler) gcEval(eval *structs.Evaluation, thresholdIndex uint64,
297297
}
298298

299299
// If the eval is from a running "batch" job we don't want to garbage
300-
// collect its allocations. If there is a long running batch job and its
301-
// terminal allocations get GC'd the scheduler would re-run the
302-
// allocations.
300+
// collect its most current allocations. If there is a long running batch job and its
301+
// terminal allocations get GC'd the scheduler would re-run the allocations. However,
302+
// we do want to GC old Evals and Allocs if there are newer ones due to an update.
303303
if eval.Type == structs.JobTypeBatch {
304304
// Check if the job is running
305305

306-
// Can collect if:
307-
// Job doesn't exist
308-
// Job is Stopped and dead
309-
// allowBatch and the job is dead
306+
// Can collect if either holds:
307+
// - Job doesn't exist
308+
// - Job is Stopped and dead
309+
// - allowBatch and the job is dead
310+
//
311+
// If we cannot collect outright, check if a partial GC may occur
310312
collect := false
311313
if job == nil {
312314
collect = true
@@ -318,12 +320,9 @@ func (c *CoreScheduler) gcEval(eval *structs.Evaluation, thresholdIndex uint64,
318320
collect = true
319321
}
320322

321-
// We don't want to gc anything related to a job which is not dead
322-
// If the batch job doesn't exist we can GC it regardless of allowBatch
323323
if !collect {
324-
// Find allocs associated with older (based on createindex) and GC them if terminal
325-
oldAllocs := olderVersionTerminalAllocs(allocs, job)
326-
return false, oldAllocs, nil
324+
oldAllocs, gcEval := olderVersionTerminalAllocs(allocs, job, thresholdIndex)
325+
return gcEval, oldAllocs, nil
327326
}
328327
}
329328

@@ -344,16 +343,20 @@ func (c *CoreScheduler) gcEval(eval *structs.Evaluation, thresholdIndex uint64,
344343
return gcEval, gcAllocIDs, nil
345344
}
346345

347-
// olderVersionTerminalAllocs returns terminal allocations whose job create index
348-
// is older than the job's create index
349-
func olderVersionTerminalAllocs(allocs []*structs.Allocation, job *structs.Job) []string {
346+
// olderVersionTerminalAllocs returns a tuplie ([]string, bool). The first element is the list of
347+
// terminal allocations which may be garbage collected for batch jobs. The second element indicates
348+
// whether or not the allocation itself may be garbage collected.
349+
func olderVersionTerminalAllocs(allocs []*structs.Allocation, job *structs.Job, thresholdIndex uint64) ([]string, bool) {
350350
var ret []string
351+
var mayGCEval = true
351352
for _, alloc := range allocs {
352-
if alloc.Job != nil && alloc.Job.CreateIndex < job.CreateIndex && alloc.TerminalStatus() {
353+
if alloc.CreateIndex < job.JobModifyIndex && alloc.ModifyIndex < thresholdIndex && alloc.TerminalStatus() {
353354
ret = append(ret, alloc.ID)
355+
} else {
356+
mayGCEval = false
354357
}
355358
}
356-
return ret
359+
return ret, mayGCEval
357360
}
358361

359362
// evalReap contacts the leader and issues a reap on the passed evals and
@@ -1121,4 +1124,4 @@ func (c *CoreScheduler) getOldestAllocationIndex() (uint64, error) {
11211124
}
11221125
}
11231126
return 0, nil
1124-
}
1127+
}

version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var (
1111
GitDescribe string
1212

1313
// The main version number that is being run at the moment.
14-
Version = "1.4.2"
14+
Version = "1.4.2.updated-gc"
1515

1616
// A pre-release marker for the version. If this is "" (empty string)
1717
// then it means that it is a final release. Otherwise, this is a pre-release

0 commit comments

Comments
 (0)