open-nomad/nomad/deploymentwatcher/deployments_watcher.go

397 lines
12 KiB
Go
Raw Normal View History

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
"fmt"
"log"
"sync"
2017-06-28 21:25:20 +00:00
"time"
2017-06-26 21:23:52 +00:00
2017-06-28 04:36:16 +00:00
"golang.org/x/time/rate"
2017-06-26 21:23:52 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
2017-07-03 19:05:01 +00:00
const (
// LimitStateQueriesPerSecond is the number of state queries allowed per
// second
LimitStateQueriesPerSecond = 100.0
// CrossDeploymentEvalBatchDuration is the duration in which evaluations are
// batched across all deployment watchers before commiting to Raft.
CrossDeploymentEvalBatchDuration = 250 * time.Millisecond
)
var (
// notEnabled is the error returned when the deployment watcher is not
// enabled
notEnabled = fmt.Errorf("deployment watcher not enabled")
)
2017-06-26 21:23:52 +00:00
// DeploymentRaftEndpoints exposes the deployment watcher to a set of functions
// to apply data transforms via Raft.
type DeploymentRaftEndpoints interface {
// UpsertEvals is used to upsert a set of evaluations
UpsertEvals([]*structs.Evaluation) (uint64, error)
// UpsertJob is used to upsert a job
UpsertJob(job *structs.Job) (uint64, error)
2017-07-03 19:05:01 +00:00
// UpdateDeploymentStatus is used to make a deployment status update
2017-06-26 21:23:52 +00:00
// and potentially create an evaluation.
2017-07-03 19:05:01 +00:00
UpdateDeploymentStatus(u *structs.DeploymentStatusUpdateRequest) (uint64, error)
2017-06-26 21:23:52 +00:00
2017-07-03 19:05:01 +00:00
// UpdateDeploymentPromotion is used to promote canaries in a deployment
UpdateDeploymentPromotion(req *structs.ApplyDeploymentPromoteRequest) (uint64, error)
2017-06-26 21:23:52 +00:00
2017-07-03 19:05:01 +00:00
// UpdateDeploymentAllocHealth is used to set the health of allocations in a
2017-06-26 21:23:52 +00:00
// deployment
2017-07-03 19:05:01 +00:00
UpdateDeploymentAllocHealth(req *structs.ApplyDeploymentAllocHealthRequest) (uint64, error)
2017-06-26 21:23:52 +00:00
}
// DeploymentStateWatchers are the set of functions required to watch objects on
// behalf of a deployment
type DeploymentStateWatchers interface {
// Evaluations returns the set of evaluations for the given job
Evaluations(args *structs.JobSpecificRequest, reply *structs.JobEvaluationsResponse) error
// Allocations returns the set of allocations that are part of the
// deployment.
Allocations(args *structs.DeploymentSpecificRequest, reply *structs.AllocListResponse) error
2017-06-27 18:52:14 +00:00
// List is used to list all the deployments in the system
List(args *structs.DeploymentListRequest, reply *structs.DeploymentListResponse) error
2017-06-29 05:00:18 +00:00
// GetDeployment is used to lookup a particular deployment.
GetDeployment(args *structs.DeploymentSpecificRequest, reply *structs.SingleDeploymentResponse) error
2017-06-26 21:23:52 +00:00
// GetJobVersions is used to lookup the versions of a job. This is used when
// rolling back to find the latest stable job
GetJobVersions(args *structs.JobVersionsRequest, reply *structs.JobVersionsResponse) error
2017-06-27 18:52:14 +00:00
// GetJob is used to lookup a particular job.
GetJob(args *structs.JobSpecificRequest, reply *structs.SingleJobResponse) error
2017-06-26 21:23:52 +00:00
}
// Watcher is used to watch deployments and their allocations created
// by the scheduler and trigger the scheduler when allocation health
// transistions.
type Watcher struct {
enabled bool
logger *log.Logger
2017-06-28 04:36:16 +00:00
// queryLimiter is used to limit the rate of blocking queries
queryLimiter *rate.Limiter
2017-06-28 21:25:20 +00:00
// evalBatchDuration is the duration to batch eval creation across all
// deployment watchers
evalBatchDuration time.Duration
2017-06-26 21:23:52 +00:00
// raft contains the set of Raft endpoints that can be used by the
// deployments watcher
raft DeploymentRaftEndpoints
// stateWatchers is the set of functions required to watch a deployment for
// state changes
stateWatchers DeploymentStateWatchers
// watchers is the set of active watchers, one per deployment
watchers map[string]*deploymentWatcher
// evalBatcher is used to batch the creation of evaluations
evalBatcher *EvalBatcher
2017-06-28 04:36:16 +00:00
// ctx and exitFn are used to cancel the watcher
ctx context.Context
exitFn context.CancelFunc
2017-06-26 21:23:52 +00:00
l sync.RWMutex
}
// NewDeploymentsWatcher returns a deployments watcher that is used to watch
// deployments and trigger the scheduler as needed.
2017-07-03 18:26:45 +00:00
func NewDeploymentsWatcher(logger *log.Logger, watchers DeploymentStateWatchers,
raft DeploymentRaftEndpoints, stateQueriesPerSecond float64,
2017-06-28 21:25:20 +00:00
evalBatchDuration time.Duration) *Watcher {
2017-06-28 22:35:52 +00:00
2017-06-26 21:23:52 +00:00
return &Watcher{
2017-07-03 18:26:45 +00:00
stateWatchers: watchers,
raft: raft,
2017-06-28 21:25:20 +00:00
queryLimiter: rate.NewLimiter(rate.Limit(stateQueriesPerSecond), 100),
evalBatchDuration: evalBatchDuration,
logger: logger,
2017-06-26 21:23:52 +00:00
}
}
// SetEnabled is used to control if the watcher is enabled. The watcher
// should only be enabled on the active leader.
2017-06-28 22:35:52 +00:00
func (w *Watcher) SetEnabled(enabled bool) error {
2017-06-26 21:23:52 +00:00
w.l.Lock()
2017-07-21 22:44:51 +00:00
defer w.l.Unlock()
2017-06-27 18:52:14 +00:00
wasEnabled := w.enabled
2017-06-26 21:23:52 +00:00
w.enabled = enabled
2017-06-28 22:35:52 +00:00
// Flush the state to create the necessary objects
2017-07-03 19:05:01 +00:00
w.flush()
2017-06-28 22:35:52 +00:00
// If we are starting now, launch the watch daemon
if enabled && !wasEnabled {
2017-07-21 22:44:51 +00:00
go w.watchDeployments(w.ctx)
2017-06-26 21:23:52 +00:00
}
2017-06-28 22:35:52 +00:00
return nil
2017-06-26 21:23:52 +00:00
}
2017-07-03 19:05:01 +00:00
// flush is used to clear the state of the watcher
func (w *Watcher) flush() {
2017-06-26 21:23:52 +00:00
// Stop all the watchers and clear it
for _, watcher := range w.watchers {
watcher.StopWatch()
}
2017-06-28 04:36:16 +00:00
// Kill everything associated with the watcher
2017-06-28 22:35:52 +00:00
if w.exitFn != nil {
w.exitFn()
}
2017-06-26 21:23:52 +00:00
w.watchers = make(map[string]*deploymentWatcher, 32)
2017-06-28 04:36:16 +00:00
w.ctx, w.exitFn = context.WithCancel(context.Background())
2017-06-28 21:25:20 +00:00
w.evalBatcher = NewEvalBatcher(w.evalBatchDuration, w.raft, w.ctx)
2017-06-26 21:23:52 +00:00
}
2017-06-27 18:52:14 +00:00
// watchDeployments is the long lived go-routine that watches for deployments to
// add and remove watchers on.
2017-07-21 22:44:51 +00:00
func (w *Watcher) watchDeployments(ctx context.Context) {
dindex := uint64(1)
2017-06-27 18:52:14 +00:00
for {
// Block getting all deployments using the last deployment index.
2017-07-21 22:44:51 +00:00
resp, err := w.getDeploys(ctx, dindex)
2017-06-28 04:36:16 +00:00
if err != nil {
2017-07-21 22:44:51 +00:00
if err == context.Canceled || ctx.Err() == context.Canceled {
2017-06-28 04:36:16 +00:00
return
}
w.logger.Printf("[ERR] nomad.deployments_watcher: failed to retrieve deploylements: %v", err)
2017-06-27 18:52:14 +00:00
}
// Guard against npe
if resp == nil {
continue
}
// Ensure we are tracking the things we should and not tracking what we
// shouldn't be
for _, d := range resp.Deployments {
if d.Active() {
if err := w.add(d); err != nil {
w.logger.Printf("[ERR] nomad.deployments_watcher: failed to track deployment %q: %v", d.ID, err)
}
} else {
w.remove(d)
}
}
// Update the latest index
dindex = resp.Index
}
}
// getDeploys retrieves all deployments blocking at the given index.
2017-07-21 22:44:51 +00:00
func (w *Watcher) getDeploys(ctx context.Context, index uint64) (*structs.DeploymentListResponse, error) {
2017-06-28 04:36:16 +00:00
// Build the request
args := &structs.DeploymentListRequest{
QueryOptions: structs.QueryOptions{
MinQueryIndex: index,
},
}
var resp structs.DeploymentListResponse
for resp.Index <= index {
2017-07-21 22:44:51 +00:00
if err := w.queryLimiter.Wait(ctx); err != nil {
2017-06-28 04:36:16 +00:00
return nil, err
2017-06-27 18:52:14 +00:00
}
2017-06-28 04:36:16 +00:00
if err := w.stateWatchers.List(args, &resp); err != nil {
return nil, err
2017-06-27 18:52:14 +00:00
}
2017-06-28 04:36:16 +00:00
}
2017-06-27 18:52:14 +00:00
2017-06-28 04:36:16 +00:00
return &resp, nil
2017-06-27 18:52:14 +00:00
}
// add adds a deployment to the watch list
func (w *Watcher) add(d *structs.Deployment) error {
2017-06-26 21:23:52 +00:00
w.l.Lock()
defer w.l.Unlock()
2017-06-29 05:00:18 +00:00
_, err := w.addLocked(d)
return err
}
2017-06-26 21:23:52 +00:00
2017-06-29 05:00:18 +00:00
// addLocked adds a deployment to the watch list and should only be called when
// locked.
func (w *Watcher) addLocked(d *structs.Deployment) (*deploymentWatcher, error) {
2017-06-26 21:23:52 +00:00
// Not enabled so no-op
if !w.enabled {
2017-06-29 05:00:18 +00:00
return nil, nil
}
if !d.Active() {
return nil, fmt.Errorf("deployment %q is terminal", d.ID)
2017-06-26 21:23:52 +00:00
}
// Already watched so no-op
if _, ok := w.watchers[d.ID]; ok {
2017-06-29 05:00:18 +00:00
return nil, nil
2017-06-27 18:52:14 +00:00
}
// Get the job the deployment is referencing
args := &structs.JobSpecificRequest{
JobID: d.JobID,
}
var resp structs.SingleJobResponse
if err := w.stateWatchers.GetJob(args, &resp); err != nil {
2017-06-29 05:00:18 +00:00
return nil, err
2017-06-27 18:52:14 +00:00
}
if resp.Job == nil {
2017-06-29 05:00:18 +00:00
return nil, fmt.Errorf("deployment %q references unknown job %q", d.ID, d.JobID)
2017-06-26 21:23:52 +00:00
}
2017-06-29 05:00:18 +00:00
watcher := newDeploymentWatcher(w.ctx, w.queryLimiter, w.logger, w.stateWatchers, d, resp.Job, w)
w.watchers[d.ID] = watcher
return watcher, nil
2017-06-26 21:23:52 +00:00
}
2017-06-27 18:52:14 +00:00
// remove stops watching a deployment. This can be because the deployment is
2017-06-26 21:23:52 +00:00
// complete or being deleted.
2017-06-27 18:52:14 +00:00
func (w *Watcher) remove(d *structs.Deployment) {
2017-06-26 21:23:52 +00:00
w.l.Lock()
defer w.l.Unlock()
// Not enabled so no-op
if !w.enabled {
return
}
if watcher, ok := w.watchers[d.ID]; ok {
watcher.StopWatch()
delete(w.watchers, d.ID)
}
}
2017-06-29 05:00:18 +00:00
// forceAdd is used to force a lookup of the given deployment object and create
// a watcher. If the deployment does not exist or is terminal an error is
// returned.
func (w *Watcher) forceAdd(dID string) (*deploymentWatcher, error) {
// Build the request
args := &structs.DeploymentSpecificRequest{DeploymentID: dID}
var resp structs.SingleDeploymentResponse
if err := w.stateWatchers.GetDeployment(args, &resp); err != nil {
return nil, err
}
if resp.Deployment == nil {
return nil, fmt.Errorf("unknown deployment %q", dID)
}
return w.addLocked(resp.Deployment)
}
2017-07-03 19:05:01 +00:00
// getOrCreateWatcher returns the deployment watcher for the given deployment ID.
func (w *Watcher) getOrCreateWatcher(dID string) (*deploymentWatcher, error) {
2017-06-26 21:23:52 +00:00
w.l.Lock()
defer w.l.Unlock()
// Not enabled so no-op
if !w.enabled {
2017-06-29 05:00:18 +00:00
return nil, notEnabled
2017-06-26 21:23:52 +00:00
}
2017-06-29 05:00:18 +00:00
watcher, ok := w.watchers[dID]
if ok {
return watcher, nil
}
return w.forceAdd(dID)
}
// SetAllocHealth is used to set the health of allocations for a deployment. If
// there are any unhealthy allocations, the deployment is updated to be failed.
// Otherwise the allocations are updated and an evaluation is created.
func (w *Watcher) SetAllocHealth(req *structs.DeploymentAllocHealthRequest, resp *structs.DeploymentUpdateResponse) error {
2017-07-03 19:05:01 +00:00
watcher, err := w.getOrCreateWatcher(req.DeploymentID)
2017-06-29 05:00:18 +00:00
if err != nil {
return err
2017-06-26 21:23:52 +00:00
}
2017-06-28 04:36:16 +00:00
return watcher.SetAllocHealth(req, resp)
2017-06-26 21:23:52 +00:00
}
// PromoteDeployment is used to promote a deployment. If promote is false,
// deployment is marked as failed. Otherwise the deployment is updated and an
// evaluation is created.
2017-06-28 04:36:16 +00:00
func (w *Watcher) PromoteDeployment(req *structs.DeploymentPromoteRequest, resp *structs.DeploymentUpdateResponse) error {
2017-07-03 19:05:01 +00:00
watcher, err := w.getOrCreateWatcher(req.DeploymentID)
2017-06-29 05:00:18 +00:00
if err != nil {
return err
2017-06-26 21:23:52 +00:00
}
2017-06-28 04:36:16 +00:00
return watcher.PromoteDeployment(req, resp)
2017-06-26 21:23:52 +00:00
}
// PauseDeployment is used to toggle the pause state on a deployment. If the
// deployment is being unpaused, an evaluation is created.
2017-06-28 04:36:16 +00:00
func (w *Watcher) PauseDeployment(req *structs.DeploymentPauseRequest, resp *structs.DeploymentUpdateResponse) error {
2017-07-03 19:05:01 +00:00
watcher, err := w.getOrCreateWatcher(req.DeploymentID)
2017-06-29 05:00:18 +00:00
if err != nil {
return err
2017-06-26 21:23:52 +00:00
}
2017-06-28 04:36:16 +00:00
return watcher.PauseDeployment(req, resp)
2017-06-26 21:23:52 +00:00
}
2017-06-28 23:29:48 +00:00
// FailDeployment is used to fail the deployment.
2017-06-29 05:00:18 +00:00
func (w *Watcher) FailDeployment(req *structs.DeploymentFailRequest, resp *structs.DeploymentUpdateResponse) error {
2017-07-03 19:05:01 +00:00
watcher, err := w.getOrCreateWatcher(req.DeploymentID)
2017-06-29 05:00:18 +00:00
if err != nil {
return err
2017-06-28 23:29:48 +00:00
}
return watcher.FailDeployment(req, resp)
}
2017-06-26 21:23:52 +00:00
// createEvaluation commits the given evaluation to Raft but batches the commit
// with other calls.
func (w *Watcher) createEvaluation(eval *structs.Evaluation) (uint64, error) {
2017-06-28 21:25:20 +00:00
return w.evalBatcher.CreateEval(eval).Results()
2017-06-26 21:23:52 +00:00
}
// upsertJob commits the given job to Raft
func (w *Watcher) upsertJob(job *structs.Job) (uint64, error) {
return w.raft.UpsertJob(job)
}
// upsertDeploymentStatusUpdate commits the given deployment update and optional
// evaluation to Raft
func (w *Watcher) upsertDeploymentStatusUpdate(
u *structs.DeploymentStatusUpdate,
e *structs.Evaluation,
j *structs.Job) (uint64, error) {
2017-07-03 19:05:01 +00:00
return w.raft.UpdateDeploymentStatus(&structs.DeploymentStatusUpdateRequest{
2017-06-26 21:23:52 +00:00
DeploymentUpdate: u,
Eval: e,
Job: j,
})
}
// upsertDeploymentPromotion commits the given deployment promotion to Raft
func (w *Watcher) upsertDeploymentPromotion(req *structs.ApplyDeploymentPromoteRequest) (uint64, error) {
2017-07-03 19:05:01 +00:00
return w.raft.UpdateDeploymentPromotion(req)
2017-06-26 21:23:52 +00:00
}
// upsertDeploymentAllocHealth commits the given allocation health changes to
// Raft
func (w *Watcher) upsertDeploymentAllocHealth(req *structs.ApplyDeploymentAllocHealthRequest) (uint64, error) {
2017-07-03 19:05:01 +00:00
return w.raft.UpdateDeploymentAllocHealth(req)
2017-06-26 21:23:52 +00:00
}