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.

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