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.

125 lines
3.3 KiB

6 years ago
6 years ago
5 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. )
  11. type MasterClient struct {
  12. name string
  13. grpcPort uint32
  14. currentMaster string
  15. masters []string
  16. grpcDialOption grpc.DialOption
  17. vidMap
  18. }
  19. func NewMasterClient(grpcDialOption grpc.DialOption, clientName string, clientGrpcPort uint32, masters []string) *MasterClient {
  20. return &MasterClient{
  21. name: clientName,
  22. grpcPort: clientGrpcPort,
  23. masters: masters,
  24. grpcDialOption: grpcDialOption,
  25. vidMap: newVidMap(),
  26. }
  27. }
  28. func (mc *MasterClient) GetMaster() string {
  29. return mc.currentMaster
  30. }
  31. func (mc *MasterClient) WaitUntilConnected() {
  32. for mc.currentMaster == "" {
  33. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  34. }
  35. }
  36. func (mc *MasterClient) KeepConnectedToMaster() {
  37. glog.V(1).Infof("%s bootstraps with masters %v", mc.name, mc.masters)
  38. for {
  39. mc.tryAllMasters()
  40. time.Sleep(time.Second)
  41. }
  42. }
  43. func (mc *MasterClient) tryAllMasters() {
  44. nextHintedLeader := ""
  45. for _, master := range mc.masters {
  46. nextHintedLeader = mc.tryConnectToMaster(master)
  47. for nextHintedLeader != "" {
  48. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  49. }
  50. mc.currentMaster = ""
  51. mc.vidMap = newVidMap()
  52. }
  53. }
  54. func (mc *MasterClient) tryConnectToMaster(master string) (nextHintedLeader string) {
  55. glog.V(1).Infof("%s Connecting to master %v", mc.name, master)
  56. gprcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  57. stream, err := client.KeepConnected(context.Background())
  58. if err != nil {
  59. glog.V(0).Infof("%s failed to keep connected to %s: %v", mc.name, master, err)
  60. return err
  61. }
  62. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.name, GrpcPort: mc.grpcPort}); err != nil {
  63. glog.V(0).Infof("%s failed to send to %s: %v", mc.name, master, err)
  64. return err
  65. }
  66. glog.V(1).Infof("%s Connected to %v", mc.name, master)
  67. mc.currentMaster = master
  68. for {
  69. volumeLocation, err := stream.Recv()
  70. if err != nil {
  71. glog.V(0).Infof("%s failed to receive from %s: %v", mc.name, master, err)
  72. return err
  73. }
  74. // maybe the leader is changed
  75. if volumeLocation.Leader != "" {
  76. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  77. nextHintedLeader = volumeLocation.Leader
  78. return nil
  79. }
  80. // process new volume location
  81. loc := Location{
  82. Url: volumeLocation.Url,
  83. PublicUrl: volumeLocation.PublicUrl,
  84. }
  85. for _, newVid := range volumeLocation.NewVids {
  86. glog.V(1).Infof("%s: %s adds volume %d", mc.name, loc.Url, newVid)
  87. mc.addLocation(newVid, loc)
  88. }
  89. for _, deletedVid := range volumeLocation.DeletedVids {
  90. glog.V(1).Infof("%s: %s removes volume %d", mc.name, loc.Url, deletedVid)
  91. mc.deleteLocation(deletedVid, loc)
  92. }
  93. }
  94. })
  95. if gprcErr != nil {
  96. glog.V(0).Infof("%s failed to connect with master %v: %v", mc.name, master, gprcErr)
  97. }
  98. return
  99. }
  100. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  101. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  102. return fn(client)
  103. })
  104. }