-
Notifications
You must be signed in to change notification settings - Fork 721
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
client: add backoff for member loop
#6995
Changes from 3 commits
2f08813
21a9820
b9deebc
6c9c6f0
2af0f29
d023333
a0562c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package retry | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pingcap/failpoint" | ||
) | ||
|
||
// BackOffer is a backoff policy for retrying operations. | ||
type BackOffer struct { | ||
maxBackoff time.Duration | ||
nextBackoff time.Duration | ||
initialBackoff time.Duration | ||
} | ||
|
||
// WithBackoff is a helper function to add backoff. | ||
func WithBackoff( | ||
ctx context.Context, | ||
fn func() error, | ||
bo *BackOffer, | ||
) error { | ||
if err := fn(); err != nil { | ||
select { | ||
case <-ctx.Done(): | ||
case <-time.After(bo.NextBackoff()): | ||
failpoint.Inject("backOffExecute", func() { | ||
testBackOffExecuteFlag = true | ||
}) | ||
} | ||
return err | ||
} | ||
// Reset backoff when fn() succeed. | ||
bo.ResetBackoff() | ||
return nil | ||
} | ||
|
||
// InitialBackOffer make the initial state for retrying. | ||
func InitialBackOffer(initialBackoff, maxBackoff time.Duration) BackOffer { | ||
return BackOffer{ | ||
maxBackoff: maxBackoff, | ||
initialBackoff: initialBackoff, | ||
nextBackoff: initialBackoff, | ||
} | ||
} | ||
|
||
// NextBackoff implements the `Backoffer`, for now use the `ExponentialBackoff`. | ||
func (rs *BackOffer) NextBackoff() time.Duration { | ||
return rs.exponentialBackoff() | ||
} | ||
|
||
// exponentialBackoff Get the exponential backoff duration. | ||
HuSharp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func (rs *BackOffer) exponentialBackoff() time.Duration { | ||
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. Will we use exponential backoff by default? When network partition a long time, it may need more time to recover. 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. limited by max backoff time... 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. how long is it? |
||
backoff := rs.nextBackoff | ||
rs.nextBackoff *= 2 | ||
if rs.nextBackoff > rs.maxBackoff { | ||
rs.nextBackoff = rs.maxBackoff | ||
} | ||
return backoff | ||
} | ||
|
||
// ResetBackoff reset the backoff to initial state. | ||
func (rs *BackOffer) ResetBackoff() { | ||
rs.nextBackoff = rs.initialBackoff | ||
} | ||
|
||
// Only used for test. | ||
var testBackOffExecuteFlag = false | ||
|
||
// TestBackOffExecute Only used for test. | ||
func TestBackOffExecute() bool { | ||
return testBackOffExecuteFlag | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package retry | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExponentialBackoff(t *testing.T) { | ||
re := require.New(t) | ||
|
||
baseBackoff := 100 * time.Millisecond | ||
maxBackoff := 1 * time.Second | ||
|
||
backoff := InitialBackOffer(baseBackoff, maxBackoff) | ||
re.Equal(backoff.NextBackoff(), baseBackoff) | ||
re.Equal(backoff.NextBackoff(), 2*baseBackoff) | ||
|
||
for i := 0; i < 10; i++ { | ||
re.LessOrEqual(backoff.NextBackoff(), maxBackoff) | ||
} | ||
re.Equal(backoff.NextBackoff(), maxBackoff) | ||
|
||
// Reset backoff | ||
backoff.ResetBackoff() | ||
err := WithBackoff(context.Background(), func() error { | ||
return errors.New("test") | ||
}, &backoff) | ||
re.Error(err) | ||
} |
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.
@lhy1024 for
member loop
max backup time time is1 second
which isupdateMemberTimeout
, PTAL, thx!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.
Got it.