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

helper: request another PD if one of them is unavailable #35750

Merged
merged 4 commits into from
Jun 28, 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
32 changes: 18 additions & 14 deletions store/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,42 +840,46 @@ func (h *Helper) requestPD(apiName, method, uri string, body io.Reader, res inte
if len(pdHosts) == 0 {
return errors.New("pd unavailable")
}
logutil.BgLogger().Debug("RequestPD URL", zap.String("url", util.InternalHTTPSchema()+"://"+pdHosts[0]+uri))
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)
err = requestPDForOneHost(host, apiName, method, uri, body, res)
if err == nil {
break
}
// Try to request from another PD node when some nodes may down.
tangenta marked this conversation as resolved.
Show resolved Hide resolved
}
return err
}

func requestPDForOneHost(host, apiName, method, uri string, body io.Reader, res interface{}) error {
urlVar := fmt.Sprintf("%s://%s%s", util.InternalHTTPSchema(), host, uri)
logutil.BgLogger().Debug("RequestPD URL", zap.String("url", urlVar))
req, err := http.NewRequest(method, urlVar, body)
if err != nil {
return err
logutil.BgLogger().Warn("requestPDForOneHost new request failed",
zap.String("url", urlVar), zap.Error(err))
return errors.Trace(err)
}
start := time.Now()
resp, err := util.InternalHTTPClient().Do(req)
if err != nil {
metrics.PDAPIRequestCounter.WithLabelValues(apiName, "network error").Inc()
logutil.BgLogger().Warn("requestPDForOneHost do request failed",
zap.String("url", urlVar), zap.Error(err))
return errors.Trace(err)
}
metrics.PDAPIExecutionHistogram.WithLabelValues(apiName).Observe(time.Since(start).Seconds())
metrics.PDAPIRequestCounter.WithLabelValues(apiName, resp.Status).Inc()

defer func() {
err = resp.Body.Close()
if err != nil {
logutil.BgLogger().Error("close body failed", zap.Error(err))
logutil.BgLogger().Warn("requestPDForOneHost close body failed",
zap.String("url", urlVar), zap.Error(err))
}
}()

err = json.NewDecoder(resp.Body).Decode(res)
if err != nil {
return errors.Trace(err)
}

return nil
}

Expand Down