-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: support backoff mechanism for memberLoop (#6978)
ref #6556 Signed-off-by: husharp <[email protected]>
- Loading branch information
Showing
8 changed files
with
209 additions
and
22 deletions.
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