2017-06-26 21:23:52 +00:00
|
|
|
package deploymentwatcher
|
|
|
|
|
|
|
|
import (
|
2017-06-28 04:36:16 +00:00
|
|
|
"context"
|
2017-06-26 21:23:52 +00:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2017-07-06 22:03:27 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2017-06-26 21:23:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-03 18:08:35 +00:00
|
|
|
// perJobEvalBatchPeriod is the batching length before creating an evaluation to
|
2017-06-26 21:23:52 +00:00
|
|
|
// trigger the scheduler when allocations are marked as healthy.
|
2017-07-03 18:08:35 +00:00
|
|
|
perJobEvalBatchPeriod = 1 * time.Second
|
2017-06-26 21:23:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// deploymentTriggers are the set of functions required to trigger changes on
|
|
|
|
// behalf of a deployment
|
|
|
|
type deploymentTriggers interface {
|
|
|
|
// createEvaluation is used to create an evaluation.
|
|
|
|
createEvaluation(eval *structs.Evaluation) (uint64, error)
|
|
|
|
|
|
|
|
// upsertJob is used to roll back a job when autoreverting for a deployment
|
|
|
|
upsertJob(job *structs.Job) (uint64, error)
|
|
|
|
|
|
|
|
// upsertDeploymentStatusUpdate is used to upsert a deployment status update
|
|
|
|
// and an optional evaluation and job to upsert
|
|
|
|
upsertDeploymentStatusUpdate(u *structs.DeploymentStatusUpdate, eval *structs.Evaluation, job *structs.Job) (uint64, error)
|
|
|
|
|
|
|
|
// upsertDeploymentPromotion is used to promote canaries in a deployment
|
|
|
|
upsertDeploymentPromotion(req *structs.ApplyDeploymentPromoteRequest) (uint64, error)
|
|
|
|
|
|
|
|
// upsertDeploymentAllocHealth is used to set the health of allocations in a
|
|
|
|
// deployment
|
|
|
|
upsertDeploymentAllocHealth(req *structs.ApplyDeploymentAllocHealthRequest) (uint64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deploymentWatcher is used to watch a single deployment and trigger the
|
|
|
|
// scheduler when allocation health transistions.
|
|
|
|
type deploymentWatcher struct {
|
2017-06-28 04:36:16 +00:00
|
|
|
// queryLimiter is used to limit the rate of blocking queries
|
|
|
|
queryLimiter *rate.Limiter
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// deploymentTriggers holds the methods required to trigger changes on behalf of the
|
|
|
|
// deployment
|
|
|
|
deploymentTriggers
|
|
|
|
|
|
|
|
// DeploymentStateWatchers holds the methods required to watch objects for
|
|
|
|
// changes on behalf of the deployment
|
2017-07-03 18:08:35 +00:00
|
|
|
watchers DeploymentStateWatchers
|
2017-06-26 21:23:52 +00:00
|
|
|
|
|
|
|
// d is the deployment being watched
|
|
|
|
d *structs.Deployment
|
|
|
|
|
|
|
|
// j is the job the deployment is for
|
|
|
|
j *structs.Job
|
|
|
|
|
|
|
|
// outstandingBatch marks whether an outstanding function exists to create
|
|
|
|
// the evaluation. Access should be done through the lock
|
|
|
|
outstandingBatch bool
|
|
|
|
|
2017-06-28 21:32:11 +00:00
|
|
|
// latestEval is the latest eval for the job. It is updated by the watch
|
|
|
|
// loop and any time an evaluation is created. The field should be accessed
|
|
|
|
// by holding the lock or using the setter and getter methods.
|
2017-06-28 20:19:41 +00:00
|
|
|
latestEval uint64
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
logger *log.Logger
|
2017-06-28 04:36:16 +00:00
|
|
|
ctx context.Context
|
|
|
|
exitFn context.CancelFunc
|
2017-06-26 21:23:52 +00:00
|
|
|
l sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDeploymentWatcher returns a deployment watcher that is used to watch
|
|
|
|
// deployments and trigger the scheduler as needed.
|
2017-06-28 21:32:11 +00:00
|
|
|
func newDeploymentWatcher(parent context.Context, queryLimiter *rate.Limiter,
|
|
|
|
logger *log.Logger, watchers DeploymentStateWatchers, d *structs.Deployment,
|
|
|
|
j *structs.Job, triggers deploymentTriggers) *deploymentWatcher {
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
ctx, exitFn := context.WithCancel(parent)
|
2017-06-26 21:23:52 +00:00
|
|
|
w := &deploymentWatcher{
|
2017-07-03 18:08:35 +00:00
|
|
|
queryLimiter: queryLimiter,
|
|
|
|
d: d,
|
|
|
|
j: j,
|
|
|
|
watchers: watchers,
|
|
|
|
deploymentTriggers: triggers,
|
|
|
|
logger: logger,
|
|
|
|
ctx: ctx,
|
|
|
|
exitFn: exitFn,
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:32:11 +00:00
|
|
|
// Start the long lived watcher that scans for allocation updates
|
2017-06-26 21:23:52 +00:00
|
|
|
go w.watch()
|
2017-06-28 21:32:11 +00:00
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
func (w *deploymentWatcher) SetAllocHealth(
|
|
|
|
req *structs.DeploymentAllocHealthRequest,
|
|
|
|
resp *structs.DeploymentUpdateResponse) error {
|
2017-06-26 21:23:52 +00:00
|
|
|
|
|
|
|
// If we are failing the deployment, update the status and potentially
|
|
|
|
// rollback
|
|
|
|
var j *structs.Job
|
|
|
|
var u *structs.DeploymentStatusUpdate
|
|
|
|
|
|
|
|
// If there are unhealthy allocations we need to mark the deployment as
|
|
|
|
// failed and check if we should roll back to a stable job.
|
|
|
|
if l := len(req.UnhealthyAllocationIDs); l != 0 {
|
|
|
|
unhealthy := make(map[string]struct{}, l)
|
|
|
|
for _, alloc := range req.UnhealthyAllocationIDs {
|
|
|
|
unhealthy[alloc] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the allocations for the deployment
|
|
|
|
args := &structs.DeploymentSpecificRequest{DeploymentID: req.DeploymentID}
|
|
|
|
var resp structs.AllocListResponse
|
2017-07-03 18:08:35 +00:00
|
|
|
if err := w.watchers.Allocations(args, &resp); err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:08:35 +00:00
|
|
|
// Determine if we should autorevert to an older job
|
2017-06-26 21:23:52 +00:00
|
|
|
desc := structs.DeploymentStatusDescriptionFailedAllocations
|
|
|
|
for _, alloc := range resp.Allocations {
|
|
|
|
// Check that the alloc has been marked unhealthy
|
|
|
|
if _, ok := unhealthy[alloc.ID]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the group has autorevert set
|
2017-06-30 19:35:59 +00:00
|
|
|
group, ok := w.d.TaskGroups[alloc.TaskGroup]
|
|
|
|
if !ok || !group.AutoRevert {
|
2017-06-26 21:23:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
j, err = w.latestStableJob()
|
|
|
|
if err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 22:03:27 +00:00
|
|
|
if j != nil {
|
|
|
|
desc = structs.DeploymentStatusDescriptionRollback(desc, j.Version)
|
|
|
|
}
|
2017-06-26 21:23:52 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
u = w.getDeploymentStatusUpdate(structs.DeploymentStatusFailed, desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the request
|
|
|
|
areq := &structs.ApplyDeploymentAllocHealthRequest{
|
|
|
|
DeploymentAllocHealthRequest: *req,
|
|
|
|
Eval: w.getEval(),
|
|
|
|
DeploymentUpdate: u,
|
|
|
|
Job: j,
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := w.upsertDeploymentAllocHealth(areq)
|
|
|
|
if err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// Build the response
|
|
|
|
resp.EvalID = areq.Eval.ID
|
|
|
|
resp.EvalCreateIndex = index
|
|
|
|
resp.DeploymentModifyIndex = index
|
2017-06-29 05:00:18 +00:00
|
|
|
resp.Index = index
|
2017-07-06 22:03:27 +00:00
|
|
|
if j != nil {
|
|
|
|
resp.RevertedJobVersion = helper.Uint64ToPtr(j.Version)
|
|
|
|
}
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(index)
|
2017-06-28 04:36:16 +00:00
|
|
|
return nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
func (w *deploymentWatcher) PromoteDeployment(
|
|
|
|
req *structs.DeploymentPromoteRequest,
|
|
|
|
resp *structs.DeploymentUpdateResponse) error {
|
2017-06-26 21:23:52 +00:00
|
|
|
|
|
|
|
// Create the request
|
|
|
|
areq := &structs.ApplyDeploymentPromoteRequest{
|
|
|
|
DeploymentPromoteRequest: *req,
|
|
|
|
Eval: w.getEval(),
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := w.upsertDeploymentPromotion(areq)
|
|
|
|
if err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// Build the response
|
|
|
|
resp.EvalID = areq.Eval.ID
|
|
|
|
resp.EvalCreateIndex = index
|
|
|
|
resp.DeploymentModifyIndex = index
|
2017-06-29 05:00:18 +00:00
|
|
|
resp.Index = index
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(index)
|
2017-06-28 04:36:16 +00:00
|
|
|
return nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
func (w *deploymentWatcher) PauseDeployment(
|
|
|
|
req *structs.DeploymentPauseRequest,
|
|
|
|
resp *structs.DeploymentUpdateResponse) error {
|
2017-06-26 21:23:52 +00:00
|
|
|
// Determine the status we should transistion to and if we need to create an
|
|
|
|
// evaluation
|
|
|
|
status, desc := structs.DeploymentStatusPaused, structs.DeploymentStatusDescriptionPaused
|
|
|
|
var eval *structs.Evaluation
|
|
|
|
evalID := ""
|
|
|
|
if !req.Pause {
|
|
|
|
status, desc = structs.DeploymentStatusRunning, structs.DeploymentStatusDescriptionRunning
|
2017-06-28 04:36:16 +00:00
|
|
|
eval = w.getEval()
|
2017-06-26 21:23:52 +00:00
|
|
|
evalID = eval.ID
|
|
|
|
}
|
|
|
|
update := w.getDeploymentStatusUpdate(status, desc)
|
|
|
|
|
|
|
|
// Commit the change
|
|
|
|
i, err := w.upsertDeploymentStatusUpdate(update, eval, nil)
|
|
|
|
if err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// Build the response
|
2017-06-29 05:00:18 +00:00
|
|
|
if evalID != "" {
|
|
|
|
resp.EvalID = evalID
|
|
|
|
resp.EvalCreateIndex = i
|
|
|
|
}
|
2017-06-28 04:36:16 +00:00
|
|
|
resp.DeploymentModifyIndex = i
|
2017-06-29 05:00:18 +00:00
|
|
|
resp.Index = i
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(i)
|
2017-06-28 04:36:16 +00:00
|
|
|
return nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 23:29:48 +00:00
|
|
|
func (w *deploymentWatcher) FailDeployment(
|
2017-06-29 05:00:18 +00:00
|
|
|
req *structs.DeploymentFailRequest,
|
2017-06-28 23:29:48 +00:00
|
|
|
resp *structs.DeploymentUpdateResponse) error {
|
|
|
|
|
|
|
|
status, desc := structs.DeploymentStatusFailed, structs.DeploymentStatusDescriptionFailedByUser
|
2017-07-06 22:03:27 +00:00
|
|
|
|
|
|
|
// Determine if we should rollback
|
|
|
|
rollback := false
|
|
|
|
for _, state := range w.d.TaskGroups {
|
|
|
|
if state.AutoRevert {
|
|
|
|
rollback = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rollbackJob *structs.Job
|
|
|
|
if rollback {
|
|
|
|
var err error
|
|
|
|
rollbackJob, err = w.latestStableJob()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rollbackJob != nil {
|
|
|
|
desc = structs.DeploymentStatusDescriptionRollback(desc, rollbackJob.Version)
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 23:29:48 +00:00
|
|
|
|
|
|
|
// Commit the change
|
2017-07-06 22:03:27 +00:00
|
|
|
update := w.getDeploymentStatusUpdate(status, desc)
|
|
|
|
eval := w.getEval()
|
|
|
|
i, err := w.upsertDeploymentStatusUpdate(update, eval, rollbackJob)
|
2017-06-28 23:29:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the response
|
|
|
|
resp.EvalID = eval.ID
|
|
|
|
resp.EvalCreateIndex = i
|
|
|
|
resp.DeploymentModifyIndex = i
|
2017-06-29 05:00:18 +00:00
|
|
|
resp.Index = i
|
2017-07-06 22:03:27 +00:00
|
|
|
if rollbackJob != nil {
|
|
|
|
resp.RevertedJobVersion = helper.Uint64ToPtr(rollbackJob.Version)
|
|
|
|
}
|
2017-06-28 23:29:48 +00:00
|
|
|
w.setLatestEval(i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// StopWatch stops watching the deployment. This should be called whenever a
|
|
|
|
// deployment is completed or the watcher is no longer needed.
|
|
|
|
func (w *deploymentWatcher) StopWatch() {
|
2017-06-28 04:36:16 +00:00
|
|
|
w.exitFn()
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// watch is the long running watcher that takes actions upon allocation changes
|
|
|
|
func (w *deploymentWatcher) watch() {
|
2017-07-03 04:54:40 +00:00
|
|
|
allocIndex := uint64(0)
|
2017-06-26 21:23:52 +00:00
|
|
|
for {
|
|
|
|
// Block getting all allocations that are part of the deployment using
|
|
|
|
// the last evaluation index. This will have us block waiting for
|
|
|
|
// something to change past what the scheduler has evaluated.
|
2017-07-03 04:54:40 +00:00
|
|
|
allocResp, err := w.getAllocs(allocIndex)
|
2017-06-28 04:36:16 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == context.Canceled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.logger.Printf("[ERR] nomad.deployment_watcher: failed to retrieve allocations for deployment %q: %v", w.d.ID, err)
|
2017-07-03 18:08:35 +00:00
|
|
|
return
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
2017-07-03 04:54:40 +00:00
|
|
|
allocIndex = allocResp.Index
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-06-28 21:32:11 +00:00
|
|
|
// Get the latest evaluation index
|
2017-06-28 20:19:41 +00:00
|
|
|
latestEval, err := w.latestEvalIndex()
|
2017-06-28 04:36:16 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == context.Canceled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.logger.Printf("[ERR] nomad.deployment_watcher: failed to determine last evaluation index for job %q: %v", w.d.JobID, err)
|
2017-07-03 18:08:35 +00:00
|
|
|
return
|
2017-06-28 04:36:16 +00:00
|
|
|
}
|
2017-06-26 21:23:52 +00:00
|
|
|
|
|
|
|
// Create an evaluation trigger if there is any allocation whose
|
|
|
|
// deployment status has been updated past the latest eval index.
|
|
|
|
createEval, failDeployment, rollback := false, false, false
|
2017-06-28 19:58:05 +00:00
|
|
|
for _, alloc := range allocResp.Allocations {
|
2017-06-28 04:36:16 +00:00
|
|
|
if alloc.DeploymentStatus == nil || alloc.DeploymentStatus.ModifyIndex <= latestEval {
|
2017-06-26 21:23:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// We need to create an eval
|
|
|
|
createEval = true
|
2017-06-26 21:23:52 +00:00
|
|
|
|
|
|
|
if alloc.DeploymentStatus.IsUnhealthy() {
|
|
|
|
// Check if the group has autorevert set
|
2017-06-30 19:35:59 +00:00
|
|
|
group, ok := w.d.TaskGroups[alloc.TaskGroup]
|
|
|
|
if ok && group.AutoRevert {
|
2017-06-26 21:23:52 +00:00
|
|
|
rollback = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we have an unhealthy allocation, fail the deployment
|
|
|
|
failDeployment = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// All conditions have been hit so we can break
|
|
|
|
if createEval && failDeployment && rollback {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the deployments status to failed
|
|
|
|
if failDeployment {
|
|
|
|
// Default description
|
|
|
|
desc := structs.DeploymentStatusDescriptionFailedAllocations
|
|
|
|
|
|
|
|
// Rollback to the old job if necessary
|
|
|
|
var j *structs.Job
|
|
|
|
if rollback {
|
|
|
|
var err error
|
|
|
|
j, err = w.latestStableJob()
|
|
|
|
if err != nil {
|
|
|
|
w.logger.Printf("[ERR] nomad.deployment_watcher: failed to lookup latest stable job for %q: %v", w.d.JobID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description should include that the job is being rolled back to
|
|
|
|
// version N
|
2017-06-28 19:58:05 +00:00
|
|
|
if j != nil {
|
|
|
|
desc = structs.DeploymentStatusDescriptionRollback(desc, j.Version)
|
|
|
|
}
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status of the deployment to failed and create an
|
|
|
|
// evaluation.
|
2017-07-03 18:08:35 +00:00
|
|
|
e := w.getEval()
|
|
|
|
u := w.getDeploymentStatusUpdate(structs.DeploymentStatusFailed, desc)
|
2017-06-26 21:23:52 +00:00
|
|
|
if index, err := w.upsertDeploymentStatusUpdate(u, e, j); err != nil {
|
|
|
|
w.logger.Printf("[ERR] nomad.deployment_watcher: failed to update deployment %q status: %v", w.d.ID, err)
|
|
|
|
} else {
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(index)
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
} else if createEval {
|
|
|
|
// Create an eval to push the deployment along
|
2017-06-28 19:58:05 +00:00
|
|
|
w.createEvalBatched(allocResp.Index)
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// latestStableJob returns the latest stable job. It may be nil if none exist
|
|
|
|
func (w *deploymentWatcher) latestStableJob() (*structs.Job, error) {
|
2017-06-30 00:16:20 +00:00
|
|
|
args := &structs.JobVersionsRequest{JobID: w.d.JobID}
|
2017-06-26 21:23:52 +00:00
|
|
|
var resp structs.JobVersionsResponse
|
2017-07-03 18:08:35 +00:00
|
|
|
if err := w.watchers.GetJobVersions(args, &resp); err != nil {
|
2017-06-26 21:23:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stable *structs.Job
|
|
|
|
for _, job := range resp.Versions {
|
|
|
|
if job.Stable {
|
|
|
|
stable = job
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stable, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createEvalBatched creates an eval but batches calls together
|
2017-06-28 19:58:05 +00:00
|
|
|
func (w *deploymentWatcher) createEvalBatched(forIndex uint64) {
|
2017-06-26 21:23:52 +00:00
|
|
|
w.l.Lock()
|
|
|
|
defer w.l.Unlock()
|
|
|
|
|
2017-06-28 20:19:41 +00:00
|
|
|
if w.outstandingBatch || forIndex < w.latestEval {
|
2017-06-26 21:23:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-28 19:58:05 +00:00
|
|
|
w.outstandingBatch = true
|
|
|
|
|
2017-07-03 18:08:35 +00:00
|
|
|
time.AfterFunc(perJobEvalBatchPeriod, func() {
|
2017-06-28 21:32:11 +00:00
|
|
|
// Create the eval
|
|
|
|
evalCreateIndex, err := w.createEvaluation(w.getEval())
|
|
|
|
if err != nil {
|
2017-06-28 20:19:41 +00:00
|
|
|
w.logger.Printf("[ERR] nomad.deployment_watcher: failed to create evaluation for deployment %q: %v", w.d.ID, err)
|
2017-06-28 21:32:11 +00:00
|
|
|
} else {
|
|
|
|
w.setLatestEval(evalCreateIndex)
|
2017-06-28 20:19:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
w.l.Lock()
|
|
|
|
w.outstandingBatch = false
|
|
|
|
w.l.Unlock()
|
|
|
|
|
2017-07-03 18:08:35 +00:00
|
|
|
})
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getEval returns an evaluation suitable for the deployment
|
|
|
|
func (w *deploymentWatcher) getEval() *structs.Evaluation {
|
|
|
|
return &structs.Evaluation{
|
|
|
|
ID: structs.GenerateUUID(),
|
|
|
|
Priority: w.j.Priority,
|
|
|
|
Type: w.j.Type,
|
2017-07-03 18:08:35 +00:00
|
|
|
TriggeredBy: structs.EvalTriggerDeploymentWatcher,
|
2017-06-26 21:23:52 +00:00
|
|
|
JobID: w.j.ID,
|
|
|
|
DeploymentID: w.d.ID,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDeploymentStatusUpdate returns a deployment status update
|
|
|
|
func (w *deploymentWatcher) getDeploymentStatusUpdate(status, desc string) *structs.DeploymentStatusUpdate {
|
|
|
|
return &structs.DeploymentStatusUpdate{
|
|
|
|
DeploymentID: w.d.ID,
|
|
|
|
Status: status,
|
|
|
|
StatusDescription: desc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllocs retrieves the allocations that are part of the deployment blocking
|
|
|
|
// at the given index.
|
2017-06-28 19:58:05 +00:00
|
|
|
func (w *deploymentWatcher) getAllocs(index uint64) (*structs.AllocListResponse, error) {
|
2017-06-28 04:36:16 +00:00
|
|
|
// Build the request
|
|
|
|
args := &structs.DeploymentSpecificRequest{
|
|
|
|
DeploymentID: w.d.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
MinQueryIndex: index,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.AllocListResponse
|
|
|
|
|
|
|
|
for resp.Index <= index {
|
|
|
|
if err := w.queryLimiter.Wait(w.ctx); err != nil {
|
|
|
|
return nil, err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:08:35 +00:00
|
|
|
if err := w.watchers.Allocations(args, &resp); err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return nil, err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
2017-06-28 04:36:16 +00:00
|
|
|
}
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-06-28 19:58:05 +00:00
|
|
|
return &resp, nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// latestEvalIndex returns the index of the last evaluation created for
|
2017-06-26 21:23:52 +00:00
|
|
|
// the job. The index is used to determine if an allocation update requires an
|
|
|
|
// evaluation to be triggered.
|
2017-06-28 04:36:16 +00:00
|
|
|
func (w *deploymentWatcher) latestEvalIndex() (uint64, error) {
|
|
|
|
if err := w.queryLimiter.Wait(w.ctx); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
args := &structs.JobSpecificRequest{
|
|
|
|
JobID: w.d.JobID,
|
|
|
|
}
|
|
|
|
var resp structs.JobEvaluationsResponse
|
2017-07-03 18:08:35 +00:00
|
|
|
err := w.watchers.Evaluations(args, &resp)
|
2017-06-26 21:23:52 +00:00
|
|
|
if err != nil {
|
2017-06-28 04:36:16 +00:00
|
|
|
return 0, err
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Evaluations) == 0 {
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(resp.Index)
|
2017-06-28 04:36:16 +00:00
|
|
|
return resp.Index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer using the snapshot index. Otherwise use the create index
|
|
|
|
e := resp.Evaluations[0]
|
|
|
|
if e.SnapshotIndex != 0 {
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(e.SnapshotIndex)
|
2017-06-28 04:36:16 +00:00
|
|
|
return e.SnapshotIndex, nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:19:41 +00:00
|
|
|
w.setLatestEval(e.CreateIndex)
|
2017-06-28 04:36:16 +00:00
|
|
|
return e.CreateIndex, nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
2017-06-28 20:19:41 +00:00
|
|
|
|
2017-06-28 21:32:11 +00:00
|
|
|
// setLatestEval sets the given index as the latest eval unless the currently
|
|
|
|
// stored index is higher.
|
2017-06-28 20:19:41 +00:00
|
|
|
func (w *deploymentWatcher) setLatestEval(index uint64) {
|
|
|
|
w.l.Lock()
|
|
|
|
defer w.l.Unlock()
|
|
|
|
if index > w.latestEval {
|
|
|
|
w.latestEval = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:32:11 +00:00
|
|
|
// getLatestEval returns the latest eval index.
|
2017-06-28 20:19:41 +00:00
|
|
|
func (w *deploymentWatcher) getLatestEval() uint64 {
|
|
|
|
w.l.Lock()
|
|
|
|
defer w.l.Unlock()
|
|
|
|
return w.latestEval
|
|
|
|
}
|