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.

66 lines
2.2 KiB

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