Skip to content
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

br: add GetTSWithRetry func #38663

Merged
merged 6 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,35 @@ func (rc *Client) GetTS(ctx context.Context) (uint64, error) {
return restoreTS, nil
}

// GetTSWithRetry gets a new timestamp with retry from PD.
func (rc *Client) GetTSWithRetry(ctx context.Context) (uint64, error) {
var (
startTS uint64
getTSErr error
retry uint
)

err := utils.WithRetry(ctx, func() error {
startTS, getTSErr = rc.GetTS(ctx)
failpoint.Inject("get-ts-error", func(val failpoint.Value) {
if val.(bool) && retry < 3 {
getTSErr = errors.Errorf("rpc error: code = Unknown desc = [PD:tso:ErrGenerateTimestamp]generate timestamp failed, requested pd is not leader of cluster")
}
})

retry++
if getTSErr != nil {
log.Warn("failed to get TS, retry it", zap.Uint("retry time", retry), logutil.ShortError(getTSErr))
}
return getTSErr
}, utils.NewPDReqBackoffer())

if err != nil {
log.Error("failed to get TS", zap.Error(err))
}
return startTS, errors.Trace(err)
}

// ResetTS resets the timestamp of PD to a bigger value.
func (rc *Client) ResetTS(ctx context.Context, pdCtrl *pdutil.PdController) error {
restoreTS := rc.backupMeta.GetEndVersion()
Expand Down Expand Up @@ -1350,7 +1379,7 @@ func (rc *Client) execChecksum(
ctx = opentracing.ContextWithSpan(ctx, span1)
}

startTS, err := rc.GetTS(ctx)
startTS, err := rc.GetTSWithRetry(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we replace all rc.GetTS with rc.GetTSWithRetry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe yes. But GetTS is widely used, so I think it needs careful evaluation and testing.

if err != nil {
return errors.Trace(err)
}
Expand Down
43 changes: 43 additions & 0 deletions br/pkg/restore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/kvproto/pkg/import_sstpb"
Expand Down Expand Up @@ -330,12 +331,54 @@ func TestPreCheckTableClusterIndex(t *testing.T) {
type fakePDClient struct {
pd.Client
stores []*metapb.Store

notLeader bool
}

var retryTimes int

func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([]*metapb.Store, error) {
return append([]*metapb.Store{}, fpdc.stores...), nil
}

func (fpdc fakePDClient) GetTS(ctx context.Context) (int64, int64, error) {
retryTimes++
if retryTimes >= 3 { // the mock PD leader switched successfully
fpdc.notLeader = false
}

if fpdc.notLeader {
return 0, 0, errors.Errorf("rpc error: code = Unknown desc = [PD:tso:ErrGenerateTimestamp]generate timestamp failed, requested pd is not leader of cluster")
}
return 1, 1, nil
}

func TestGetTSWithRetry(t *testing.T) {
t.Run("PD leader is healthy:", func(t *testing.T) {
retryTimes = -1000
pDClient := fakePDClient{notLeader: false}
client := restore.NewRestoreClient(pDClient, nil, defaultKeepaliveCfg, false)
_, err := client.GetTSWithRetry(context.Background())
require.NoError(t, err)
})

t.Run("PD leader failure:", func(t *testing.T) {
retryTimes = -1000
pDClient := fakePDClient{notLeader: true}
client := restore.NewRestoreClient(pDClient, nil, defaultKeepaliveCfg, false)
_, err := client.GetTSWithRetry(context.Background())
require.Error(t, err)
})

t.Run("PD leader switch successfully", func(t *testing.T) {
retryTimes = 0
pDClient := fakePDClient{notLeader: true}
client := restore.NewRestoreClient(pDClient, nil, defaultKeepaliveCfg, false)
_, err := client.GetTSWithRetry(context.Background())
require.NoError(t, err)
})
}

func TestPreCheckTableTiFlashReplicas(t *testing.T) {
m := mc
mockStores := []*metapb.Store{
Expand Down