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.

191 lines
5.9 KiB

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