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

fix(metrics-operator): use context with timeout for fetching analysis values #2213

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions metrics-operator/controllers/analysis/worker_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ type IAnalysisPool interface {
DispatchAndCollect(ctx context.Context) (map[string]metricsapi.ProviderResult, error)
}

const workerPoolTimeout = 10 * time.Second
bacherfl marked this conversation as resolved.
Show resolved Hide resolved

type NewWorkersPoolFactory func(ctx context.Context, analysis *metricsapi.Analysis, objectives []metricsapi.Objective, numWorkers int, c client.Client, log logr.Logger, namespace string) (context.Context, IAnalysisPool)

func NewWorkersPool(ctx context.Context, analysis *metricsapi.Analysis, objectives []metricsapi.Objective, numWorkers int, c client.Client, log logr.Logger, namespace string) (context.Context, IAnalysisPool) {
numJobs := len(objectives)
if numJobs <= numWorkers { // do not start useless go routines
numWorkers = numJobs
}
_, cancel := context.WithTimeout(ctx, 10*time.Second)
childCtx, cancel := context.WithTimeout(ctx, workerPoolTimeout)
providerChans := make(map[string]chan metricstypes.ProviderRequest, len(providers.SupportedProviders))

assigner := TaskAssigner{tasks: objectives, numWorkers: numWorkers}
Expand All @@ -49,7 +51,7 @@ func NewWorkersPool(ctx context.Context, analysis *metricsapi.Analysis, objectiv
providers: providerChans,
cancel: cancel,
}
return ctx, WorkersPool{
return childCtx, WorkersPool{
numWorkers: numWorkers,
numJobs: numJobs,
cancel: cancel,
Expand Down
6 changes: 5 additions & 1 deletion metrics-operator/controllers/analysis/worker_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ func TestNewWorkerPool(t *testing.T) {
}

// no objectives to evaluate
_, got := NewWorkersPool(context.TODO(), &analysis, []metricsapi.Objective{}, 4, nil, log, "default")
ctx, got := NewWorkersPool(context.TODO(), &analysis, []metricsapi.Objective{}, 4, nil, log, "default")
require.Equal(t, 0, got.(WorkersPool).numWorkers)
require.Equal(t, 0, got.(WorkersPool).numJobs)
deadline, ok := ctx.Deadline()
require.True(t, ok)
// verify that we get a context with a timeout being set
require.WithinDuration(t, time.Now().Add(workerPoolTimeout), deadline, 1*time.Second)

_, got = NewWorkersPool(context.TODO(), &analysis, objs, 4, nil, log, "default")
//make sure never to create more workers than needed
Expand Down
Loading