|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +package api |
| 22 | + |
| 23 | +import ( |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/apache/kvrocks-controller/consts" |
| 29 | + "github.com/apache/kvrocks-controller/logger" |
| 30 | + "github.com/apache/kvrocks-controller/server/helper" |
| 31 | + "github.com/apache/kvrocks-controller/store/engine/raft" |
| 32 | + |
| 33 | + "github.com/gin-gonic/gin" |
| 34 | + "go.uber.org/zap" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + OperationAdd = "add" |
| 39 | + OperationRemove = "remove" |
| 40 | +) |
| 41 | + |
| 42 | +type RaftHandler struct{} |
| 43 | + |
| 44 | +type MemberRequest struct { |
| 45 | + ID uint64 `json:"id" validate:"required,gt=0"` |
| 46 | + Operation string `json:"operation" validate:"required"` |
| 47 | + Peer string `json:"peer"` |
| 48 | +} |
| 49 | + |
| 50 | +func (r *MemberRequest) validate() error { |
| 51 | + r.Operation = strings.ToLower(r.Operation) |
| 52 | + if r.Operation != OperationAdd && r.Operation != OperationRemove { |
| 53 | + return fmt.Errorf("operation must be one of [%s]", |
| 54 | + strings.Join([]string{OperationAdd, OperationRemove}, ",")) |
| 55 | + } |
| 56 | + if r.Operation == OperationAdd && r.Peer == "" { |
| 57 | + return fmt.Errorf("peer should NOT be empty") |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (handler *RaftHandler) ListPeers(c *gin.Context) { |
| 63 | + raftNode, _ := c.MustGet(consts.ContextKeyRaftNode).(*raft.Node) |
| 64 | + helper.ResponseOK(c, gin.H{ |
| 65 | + "leader": raftNode.GetRaftLead(), |
| 66 | + "peers": raftNode.ListPeers(), |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func (handler *RaftHandler) UpdatePeer(c *gin.Context) { |
| 71 | + var req MemberRequest |
| 72 | + if err := c.BindJSON(&req); err != nil { |
| 73 | + helper.ResponseBadRequest(c, err) |
| 74 | + return |
| 75 | + } |
| 76 | + if err := req.validate(); err != nil { |
| 77 | + helper.ResponseBadRequest(c, err) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + raftNode, _ := c.MustGet(consts.ContextKeyRaftNode).(*raft.Node) |
| 82 | + peers := raftNode.ListPeers() |
| 83 | + |
| 84 | + var err error |
| 85 | + if req.Operation == OperationAdd { |
| 86 | + for _, peer := range peers { |
| 87 | + if peer == req.Peer { |
| 88 | + helper.ResponseError(c, fmt.Errorf("peer '%s' already exists", req.Peer)) |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + err = raftNode.AddPeer(c, req.ID, req.Peer) |
| 93 | + } else { |
| 94 | + if _, ok := peers[req.ID]; !ok { |
| 95 | + helper.ResponseBadRequest(c, errors.New("peer not exists")) |
| 96 | + return |
| 97 | + } |
| 98 | + if len(peers) == 1 { |
| 99 | + helper.ResponseBadRequest(c, errors.New("can't remove the last peer")) |
| 100 | + return |
| 101 | + } |
| 102 | + err = raftNode.RemovePeer(c, req.ID) |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + helper.ResponseError(c, err) |
| 106 | + } else { |
| 107 | + logger.Get().With(zap.Any("request", req)).Info("Update peer success") |
| 108 | + helper.ResponseOK(c, nil) |
| 109 | + } |
| 110 | +} |
0 commit comments