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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvalBatcher is used to batch the creation of evaluations
|
|
|
|
type EvalBatcher struct {
|
2017-06-28 21:25:20 +00:00
|
|
|
// batch is the batching duration
|
|
|
|
batch time.Duration
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// raft is used to actually commit the evaluations
|
|
|
|
raft DeploymentRaftEndpoints
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
// workCh is used to pass evaluations to the daemon process
|
|
|
|
workCh chan *evalWrapper
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewEvalBatcher returns an EvalBatcher that uses the passed raft endpoints to
|
|
|
|
// create the evaluations and exits the batcher when the passed exit channel is
|
|
|
|
// closed.
|
2017-06-28 21:25:20 +00:00
|
|
|
func NewEvalBatcher(batchDuration time.Duration, raft DeploymentRaftEndpoints, ctx context.Context) *EvalBatcher {
|
2017-06-26 21:23:52 +00:00
|
|
|
b := &EvalBatcher{
|
2017-08-17 20:23:58 +00:00
|
|
|
batch: batchDuration,
|
|
|
|
raft: raft,
|
|
|
|
ctx: ctx,
|
|
|
|
workCh: make(chan *evalWrapper, 10),
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go b.batcher()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEval batches the creation of the evaluation and returns a future that
|
|
|
|
// tracks the evaluations creation.
|
|
|
|
func (b *EvalBatcher) CreateEval(e *structs.Evaluation) *EvalFuture {
|
2017-08-17 20:23:58 +00:00
|
|
|
wrapper := &evalWrapper{
|
|
|
|
e: e,
|
|
|
|
f: make(chan *EvalFuture, 1),
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
b.workCh <- wrapper
|
|
|
|
return <-wrapper.f
|
|
|
|
}
|
|
|
|
|
|
|
|
type evalWrapper struct {
|
|
|
|
e *structs.Evaluation
|
|
|
|
f chan *EvalFuture
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// batcher is the long lived batcher goroutine
|
|
|
|
func (b *EvalBatcher) batcher() {
|
2017-07-03 18:51:58 +00:00
|
|
|
var timerCh <-chan time.Time
|
2017-06-26 21:23:52 +00:00
|
|
|
evals := make(map[string]*structs.Evaluation)
|
2017-08-17 20:23:58 +00:00
|
|
|
future := NewEvalFuture()
|
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
|
|
|
}
|
|
|
|
|
2017-08-17 20:23:58 +00:00
|
|
|
// Store the eval and attach the future
|
|
|
|
evals[w.e.DeploymentID] = w.e
|
|
|
|
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
|
|
|
|
future = NewEvalFuture()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Capture the evals
|
|
|
|
all := make([]*structs.Evaluation, 0, len(evals))
|
|
|
|
for _, e := range evals {
|
|
|
|
all = append(all, e)
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:51:58 +00:00
|
|
|
// Upsert the evals in a go routine
|
|
|
|
go f.Set(b.raft.UpsertEvals(all))
|
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)
|
2017-07-03 18:51:58 +00:00
|
|
|
timerCh = nil
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalFuture is a future that can be used to retrieve the index the eval was
|
|
|
|
// created at or any error in the creation process
|
|
|
|
type EvalFuture struct {
|
|
|
|
index uint64
|
|
|
|
err error
|
|
|
|
waitCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEvalFuture returns a new EvalFuture
|
|
|
|
func NewEvalFuture() *EvalFuture {
|
|
|
|
return &EvalFuture{
|
|
|
|
waitCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the results of the future, unblocking any client.
|
|
|
|
func (f *EvalFuture) Set(index uint64, err error) {
|
|
|
|
f.index = index
|
|
|
|
f.err = err
|
|
|
|
close(f.waitCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Results returns the creation index and any error.
|
|
|
|
func (f *EvalFuture) Results() (uint64, error) {
|
|
|
|
<-f.waitCh
|
|
|
|
return f.index, f.err
|
|
|
|
}
|