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

🐛Implement priorityqueue as default on handlers if using priorityqueue interface #3111

Conversation

troy0820
Copy link
Member

@troy0820 troy0820 commented Feb 10, 2025

Resolves #3105

Handlers:

  • TypedEnqueueRequestForObject
  • EnqueueRequestForOwner
  • EnqueueRequestFromMapFunc

This brings the handler TypedEnqueueRequestForObject , enqueueRequestForOwner and enqueuRequestFromMapFunc handlers to use priority queue if the workqueue.TypedRateLimitingInterface[reconcile.Request] is of type priorityqueue.PriorityQueue[reconcile.Request] without having to use the wrapper WithLowPriorityWhenUnchanged.

NOTE: Added tests implement the same logic to show that this maps to the functionality of the wrapped handler in the tests

/assign @alvaroaleman @sbueringer

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 10, 2025
@troy0820
Copy link
Member Author

/test pull-controller-runtime-test

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 2 times, most recently from f7f9fbe to 753b14a Compare February 11, 2025 18:57
@troy0820 troy0820 changed the title 🐛Implement priority queue in EnqueueRequestForObject handler 🐛Implement priorityqueue for EnqueueRequestForObject and EnqueueRequestForOwner handler Feb 11, 2025
@troy0820
Copy link
Member Author

I am not sure if you wanted to do this for the handler *enqueueRequestsFromMapFunc[object, request]) but I can add this here or make another PR with the change.

@alvaroaleman
Copy link
Member

I am not sure if you wanted to do this for the handler *enqueueRequestsFromMapFunc[object, request]) but I can add this here or make another PR with the change.

Yeah we would want this for all handlers we offer if possible. This PR is on my list but it might take me a few more days to find the time, sorry

@troy0820
Copy link
Member Author

Yeah we would want this for all handlers we offer if possible. This PR is on my list but it might take me a few more days to find the time, sorry

No rush, just wanted to understand the scope/ask of the issue and what work may still need to be done.

@troy0820 troy0820 changed the title 🐛Implement priorityqueue for EnqueueRequestForObject and EnqueueRequestForOwner handler 🐛Implement priorityqueue as default on handlers if using priorityqueue interface Feb 12, 2025

// addToPriorityQueueCreate adds the reconcile.Request to the priorityqueue in the handler
// for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
func addToPriorityQueueCreate[T client.Object](q priorityqueue.PriorityQueue[reconcile.Request], evt event.TypedCreateEvent[T], item reconcile.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, lets just wrap the handler in the constructor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are functions to not repeat the logic in the TypedEnqueueRequestForObject[T] Create/Update as we do export this type.

Do you want this to have a constructor to wrap with WithLowPriorityWhenUnchanged because this will require adding more constraints to deal with the func signature with ^^ to equal TypedEventHandler[object,request]. Unless I am misunderstanding something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I think I just commented on the wrong place here but I see you already updated it 👍

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch from 0f9055e to 8ef6629 Compare February 18, 2025 16:08

// addToPriorityQueueCreate adds the reconcile.Request to the priorityqueue in the handler
// for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
func addToPriorityQueueCreate[T client.Object](q priorityqueue.PriorityQueue[reconcile.Request], evt event.TypedCreateEvent[T], item reconcile.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I think I just commented on the wrong place here but I see you already updated it 👍

}

// Update implements EventHandler.
func (e *TypedEnqueueRequestForObject[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
switch {
case !isNil(evt.ObjectNew):
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
item := reconcile.Request{NamespacedName: types.NamespacedName{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also do the same for Funcs? That is the only remaining handler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is how you wanted this done but any feedback is appreciated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in Funcs we have to do the same dance as in TypedEnqueueRequestForObject because we export the type and not a constructor, otherwise the original goal of get this by default won't be achieved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I did what we did with TypedEnqueueRequestForObject because I can't add methods to the Funcs type. So I did the same thing we did with this.

@alvaroaleman
Copy link
Member

/label tide-merge-method-squash

@k8s-ci-robot
Copy link
Contributor

@alvaroaleman: The label(s) /label tide-merge-method-squash cannot be applied. These labels are supported: api-review, tide/merge-method-merge, tide/merge-method-rebase, tide/merge-method-squash, team/katacoda, refactor, ci-short, ci-extended, ci-full. Is this label configured under labels -> additional_labels or labels -> restricted_labels in plugin.yaml?

In response to this:

/label tide-merge-method-squash

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@alvaroaleman alvaroaleman added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Feb 19, 2025
@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 3 times, most recently from aff6932 to eaf978d Compare February 19, 2025 20:52
Comment on lines 62 to 66
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[reconcile.Request])
if !isPriorityQueue {
q.Add(item)
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also move this into the util func? (and rename the func to addToQueueCreate)

Same for update

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can. I would have to move isObjectUnchanged as well. Not sure where to move it if we want that exposed or in some internal directory.

Copy link
Member

@sbueringer sbueringer Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean :)

I only mean that these 5 lines should be inside of the addToQueueCreate func. I wouldn't move them to another package or export

(the util func == addToQueueCreate)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I misunderstood. I can move them as well to do the check within the function. I did rename them but can move those lines inside.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Just a lot of code we can deduplicate :)

@@ -199,3 +205,23 @@ func (w workqueueWithCustomAddFunc[request]) Add(item request) {
func isObjectUnchanged[object client.Object](e event.TypedCreateEvent[object]) bool {
return e.Object.GetCreationTimestamp().Time.Before(time.Now().Add(-time.Minute))
}

// addToPriorityQueueCreate adds the reconcile.Request to the priorityqueue in the handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good if we can use these two funcs in WithLowPriorityWhenUnchanged

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 4 times, most recently from 10068c8 to 1d97d46 Compare February 20, 2025 22:08
@@ -82,10 +83,72 @@ type TypedEventHandler[object any, request comparable] interface {
Generic(context.Context, event.TypedGenericEvent[object], workqueue.TypedRateLimitingInterface[request])
}

var _ EventHandler = Funcs{}
var _ EventHandler = &Funcs{}
Copy link
Member

@alvaroaleman alvaroaleman Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can not be a pointer receiver otherwise this is a breaking change 😬 Can we also typecheck that TypedFuncs which is now a different implementation is a TypedEventHandler?

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch from 1d97d46 to 8d804ba Compare February 20, 2025 22:39
@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 5 times, most recently from 7aa17da to 5bbe24d Compare February 21, 2025 22:33
@troy0820
Copy link
Member Author

Test logic needs updating because now that this is wrapped in the WithLow..... tests are showing back with the wrong length for reconcile requests? 🤔 I can debug the test cases but also want to make sure I do not ruin any existing functionality as that would be a breaking change and change that 🌱 into a ⚠️

@alvaroaleman
Copy link
Member

Test logic needs updating because now that this is wrapped in the WithLow..... tests are showing back with the wrong length for reconcile requests? 🤔 I can debug the test cases but also want to make sure I do not ruin any existing functionality as that would be a breaking change and change that 🌱 into a ⚠️

No, this should have no effect on existing functionality. I can see a bunch of panics in CI, so something seems to not be working correctly.

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 2 times, most recently from 58e6a86 to 90b1a3f Compare March 3, 2025 16:53
@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch 5 times, most recently from 489410a to a71f2cb Compare March 6, 2025 21:14
@alvaroaleman
Copy link
Member

@troy0820 how about you leave out EnqueueRequestFromMapFunc from this change and I will have a look into that one, as it seems a bit more tricky?

@troy0820
Copy link
Member Author

troy0820 commented Mar 9, 2025

@troy0820 how about you leave out EnqueueRequestFromMapFunc from this change and I will have a look into that one, as it seems a bit more tricky?

That’ll work. I’ll queue up the change in a second.

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch from a71f2cb to 4e82bce Compare March 9, 2025 01:02
if ok {
evt, ok := any(e).(event.TypedCreateEvent[client.Object])
if ok {
item := reconcile.Request{NamespacedName: types.NamespacedName{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite correct, because we need to run CreateFunc to know what item looks like, here we basically hardcode the same behavior as EnqueueRequestForObjec - Lets maybe leave the TypedFuncs out as well of this PR?

@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch from 4e82bce to 1d0e50b Compare March 10, 2025 00:18
Signed-off-by: Troy Connor <[email protected]>
@troy0820 troy0820 force-pushed the troy0820/priority-queue-on-enqueue-handler branch from 1d0e50b to 16ff4af Compare March 10, 2025 00:19
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 10, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 3518285291eae78304b39908f55205b724196db9

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, troy0820

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 10, 2025
@k8s-ci-robot k8s-ci-robot merged commit f80bc5d into kubernetes-sigs:main Mar 10, 2025
14 checks passed
Copy link
Member

@sbueringer sbueringer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@troy0820 Thx for working on this!

Just one small finding, would be nice if you can follow-up

@@ -65,7 +65,7 @@ type EventHandler = TypedEventHandler[client.Object, reconcile.Request]
//
// Unless you are implementing your own TypedEventHandler, you can ignore the functions on the TypedEventHandler interface.
// Most users shouldn't need to implement their own TypedEventHandler.
//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks godoc rendering of TypedEventHandler. Now only l.69 shows up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handlers should default to LowPriorityWhenUnchanged without a wrapper
4 participants