Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#1088
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Relax4Life authored and ti-chi-bot committed May 10, 2021
1 parent fd8d947 commit c13c526
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/pdutil/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const (
maxMsgSize = int(128 * utils.MB) // pd.ScanRegion may return a large response
scheduleConfigPrefix = "pd/api/v1/config/schedule"
pauseTimeout = 5 * time.Minute
<<<<<<< HEAD
=======

// pd request retry time when connection fail
pdRequestRetryTime = 10

// set max-pending-peer-count to a large value to avoid scatter region failed.
maxPendingPeerUnlimited uint64 = math.MaxInt32
>>>>>>> 7c98ff04 (pd: Add pd request retry logic (#1088))
)

type pauseConfigExpectation uint8
Expand Down Expand Up @@ -126,6 +135,19 @@ func pdRequest(
if err != nil {
return nil, errors.Trace(err)
}
count := 0
for {
count++
if count > pdRequestRetryTime || resp.StatusCode < 500 {
break
}
resp.Body.Close()
time.Sleep(time.Second)
resp, err = cli.Do(req)
if err != nil {
return nil, errors.Trace(err)
}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
res, _ := ioutil.ReadAll(resp.Body)
Expand Down
32 changes: 32 additions & 0 deletions pkg/pdutil/pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"

Expand Down Expand Up @@ -167,3 +168,34 @@ func (s *testPDControllerSuite) TestPDVersion(c *C) {
c.Assert(r.Minor, Equals, expectV.Minor)
c.Assert(r.PreRelease, Equals, expectV.PreRelease)
}

func (s *testPDControllerSuite) TestPDRequestRetry(c *C) {
ctx := context.Background()
count := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count++
if count <= 5 {
w.WriteHeader(http.StatusGatewayTimeout)
return
}
w.WriteHeader(http.StatusOK)
}))
cli := http.DefaultClient
taddr := ts.URL
_, reqErr := pdRequest(ctx, taddr, "", cli, http.MethodGet, nil)
c.Assert(reqErr, IsNil)
ts.Close()
count = 0
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count++
if count <= 11 {
w.WriteHeader(http.StatusGatewayTimeout)
return
}
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
taddr = ts.URL
_, reqErr = pdRequest(ctx, taddr, "", cli, http.MethodGet, nil)
c.Assert(reqErr, NotNil)
}

0 comments on commit c13c526

Please sign in to comment.