Skip to content

Commit

Permalink
cherry pick #35285 to release-6.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
JmPotato authored and ti-srebot committed Jun 20, 2022
1 parent 1a89dec commit b2e0f58
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/stmtsummary"
"go.uber.org/zap"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/placement"
Expand Down Expand Up @@ -1645,18 +1647,33 @@ func GetPDServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
if err != nil {
return nil, errors.Trace(err)
}
var servers = make([]ServerInfo, 0, len(members))
// TODO: maybe we should unify the PD API request interface.
var (
memberNum = len(members)
servers = make([]ServerInfo, 0, memberNum)
errs = make([]error, 0, memberNum)
)
if memberNum == 0 {
return servers, nil
}
// Try on each member until one succeeds or all fail.
for _, addr := range members {
// Get PD version, git_hash
url := fmt.Sprintf("%s://%s%s", util.InternalHTTPSchema(), addr, pdapi.Status)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Trace(err)
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
logutil.BgLogger().Warn("create pd server info request error", zap.String("url", url), zap.Error(err))
errs = append(errs, err)
continue
}
req.Header.Add("PD-Allow-follower-handle", "true")
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
return nil, errors.Trace(err)
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
logutil.BgLogger().Warn("request pd server info error", zap.String("url", url), zap.Error(err))
errs = append(errs, err)
continue
}
var content = struct {
Version string `json:"version"`
Expand All @@ -1666,7 +1683,10 @@ func GetPDServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
err = json.NewDecoder(resp.Body).Decode(&content)
terror.Log(resp.Body.Close())
if err != nil {
return nil, errors.Trace(err)
ctx.GetSessionVars().StmtCtx.AppendWarning(err)
logutil.BgLogger().Warn("close pd server info request error", zap.String("url", url), zap.Error(err))
errs = append(errs, err)
continue
}
if len(content.Version) > 0 && content.Version[0] == 'v' {
content.Version = content.Version[1:]
Expand All @@ -1681,6 +1701,17 @@ func GetPDServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
StartTimestamp: content.StartTimestamp,
})
}
// Return the errors if all members' requests fail.
if len(errs) == memberNum {
errorMsg := ""
for idx, err := range errs {
errorMsg += err.Error()
if idx < memberNum-1 {
errorMsg += "; "
}
}
return nil, errors.Trace(fmt.Errorf("%s", errorMsg))
}
return servers, nil
}

Expand Down

0 comments on commit b2e0f58

Please sign in to comment.