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

Add WithDeadlineTimer interface to clock #312

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tatsuhiro-t
Copy link

@tatsuhiro-t tatsuhiro-t commented Jun 20, 2024

What type of PR is this?
/kind feature

What this PR does / why we need it:

This is a part of effort to fix kubernetes/kubernetes#125581

WithDeadlineTimer interface extends Clock to add NewDeadlineTimer. NewDeadlineTimer is similar to NewTimer, but it returns a timer which fires at the given timestamp. It makes unit testing easier with FakeClock.Step when a test subject is making a timer inside goroutine (e.g., workqueue.NewDelayingQueue). With NewTimer, in some race conditions, a timer never fire if FakeClock.Step is called after getting the base time Now() and calling NewTimer(). This is because the duration passed to timer is calculated against the old timestamp, but inside FakeClock.NewTimer, the targetTime is calculated against the time adjusted by Step. With NewDeadlineTimer, targetTime is not adjusted, which works around this issue.

Because workqueue.DelayingQueueConfig uses clock.WithTicker and changing it breaks API compatibility, I plan to do type assertion like so to use NewDeadlineTimer if available:

diff --git a/staging/src/k8s.io/client-go/util/workqueue/delaying_queue.go b/staging/src/k8s.io/client-go/util/workqueue/delaying_queue.go
index 958b96a80c3..e3bebd36987 100644
--- a/staging/src/k8s.io/client-go/util/workqueue/delaying_queue.go
+++ b/staging/src/k8s.io/client-go/util/workqueue/delaying_queue.go
@@ -251,6 +251,14 @@ func (q *delayingType[T]) AddAfter(item T, duration time.Duration) {
        }
 }
 
+type deadlineTimerClock struct {
+       clock.Clock
+}
+
+func (c deadlineTimerClock) NewDeadlineTimer(ts time.Time) clock.Timer {
+       return c.NewTimer(ts.Sub(c.Now()))
+}
+
 // maxWait keeps a max bound on the wait time. It's just insurance against weird things happening.
 // Checking the queue every 10 seconds isn't expensive and we know that we'll never end up with an
 // expired item sitting for more than 10 seconds.
@@ -271,6 +279,11 @@ func (q *delayingType[T]) waitingLoop() {
 
        waitingEntryByData := map[t]*waitFor{}
 
+       deadlineClock, ok := q.clock.(clock.WithDeadlineTimer)
+       if !ok {
+               deadlineClock = deadlineTimerClock{Clock: q.clock}
+       }
+
        for {
                if q.TypedInterface.ShuttingDown() {
                        return
@@ -297,7 +310,7 @@ func (q *delayingType[T]) waitingLoop() {
                                nextReadyAtTimer.Stop()
                        }
                        entry := waitingForQueue.Peek().(*waitFor)
-                       nextReadyAtTimer = q.clock.NewTimer(entry.readyAt.Sub(now))
+                       nextReadyAtTimer = deadlineClock.NewDeadlineTimer(entry.readyAt)
                        nextReadyAt = nextReadyAtTimer.C()
                } 

Which issue(s) this PR fixes:

kubernetes/kubernetes#125581

Special notes for your reviewer:

Release note:

This PR adds WithDeadlineTimer interface extends Clock to add NewDeadlineTimer. NewDeadlineTimer is similar to NewTimer, but it returns a timer which fires at the given timestamp.

WithDeadlineTimer interface extends Clock to add NewDeadlineTimer.
NewDeadlineTimer is similar to NewTimer, but it returns a timer which
fires at the given timestamp.  It makes unit testing easier with
FakeClock.Step when a test subject is making a timer inside goroutine
(e.g., workqueue.NewDelayingQueue).  With NewTimer, in some race
conditions, a timer never fire if FakeClock.Step is called after
getting the base time Now() and calling NewTimer().  This is because
the duration passed to timer is calculated against the old timestamp,
but inside FakeClock.NewTimer, the targetTime is calculated against
the time adjusted by Step.  With NewDeadlineTimer, targetTime is not
adjusted, which works around this issue.
@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jun 20, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tatsuhiro-t
Once this PR has been reviewed and has the lgtm label, please assign thockin for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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
Copy link
Contributor

Welcome @tatsuhiro-t!

It looks like this is your first PR to kubernetes/utils 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/utils has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 20, 2024
clock/clock.go Outdated Show resolved Hide resolved
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky test failure in staging/src/k8s.io/client-go/util/workqueue
4 participants