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.

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