Skip to content

Commit

Permalink
fleetctl: determine job state to be changed more accurately
Browse files Browse the repository at this point in the history
So far assertUnitState() has determined whether a unit's job state is to
be changed, simply by comparing current unit state with the target unit
state. That's actually not always correct, as it's unnecessary to do
that also for downgrading states. (e.g. launched -> loaded)

To run it only when upgrading states (e.g. loaded -> launched),
introduce a helper jsToBeChanged(), which returns true only the target
state is a more activated one than the current state. Use
jsToBeChanged() instead of simple comparison in assertUnitState().
  • Loading branch information
Dongsu Park committed Jul 28, 2016
1 parent 063e74f commit 7871dc0
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion fleetctl/fleetctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ func assertUnitState(name string, js job.JobState, out io.Writer) (ret bool) {
state = u.CurrentState
}

if job.JobState(state) != js {
if jsToBeChanged(job.JobState(state), js) {
log.Debugf("Waiting for Unit(%s) state(%s) to be %s", name, job.JobState(state), js)
return
}
Expand Down Expand Up @@ -1158,6 +1158,32 @@ func machineState(machID string) (*machine.MachineState, error) {
return nil, nil
}

// jsToBeChanged returns true if the target state is a more activated state
// than the current state. For example, it returns true if it's a transition
// from inactive to launched, but false for other way around.
func jsToBeChanged(cur job.JobState, tgt job.JobState) bool {
switch tgt {
case job.JobStateInactive:
if cur == job.JobStateLoaded || cur == job.JobStateLaunched {
return true
}
break
case job.JobStateLoaded:
if cur == job.JobStateInactive {
return true
}
break
case job.JobStateLaunched:
if cur == job.JobStateInactive || cur == job.JobStateLoaded {
return true
}
break
default:
break
}
return false
}

// cachedMachineState makes a best-effort to retrieve the MachineState of the given machine ID.
// It memoizes MachineState information for the life of a fleetctl invocation.
// Any error encountered retrieving the list of machines is ignored.
Expand Down

0 comments on commit 7871dc0

Please sign in to comment.