2015-11-05 19:12:31 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-03-24 22:43:55 +00:00
|
|
|
"fmt"
|
2015-12-17 18:37:53 +00:00
|
|
|
"math/rand"
|
2016-02-04 03:43:44 +00:00
|
|
|
"sync"
|
2015-11-05 19:12:31 +00:00
|
|
|
"time"
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2016-10-23 01:08:30 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2015-11-14 06:07:13 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-05 19:12:31 +00:00
|
|
|
)
|
|
|
|
|
2016-03-24 22:43:55 +00:00
|
|
|
const (
|
|
|
|
// jitter is the percent of jitter added to restart delays.
|
|
|
|
jitter = 0.25
|
|
|
|
|
|
|
|
ReasonNoRestartsAllowed = "Policy allows no restarts"
|
|
|
|
ReasonUnrecoverableErrror = "Error was unrecoverable"
|
|
|
|
ReasonWithinPolicy = "Restart within policy"
|
|
|
|
ReasonDelay = "Exceeded allowed attempts, applying a delay"
|
|
|
|
)
|
2015-12-18 20:11:12 +00:00
|
|
|
|
2016-02-02 23:08:07 +00:00
|
|
|
func newRestartTracker(policy *structs.RestartPolicy, jobType string) *RestartTracker {
|
|
|
|
onSuccess := true
|
|
|
|
if jobType == structs.JobTypeBatch {
|
|
|
|
onSuccess = false
|
|
|
|
}
|
2015-12-18 20:17:13 +00:00
|
|
|
return &RestartTracker{
|
|
|
|
startTime: time.Now(),
|
2016-02-02 23:08:07 +00:00
|
|
|
onSuccess: onSuccess,
|
2015-12-18 20:17:13 +00:00
|
|
|
policy: policy,
|
2015-12-17 18:37:53 +00:00
|
|
|
rand: rand.New(rand.NewSource(time.Now().Unix())),
|
2015-11-05 19:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
type RestartTracker struct {
|
2016-10-23 01:08:30 +00:00
|
|
|
waitRes *dstructs.WaitResult
|
2016-10-05 22:11:09 +00:00
|
|
|
startErr error
|
|
|
|
restartTriggered bool // Whether the task has been signalled to be restarted
|
2017-09-10 23:25:13 +00:00
|
|
|
failure bool // Whether a failure triggered the restart
|
2016-10-05 22:11:09 +00:00
|
|
|
count int // Current number of attempts.
|
|
|
|
onSuccess bool // Whether to restart on successful exit code.
|
|
|
|
startTime time.Time // When the interval began
|
|
|
|
reason string // The reason for the last state
|
|
|
|
policy *structs.RestartPolicy
|
|
|
|
rand *rand.Rand
|
|
|
|
lock sync.Mutex
|
2015-11-14 06:07:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:43:44 +00:00
|
|
|
// SetPolicy updates the policy used to determine restarts.
|
|
|
|
func (r *RestartTracker) SetPolicy(policy *structs.RestartPolicy) {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
r.policy = policy
|
|
|
|
}
|
|
|
|
|
2016-02-29 00:56:05 +00:00
|
|
|
// SetStartError is used to mark the most recent start error. If starting was
|
|
|
|
// successful the error should be nil.
|
|
|
|
func (r *RestartTracker) SetStartError(err error) *RestartTracker {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
r.startErr = err
|
2017-09-14 17:21:49 +00:00
|
|
|
r.failure = true
|
2016-02-29 00:56:05 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWaitResult is used to mark the most recent wait result.
|
2016-10-23 01:08:30 +00:00
|
|
|
func (r *RestartTracker) SetWaitResult(res *dstructs.WaitResult) *RestartTracker {
|
2016-02-29 00:56:05 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
r.waitRes = res
|
2017-09-14 17:21:49 +00:00
|
|
|
r.failure = true
|
2016-02-29 00:56:05 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:11:09 +00:00
|
|
|
// SetRestartTriggered is used to mark that the task has been signalled to be
|
2017-09-15 21:34:36 +00:00
|
|
|
// restarted. Setting the failure to true restarts according to the restart
|
|
|
|
// policy. When failure is false the task is restarted without considering the
|
|
|
|
// restart policy.
|
2017-09-14 22:27:39 +00:00
|
|
|
func (r *RestartTracker) SetRestartTriggered(failure bool) *RestartTracker {
|
2016-10-05 22:11:09 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
2017-09-14 22:27:39 +00:00
|
|
|
if failure {
|
|
|
|
r.failure = true
|
|
|
|
} else {
|
|
|
|
r.restartTriggered = true
|
|
|
|
}
|
2017-09-10 23:25:13 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-24 22:43:55 +00:00
|
|
|
// GetReason returns a human-readable description for the last state returned by
|
|
|
|
// GetState.
|
|
|
|
func (r *RestartTracker) GetReason() string {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
return r.reason
|
|
|
|
}
|
|
|
|
|
2016-02-29 00:56:05 +00:00
|
|
|
// GetState returns the tasks next state given the set exit code and start
|
|
|
|
// error. One of the following states are returned:
|
|
|
|
// * TaskRestarting - Task should be restarted
|
|
|
|
// * TaskNotRestarting - Task should not be restarted and has exceeded its
|
|
|
|
// restart policy.
|
|
|
|
// * TaskTerminated - Task has terminated successfully and does not need a
|
|
|
|
// restart.
|
|
|
|
//
|
|
|
|
// If TaskRestarting is returned, the duration is how long to wait until
|
|
|
|
// starting the task again.
|
|
|
|
func (r *RestartTracker) GetState() (string, time.Duration) {
|
2016-02-04 03:43:44 +00:00
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
2016-10-18 18:22:16 +00:00
|
|
|
// Clear out the existing state
|
|
|
|
defer func() {
|
|
|
|
r.startErr = nil
|
|
|
|
r.waitRes = nil
|
|
|
|
r.restartTriggered = false
|
2017-09-10 23:25:13 +00:00
|
|
|
r.failure = false
|
2016-10-18 18:22:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Hot path if a restart was triggered
|
|
|
|
if r.restartTriggered {
|
|
|
|
r.reason = ""
|
|
|
|
return structs.TaskRestarting, 0
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:17:39 +00:00
|
|
|
// Hot path if no attempts are expected
|
|
|
|
if r.policy.Attempts == 0 {
|
2016-03-24 22:43:55 +00:00
|
|
|
r.reason = ReasonNoRestartsAllowed
|
2016-02-29 00:56:05 +00:00
|
|
|
if r.waitRes != nil && r.waitRes.Successful() {
|
|
|
|
return structs.TaskTerminated, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return structs.TaskNotRestarting, 0
|
2016-02-02 22:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 00:56:05 +00:00
|
|
|
r.count++
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
// Check if we have entered a new interval.
|
|
|
|
end := r.startTime.Add(r.policy.Interval)
|
|
|
|
now := time.Now()
|
|
|
|
if now.After(end) {
|
|
|
|
r.count = 0
|
|
|
|
r.startTime = now
|
|
|
|
}
|
2015-11-05 19:12:31 +00:00
|
|
|
|
2017-09-14 17:21:49 +00:00
|
|
|
// Handle restarts due to failures
|
2017-09-15 21:54:37 +00:00
|
|
|
if !r.failure {
|
|
|
|
return "", 0
|
|
|
|
}
|
2016-02-29 00:56:05 +00:00
|
|
|
|
2017-09-15 21:54:37 +00:00
|
|
|
if r.startErr != nil {
|
|
|
|
// If the error is not recoverable, do not restart.
|
|
|
|
if !structs.IsRecoverable(r.startErr) {
|
|
|
|
r.reason = ReasonUnrecoverableErrror
|
|
|
|
return structs.TaskNotRestarting, 0
|
|
|
|
}
|
|
|
|
} else if r.waitRes != nil {
|
|
|
|
// If the task started successfully and restart on success isn't specified,
|
|
|
|
// don't restart but don't mark as failed.
|
|
|
|
if r.waitRes.Successful() && !r.onSuccess {
|
|
|
|
r.reason = "Restart unnecessary as task terminated successfully"
|
|
|
|
return structs.TaskTerminated, 0
|
2017-09-10 23:25:13 +00:00
|
|
|
}
|
2017-09-15 21:54:37 +00:00
|
|
|
}
|
2017-09-10 23:25:13 +00:00
|
|
|
|
2017-09-15 21:54:37 +00:00
|
|
|
// If this task has been restarted due to failures more times
|
|
|
|
// than the restart policy allows within an interval fail
|
|
|
|
// according to the restart policy's mode.
|
|
|
|
if r.count > r.policy.Attempts {
|
|
|
|
if r.policy.Mode == structs.RestartPolicyModeFail {
|
|
|
|
r.reason = fmt.Sprintf(
|
|
|
|
`Exceeded allowed attempts %d in interval %v and mode is "fail"`,
|
|
|
|
r.policy.Attempts, r.policy.Interval)
|
|
|
|
return structs.TaskNotRestarting, 0
|
|
|
|
} else {
|
|
|
|
r.reason = ReasonDelay
|
|
|
|
return structs.TaskRestarting, r.getDelay()
|
|
|
|
}
|
2016-02-29 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 21:54:37 +00:00
|
|
|
r.reason = ReasonWithinPolicy
|
|
|
|
return structs.TaskRestarting, r.jitter()
|
2016-02-29 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDelay returns the delay time to enter the next interval.
|
|
|
|
func (r *RestartTracker) getDelay() time.Duration {
|
|
|
|
end := r.startTime.Add(r.policy.Interval)
|
|
|
|
now := time.Now()
|
|
|
|
return end.Sub(now)
|
2015-12-17 18:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// jitter returns the delay time plus a jitter.
|
|
|
|
func (r *RestartTracker) jitter() time.Duration {
|
2016-01-20 20:00:20 +00:00
|
|
|
// Get the delay and ensure it is valid.
|
2015-12-17 18:37:53 +00:00
|
|
|
d := r.policy.Delay.Nanoseconds()
|
2016-01-20 20:00:20 +00:00
|
|
|
if d == 0 {
|
|
|
|
d = 1
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:11:12 +00:00
|
|
|
j := float64(r.rand.Int63n(d)) * jitter
|
|
|
|
return time.Duration(d + int64(j))
|
2015-11-06 01:13:25 +00:00
|
|
|
}
|