generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Limit the number of goroutines used when deleting jobs #123
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:main
from
tenzen-y:use-workqueue-when-deleting-jobs
May 10, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"sync" | ||
|
@@ -25,6 +26,7 @@ import ( | |
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/client-go/util/workqueue" | ||
"k8s.io/klog/v2" | ||
"k8s.io/utils/pointer" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
@@ -33,7 +35,10 @@ import ( | |
jobset "sigs.k8s.io/jobset/api/v1alpha1" | ||
) | ||
|
||
const RestartsKey string = "jobset.sigs.k8s.io/restart-attempt" | ||
const ( | ||
RestartsKey string = "jobset.sigs.k8s.io/restart-attempt" | ||
parallelDeletions int = 50 | ||
) | ||
|
||
var ( | ||
jobOwnerKey = ".metadata.controller" | ||
|
@@ -97,7 +102,7 @@ func (r *JobSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr | |
} | ||
|
||
// Delete any jobs marked for deletion. | ||
if err := r.deleteJobs(ctx, &js, ownedJobs.delete); err != nil { | ||
if err := r.deleteJobs(ctx, ownedJobs.delete); err != nil { | ||
log.Error(err, "deleting jobs") | ||
return ctrl.Result{}, err | ||
} | ||
|
@@ -364,32 +369,24 @@ func (r *JobSetReconciler) restartPolicyRecreateAll(ctx context.Context, js *job | |
return nil | ||
} | ||
|
||
func (r *JobSetReconciler) deleteJobs(ctx context.Context, js *jobset.JobSet, jobsForDeletion []*batchv1.Job) error { | ||
func (r *JobSetReconciler) deleteJobs(ctx context.Context, jobsForDeletion []*batchv1.Job) error { | ||
log := ctrl.LoggerFrom(ctx) | ||
|
||
var wg sync.WaitGroup | ||
var finalErr error | ||
|
||
// Delete all jobs in parallel. | ||
// TODO: limit number of goroutines used here. | ||
for _, job := range jobsForDeletion { | ||
job := job | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
// Delete job. This deletion event will trigger another reconcilliation, | ||
// where the jobs are recreated. | ||
backgroundPolicy := metav1.DeletePropagationBackground | ||
if err := r.Delete(ctx, job, &client.DeleteOptions{PropagationPolicy: &backgroundPolicy}); client.IgnoreNotFound(err) != nil { | ||
finalErr = err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a potential issue, a race condition may occur. |
||
return | ||
} | ||
log.V(2).Info("successfully deleted job", "job", klog.KObj(job), "restart attempt", job.Labels[job.Labels[RestartsKey]]) | ||
}() | ||
} | ||
wg.Wait() | ||
return finalErr | ||
lock := &sync.Mutex{} | ||
var finalErrs []error | ||
workqueue.ParallelizeUntil(ctx, parallelDeletions, len(jobsForDeletion), func(i int) { | ||
targetJob := jobsForDeletion[i] | ||
// Delete job. This deletion event will trigger another reconciliation, | ||
// where the jobs are recreated. | ||
backgroundPolicy := metav1.DeletePropagationBackground | ||
if err := r.Delete(ctx, targetJob, &client.DeleteOptions{PropagationPolicy: &backgroundPolicy}); client.IgnoreNotFound(err) != nil { | ||
lock.Lock() | ||
danielvegamyhre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer lock.Unlock() | ||
finalErrs = append(finalErrs, err) | ||
return | ||
} | ||
log.V(2).Info("successfully deleted job", "job", klog.KObj(targetJob), "restart attempt", targetJob.Labels[targetJob.Labels[RestartsKey]]) | ||
}) | ||
return errors.Join(finalErrs...) | ||
} | ||
|
||
// updateStatus updates the status of a JobSet. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesn't use the
JobSet
.