Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
pd: Add pd request retry logic (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
Relax4Life authored May 10, 2021
1 parent 70b0a35 commit 7c98ff0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/pdutil/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
scheduleConfigPrefix = "pd/api/v1/config/schedule"
pauseTimeout = 5 * time.Minute

// 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
)
Expand Down Expand Up @@ -147,6 +150,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 @@ -168,3 +169,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 7c98ff0

Please sign in to comment.