mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: resource and slot cancel and broadcast improvements (#696)
* fn: resource and slot cancel and broadcast improvements *) Context argument does not wake up the waiters correctly upon cancellation/timeout. *) Avoid unnecessary broadcasts in slot and resource. * fn: limit scope of context in resource/slot calls in agent
This commit is contained in:
@@ -24,7 +24,7 @@ const (
|
||||
// A simple resource (memory, cpu, disk, etc.) tracker for scheduling.
|
||||
// TODO: add cpu, disk, network IO for future
|
||||
type ResourceTracker interface {
|
||||
WaitAsyncResource() (chan struct{}, context.CancelFunc)
|
||||
WaitAsyncResource(ctx context.Context) chan struct{}
|
||||
// returns a closed channel if the resource can never me met.
|
||||
GetResourceToken(ctx context.Context, memory uint64, cpuQuota uint64, isAsync bool) <-chan ResourceToken
|
||||
}
|
||||
@@ -115,6 +115,7 @@ func (a *resourceTracker) GetResourceToken(ctx context.Context, memory uint64, c
|
||||
memory = memory * Mem1MB
|
||||
|
||||
c := a.cond
|
||||
isWaiting := false
|
||||
ch := make(chan ResourceToken)
|
||||
|
||||
if !a.isResourcePossible(memory, cpuQuota, isAsync) {
|
||||
@@ -122,12 +123,23 @@ func (a *resourceTracker) GetResourceToken(ctx context.Context, memory uint64, c
|
||||
return ch
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
c.L.Lock()
|
||||
if isWaiting {
|
||||
c.Broadcast()
|
||||
}
|
||||
c.L.Unlock()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
c.L.Lock()
|
||||
|
||||
isWaiting = true
|
||||
for !a.isResourceAvailableLocked(memory, cpuQuota, isAsync) && ctx.Err() == nil {
|
||||
c.Wait()
|
||||
}
|
||||
isWaiting = false
|
||||
|
||||
if ctx.Err() != nil {
|
||||
c.L.Unlock()
|
||||
@@ -184,24 +196,28 @@ func (a *resourceTracker) GetResourceToken(ctx context.Context, memory uint64, c
|
||||
|
||||
// WaitAsyncResource will send a signal on the returned channel when RAM and CPU in-use
|
||||
// in the async area is less than high water mark
|
||||
func (a *resourceTracker) WaitAsyncResource() (chan struct{}, context.CancelFunc) {
|
||||
func (a *resourceTracker) WaitAsyncResource(ctx context.Context) chan struct{} {
|
||||
ch := make(chan struct{}, 1)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
isWaiting := false
|
||||
c := a.cond
|
||||
|
||||
myCancel := func() {
|
||||
cancel()
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
c.L.Lock()
|
||||
c.Broadcast()
|
||||
if isWaiting {
|
||||
c.Broadcast()
|
||||
}
|
||||
c.L.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
c.L.Lock()
|
||||
isWaiting = true
|
||||
for (a.ramAsyncUsed >= a.ramAsyncHWMark || a.cpuAsyncUsed >= a.cpuAsyncHWMark) && ctx.Err() == nil {
|
||||
c.Wait()
|
||||
}
|
||||
isWaiting = false
|
||||
c.L.Unlock()
|
||||
|
||||
if ctx.Err() == nil {
|
||||
@@ -209,7 +225,7 @@ func (a *resourceTracker) WaitAsyncResource() (chan struct{}, context.CancelFunc
|
||||
}
|
||||
}()
|
||||
|
||||
return ch, myCancel
|
||||
return ch
|
||||
}
|
||||
|
||||
func minUint64(a, b uint64) uint64 {
|
||||
|
||||
Reference in New Issue
Block a user