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

util: add check delete json function #7113

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
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
30 changes: 15 additions & 15 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -531,21 +531,6 @@ error = '''
plugin is not found: %s
'''

["PD:operator:ErrRegionAbnormalPeer"]
error = '''
region %v has abnormal peer
'''

["PD:operator:ErrRegionNotAdjacent"]
error = '''
two regions are not adjacent
'''

["PD:operator:ErrRegionNotFound"]
error = '''
region %v not found
'''

["PD:os:ErrOSOpen"]
error = '''
open error
Expand Down Expand Up @@ -616,6 +601,21 @@ error = '''
failed to unmarshal proto
'''

["PD:region:ErrRegionAbnormalPeer"]
error = '''
region %v has abnormal peer
'''

["PD:region:ErrRegionNotAdjacent"]
error = '''
two regions are not adjacent
'''

["PD:region:ErrRegionNotFound"]
error = '''
region %v not found
'''

["PD:region:ErrRegionRuleContent"]
error = '''
invalid region rule content, %s
Expand Down
4 changes: 2 additions & 2 deletions pkg/autoscaling/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func makeJSONResponse(promResp *response) (*http.Response, []byte, error) {

response := &http.Response{
Status: "200 OK",
StatusCode: 200,
StatusCode: http.StatusOK,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Expand Down Expand Up @@ -246,7 +246,7 @@ func (c *errorHTTPStatusClient) Do(_ context.Context, req *http.Request) (r *htt

r, body, err = makeJSONResponse(promResp)

r.StatusCode = 500
r.StatusCode = http.StatusInternalServerError
r.Status = "500 Internal Server Error"

return
Expand Down
6 changes: 3 additions & 3 deletions pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ var (
// region errors
var (
// ErrRegionNotAdjacent is error info for region not adjacent.
ErrRegionNotAdjacent = errors.Normalize("two regions are not adjacent", errors.RFCCodeText("PD:operator:ErrRegionNotAdjacent"))
ErrRegionNotAdjacent = errors.Normalize("two regions are not adjacent", errors.RFCCodeText("PD:region:ErrRegionNotAdjacent"))
// ErrRegionNotFound is error info for region not found.
ErrRegionNotFound = errors.Normalize("region %v not found", errors.RFCCodeText("PD:operator:ErrRegionNotFound"))
ErrRegionNotFound = errors.Normalize("region %v not found", errors.RFCCodeText("PD:region:ErrRegionNotFound"))
// ErrRegionAbnormalPeer is error info for region has abnormal peer.
ErrRegionAbnormalPeer = errors.Normalize("region %v has abnormal peer", errors.RFCCodeText("PD:operator:ErrRegionAbnormalPeer"))
ErrRegionAbnormalPeer = errors.Normalize("region %v has abnormal peer", errors.RFCCodeText("PD:region:ErrRegionAbnormalPeer"))
)

// plugin errors
Expand Down
14 changes: 8 additions & 6 deletions pkg/tso/keyspace_group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,16 +1226,17 @@ func (kgm *KeyspaceGroupManager) finishSplitKeyspaceGroup(id uint32) error {
return nil
}
startRequest := time.Now()
statusCode, err := apiutil.DoDelete(
resp, err := apiutil.DoDelete(
kgm.httpClient,
kgm.cfg.GeBackendEndpoints()+keyspaceGroupsAPIPrefix+fmt.Sprintf("/%d/split", id))
if err != nil {
return err
}
if statusCode != http.StatusOK {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Warn("failed to finish split keyspace group",
zap.Uint32("keyspace-group-id", id),
zap.Int("status-code", statusCode))
zap.Int("status-code", resp.StatusCode))
return errs.ErrSendRequest.FastGenByArgs()
}
kgm.metrics.finishSplitSendDuration.Observe(time.Since(startRequest).Seconds())
Expand Down Expand Up @@ -1264,16 +1265,17 @@ func (kgm *KeyspaceGroupManager) finishMergeKeyspaceGroup(id uint32) error {
return nil
}
startRequest := time.Now()
statusCode, err := apiutil.DoDelete(
resp, err := apiutil.DoDelete(
kgm.httpClient,
kgm.cfg.GeBackendEndpoints()+keyspaceGroupsAPIPrefix+fmt.Sprintf("/%d/merge", id))
if err != nil {
return err
}
if statusCode != http.StatusOK {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Warn("failed to finish merging keyspace group",
zap.Uint32("keyspace-group-id", id),
zap.Int("status-code", statusCode))
zap.Int("status-code", resp.StatusCode))
return errs.ErrSendRequest.FastGenByArgs()
}
kgm.metrics.finishMergeSendDuration.Observe(time.Since(startRequest).Seconds())
Expand Down
11 changes: 3 additions & 8 deletions pkg/utils/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,12 @@ func PostJSONIgnoreResp(client *http.Client, url string, data []byte) error {
}

// DoDelete is used to send delete request and return http response code.
func DoDelete(client *http.Client, url string) (int, error) {
func DoDelete(client *http.Client, url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return http.StatusBadRequest, err
}
res, err := client.Do(req)
if err != nil {
return 0, err
return nil, err
}
defer res.Body.Close()
return res.StatusCode, nil
return client.Do(req)
}

func checkResponse(resp *http.Response, err error) error {
Expand Down
11 changes: 10 additions & 1 deletion pkg/utils/testutil/api_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,18 @@ func CheckPatchJSON(client *http.Client, url string, data []byte, checkOpts ...f
return checkResp(resp, checkOpts...)
}

// CheckDelete is used to do delete request and do check options.
func CheckDelete(client *http.Client, url string, checkOpts ...func([]byte, int, http.Header)) error {
resp, err := apiutil.DoDelete(client, url)
if err != nil {
return err
}
return checkResp(resp, checkOpts...)
}

func checkResp(resp *http.Response, checkOpts ...func([]byte, int, http.Header)) error {
res, err := io.ReadAll(resp.Body)
resp.Body.Close()
defer resp.Body.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seem no need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems so.

if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions server/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
)
Expand Down Expand Up @@ -271,9 +270,8 @@ func (suite *adminTestSuite) TestMarkSnapshotRecovering() {
suite.NoError(err2)
suite.True(resp.Marked)
// unmark
code, err := apiutil.DoDelete(testDialClient, url)
err := tu.CheckDelete(testDialClient, url, tu.StatusOK(re))
suite.NoError(err)
suite.Equal(200, code)
suite.NoError(tu.CheckGetJSON(testDialClient, url, nil,
tu.StatusOK(re), tu.StringContain(re, "false")))
}
Expand Down Expand Up @@ -310,9 +308,8 @@ func (suite *adminTestSuite) TestRecoverAllocID() {
suite.NoError(err2)
suite.Equal(id, uint64(99000001))
// unmark
code, err := apiutil.DoDelete(testDialClient, markRecoveringURL)
err := tu.CheckDelete(testDialClient, markRecoveringURL, tu.StatusOK(re))
suite.NoError(err)
suite.Equal(200, code)
suite.NoError(tu.CheckGetJSON(testDialClient, markRecoveringURL, nil,
tu.StatusOK(re), tu.StringContain(re, "false")))
suite.NoError(tu.CheckPostJSON(testDialClient, url, []byte(`{"id": "100000"}`),
Expand Down
3 changes: 1 addition & 2 deletions server/api/diagnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/schedule/schedulers"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/config"
Expand Down Expand Up @@ -129,7 +128,7 @@ func (suite *diagnosticTestSuite) TestSchedulerDiagnosticAPI() {
suite.checkStatus("normal", balanceRegionURL)

deleteURL := fmt.Sprintf("%s/%s", suite.schedulerPrifex, schedulers.BalanceRegionName)
_, err = apiutil.DoDelete(testDialClient, deleteURL)
err = tu.CheckDelete(testDialClient, deleteURL, tu.StatusOK(re))
suite.NoError(err)
suite.checkStatus("disabled", balanceRegionURL)
}
9 changes: 5 additions & 4 deletions server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/tikv/pd/pkg/mock/mockhbstream"
pdoperator "github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/versioninfo"
"github.com/tikv/pd/server"
Expand Down Expand Up @@ -99,7 +98,7 @@ func (suite *operatorTestSuite) TestAddRemovePeer() {
suite.Contains(operator, "add learner peer 1 on store 3")
suite.Contains(operator, "RUNNING")

_, err = apiutil.DoDelete(testDialClient, regionURL)
err = tu.CheckDelete(testDialClient, regionURL, tu.StatusOK(re))
suite.NoError(err)
records = mustReadURL(re, recordURL)
suite.Contains(records, "admin-add-peer {add peer: store [3]}")
Expand All @@ -110,7 +109,7 @@ func (suite *operatorTestSuite) TestAddRemovePeer() {
suite.Contains(operator, "RUNNING")
suite.Contains(operator, "remove peer on store 2")

_, err = apiutil.DoDelete(testDialClient, regionURL)
err = tu.CheckDelete(testDialClient, regionURL, tu.StatusOK(re))
suite.NoError(err)
records = mustReadURL(re, recordURL)
suite.Contains(records, "admin-remove-peer {rm peer: store [2]}")
Expand Down Expand Up @@ -406,8 +405,10 @@ func (suite *transferRegionOperatorTestSuite) TestTransferRegionWithPlacementRul
if len(testCase.expectSteps) > 0 {
operator = mustReadURL(re, regionURL)
suite.Contains(operator, testCase.expectSteps)
err = tu.CheckDelete(testDialClient, regionURL, tu.StatusOK(re))
} else {
err = tu.CheckDelete(testDialClient, regionURL, tu.StatusNotOK(re))
}
_, err = apiutil.DoDelete(testDialClient, regionURL)
suite.NoError(err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions server/api/region_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pingcap/failpoint"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/schedule/labeler"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
)
Expand Down Expand Up @@ -86,7 +85,7 @@ func (suite *regionLabelTestSuite) TestGetSet() {
expects := []*labeler.LabelRule{rules[0], rules[2]}
suite.Equal(expects, resp)

_, err = apiutil.DoDelete(testDialClient, suite.urlPrefix+"rule/"+url.QueryEscape("rule2/a/b"))
err = tu.CheckDelete(testDialClient, suite.urlPrefix+"rule/"+url.QueryEscape("rule2/a/b"), tu.StatusOK(re))
suite.NoError(err)
err = tu.ReadGetJSON(re, testDialClient, suite.urlPrefix+"rules", &resp)
suite.NoError(err)
Expand Down
12 changes: 5 additions & 7 deletions server/api/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/config"
Expand Down Expand Up @@ -202,13 +201,13 @@ func (suite *ruleTestSuite) TestGet() {
name: "found",
rule: rule,
found: true,
code: 200,
code: http.StatusOK,
},
{
name: "not found",
rule: placement.Rule{GroupID: "a", ID: "30", StartKeyHex: "1111", EndKeyHex: "3333", Role: "voter", Count: 1},
found: false,
code: 404,
code: http.StatusNotFound,
},
}
for _, testCase := range testCases {
Expand Down Expand Up @@ -533,9 +532,8 @@ func (suite *ruleTestSuite) TestDelete() {
url := fmt.Sprintf("%s/rule/%s/%s", suite.urlPrefix, testCase.groupID, testCase.id)
// clear suspect keyRanges to prevent test case from others
suite.svr.GetRaftCluster().ClearSuspectKeyRanges()
statusCode, err := apiutil.DoDelete(testDialClient, url)
err = tu.CheckDelete(testDialClient, url, tu.StatusOK(suite.Require()))
suite.NoError(err)
suite.Equal(http.StatusOK, statusCode)
if len(testCase.popKeyRange) > 0 {
popKeyRangeMap := map[string]struct{}{}
for i := 0; i < len(testCase.popKeyRange)/2; i++ {
Expand Down Expand Up @@ -726,7 +724,7 @@ func (suite *ruleTestSuite) TestBundle() {
suite.compareBundle(bundles[1], b2)

// Delete
_, err = apiutil.DoDelete(testDialClient, suite.urlPrefix+"/placement-rule/pd")
err = tu.CheckDelete(testDialClient, suite.urlPrefix+"/placement-rule/pd", tu.StatusOK(suite.Require()))
suite.NoError(err)

// GetAll again
Expand All @@ -753,7 +751,7 @@ func (suite *ruleTestSuite) TestBundle() {
suite.compareBundle(bundles[2], b3)

// Delete using regexp
_, err = apiutil.DoDelete(testDialClient, suite.urlPrefix+"/placement-rule/"+url.PathEscape("foo.*")+"?regexp")
err = tu.CheckDelete(testDialClient, suite.urlPrefix+"/placement-rule/"+url.PathEscape("foo.*")+"?regexp", tu.StatusOK(suite.Require()))
suite.NoError(err)

// GetAll again
Expand Down
7 changes: 4 additions & 3 deletions server/api/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,13 @@ func (h *schedulerHandler) redirectSchedulerDelete(w http.ResponseWriter, name,
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}
statusCode, err := apiutil.DoDelete(h.svr.GetHTTPClient(), deleteURL)
resp, err := apiutil.DoDelete(h.svr.GetHTTPClient(), deleteURL)
if err != nil {
h.r.JSON(w, statusCode, err.Error())
h.r.JSON(w, resp.StatusCode, err.Error())
return
}
h.r.JSON(w, statusCode, nil)
defer resp.Body.Close()
h.r.JSON(w, resp.StatusCode, nil)
}

// FIXME: details of input json body params
Expand Down
Loading
Loading