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.

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