Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: try on each PD member until one succeeds or all fail (#35285) #35508

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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