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

api: return not found when region doesn't exist #8869

Merged
merged 4 commits into from
Dec 25, 2024
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
4 changes: 4 additions & 0 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,10 @@ func getRegionByID(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
return
}
if regionID == 0 {
c.String(http.StatusBadRequest, errs.ErrRegionInvalidID.FastGenByArgs().Error())
return
}
regionInfo := svr.GetBasicCluster().GetRegion(regionID)
if regionInfo == nil {
c.String(http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs(regionID).Error())
Expand Down
12 changes: 12 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ func (h *regionHandler) GetRegionByID(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
if regionID == 0 {
h.rd.JSON(w, http.StatusBadRequest, errs.ErrRegionInvalidID.FastGenByArgs())
return
}

regionInfo := rc.GetRegion(regionID)
if regionInfo == nil {
h.rd.JSON(w, http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs(regionID).Error())
return
}
b, err := response.MarshalRegionInfoJSON(r.Context(), regionInfo)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -101,6 +109,10 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) {
}

regionInfo := rc.GetRegionByKey(paramsByte[0])
if regionInfo == nil {
h.rd.JSON(w, http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs().Error())
lhy1024 marked this conversation as resolved.
Show resolved Hide resolved
return
}
b, err := response.MarshalRegionInfoJSON(r.Context(), regionInfo)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down
8 changes: 7 additions & 1 deletion server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (suite *regionTestSuite) TestRegion() {
r.UpdateBuckets(buckets, r.GetBuckets())
re := suite.Require()
mustRegionHeartbeat(re, suite.svr, r)
url := fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, r.GetID())
url := fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, 0)
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusBadRequest)))
url = fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, 2333)
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusNotFound)))
url = fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, r.GetID())
r1 := &response.RegionInfo{}
r1m := make(map[string]any)
re.NoError(tu.ReadGetJSON(re, testDialClient, url, r1))
Expand All @@ -96,6 +100,8 @@ func (suite *regionTestSuite) TestRegion() {
re.Equal(core.HexRegionKeyStr([]byte("a")), keys[0].(string))
re.Equal(core.HexRegionKeyStr([]byte("b")), keys[1].(string))

url = fmt.Sprintf("%s/region/key/%s", suite.urlPrefix, "c")
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusNotFound)))
url = fmt.Sprintf("%s/region/key/%s", suite.urlPrefix, "a")
r2 := &response.RegionInfo{}
re.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,9 @@ func (suite *apiTestSuite) checkRegions(cluster *tests.TestCluster) {
err = testutil.ReadGetJSON(re, tests.TestDialClient, urlPrefix, &resp)
re.NoError(err)
re.Equal(3., resp["count"])
urlPrefix = fmt.Sprintf("%s/scheduling/api/v1/regions/0", scheServerAddr)
testutil.CheckGetJSON(tests.TestDialClient, urlPrefix, nil,
testutil.Status(re, http.StatusBadRequest))
urlPrefix = fmt.Sprintf("%s/scheduling/api/v1/regions/233", scheServerAddr)
testutil.CheckGetJSON(tests.TestDialClient, urlPrefix, nil,
testutil.Status(re, http.StatusNotFound), testutil.StringContain(re, "not found"))
Expand Down
Loading