Skip to content

Commit 3697338

Browse files
rickifti-chi-bot
andauthored
api: fix resign myself error (#3560)
* api: fix resign myself error Signed-off-by: ricky <rickif@qq.com> * api: change resign response Signed-off-by: ricky <rickif@qq.com> Co-authored-by: Ti Chi Robot <71242396+ti-chi-bot@users.noreply.github.com>
1 parent 471b3da commit 3697338

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

server/api/member.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func newLeaderHandler(svr *server.Server, rd *render.Render) *leaderHandler {
275275
// @Tags leader
276276
// @Summary Get the leader PD server of the cluster.
277277
// @Produce json
278-
// @Success 200 {string} string "The transfer command is submitted."
278+
// @Success 200 {object} pdpb.Member
279279
// @Router /leader [get]
280280
func (h *leaderHandler) Get(w http.ResponseWriter, r *http.Request) {
281281
h.rd.JSON(w, http.StatusOK, h.svr.GetLeader())
@@ -284,7 +284,7 @@ func (h *leaderHandler) Get(w http.ResponseWriter, r *http.Request) {
284284
// @Tags leader
285285
// @Summary Transfer etcd leadership to another PD server.
286286
// @Produce json
287-
// @Success 200 {string} string "The transfer command is submitted."
287+
// @Success 200 {string} string "The resign command is submitted."
288288
// @Failure 500 {string} string "PD server failed to proceed the request."
289289
// @Router /leader/resign [post]
290290
func (h *leaderHandler) Resign(w http.ResponseWriter, r *http.Request) {
@@ -294,7 +294,7 @@ func (h *leaderHandler) Resign(w http.ResponseWriter, r *http.Request) {
294294
return
295295
}
296296

297-
h.rd.JSON(w, http.StatusOK, "The transfer command is submitted.")
297+
h.rd.JSON(w, http.StatusOK, "The resign command is submitted.")
298298
}
299299

300300
// @Tags leader

server/api/member_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"encoding/json"
1919
"fmt"
20+
"io"
2021
"io/ioutil"
2122
"math/rand"
2223
"net/http"
@@ -30,6 +31,7 @@ import (
3031
)
3132

3233
var _ = Suite(&testMemberAPISuite{})
34+
var _ = Suite(&testResignAPISuite{})
3335

3436
type testMemberAPISuite struct {
3537
cfgs []*config.Config
@@ -147,3 +149,26 @@ func changeLeaderPeerUrls(c *C, leader *pdpb.Member, id uint64, urls []string) {
147149
c.Assert(err, IsNil)
148150
c.Assert(resp.StatusCode, Equals, 204)
149151
}
152+
153+
type testResignAPISuite struct {
154+
cfgs []*config.Config
155+
servers []*server.Server
156+
clean func()
157+
}
158+
159+
func (s *testResignAPISuite) SetUpSuite(c *C) {
160+
s.cfgs, s.servers, s.clean = mustNewCluster(c, 1)
161+
}
162+
163+
func (s *testResignAPISuite) TearDownSuite(c *C) {
164+
s.clean()
165+
}
166+
167+
func (s *testResignAPISuite) TestResignMyself(c *C) {
168+
addr := s.cfgs[0].ClientUrls + apiPrefix + "/api/v1/leader/resign"
169+
resp, err := testDialClient.Post(addr, "", nil)
170+
c.Assert(err, IsNil)
171+
c.Assert(resp.StatusCode, Equals, http.StatusOK)
172+
_, _ = io.Copy(ioutil.Discard, resp.Body)
173+
resp.Body.Close()
174+
}

server/member/member.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ func (m *Member) ResignEtcdLeader(ctx context.Context, from string, nextEtcdLead
275275
if err != nil {
276276
return err
277277
}
278+
279+
// Do nothing when I am the only member of cluster.
280+
if len(res.Members) == 1 && res.Members[0].ID == m.id && nextEtcdLeader == "" {
281+
return nil
282+
}
283+
278284
for _, member := range res.Members {
279285
if (nextEtcdLeader == "" && member.ID != m.id) || (nextEtcdLeader != "" && member.Name == nextEtcdLeader) {
280286
etcdLeaderIDs = append(etcdLeaderIDs, member.GetID())

0 commit comments

Comments
 (0)