Skip to content

Commit

Permalink
br: fix restore full fail when restore cluster <= 6.2 using br 6.3 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
D3Hunter authored Sep 22, 2022
1 parent 59f6a53 commit 6a0638b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
30 changes: 22 additions & 8 deletions br/pkg/pdutil/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,26 @@ func pdRequest(
ctx context.Context,
addr string, prefix string,
cli *http.Client, method string, body io.Reader) ([]byte, error) {
_, respBody, err := pdRequestWithCode(ctx, addr, prefix, cli, method, body)
return respBody, err
}

func pdRequestWithCode(
ctx context.Context,
addr string, prefix string,
cli *http.Client, method string, body io.Reader) (int, []byte, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, errors.Trace(err)
return 0, nil, errors.Trace(err)
}
reqURL := fmt.Sprintf("%s/%s", u, prefix)
req, err := http.NewRequestWithContext(ctx, method, reqURL, body)
if err != nil {
return nil, errors.Trace(err)
return 0, nil, errors.Trace(err)
}
resp, err := cli.Do(req)
if err != nil {
return nil, errors.Trace(err)
return 0, nil, errors.Trace(err)
}
count := 0
for {
Expand All @@ -170,7 +178,7 @@ func pdRequest(
time.Sleep(pdRequestRetryInterval())
resp, err = cli.Do(req)
if err != nil {
return nil, errors.Trace(err)
return 0, nil, errors.Trace(err)
}
}
defer func() {
Expand All @@ -179,14 +187,14 @@ func pdRequest(

if resp.StatusCode != http.StatusOK {
res, _ := io.ReadAll(resp.Body)
return nil, errors.Annotatef(berrors.ErrPDInvalidResponse, "[%d] %s %s", resp.StatusCode, res, reqURL)
return resp.StatusCode, nil, errors.Annotatef(berrors.ErrPDInvalidResponse, "[%d] %s %s", resp.StatusCode, res, reqURL)
}

r, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
return resp.StatusCode, nil, errors.Trace(err)
}
return r, nil
return resp.StatusCode, r, nil
}

func pdRequestRetryInterval() time.Duration {
Expand Down Expand Up @@ -841,8 +849,14 @@ func (p *PdController) ResetTS(ctx context.Context, ts uint64) error {
})
var err error
for _, addr := range p.addrs {
_, e := pdRequest(ctx, addr, resetTSPrefix, p.cli, http.MethodPost, bytes.NewBuffer(reqData))
code, _, e := pdRequestWithCode(ctx, addr, resetTSPrefix, p.cli, http.MethodPost, bytes.NewBuffer(reqData))
if e != nil {
// for pd version <= 6.2, if the given ts < current ts of pd, pd returns StatusForbidden.
// it's not an error for br
if code == http.StatusForbidden {
log.Info("reset-ts returns with status forbidden, ignore")
return nil
}
log.Warn("failed to reset ts", zap.Uint64("ts", ts), zap.String("addr", addr), zap.Error(e))
err = e
continue
Expand Down
19 changes: 19 additions & 0 deletions br/pkg/pdutil/pd_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ func TestPDRequestRetry(t *testing.T) {
require.Error(t, reqErr)
}

func TestPDResetTSCompatibility(t *testing.T) {
ctx := context.Background()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
}))
defer ts.Close()
pd := PdController{addrs: []string{ts.URL}, cli: http.DefaultClient}
reqErr := pd.ResetTS(ctx, 123)
require.NoError(t, reqErr)

ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts2.Close()
pd = PdController{addrs: []string{ts2.URL}, cli: http.DefaultClient}
reqErr = pd.ResetTS(ctx, 123)
require.NoError(t, reqErr)
}

func TestStoreInfo(t *testing.T) {
storeInfo := pdtypes.StoreInfo{
Status: &pdtypes.StoreStatus{
Expand Down
1 change: 1 addition & 0 deletions br/pkg/task/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_library(
"//br/pkg/utils",
"//br/pkg/version",
"//config",
"//ddl",
"//kv",
"//parser/model",
"//parser/mysql",
Expand Down
2 changes: 1 addition & 1 deletion executor/kvtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ go_test(
"kv_test.go",
"main_test.go",
],
race = "on",
flaky = True,
race = "on",
deps = [
"//config",
"//meta/autoid",
Expand Down

0 comments on commit 6a0638b

Please sign in to comment.