br: wait more time to wait spitting the region (#42182)

close pingcap/tidb#42001
This commit is contained in:
Zak Zhao
2023-03-16 14:24:39 +08:00
committed by GitHub
parent a8733c0050
commit 2ea6d00a56
4 changed files with 107 additions and 12 deletions

View File

@ -43,10 +43,16 @@ go_library(
go_test(
name = "split_test",
timeout = "short",
srcs = ["sum_sorted_test.go"],
srcs = [
"split_test.go",
"sum_sorted_test.go",
],
flaky = True,
deps = [
":split",
"//br/pkg/errors",
"//br/pkg/utils",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
],
)

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/logutil"
@ -17,7 +18,7 @@ import (
)
var (
ScanRegionAttemptTimes = 128
ScanRegionAttemptTimes = 150
)
// Constants for split retry machinery.
@ -115,7 +116,7 @@ func PaginateScanRegion(
return err
}
return nil
}, newScanRegionBackoffer())
}, NewScanRegionBackoffer())
return regions, err
}
@ -174,33 +175,44 @@ func ScanRegionsWithRetry(
}
return nil
}, newScanRegionBackoffer())
}, NewScanRegionBackoffer())
return regions, err
}
type scanRegionBackoffer struct {
attempt int
stat utils.RetryState
}
func newScanRegionBackoffer() utils.Backoffer {
// NewScanRegionBackoffer create a backoff to retry to scan regions.
func NewScanRegionBackoffer() utils.Backoffer {
return &scanRegionBackoffer{
attempt: ScanRegionAttemptTimes,
stat: utils.InitialRetryState(
ScanRegionAttemptTimes,
time.Millisecond*10,
time.Second*2,
),
}
}
// NextBackoff returns a duration to wait before retrying again
func (b *scanRegionBackoffer) NextBackoff(err error) time.Duration {
if berrors.ErrPDBatchScanRegion.Equal(err) {
// 1s * 60 could be enough for splitting remain regions in the hole.
b.attempt--
return time.Second
// it needs more time to wait splitting the regions that contains data in PITR.
// 2s * 150
delayTime := b.stat.ExponentialBackoff()
failpoint.Inject("hint-scan-region-backoff", func(val failpoint.Value) {
if val.(bool) {
delayTime = time.Microsecond
}
})
return delayTime
}
b.attempt = 0
b.stat.StopRetry()
return 0
}
// Attempt returns the remain attempt times
func (b *scanRegionBackoffer) Attempt() int {
return b.attempt
return b.stat.Attempt()
}

View File

@ -0,0 +1,73 @@
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0.
package split_test
import (
"context"
"testing"
"github.com/pingcap/failpoint"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/restore/split"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/stretchr/testify/require"
)
func TestScanRegionBackOfferWithSuccess(t *testing.T) {
var counter int
bo := split.NewScanRegionBackoffer()
err := utils.WithRetry(context.Background(), func() error {
defer func() {
counter++
}()
if counter == 3 {
return nil
}
return berrors.ErrPDBatchScanRegion
}, bo)
require.NoError(t, err)
require.Equal(t, counter, 4)
}
func TestScanRegionBackOfferWithFail(t *testing.T) {
_ = failpoint.Enable("github.com/pingcap/tidb/br/pkg/restore/split/hint-scan-region-backoff", "return(true)")
defer func() {
_ = failpoint.Disable("github.com/pingcap/tidb/br/pkg/restore/split/hint-scan-region-backoff")
}()
var counter int
bo := split.NewScanRegionBackoffer()
err := utils.WithRetry(context.Background(), func() error {
defer func() {
counter++
}()
return berrors.ErrPDBatchScanRegion
}, bo)
require.Error(t, err)
require.Equal(t, counter, split.ScanRegionAttemptTimes)
}
func TestScanRegionBackOfferWithStopRetry(t *testing.T) {
_ = failpoint.Enable("github.com/pingcap/tidb/br/pkg/restore/split/hint-scan-region-backoff", "return(true)")
defer func() {
_ = failpoint.Disable("github.com/pingcap/tidb/br/pkg/restore/split/hint-scan-region-backoff")
}()
var counter int
bo := split.NewScanRegionBackoffer()
err := utils.WithRetry(context.Background(), func() error {
defer func() {
counter++
}()
if counter < 5 {
return berrors.ErrPDBatchScanRegion
}
return berrors.ErrKVUnknown
}, bo)
require.Error(t, err)
require.Equal(t, counter, 6)
}

View File

@ -98,6 +98,10 @@ func (rs *RetryState) Attempt() int {
return rs.maxRetry - rs.retryTimes
}
func (rs *RetryState) StopRetry() {
rs.retryTimes = rs.maxRetry
}
// NextBackoff implements the `Backoffer`.
func (rs *RetryState) NextBackoff(error) time.Duration {
return rs.ExponentialBackoff()