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: use suitable status code and make TestResetTS stable #7368

Merged
merged 8 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ func ResetTS(c *gin.Context) {
if err = svr.ResetTS(ts, ignoreSmaller, skipUpperBoundCheck, 0); err != nil {
if err == errs.ErrServerNotStarted {
c.String(http.StatusInternalServerError, err.Error())
} else if err == errs.ErrEtcdTxnConflict {
Copy link
Member

Choose a reason for hiding this comment

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

This is not only for test? Because it will effect the resp to users.
I want to know whether this PR needs to cherry-pick.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, not only for test. But I am not sure whether it is a bug to need cherry-pick.

Copy link
Member

Choose a reason for hiding this comment

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

If it's only for TSO services, IMO we don't need to.

Copy link
Contributor Author

@lhy1024 lhy1024 Nov 16, 2023

Choose a reason for hiding this comment

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

PD mode also returns 403 when meeting ErrEtcdTxnConflict before this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After discussion with @niubell, we will only fix it on master branch.

c.String(http.StatusServiceUnavailable, err.Error())
} else {
c.String(http.StatusForbidden, err.Error())
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/tso/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (h *AdminHandler) ResetTS(w http.ResponseWriter, r *http.Request) {
if err = handler.ResetTS(ts, ignoreSmaller, skipUpperBoundCheck, 0); err != nil {
if err == errs.ErrServerNotStarted {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
} else if err == errs.ErrEtcdTxnConflict {
h.rd.JSON(w, http.StatusServiceUnavailable, err.Error())
} else {
h.rd.JSON(w, http.StatusForbidden, err.Error())
}
Expand Down
23 changes: 20 additions & 3 deletions server/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"time"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/replication"
"github.com/tikv/pd/pkg/utils/apiutil"
tu "github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
)
Expand Down Expand Up @@ -188,9 +190,24 @@ func (suite *adminTestSuite) TestResetTS() {
values, err := json.Marshal(args)
suite.NoError(err)
re := suite.Require()
err = tu.CheckPostJSON(testDialClient, url, values,
tu.StatusOK(re),
tu.StringEqual(re, "\"Reset ts successfully.\"\n"))
tu.Eventually(re, func() bool {
resp, err := apiutil.PostJSON(testDialClient, url, values)
re.NoError(err)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
re.NoError(err)
switch resp.StatusCode {
case http.StatusOK:
re.Contains(string(b), "Reset ts successfully.")
return true
case http.StatusServiceUnavailable:
re.Contains(string(b), "[PD:etcd:ErrEtcdTxnConflict]etcd transaction failed, conflicted and rolled back")
return false
default:
re.FailNow("unexpected status code %d", resp.StatusCode)
return false
}
})
suite.NoError(err)
t2 := makeTS(32 * time.Hour)
args["tso"] = fmt.Sprintf("%d", t2)
Expand Down
Loading