-
Notifications
You must be signed in to change notification settings - Fork 720
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: support backoff mechanism for memberLoop #6978
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9975663
abondon sync
HuSharp 3199dfe
Merge remote-tracking branch 'upstream/release-6.5' into release-6.5
HuSharp e3d16ba
add retry
HuSharp 173337d
Merge branch 'release-6.5' into release-6.5-add_ready
HuSharp 178e30a
refine test
HuSharp 6f5baee
refine backoff
HuSharp 23318b6
refine backoff
HuSharp 6405f53
address comment
HuSharp 83ddbf5
address comment
HuSharp c700180
address comment
HuSharp dda1bb4
make TestScheduler stable
HuSharp 7fc588f
rename backoff
HuSharp 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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 { | ||
max time.Duration | ||
next time.Duration | ||
base time.Duration | ||
} | ||
|
||
// Exec is a helper function to exec backoff. | ||
func (bo *BackOffer) Exec( | ||
ctx context.Context, | ||
fn func() error, | ||
) error { | ||
if err := fn(); err != nil { | ||
select { | ||
case <-ctx.Done(): | ||
case <-time.After(bo.nextInterval()): | ||
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(base, max time.Duration) BackOffer { | ||
return BackOffer{ | ||
max: max, | ||
base: base, | ||
next: base, | ||
} | ||
} | ||
|
||
// nextInterval for now use the `exponentialInterval`. | ||
func (bo *BackOffer) nextInterval() time.Duration { | ||
return bo.exponentialInterval() | ||
} | ||
|
||
// exponentialInterval returns the exponential backoff duration. | ||
func (bo *BackOffer) exponentialInterval() time.Duration { | ||
backoffInterval := bo.next | ||
bo.next *= 2 | ||
if bo.next > bo.max { | ||
bo.next = bo.max | ||
} | ||
return backoffInterval | ||
} | ||
|
||
// resetBackoff resets the backoff to initial state. | ||
func (bo *BackOffer) resetBackoff() { | ||
bo.next = bo.base | ||
} | ||
|
||
// Only used for test. | ||
var testBackOffExecuteFlag = false | ||
|
||
// TestBackOffExecute Only used for test. | ||
func TestBackOffExecute() bool { | ||
return testBackOffExecuteFlag | ||
} |
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 |
---|---|---|
@@ -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.nextInterval(), baseBackoff) | ||
re.Equal(backoff.nextInterval(), 2*baseBackoff) | ||
|
||
for i := 0; i < 10; i++ { | ||
re.LessOrEqual(backoff.nextInterval(), maxBackoff) | ||
} | ||
re.Equal(backoff.nextInterval(), maxBackoff) | ||
|
||
// Reset backoff | ||
backoff.resetBackoff() | ||
err := backoff.Exec(context.Background(), func() error { | ||
return errors.New("test") | ||
}) | ||
re.Error(err) | ||
} |
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 |
---|---|---|
|
@@ -397,22 +397,20 @@ func TestScheduler(t *testing.T) { | |
pdctl.MustPutStore(re, leaderServer.GetServer(), store) | ||
} | ||
re.Equal("5.2.0", leaderServer.GetClusterVersion().String()) | ||
mustExec([]string{"-u", pdAddr, "scheduler", "config", "balance-hot-region-scheduler"}, &conf1) | ||
// After upgrading, we should not use query. | ||
expected1["read-priorities"] = []interface{}{"query", "byte"} | ||
re.NotEqual(expected1, conf1) | ||
expected1["read-priorities"] = []interface{}{"key", "byte"} | ||
re.Equal(expected1, conf1) | ||
mustExec([]string{"-u", pdAddr, "scheduler", "config", "balance-hot-region-scheduler"}, &conf1) | ||
re.Equal(conf1["read-priorities"], []interface{}{"key", "byte"}) | ||
// cannot set qps as write-peer-priorities | ||
echo = mustExec([]string{"-u", pdAddr, "scheduler", "config", "balance-hot-region-scheduler", "set", "write-peer-priorities", "query,byte"}, nil) | ||
re.Contains(echo, "query is not allowed to be set in priorities for write-peer-priorities") | ||
mustExec([]string{"-u", pdAddr, "scheduler", "config", "balance-hot-region-scheduler"}, &conf1) | ||
re.Equal(expected1, conf1) | ||
re.Equal(conf1["write-peer-priorities"], []interface{}{"byte", "key"}) | ||
|
||
// test remove and add | ||
mustExec([]string{"-u", pdAddr, "scheduler", "remove", "balance-hot-region-scheduler"}, nil) | ||
mustExec([]string{"-u", pdAddr, "scheduler", "add", "balance-hot-region-scheduler"}, nil) | ||
re.Equal(expected1, conf1) | ||
echo = mustExec([]string{"-u", pdAddr, "scheduler", "remove", "balance-hot-region-scheduler"}, nil) | ||
re.Contains(echo, "Success") | ||
echo = mustExec([]string{"-u", pdAddr, "scheduler", "add", "balance-hot-region-scheduler"}, nil) | ||
re.Contains(echo, "Success") | ||
Comment on lines
-400
to
+413
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. cp #5847 to make TestScheduler stable |
||
|
||
// test balance leader config | ||
conf = make(map[string]interface{}) | ||
|
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.
can it be removed?
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.
want to make sure back off executed