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 exponential backoff minimum duration #2578

Merged
merged 19 commits into from
May 12, 2023
Merged

Conversation

noisersup
Copy link
Member

@noisersup noisersup commented May 8, 2023

Description

Closes #1720.

Readiness checklist

  • I added/updated unit tests.
  • I added/updated integration/compatibility tests.
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I updated user documentation.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

@codecov
Copy link

codecov bot commented May 8, 2023

Codecov Report

Merging #2578 (3d36bc8) into main (16c6a18) will decrease coverage by 38.17%.
The diff coverage is 87.50%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2578       +/-   ##
===========================================
- Coverage   65.96%   27.80%   -38.17%     
===========================================
  Files         419      419               
  Lines       20545    20553        +8     
===========================================
- Hits        13553     5715     -7838     
- Misses       6039    14252     +8213     
+ Partials      953      586      -367     
Impacted Files Coverage Δ
internal/util/ctxutil/ctxutil.go 46.51% <87.50%> (-24.92%) ⬇️

... and 137 files with indirect coverage changes

Flag Coverage Δ
integration 5.39% <0.00%> (-53.48%) ⬇️
mongodb 5.39% <0.00%> (-0.01%) ⬇️
pg ?
unit 26.24% <87.50%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@noisersup noisersup added the code/chore Code maintenance improvements label May 8, 2023
@noisersup noisersup changed the title Fix jitter Fix exponential backoff min duration May 8, 2023
@noisersup noisersup changed the title Fix exponential backoff min duration Fix exponential backoff minimum duration May 8, 2023
@noisersup noisersup marked this pull request as ready for review May 8, 2023 17:04
@noisersup noisersup requested a review from a team as a code owner May 8, 2023 17:04
@noisersup noisersup requested review from rumyantseva, chilagrow, a team and w84thesun May 8, 2023 17:04
@noisersup noisersup enabled auto-merge (squash) May 8, 2023 17:08
lowestValue := int64(math.Min(capMilliseconds, maxMilliseconds)) - 100

// Math/rand is good enough because we don't need the randomness to be cryptographically secure.
return time.Duration(rand.Int63n(lowestValue)+100) * time.Millisecond
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 that would panic with cap = 50 * time.Millisecond, for example. Please add a test

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we add a comment that provided cap cannot be smaller than 100ms, or should we for example decrease min to 0ms if the cap is smaller than 100ms?

Copy link
Member Author

@noisersup noisersup May 8, 2023

Choose a reason for hiding this comment

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

Also, do we even need minimum duration?

Copy link
Member Author

Choose a reason for hiding this comment

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

For now I've updated description of function to mention expected parameters, added custom panic message similarly to the one that we already have for negative retry, and added test case that validates if it actually panics.


return time.Duration(rand.Int63n(lowestValue)) * time.Millisecond
lowestValue := int64(math.Min(capMilliseconds, maxMilliseconds)) - 100
Copy link
Member

Choose a reason for hiding this comment

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

Is it the same 100 as base?

Copy link
Member Author

@noisersup noisersup May 8, 2023

Choose a reason for hiding this comment

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

It's the same 100 which is added to the rand.Int63n later to set min duration as 100.

The base is constant that is multiplied each retry to set max duration.

@noisersup noisersup requested a review from AlekSi May 8, 2023 18:07
@noisersup noisersup requested review from rumyantseva and a team May 8, 2023 18:40
Copy link
Member

@chilagrow chilagrow left a comment

Choose a reason for hiding this comment

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

Just tiny comment 🤗 nice work!

internal/util/ctxutil/ctxutil.go Outdated Show resolved Hide resolved
@noisersup noisersup requested review from rumyantseva and chilagrow May 9, 2023 11:23
chilagrow
chilagrow previously approved these changes May 10, 2023
@noisersup noisersup requested review from w84thesun and chilagrow May 11, 2023 08:25
@AlekSi AlekSi added this to the Next milestone May 11, 2023
chilagrow
chilagrow previously approved these changes May 11, 2023
w84thesun
w84thesun previously approved these changes May 11, 2023
Copy link
Contributor

@w84thesun w84thesun left a comment

Choose a reason for hiding this comment

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

:shipit:

@AlekSi
Copy link
Member

AlekSi commented May 11, 2023

Didn't know how exactly should I model the data

Well, we want the graph of exponential backoff to looks like an exponent. Like in the original article. I'm not sure how to read the current graph.

@noisersup
Copy link
Member Author

noisersup commented May 11, 2023

Well, we want the graph of exponential backoff to looks like an exponent. Like in the original article. I'm not sure how to read the current graph.

Are you talking about dot graph or the linear graph from the article? The dot one shows call count over time and it seems to me that we don't produce enough data to tell how many "calls" we have over time as we don't collect data over time, but just collect all generated durations.

I've made the graph that shows how often each duration was returned and it seems that the function return durations pretty equally, and repetitions are mostly within the first "chunk" of values which is also expected.

@noisersup
Copy link
Member Author

The exponent graph on their article shows impact of amount of competing clients on the amount of calls, which requires implementation of clients that will reconnect and make their connection successful or not.

Copy link
Contributor

@rumyantseva rumyantseva left a comment

Choose a reason for hiding this comment

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

I'm still curious to see the distribution, but the approach looks good to me.

In principle, we can call a few loops that call DurationWithJitter in parallel (with retry being increased in each loop) and save results. That will give some distribution similar to the idea of parallel connections.

rumyantseva
rumyantseva previously approved these changes May 11, 2023
@noisersup noisersup dismissed stale reviews from chilagrow, rumyantseva, and w84thesun via 07bf915 May 12, 2023 10:57
@noisersup
Copy link
Member Author

image

@AlekSi New chart based on the results of new test. It shows total amount of calls for simulation with n clients.

@AlekSi AlekSi disabled auto-merge May 12, 2023 11:08
@AlekSi AlekSi merged commit 11a979f into FerretDB:main May 12, 2023
@noisersup noisersup deleted the fix-jitter branch May 12, 2023 11:09
@AlekSi
Copy link
Member

AlekSi commented May 12, 2023

On a call with Elena, I just released that I misread graphs in the original article, and that was the source of confusion. I will send a PR explaining what I meant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/chore Code maintenance improvements
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use exponential backoff with jitter
5 participants