2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-12-05 17:17:33 +00:00
|
|
|
package allocwatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2023-05-12 17:29:44 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-12-05 17:17:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type groupPrevAllocWatcher struct {
|
2023-05-12 17:29:44 +00:00
|
|
|
prevAllocs []config.PrevAllocWatcher
|
2018-12-05 17:17:33 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
// waiting and migrating are true when alloc runner is waiting on the
|
|
|
|
// prevAllocWatcher. Writers must acquire the waitingLock and readers
|
|
|
|
// should use the helper methods IsWaiting and IsMigrating.
|
|
|
|
waiting bool
|
|
|
|
waitingLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2023-05-12 17:29:44 +00:00
|
|
|
func NewGroupAllocWatcher(watchers ...config.PrevAllocWatcher) config.PrevAllocWatcher {
|
2018-12-05 17:17:33 +00:00
|
|
|
return &groupPrevAllocWatcher{
|
|
|
|
prevAllocs: watchers,
|
2018-12-06 11:15:59 +00:00
|
|
|
}
|
2018-12-05 17:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait on the previous allocs to become terminal, exit, or, return due to
|
|
|
|
// context termination. Usage of the groupPrevAllocWatcher requires that all
|
|
|
|
// sub-watchers correctly handle context cancellation.
|
|
|
|
// We may need to adjust this to use channels rather than a wait group, if we
|
|
|
|
// wish to more strictly enforce timeouts.
|
|
|
|
func (g *groupPrevAllocWatcher) Wait(ctx context.Context) error {
|
|
|
|
g.waitingLock.Lock()
|
|
|
|
g.waiting = true
|
|
|
|
g.waitingLock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
g.waitingLock.Lock()
|
|
|
|
g.waiting = false
|
|
|
|
g.waitingLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var merr multierror.Error
|
|
|
|
var errmu sync.Mutex
|
|
|
|
|
|
|
|
g.wg.Add(len(g.prevAllocs))
|
|
|
|
|
|
|
|
for _, alloc := range g.prevAllocs {
|
2023-05-12 17:29:44 +00:00
|
|
|
go func(ctx context.Context, alloc config.PrevAllocWatcher) {
|
2018-12-05 17:17:33 +00:00
|
|
|
defer g.wg.Done()
|
|
|
|
err := alloc.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errmu.Lock()
|
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
errmu.Unlock()
|
|
|
|
}
|
|
|
|
}(ctx, alloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.wg.Wait()
|
|
|
|
|
|
|
|
// Check ctx.Err first, to avoid returning an mErr of ctx.Err from prevAlloc
|
|
|
|
// Wait routines.
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return merr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *groupPrevAllocWatcher) IsWaiting() bool {
|
|
|
|
g.waitingLock.RLock()
|
|
|
|
defer g.waitingLock.RUnlock()
|
|
|
|
|
|
|
|
return g.waiting
|
|
|
|
}
|