Skip to content

Commit

Permalink
errors,version(ticdc): refine ErrCheckClusterVersionFromPD error (#4435)
Browse files Browse the repository at this point in the history
close #4341
  • Loading branch information
overvenus authored Jan 25, 2022
1 parent 180fa4e commit f51e410
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ changefeed update error: %s

["CDC:ErrCheckClusterVersionFromPD"]
error = '''
failed to request PD
failed to request PD %s, please try again later
'''

["CDC:ErrCheckDataDirSatisfied"]
Expand Down
2 changes: 1 addition & 1 deletion pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var (

// utilities related errors
ErrToTLSConfigFailed = errors.Normalize("generate tls config failed", errors.RFCCodeText("CDC:ErrToTLSConfigFailed"))
ErrCheckClusterVersionFromPD = errors.Normalize("failed to request PD", errors.RFCCodeText("CDC:ErrCheckClusterVersionFromPD"))
ErrCheckClusterVersionFromPD = errors.Normalize("failed to request PD %s, please try again later", errors.RFCCodeText("CDC:ErrCheckClusterVersionFromPD"))
ErrNewSemVersion = errors.Normalize("create sem version", errors.RFCCodeText("CDC:ErrNewSemVersion"))
ErrCheckDirWritable = errors.Normalize("check dir writable failed", errors.RFCCodeText("CDC:ErrCheckDirWritable"))
ErrCheckDirReadable = errors.Normalize("check dir readable failed", errors.RFCCodeText("CDC:ErrCheckDirReadable"))
Expand Down
21 changes: 11 additions & 10 deletions pkg/version/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,29 @@ func CheckPDVersion(ctx context.Context, pdAddr string, credential *security.Cre
req, err := http.NewRequestWithContext(
ctx, http.MethodGet, fmt.Sprintf("%s/pd/api/v1/version", pdAddr), nil)
if err != nil {
return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err)
return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err)
}

resp, err := httpClient.Do(req)
if err != nil {
return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err)
return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err)
}
defer resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
arg := fmt.Sprintf("response status: %s", resp.Status)
return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(arg)
}

content, err := io.ReadAll(resp.Body)
if err != nil {
return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err)
if err != nil || resp.StatusCode < 200 || resp.StatusCode >= 300 {
var arg string
if err != nil {
arg = fmt.Sprintf("%s %s %s", resp.Status, content, err)
} else {
arg = fmt.Sprintf("%s %s", resp.Status, content)
}
return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(arg)
}

err = json.Unmarshal(content, &pdVer)
if err != nil {
return cerror.WrapError(cerror.ErrCheckClusterVersionFromPD, err)
return cerror.ErrCheckClusterVersionFromPD.GenWithStackByArgs(err)
}

ver, err := semver.NewVersion(removeVAndHash(pdVer.Version))
Expand Down
20 changes: 19 additions & 1 deletion pkg/version/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
Expand Down Expand Up @@ -171,7 +172,7 @@ func TestCheckClusterVersion(t *testing.T) {
}

err := CheckClusterVersion(context.Background(), &mock, pdAddrs, nil, false)
require.Regexp(t, ".*response status: .*", err)
require.Regexp(t, ".*400 Bad Request.*", err)
}
}

Expand Down Expand Up @@ -361,3 +362,20 @@ func TestCheckTiCDCClusterVersion(t *testing.T) {
}
}
}

func TestCheckPDVersionError(t *testing.T) {
t.Parallel()

var resp func(w http.ResponseWriter, r *http.Request)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp(w, r)
}))
defer ts.Close()

resp = func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
require.Contains(t, CheckPDVersion(context.TODO(), ts.URL, nil).Error(),
"[CDC:ErrCheckClusterVersionFromPD]failed to request PD 500 Internal Server Error , please try again later",
)
}
2 changes: 1 addition & 1 deletion tools/check/check-errdoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ set -euo pipefail
cd -P .

cp errors.toml /tmp/errors.toml.before
./tools/bin/errdoc-gen --source . --module github.com/pingcap/tiflow --output errors.toml --ignore proto,dm
./tools/bin/errdoc-gen --source . --module github.com/pingcap/tiflow --output errors.toml --ignore proto,dm,deployments
diff -q errors.toml /tmp/errors.toml.before

0 comments on commit f51e410

Please sign in to comment.