2020-04-30 13:13:00 +00:00
|
|
|
package volumewatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2020-05-08 20:53:57 +00:00
|
|
|
// encoding a 100 claim batch is about 31K on the wire, which
|
|
|
|
// is a reasonable batch size
|
|
|
|
const maxBatchSize = 100
|
|
|
|
|
2020-04-30 13:13:00 +00:00
|
|
|
// VolumeUpdateBatcher is used to batch the updates for volume claims
|
|
|
|
type VolumeUpdateBatcher struct {
|
2020-05-08 20:53:57 +00:00
|
|
|
// batchDuration is the batching duration
|
|
|
|
batchDuration time.Duration
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
// raft is used to actually commit the updates
|
|
|
|
raft VolumeRaftEndpoints
|
|
|
|
|
|
|
|
// workCh is used to pass evaluations to the daemon process
|
|
|
|
workCh chan *updateWrapper
|
|
|
|
|
|
|
|
// ctx is used to exit the daemon batcher
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVolumeUpdateBatcher returns an VolumeUpdateBatcher that uses the
|
|
|
|
// passed raft endpoints to create the updates to volume claims, and
|
|
|
|
// exits the batcher when the passed exit channel is closed.
|
2020-05-26 13:24:50 +00:00
|
|
|
func NewVolumeUpdateBatcher(ctx context.Context, batchDuration time.Duration, raft VolumeRaftEndpoints) *VolumeUpdateBatcher {
|
2020-04-30 13:13:00 +00:00
|
|
|
b := &VolumeUpdateBatcher{
|
2020-05-08 20:53:57 +00:00
|
|
|
batchDuration: batchDuration,
|
|
|
|
raft: raft,
|
|
|
|
ctx: ctx,
|
|
|
|
workCh: make(chan *updateWrapper, 10),
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go b.batcher()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUpdate batches the volume claim update and returns a future
|
2020-05-08 20:53:57 +00:00
|
|
|
// that can be used to track the completion of the batch. Note we
|
|
|
|
// only return the *last* future if the claims gets broken up across
|
|
|
|
// multiple batches because only the last one has useful information
|
|
|
|
// for the caller.
|
2020-04-30 13:13:00 +00:00
|
|
|
func (b *VolumeUpdateBatcher) CreateUpdate(claims []structs.CSIVolumeClaimRequest) *BatchFuture {
|
|
|
|
wrapper := &updateWrapper{
|
|
|
|
claims: claims,
|
|
|
|
f: make(chan *BatchFuture, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
b.workCh <- wrapper
|
|
|
|
return <-wrapper.f
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateWrapper struct {
|
|
|
|
claims []structs.CSIVolumeClaimRequest
|
|
|
|
f chan *BatchFuture
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:53:57 +00:00
|
|
|
type claimBatch struct {
|
|
|
|
claims map[string]structs.CSIVolumeClaimRequest
|
|
|
|
future *BatchFuture
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:13:00 +00:00
|
|
|
// batcher is the long lived batcher goroutine
|
|
|
|
func (b *VolumeUpdateBatcher) batcher() {
|
2020-05-08 20:53:57 +00:00
|
|
|
|
|
|
|
// we track claimBatches rather than a slice of
|
|
|
|
// CSIVolumeClaimBatchRequest so that we can deduplicate updates
|
|
|
|
// for the same volume
|
|
|
|
batches := []*claimBatch{{
|
|
|
|
claims: make(map[string]structs.CSIVolumeClaimRequest),
|
|
|
|
future: NewBatchFuture(),
|
|
|
|
}}
|
|
|
|
ticker := time.NewTicker(b.batchDuration)
|
|
|
|
defer ticker.Stop()
|
2020-04-30 13:13:00 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-b.ctx.Done():
|
|
|
|
// note: we can't flush here because we're likely no
|
|
|
|
// longer the leader
|
|
|
|
return
|
|
|
|
case w := <-b.workCh:
|
2020-05-08 20:53:57 +00:00
|
|
|
future := NewBatchFuture()
|
2020-04-30 13:13:00 +00:00
|
|
|
|
2020-05-08 20:53:57 +00:00
|
|
|
NEXT_CLAIM:
|
2020-04-30 13:13:00 +00:00
|
|
|
// de-dupe and store the claim update, and attach the future
|
|
|
|
for _, upd := range w.claims {
|
2020-05-08 20:53:57 +00:00
|
|
|
id := upd.VolumeID + upd.RequestNamespace()
|
|
|
|
|
|
|
|
for _, batch := range batches {
|
|
|
|
// first see if we can dedupe the update
|
|
|
|
_, ok := batch.claims[id]
|
|
|
|
if ok {
|
|
|
|
batch.claims[id] = upd
|
|
|
|
future = batch.future
|
|
|
|
continue NEXT_CLAIM
|
|
|
|
}
|
|
|
|
// otherwise append to the first non-full batch
|
|
|
|
if len(batch.claims) < maxBatchSize {
|
|
|
|
batch.claims[id] = upd
|
|
|
|
future = batch.future
|
|
|
|
continue NEXT_CLAIM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// all batches were full, so add a new batch
|
|
|
|
newBatch := &claimBatch{
|
|
|
|
claims: map[string]structs.CSIVolumeClaimRequest{id: upd},
|
|
|
|
future: NewBatchFuture(),
|
|
|
|
}
|
|
|
|
batches = append(batches, newBatch)
|
|
|
|
future = newBatch.future
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
2020-05-08 20:53:57 +00:00
|
|
|
|
|
|
|
// we send batches to raft FIFO, so we return the last
|
|
|
|
// future to the caller so that it can wait until the
|
|
|
|
// last batch has been sent
|
2020-04-30 13:13:00 +00:00
|
|
|
w.f <- future
|
|
|
|
|
2020-05-08 20:53:57 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
if len(batches) > 0 && len(batches[0].claims) > 0 {
|
|
|
|
batch := batches[0]
|
|
|
|
|
|
|
|
f := batch.future
|
|
|
|
|
|
|
|
// Create the batch request for the oldest batch
|
|
|
|
req := structs.CSIVolumeClaimBatchRequest{}
|
|
|
|
for _, claim := range batch.claims {
|
|
|
|
req.Claims = append(req.Claims, claim)
|
|
|
|
}
|
2020-04-30 13:13:00 +00:00
|
|
|
|
2020-05-08 20:53:57 +00:00
|
|
|
// Upsert the claims in a go routine
|
|
|
|
go f.Set(b.raft.UpsertVolumeClaims(&req))
|
|
|
|
|
|
|
|
// Reset the batches list
|
|
|
|
batches = batches[1:]
|
|
|
|
}
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatchFuture is a future that can be used to retrieve the index for
|
|
|
|
// the update or any error in the update process
|
|
|
|
type BatchFuture struct {
|
|
|
|
index uint64
|
|
|
|
err error
|
|
|
|
waitCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatchFuture returns a new BatchFuture
|
|
|
|
func NewBatchFuture() *BatchFuture {
|
|
|
|
return &BatchFuture{
|
|
|
|
waitCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the results of the future, unblocking any client.
|
|
|
|
func (f *BatchFuture) Set(index uint64, err error) {
|
|
|
|
f.index = index
|
|
|
|
f.err = err
|
|
|
|
close(f.waitCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Results returns the creation index and any error.
|
|
|
|
func (f *BatchFuture) Results() (uint64, error) {
|
|
|
|
<-f.waitCh
|
|
|
|
return f.index, f.err
|
|
|
|
}
|