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
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2018-04-10 22:02:52 +00:00
|
|
|
// AllocUpdateBatcher is used to batch the updates to the desired transitions
|
2018-04-06 22:53:14 +00:00
|
|
|
// of allocations and the creation of evals.
|
|
|
|
type AllocUpdateBatcher struct {
|
2017-06-28 21:25:20 +00:00
|
|
|
// batch is the batching duration
|
|
|
|
batch time.Duration
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// raft is used to actually commit the updates
|
2017-06-26 21:23:52 +00:00
|
|
|
raft DeploymentRaftEndpoints
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
// workCh is used to pass evaluations to the daemon process
|
2018-04-06 22:53:14 +00:00
|
|
|
workCh chan *updateWrapper
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-06-28 04:36:16 +00:00
|
|
|
// ctx is used to exit the daemon batcher
|
|
|
|
ctx context.Context
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// NewAllocUpdateBatcher returns an AllocUpdateBatcher that uses the passed raft endpoints to
|
2018-04-10 22:02:52 +00:00
|
|
|
// create the allocation desired transition updates and new evaluations and
|
2018-04-06 22:53:14 +00:00
|
|
|
// exits the batcher when the passed exit channel is closed.
|
|
|
|
func NewAllocUpdateBatcher(batchDuration time.Duration, raft DeploymentRaftEndpoints, ctx context.Context) *AllocUpdateBatcher {
|
|
|
|
b := &AllocUpdateBatcher{
|
2017-08-17 20:23:58 +00:00
|
|
|
batch: batchDuration,
|
|
|
|
raft: raft,
|
|
|
|
ctx: ctx,
|
2018-04-06 22:53:14 +00:00
|
|
|
workCh: make(chan *updateWrapper, 10),
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go b.batcher()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:02:52 +00:00
|
|
|
// CreateUpdate batches the allocation desired transition update and returns a
|
2018-04-06 22:53:14 +00:00
|
|
|
// future that tracks the completion of the request.
|
|
|
|
func (b *AllocUpdateBatcher) CreateUpdate(allocs map[string]*structs.DesiredTransition, eval *structs.Evaluation) *BatchFuture {
|
|
|
|
wrapper := &updateWrapper{
|
|
|
|
allocs: allocs,
|
|
|
|
e: eval,
|
|
|
|
f: make(chan *BatchFuture, 1),
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
b.workCh <- wrapper
|
|
|
|
return <-wrapper.f
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
type updateWrapper struct {
|
|
|
|
allocs map[string]*structs.DesiredTransition
|
|
|
|
e *structs.Evaluation
|
|
|
|
f chan *BatchFuture
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// batcher is the long lived batcher goroutine
|
2018-04-06 22:53:14 +00:00
|
|
|
func (b *AllocUpdateBatcher) batcher() {
|
2017-07-03 18:51:58 +00:00
|
|
|
var timerCh <-chan time.Time
|
2018-04-06 22:53:14 +00:00
|
|
|
allocs := make(map[string]*structs.DesiredTransition)
|
2017-06-26 21:23:52 +00:00
|
|
|
evals := make(map[string]*structs.Evaluation)
|
2018-04-06 22:53:14 +00:00
|
|
|
future := NewBatchFuture()
|
2017-06-26 21:23:52 +00:00
|
|
|
for {
|
|
|
|
select {
|
2017-06-28 04:36:16 +00:00
|
|
|
case <-b.ctx.Done():
|
2017-06-26 21:23:52 +00:00
|
|
|
return
|
2017-08-17 20:23:58 +00:00
|
|
|
case w := <-b.workCh:
|
2017-07-03 18:51:58 +00:00
|
|
|
if timerCh == nil {
|
|
|
|
timerCh = time.After(b.batch)
|
2017-06-28 21:25:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// Store the eval and alloc updates, and attach the future
|
2017-08-17 20:23:58 +00:00
|
|
|
evals[w.e.DeploymentID] = w.e
|
2018-04-06 22:53:14 +00:00
|
|
|
for id, upd := range w.allocs {
|
|
|
|
allocs[id] = upd
|
|
|
|
}
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
w.f <- future
|
2017-07-03 18:51:58 +00:00
|
|
|
case <-timerCh:
|
2017-08-17 20:23:58 +00:00
|
|
|
// Capture the future and create a new one
|
|
|
|
f := future
|
2018-04-06 22:53:14 +00:00
|
|
|
future = NewBatchFuture()
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-07-03 18:51:58 +00:00
|
|
|
// Shouldn't be possible
|
2017-06-26 21:23:52 +00:00
|
|
|
if f == nil {
|
2017-07-03 18:51:58 +00:00
|
|
|
panic("no future")
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// Create the request
|
|
|
|
req := &structs.AllocUpdateDesiredTransitionRequest{
|
|
|
|
Allocs: allocs,
|
|
|
|
Evals: make([]*structs.Evaluation, 0, len(evals)),
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
for _, e := range evals {
|
2018-04-06 22:53:14 +00:00
|
|
|
req.Evals = append(req.Evals, e)
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:51:58 +00:00
|
|
|
// Upsert the evals in a go routine
|
2018-04-10 22:02:52 +00:00
|
|
|
go f.Set(b.raft.UpdateAllocDesiredTransition(req))
|
2017-06-26 21:23:52 +00:00
|
|
|
|
2017-07-03 18:51:58 +00:00
|
|
|
// Reset the evals list and timer
|
2017-06-26 21:23:52 +00:00
|
|
|
evals = make(map[string]*structs.Evaluation)
|
2018-04-06 22:53:14 +00:00
|
|
|
allocs = make(map[string]*structs.DesiredTransition)
|
2017-07-03 18:51:58 +00:00
|
|
|
timerCh = nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// BatchFuture is a future that can be used to retrieve the index the eval was
|
2017-06-26 21:23:52 +00:00
|
|
|
// created at or any error in the creation process
|
2018-04-06 22:53:14 +00:00
|
|
|
type BatchFuture struct {
|
2017-06-26 21:23:52 +00:00
|
|
|
index uint64
|
|
|
|
err error
|
|
|
|
waitCh chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-04-06 22:53:14 +00:00
|
|
|
// NewBatchFuture returns a new BatchFuture
|
|
|
|
func NewBatchFuture() *BatchFuture {
|
|
|
|
return &BatchFuture{
|
2017-06-26 21:23:52 +00:00
|
|
|
waitCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the results of the future, unblocking any client.
|
2018-04-06 22:53:14 +00:00
|
|
|
func (f *BatchFuture) Set(index uint64, err error) {
|
2017-06-26 21:23:52 +00:00
|
|
|
f.index = index
|
|
|
|
f.err = err
|
|
|
|
close(f.waitCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Results returns the creation index and any error.
|
2018-04-06 22:53:14 +00:00
|
|
|
func (f *BatchFuture) Results() (uint64, error) {
|
2017-06-26 21:23:52 +00:00
|
|
|
<-f.waitCh
|
|
|
|
return f.index, f.err
|
|
|
|
}
|