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.

254 lines
8.4 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. }
  114. }
  115. func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedLeader pb.ServerAddress) {
  116. glog.V(1).Infof("%s.%s masterClient Connecting to master %v", mc.FilerGroup, mc.clientType, master)
  117. stats.MasterClientConnectCounter.WithLabelValues("total").Inc()
  118. gprcErr := pb.WithMasterClient(true, master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  119. ctx, cancel := context.WithCancel(context.Background())
  120. defer cancel()
  121. stream, err := client.KeepConnected(ctx)
  122. if err != nil {
  123. glog.V(1).Infof("%s.%s masterClient failed to keep connected to %s: %v", mc.FilerGroup, mc.clientType, master, err)
  124. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToKeepConnected).Inc()
  125. return err
  126. }
  127. if err = stream.Send(&master_pb.KeepConnectedRequest{
  128. FilerGroup: mc.FilerGroup,
  129. ClientType: mc.clientType,
  130. ClientAddress: string(mc.clientHost),
  131. Version: util.Version(),
  132. }); err != nil {
  133. glog.V(0).Infof("%s.%s masterClient failed to send to %s: %v", mc.FilerGroup, mc.clientType, master, err)
  134. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToSend).Inc()
  135. return err
  136. }
  137. glog.V(1).Infof("%s.%s masterClient Connected to %v", mc.FilerGroup, mc.clientType, master)
  138. resp, err := stream.Recv()
  139. if err != nil {
  140. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  141. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  142. return err
  143. }
  144. // check if it is the leader to determine whether to reset the vidMap
  145. if resp.VolumeLocation != nil && resp.VolumeLocation.Leader != "" {
  146. glog.V(0).Infof("redirected to leader %v", resp.VolumeLocation.Leader)
  147. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  148. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToleader).Inc()
  149. return nil
  150. }
  151. mc.currentMaster = master
  152. mc.vidMap = newVidMap("")
  153. for {
  154. resp, err := stream.Recv()
  155. if err != nil {
  156. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  157. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  158. return err
  159. }
  160. if resp.VolumeLocation != nil {
  161. // maybe the leader is changed
  162. if resp.VolumeLocation.Leader != "" {
  163. glog.V(0).Infof("redirected to leader %v", resp.VolumeLocation.Leader)
  164. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  165. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToleader).Inc()
  166. return nil
  167. }
  168. // process new volume location
  169. loc := Location{
  170. Url: resp.VolumeLocation.Url,
  171. PublicUrl: resp.VolumeLocation.PublicUrl,
  172. DataCenter: resp.VolumeLocation.DataCenter,
  173. GrpcPort: int(resp.VolumeLocation.GrpcPort),
  174. }
  175. for _, newVid := range resp.VolumeLocation.NewVids {
  176. glog.V(1).Infof("%s.%s: %s masterClient adds volume %d", mc.FilerGroup, mc.clientType, loc.Url, newVid)
  177. mc.addLocation(newVid, loc)
  178. }
  179. for _, deletedVid := range resp.VolumeLocation.DeletedVids {
  180. glog.V(1).Infof("%s.%s: %s masterClient removes volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedVid)
  181. mc.deleteLocation(deletedVid, loc)
  182. }
  183. for _, newEcVid := range resp.VolumeLocation.NewEcVids {
  184. glog.V(1).Infof("%s.%s: %s masterClient adds ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, newEcVid)
  185. mc.addEcLocation(newEcVid, loc)
  186. }
  187. for _, deletedEcVid := range resp.VolumeLocation.DeletedEcVids {
  188. glog.V(1).Infof("%s.%s: %s masterClient removes ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedEcVid)
  189. mc.deleteEcLocation(deletedEcVid, loc)
  190. }
  191. }
  192. if resp.ClusterNodeUpdate != nil {
  193. update := resp.ClusterNodeUpdate
  194. if mc.OnPeerUpdate != nil {
  195. if update.FilerGroup == mc.FilerGroup {
  196. if update.IsAdd {
  197. glog.V(0).Infof("+ %s.%s %s leader:%v\n", update.FilerGroup, update.NodeType, update.Address, update.IsLeader)
  198. } else {
  199. glog.V(0).Infof("- %s.%s %s leader:%v\n", update.FilerGroup, update.NodeType, update.Address, update.IsLeader)
  200. }
  201. stats.MasterClientConnectCounter.WithLabelValues(stats.OnPeerUpdate).Inc()
  202. mc.OnPeerUpdate(update, time.Now())
  203. }
  204. }
  205. }
  206. }
  207. })
  208. if gprcErr != nil {
  209. stats.MasterClientConnectCounter.WithLabelValues(stats.Failed).Inc()
  210. glog.V(1).Infof("%s.%s masterClient failed to connect with master %v: %v", mc.FilerGroup, mc.clientType, master, gprcErr)
  211. }
  212. return
  213. }
  214. func (mc *MasterClient) WithClient(streamingMode bool, fn func(client master_pb.SeaweedClient) error) error {
  215. return util.Retry("master grpc", func() error {
  216. for mc.currentMaster == "" {
  217. time.Sleep(3 * time.Second)
  218. }
  219. return pb.WithMasterClient(streamingMode, mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  220. return fn(client)
  221. })
  222. })
  223. }