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.

167 lines
4.8 KiB

7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. )
  12. type MasterClient struct {
  13. clientType string
  14. clientHost pb.ServerAddress
  15. currentMaster pb.ServerAddress
  16. masters []pb.ServerAddress
  17. grpcDialOption grpc.DialOption
  18. vidMap
  19. }
  20. func NewMasterClient(grpcDialOption grpc.DialOption, clientType string, clientHost pb.ServerAddress, clientDataCenter string, masters []pb.ServerAddress) *MasterClient {
  21. return &MasterClient{
  22. clientType: clientType,
  23. clientHost: clientHost,
  24. masters: masters,
  25. grpcDialOption: grpcDialOption,
  26. vidMap: newVidMap(clientDataCenter),
  27. }
  28. }
  29. func (mc *MasterClient) GetMaster() pb.ServerAddress {
  30. mc.WaitUntilConnected()
  31. return mc.currentMaster
  32. }
  33. func (mc *MasterClient) WaitUntilConnected() {
  34. for mc.currentMaster == "" {
  35. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  36. }
  37. }
  38. func (mc *MasterClient) LoopConnectToMaster() {
  39. glog.V(1).Infof("%s masterClient bootstraps with masters %v", mc.clientType, mc.masters)
  40. for {
  41. mc.tryAllMasters()
  42. time.Sleep(time.Second)
  43. }
  44. }
  45. func (mc *MasterClient) FindLeaderFromOtherPeers(myMasterAddress pb.ServerAddress) (leader string) {
  46. for _, master := range mc.masters {
  47. if master == myMasterAddress {
  48. continue
  49. }
  50. if grpcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  51. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Millisecond)
  52. defer cancel()
  53. resp, err := client.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  54. if err != nil {
  55. return err
  56. }
  57. leader = resp.Leader
  58. return nil
  59. }); grpcErr != nil {
  60. glog.V(0).Infof("connect to %s: %v", master, grpcErr)
  61. }
  62. if leader != "" {
  63. glog.V(0).Infof("existing leader is %s", leader)
  64. return
  65. }
  66. }
  67. glog.V(0).Infof("No existing leader found!")
  68. return
  69. }
  70. func (mc *MasterClient) tryAllMasters() {
  71. var nextHintedLeader pb.ServerAddress
  72. for _, master := range mc.masters {
  73. nextHintedLeader = mc.tryConnectToMaster(master)
  74. for nextHintedLeader != "" {
  75. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  76. }
  77. mc.currentMaster = ""
  78. mc.vidMap = newVidMap("")
  79. }
  80. }
  81. func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedLeader pb.ServerAddress) {
  82. glog.V(1).Infof("%s masterClient Connecting to master %v", mc.clientType, master)
  83. gprcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  84. ctx, cancel := context.WithCancel(context.Background())
  85. defer cancel()
  86. stream, err := client.SubscribeVolumeLocationUpdates(ctx)
  87. if err != nil {
  88. glog.V(1).Infof("%s masterClient failed to keep connected to %s: %v", mc.clientType, master, err)
  89. return err
  90. }
  91. if err = stream.Send(&master_pb.SubscribeVolumeLocationUpdatesRequest{
  92. ClientType: mc.clientType,
  93. ClientAddress: string(mc.clientHost),
  94. Version: util.Version(),
  95. }); err != nil {
  96. glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  97. return err
  98. }
  99. glog.V(1).Infof("%s masterClient Connected to %v", mc.clientType, master)
  100. mc.currentMaster = master
  101. for {
  102. volumeLocation, err := stream.Recv()
  103. if err != nil {
  104. glog.V(0).Infof("%s masterClient failed to receive from %s: %v", mc.clientType, master, err)
  105. return err
  106. }
  107. // maybe the leader is changed
  108. if volumeLocation.Leader != "" {
  109. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  110. nextHintedLeader = pb.ServerAddress(volumeLocation.Leader)
  111. return nil
  112. }
  113. // process new volume location
  114. loc := Location{
  115. Url: volumeLocation.Url,
  116. PublicUrl: volumeLocation.PublicUrl,
  117. DataCenter: volumeLocation.DataCenter,
  118. GrpcPort: int(volumeLocation.GrpcPort),
  119. }
  120. for _, newVid := range volumeLocation.NewVids {
  121. glog.V(1).Infof("%s: %s masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  122. mc.addLocation(newVid, loc)
  123. }
  124. for _, deletedVid := range volumeLocation.DeletedVids {
  125. glog.V(1).Infof("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  126. mc.deleteLocation(deletedVid, loc)
  127. }
  128. }
  129. })
  130. if gprcErr != nil {
  131. glog.V(1).Infof("%s masterClient failed to connect with master %v: %v", mc.clientType, master, gprcErr)
  132. }
  133. return
  134. }
  135. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  136. return util.Retry("master grpc", func() error {
  137. for mc.currentMaster == "" {
  138. time.Sleep(3 * time.Second)
  139. }
  140. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  141. return fn(client)
  142. })
  143. })
  144. }