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.

209 lines
6.9 KiB

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