You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.4 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hashicorp/raft"
  6. "github.com/seaweedfs/seaweedfs/weed/cluster"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  8. )
  9. func (ms *MasterServer) RaftListClusterServers(ctx context.Context, req *master_pb.RaftListClusterServersRequest) (*master_pb.RaftListClusterServersResponse, error) {
  10. resp := &master_pb.RaftListClusterServersResponse{}
  11. if ms.Topo.HashicorpRaft == nil {
  12. return resp, nil
  13. }
  14. servers := ms.Topo.HashicorpRaft.GetConfiguration().Configuration().Servers
  15. for _, server := range servers {
  16. resp.ClusterServers = append(resp.ClusterServers, &master_pb.RaftListClusterServersResponse_ClusterServers{
  17. Id: string(server.ID),
  18. Address: string(server.Address),
  19. Suffrage: server.Suffrage.String(),
  20. })
  21. }
  22. return resp, nil
  23. }
  24. func (ms *MasterServer) RaftAddServer(ctx context.Context, req *master_pb.RaftAddServerRequest) (*master_pb.RaftAddServerResponse, error) {
  25. resp := &master_pb.RaftAddServerResponse{}
  26. if ms.Topo.HashicorpRaft == nil {
  27. return resp, nil
  28. }
  29. if ms.Topo.HashicorpRaft.State() != raft.Leader {
  30. return nil, fmt.Errorf("raft add server %s failed: %s is no current leader", req.Id, ms.Topo.HashicorpRaft.String())
  31. }
  32. var idxFuture raft.IndexFuture
  33. if req.Voter {
  34. idxFuture = ms.Topo.HashicorpRaft.AddVoter(raft.ServerID(req.Id), raft.ServerAddress(req.Address), 0, 0)
  35. } else {
  36. idxFuture = ms.Topo.HashicorpRaft.AddNonvoter(raft.ServerID(req.Id), raft.ServerAddress(req.Address), 0, 0)
  37. }
  38. if err := idxFuture.Error(); err != nil {
  39. return nil, err
  40. }
  41. return resp, nil
  42. }
  43. func (ms *MasterServer) RaftRemoveServer(ctx context.Context, req *master_pb.RaftRemoveServerRequest) (*master_pb.RaftRemoveServerResponse, error) {
  44. resp := &master_pb.RaftRemoveServerResponse{}
  45. if ms.Topo.HashicorpRaft == nil {
  46. return resp, nil
  47. }
  48. if ms.Topo.HashicorpRaft.State() != raft.Leader {
  49. return nil, fmt.Errorf("raft remove server %s failed: %s is no current leader", req.Id, ms.Topo.HashicorpRaft.String())
  50. }
  51. if !req.Force {
  52. ms.clientChansLock.RLock()
  53. _, ok := ms.clientChans[fmt.Sprintf("%s@%s", cluster.MasterType, req.Id)]
  54. ms.clientChansLock.RUnlock()
  55. if ok {
  56. return resp, fmt.Errorf("raft remove server %s failed: client connection to master exists", req.Id)
  57. }
  58. }
  59. idxFuture := ms.Topo.HashicorpRaft.RemoveServer(raft.ServerID(req.Id), 0, 0)
  60. if err := idxFuture.Error(); err != nil {
  61. return nil, err
  62. }
  63. return resp, nil
  64. }