Skip to content

Commit

Permalink
stop_after_client_disconnect: use an iota for AllocState.Field
Browse files Browse the repository at this point in the history
  • Loading branch information
langmartin committed May 13, 2020
1 parent 57e335a commit 7b7c3f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6513,8 +6513,14 @@ func (t *Template) Warnings() error {
}

// AllocState records a single event that changes the state of the whole allocation
type AllocStateField uint8

const (
AllocStateFieldClientStatus AllocStateField = iota
)

type AllocState struct {
Field string
Field AllocStateField
Value string
Time time.Time
}
Expand Down Expand Up @@ -8445,7 +8451,7 @@ func (a *Allocation) WaitClientStop() time.Time {
// 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 == "ClientStatus" &&
if s.Field == AllocStateFieldClientStatus &&
s.Value == AllocClientStatusLost {
t = s.Time
break
Expand Down Expand Up @@ -8530,12 +8536,12 @@ func (a *Allocation) SetStop(clientStatus, clientDesc string) {
a.DesiredStatus = AllocDesiredStatusStop
a.ClientStatus = clientStatus
a.ClientDescription = clientDesc
a.AppendState("ClientStatus", clientStatus)
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, value string) {
func (a *Allocation) AppendState(field AllocStateField, value string) {
a.AllocStates = append(a.AllocStates, &AllocState{
Field: field,
Value: value,
Expand Down Expand Up @@ -9446,7 +9452,7 @@ func (p *Plan) AppendStoppedAlloc(alloc *Allocation, desiredDesc, clientStatus s
newAlloc.ClientStatus = clientStatus
}

newAlloc.AppendState("ClientStatus", clientStatus)
newAlloc.AppendState(AllocStateFieldClientStatus, clientStatus)

node := alloc.NodeID
existing := p.NodeUpdate[node]
Expand Down
4 changes: 2 additions & 2 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3605,7 +3605,7 @@ func TestPlan_AppendStoppedAllocAppendsAllocWithUpdatedAttrs(t *testing.T) {
expectedAlloc.ClientStatus = AllocClientStatusLost
expectedAlloc.Job = nil
expectedAlloc.AllocStates = []*AllocState{{
Field: "ClientStatus",
Field: AllocStateFieldClientStatus,
Value: "lost",
}}

Expand Down Expand Up @@ -4422,7 +4422,7 @@ func TestAllocation_WaitClientStop(t *testing.T) {
}

if tc.status == AllocClientStatusLost {
a.AppendState("ClientStatus", AllocClientStatusLost)
a.AppendState(AllocStateFieldClientStatus, AllocClientStatusLost)
}

j.TaskGroups[0].StopAfterClientDisconnect = &tc.stop
Expand Down
2 changes: 1 addition & 1 deletion scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,7 @@ func TestServiceSched_StopAfterClientDisconnect(t *testing.T) {
alloc.ClientStatus = structs.AllocClientStatusRunning
if !tc.when.IsZero() {
alloc.AllocStates = []*structs.AllocState{{
Field: "ClientStatus",
Field: structs.AllocStateFieldClientStatus,
Value: structs.AllocClientStatusLost,
Time: tc.when,
}}
Expand Down

0 comments on commit 7b7c3f4

Please sign in to comment.