-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat: exponential backoff for clients (KIP-580) #3099
Open
wanwenli
wants to merge
1
commit into
IBM:main
Choose a base branch
from
wanwenli:kip-580-exponential-backoff-func
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
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 |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
|
||
package sarama | ||
|
||
import "testing" | ||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestVersionCompare(t *testing.T) { | ||
if V0_8_2_0.IsAtLeast(V0_8_2_1) { | ||
|
@@ -95,3 +98,47 @@ func TestVersionParsing(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
dnwe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func TestExponentialBackoffValidCases(t *testing.T) { | ||
testCases := []struct { | ||
retries int | ||
maxRetries int | ||
minBackoff time.Duration | ||
maxBackoffExpected time.Duration | ||
}{ | ||
{1, 5, 80 * time.Millisecond, 120 * time.Millisecond}, | ||
{3, 5, 320 * time.Millisecond, 480 * time.Millisecond}, | ||
{5, 5, 1280 * time.Millisecond, 1920 * time.Millisecond}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
backoffFunc := NewExponentialBackoff(100*time.Millisecond, 2*time.Second) | ||
backoff := backoffFunc(tc.retries, tc.maxRetries) | ||
if backoff < tc.minBackoff || backoff > tc.maxBackoffExpected { | ||
t.Errorf("backoff(%d, %d): expected between %v and %v, got %v", tc.retries, tc.maxRetries, tc.minBackoff, tc.maxBackoffExpected, backoff) | ||
} | ||
} | ||
} | ||
|
||
func TestExponentialBackoffDefaults(t *testing.T) { | ||
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. 👍 |
||
testCases := []struct { | ||
backoff time.Duration | ||
maxBackoff time.Duration | ||
}{ | ||
{-100 * time.Millisecond, 2 * time.Second}, | ||
{100 * time.Millisecond, -2 * time.Second}, | ||
{-100 * time.Millisecond, -2 * time.Second}, | ||
{0 * time.Millisecond, 2 * time.Second}, | ||
{100 * time.Millisecond, 0 * time.Second}, | ||
{0 * time.Millisecond, 0 * time.Second}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
backoffFunc := NewExponentialBackoff(tc.backoff, tc.maxBackoff) | ||
backoff := backoffFunc(2, 5) | ||
if backoff < defaultRetryBackoff || backoff > defaultRetryMaxBackoff { | ||
t.Errorf("backoff(%v, %v): expected between %v and %v, got %v", | ||
tc.backoff, tc.maxBackoff, defaultRetryBackoff, defaultRetryMaxBackoff, backoff) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
I suppose, I would like to see a version that can take
sarama.NewExponentialBackoff(config)
so that the appropriate fields from the config will be extracted and used without me needing to dig them out.We probably also need a
config.Producer.Retry.BackoffMax
.PS: I guess we cannot actually provide a simple
config
option, because each of the backoffs has its own sub-struct. 🤦♀️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.
That makes sense! I also considered making
sarama.NewExponentialBackoff(config)
, but as you pointed out in the PS section, each backoff belongs to its own sub-struct, which makes a simple config-based approach impractical.This is exactly why I made
NewExponentialBackoff
a standalone function—neither a method ofConfig
nor a function that takesConfig
as an input parameter. This approach avoids ambiguity and ensures flexibility across different contexts (producer, metadata, transaction manager) without forcing unnecessary dependencies.Regarding
config.Producer.Retry.BackoffMax
, I chose not to introduce it because it would require adding a similar field to every retry-related sub-struct (Metadata
,TransactionManager
), making the API more tedious to maintain. Keeping the backoff logic encapsulated in a separate function simplifies the design while still allowing explicit configuration per use case.Let me know your thoughts!
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.
I don’t know that there is a significantly stronger maintenance in adding the
BackoffMax
. Is it a lot of “toil”? Yes, but as you note, there is already a retry struct in a lot of the other config structs. So, the choice has kind of already been made.