Skip to content

Commit

Permalink
br: wait more time to wait spitting the region (#42182)
Browse files Browse the repository at this point in the history
close #42001
  • Loading branch information
joccau authored Mar 16, 2023
1 parent a8733c0 commit 2ea6d00
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
8 changes: 7 additions & 1 deletion br/pkg/restore/split/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
34 changes: 23 additions & 11 deletions br/pkg/restore/split/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,7 +18,7 @@ import (
)

var (
ScanRegionAttemptTimes = 128
ScanRegionAttemptTimes = 150
)

// Constants for split retry machinery.
Expand Down Expand Up @@ -115,7 +116,7 @@ func PaginateScanRegion(
return err
}
return nil
}, newScanRegionBackoffer())
}, NewScanRegionBackoffer())

return regions, err
}
Expand Down Expand Up @@ -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()
}
73 changes: 73 additions & 0 deletions br/pkg/restore/split/split_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions br/pkg/utils/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2ea6d00

Please sign in to comment.