Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor pod warmup to avoid vet warning #3713

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions porch/func/internal/podevaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,44 @@ func (pcm *podCacheManager) warmupCache(podTTLConfig string) error {
return err
}

// We create the pods concurrently to speed it up.
// We precreate the pods (concurrently) to speed it up.
forEachConcurrently(podsTTL, func(fnImage string, ttlStr string) {
klog.Infof("preloading pod cache for function %v with TTL %v", fnImage, ttlStr)

ttl, err := time.ParseDuration(ttlStr)
if err != nil {
klog.Warningf("unable to parse duration from the config file for function %v: %w", fnImage, err)
ttl = pcm.podTTL
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// We invoke the function with useGenerateName=false so that the pod name is fixed,
// since we want to ensure only one pod is created for each function.
pcm.podManager.getFuncEvalPodClient(ctx, fnImage, ttl, false)
klog.Infof("preloaded pod cache for function %v", fnImage)
})

return nil
}

// forEachConcurrently runs fn for each entry in the map m, in parallel goroutines.
// It waits for each to finish before returning.
func forEachConcurrently(m map[string]string, fn func(k string, v string)) {
var wg sync.WaitGroup
for fnImage, ttlStr := range podsTTL {
for k, v := range m {
k := k
v := v

wg.Add(1)
go func(img, ttlSt string) {
klog.Infof("preloading pod cache for function %v with TTL %v", img, ttlSt)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
ttl, err := time.ParseDuration(ttlSt)
if err != nil {
klog.Warningf("unable to parse duration from the config file for function %v: %w", fnImage, err)
ttl = pcm.podTTL
}
// We invoke the function with useGenerateName=false so that the pod name is fixed,
// since we want to ensure only one pod is created for each function.
pcm.podManager.getFuncEvalPodClient(ctx, img, ttl, false)
klog.Infof("preloaded pod cache for function %v", img)
}(fnImage, ttlStr)
fn(k, v)
}()
}
// Wait for the cache warming up to finish before returning.
// Wait for all the functions to complete.
wg.Wait()
return nil
}

// podCacheManager responds to the requestCh and the podReadyCh and does the
Expand Down