-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
server: stop after client disconnect #7939
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0463600
nomad/state/state_store: error message typo
langmartin 0e39816
jobspec, api: add stop_after_client_disconnect
langmartin c2cdeba
structs: alloc methods to support stop_after_client_disconnect
langmartin 6ac4c19
scheduler/reconcile_util: delayByStopAfterClientDisconnect
langmartin 107efae
scheduler/reconcile: use delayByStopAfterClientDisconnect
langmartin ac9878c
scheduler/util: updateNonTerminalAllocsToLost comments
langmartin 003ac8d
scheduler/testing: AssertEvalStatus just use require
langmartin 956b486
scheduler/generic_sched: don't create a blocked eval if delayed
langmartin aa218a3
scheduler/generic_sched_test: several scheduling cases
langmartin fc5f692
nomad/structs/structs_test: expect AllocStates
langmartin a53af87
stop_after_client_disconnect: use an iota for AllocState.Field
langmartin ef0f6d5
stop_after_client_disconnect: job copy, conversion bits
langmartin c7cc976
client/heartbeatstop: logger messages
langmartin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6516,6 +6516,19 @@ func (t *Template) Warnings() error { | |
return mErr.ErrorOrNil() | ||
} | ||
|
||
// AllocState records a single event that changes the state of the whole allocation | ||
type AllocStateField uint8 | ||
|
||
const ( | ||
AllocStateFieldClientStatus AllocStateField = iota | ||
) | ||
|
||
type AllocState struct { | ||
Field AllocStateField | ||
Value string | ||
Time time.Time | ||
} | ||
|
||
// Set of possible states for a task. | ||
const ( | ||
TaskStatePending = "pending" // The task is waiting to be run. | ||
|
@@ -8152,6 +8165,9 @@ type Allocation struct { | |
// TaskStates stores the state of each task, | ||
TaskStates map[string]*TaskState | ||
|
||
// AllocStates track meta data associated with changes to the state of the whole allocation, like becoming lost | ||
AllocStates []*AllocState | ||
|
||
// PreviousAllocation is the allocation that this allocation is replacing | ||
PreviousAllocation string | ||
|
||
|
@@ -8420,6 +8436,49 @@ func (a *Allocation) NextRescheduleTime() (time.Time, bool) { | |
return nextRescheduleTime, rescheduleEligible | ||
} | ||
|
||
// ShouldClientStop tests an alloc for StopAfterClientDisconnect configuration | ||
func (a *Allocation) ShouldClientStop() bool { | ||
tg := a.Job.LookupTaskGroup(a.TaskGroup) | ||
if tg == nil || | ||
tg.StopAfterClientDisconnect == nil || | ||
*tg.StopAfterClientDisconnect == 0*time.Nanosecond { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// WaitClientStop uses the reschedule delay mechanism to block rescheduling until | ||
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. I forget whether this was in the RFC or not, but either way this is a good catch. We would have had some really wild behavior without this. Also, like how this API gives us a |
||
// StopAfterClientDisconnect's block interval passes | ||
func (a *Allocation) WaitClientStop() time.Time { | ||
tg := a.Job.LookupTaskGroup(a.TaskGroup) | ||
|
||
// An alloc can only be marked lost once, so use the first lost transition | ||
var t time.Time | ||
for _, s := range a.AllocStates { | ||
if s.Field == AllocStateFieldClientStatus && | ||
s.Value == AllocClientStatusLost { | ||
t = s.Time | ||
break | ||
} | ||
} | ||
|
||
// On the first pass, the alloc hasn't been marked lost yet, and so we start | ||
// counting from now | ||
if t.IsZero() { | ||
t = time.Now().UTC() | ||
} | ||
|
||
// Find the max kill timeout | ||
kill := DefaultKillTimeout | ||
for _, t := range tg.Tasks { | ||
if t.KillTimeout > kill { | ||
kill = t.KillTimeout | ||
} | ||
} | ||
|
||
return t.Add(*tg.StopAfterClientDisconnect + kill) | ||
} | ||
|
||
// NextDelay returns a duration after which the allocation can be rescheduled. | ||
// It is calculated according to the delay function and previous reschedule attempts. | ||
func (a *Allocation) NextDelay() time.Duration { | ||
|
@@ -8476,6 +8535,24 @@ func (a *Allocation) Terminated() bool { | |
return false | ||
} | ||
|
||
// SetStopped updates the allocation in place to a DesiredStatus stop, with the ClientStatus | ||
func (a *Allocation) SetStop(clientStatus, clientDesc string) { | ||
a.DesiredStatus = AllocDesiredStatusStop | ||
a.ClientStatus = clientStatus | ||
a.ClientDescription = clientDesc | ||
a.AppendState(AllocStateFieldClientStatus, clientStatus) | ||
} | ||
|
||
// AppendState creates and appends an AllocState entry recording the time of the state | ||
// transition. Used to mark the transition to lost | ||
func (a *Allocation) AppendState(field AllocStateField, value string) { | ||
a.AllocStates = append(a.AllocStates, &AllocState{ | ||
Field: field, | ||
Value: value, | ||
Time: time.Now().UTC(), | ||
}) | ||
} | ||
|
||
// RanSuccessfully returns whether the client has ran the allocation and all | ||
// tasks finished successfully. Critically this function returns whether the | ||
// allocation has ran to completion and not just that the alloc has converged to | ||
|
@@ -9384,6 +9461,8 @@ func (p *Plan) AppendStoppedAlloc(alloc *Allocation, desiredDesc, clientStatus s | |
newAlloc.ClientStatus = clientStatus | ||
} | ||
|
||
newAlloc.AppendState(AllocStateFieldClientStatus, clientStatus) | ||
|
||
node := alloc.NodeID | ||
existing := p.NodeUpdate[node] | ||
p.NodeUpdate[node] = append(existing, newAlloc) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the only new field, the rest are whitespace differences