Skip to content

Commit

Permalink
store: requst another PD node if some of them is down (pingcap#26326)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored Jul 19, 2021
1 parent 07c804b commit cf2ab29
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
76 changes: 16 additions & 60 deletions store/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,35 +222,9 @@ func (h *Helper) ScrapeHotInfo(rw string, allSchemas []*model.DBInfo) ([]HotTabl

// FetchHotRegion fetches the hot region information from PD's http api.
func (h *Helper) FetchHotRegion(rw string) (map[uint64]RegionMetric, error) {
etcd, ok := h.Store.(kv.EtcdBackend)
if !ok {
return nil, errors.WithStack(errors.New("not implemented"))
}
pdHosts, err := etcd.EtcdAddrs()
if err != nil {
return nil, err
}
if len(pdHosts) == 0 {
return nil, errors.New("pd unavailable")
}
req, err := http.NewRequest("GET", util.InternalHTTPSchema()+"://"+pdHosts[0]+rw, nil)
if err != nil {
return nil, errors.Trace(err)
}
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return nil, errors.Trace(err)
}
defer func() {
err = resp.Body.Close()
if err != nil {
logutil.BgLogger().Error("close body failed", zap.Error(err))
}
}()
var regionResp StoreHotRegionInfos
err = json.NewDecoder(resp.Body).Decode(&regionResp)
if err != nil {
return nil, errors.Trace(err)
if err := h.requestPD("GET", rw, nil, &regionResp); err != nil {
return nil, err
}
metricCnt := 0
for _, hotRegions := range regionResp.AsLeader {
Expand Down Expand Up @@ -777,9 +751,19 @@ func (h *Helper) requestPD(method, uri string, body io.Reader, res interface{})
return errors.New("pd unavailable")
}
logutil.BgLogger().Debug("RequestPD URL", zap.String("url", util.InternalHTTPSchema()+"://"+pdHosts[0]+uri))
req, err := http.NewRequest(method, util.InternalHTTPSchema()+"://"+pdHosts[0]+uri, body)
req := new(http.Request)
for _, host := range pdHosts {
req, err = http.NewRequest(method, util.InternalHTTPSchema()+"://"+host+uri, body)
if err != nil {
// Try to request from another PD node when some nodes may down.
if strings.Contains(err.Error(), "connection refused") {
continue
}
return errors.Trace(err)
}
}
if err != nil {
return errors.Trace(err)
return err
}
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
Expand Down Expand Up @@ -851,37 +835,9 @@ type StoreDetailStat struct {

// GetStoresStat gets the TiKV store information by accessing PD's api.
func (h *Helper) GetStoresStat() (*StoresStat, error) {
etcd, ok := h.Store.(kv.EtcdBackend)
if !ok {
return nil, errors.WithStack(errors.New("not implemented"))
}
pdHosts, err := etcd.EtcdAddrs()
if err != nil {
return nil, err
}
if len(pdHosts) == 0 {
return nil, errors.New("pd unavailable")
}
req, err := http.NewRequest("GET", util.InternalHTTPSchema()+"://"+pdHosts[0]+pdapi.Stores, nil)
if err != nil {
return nil, errors.Trace(err)
}
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return nil, errors.Trace(err)
}
defer func() {
err = resp.Body.Close()
if err != nil {
logutil.BgLogger().Error("close body failed", zap.Error(err))
}
}()
var storesStat StoresStat
err = json.NewDecoder(resp.Body).Decode(&storesStat)
if err != nil {
return nil, errors.Trace(err)
}
return &storesStat, nil
err := h.requestPD("GET", pdapi.Stores, nil, &storesStat)
return &storesStat, err
}

// GetPDAddr return the PD Address.
Expand Down
2 changes: 1 addition & 1 deletion store/helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *HelperTestSuite) SetUpSuite(c *C) {

s.store = &mockStore{
mockTikvStore.(helper.Storage),
[]string{url[len("http://"):]},
[]string{"invalid_pd_address", url[len("http://"):]},
}
c.Assert(err, IsNil)
}
Expand Down

0 comments on commit cf2ab29

Please sign in to comment.