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.

265 lines
8.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. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/stats"
  6. "math/rand"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "google.golang.org/grpc"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. )
  14. type MasterClient struct {
  15. FilerGroup string
  16. clientType string
  17. clientHost pb.ServerAddress
  18. currentMaster pb.ServerAddress
  19. masters map[string]pb.ServerAddress
  20. grpcDialOption grpc.DialOption
  21. vidMap
  22. OnPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time)
  23. }
  24. func NewMasterClient(grpcDialOption grpc.DialOption, filerGroup string, clientType string, clientHost pb.ServerAddress, clientDataCenter string, masters map[string]pb.ServerAddress) *MasterClient {
  25. return &MasterClient{
  26. FilerGroup: filerGroup,
  27. clientType: clientType,
  28. clientHost: clientHost,
  29. masters: masters,
  30. grpcDialOption: grpcDialOption,
  31. vidMap: newVidMap(clientDataCenter),
  32. }
  33. }
  34. func (mc *MasterClient) GetLookupFileIdFunction() LookupFileIdFunctionType {
  35. return mc.LookupFileIdWithFallback
  36. }
  37. func (mc *MasterClient) LookupFileIdWithFallback(fileId string) (fullUrls []string, err error) {
  38. fullUrls, err = mc.vidMap.LookupFileId(fileId)
  39. if err == nil && len(fullUrls) > 0 {
  40. return
  41. }
  42. err = pb.WithMasterClient(false, mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  43. resp, err := client.LookupVolume(context.Background(), &master_pb.LookupVolumeRequest{
  44. VolumeOrFileIds: []string{fileId},
  45. })
  46. if err != nil {
  47. return fmt.Errorf("LookupVolume failed: %v", err)
  48. }
  49. for vid, vidLocation := range resp.VolumeIdLocations {
  50. for _, vidLoc := range vidLocation.Locations {
  51. loc := Location{
  52. Url: vidLoc.Url,
  53. PublicUrl: vidLoc.PublicUrl,
  54. GrpcPort: int(vidLoc.GrpcPort),
  55. }
  56. mc.vidMap.addLocation(uint32(vid), loc)
  57. fullUrls = append(fullUrls, "http://"+loc.Url+"/"+fileId)
  58. }
  59. }
  60. return nil
  61. })
  62. return
  63. }
  64. func (mc *MasterClient) GetMaster() pb.ServerAddress {
  65. mc.WaitUntilConnected()
  66. return mc.currentMaster
  67. }
  68. func (mc *MasterClient) GetMasters() map[string]pb.ServerAddress {
  69. mc.WaitUntilConnected()
  70. return mc.masters
  71. }
  72. func (mc *MasterClient) WaitUntilConnected() {
  73. for mc.currentMaster == "" {
  74. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  75. }
  76. }
  77. func (mc *MasterClient) KeepConnectedToMaster() {
  78. glog.V(1).Infof("%s.%s masterClient bootstraps with masters %v", mc.FilerGroup, mc.clientType, mc.masters)
  79. for {
  80. mc.tryAllMasters()
  81. time.Sleep(time.Second)
  82. }
  83. }
  84. func (mc *MasterClient) FindLeaderFromOtherPeers(myMasterAddress pb.ServerAddress) (leader string) {
  85. for _, master := range mc.masters {
  86. if master == myMasterAddress {
  87. continue
  88. }
  89. if grpcErr := pb.WithMasterClient(false, master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  90. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Millisecond)
  91. defer cancel()
  92. resp, err := client.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  93. if err != nil {
  94. return err
  95. }
  96. leader = resp.Leader
  97. return nil
  98. }); grpcErr != nil {
  99. glog.V(0).Infof("connect to %s: %v", master, grpcErr)
  100. }
  101. if leader != "" {
  102. glog.V(0).Infof("existing leader is %s", leader)
  103. return
  104. }
  105. }
  106. glog.V(0).Infof("No existing leader found!")
  107. return
  108. }
  109. func (mc *MasterClient) tryAllMasters() {
  110. var nextHintedLeader pb.ServerAddress
  111. for _, master := range mc.masters {
  112. nextHintedLeader = mc.tryConnectToMaster(master)
  113. for nextHintedLeader != "" {
  114. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  115. }
  116. mc.currentMaster = ""
  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. resp, err := stream.Recv()
  143. if err != nil {
  144. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  145. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  146. return err
  147. }
  148. // check if it is the leader to determine whether to reset the vidMap
  149. if resp.VolumeLocation != nil {
  150. if resp.VolumeLocation.Leader != "" && string(master) != resp.VolumeLocation.Leader {
  151. glog.V(0).Infof("master %v redirected to leader %v", master, resp.VolumeLocation.Leader)
  152. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  153. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToleader).Inc()
  154. return nil
  155. }
  156. mc.vidMap = newVidMap("")
  157. mc.updateVidMap(resp)
  158. } else {
  159. mc.vidMap = newVidMap("")
  160. }
  161. mc.currentMaster = master
  162. for {
  163. resp, err := stream.Recv()
  164. if err != nil {
  165. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  166. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  167. return err
  168. }
  169. if resp.VolumeLocation != nil {
  170. // maybe the leader is changed
  171. if resp.VolumeLocation.Leader != "" && string(mc.currentMaster) != resp.VolumeLocation.Leader {
  172. glog.V(0).Infof("currentMaster %v redirected to leader %v", mc.currentMaster, resp.VolumeLocation.Leader)
  173. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  174. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToleader).Inc()
  175. return nil
  176. }
  177. mc.updateVidMap(resp)
  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) updateVidMap(resp *master_pb.KeepConnectedResponse) {
  202. // process new volume location
  203. loc := Location{
  204. Url: resp.VolumeLocation.Url,
  205. PublicUrl: resp.VolumeLocation.PublicUrl,
  206. DataCenter: resp.VolumeLocation.DataCenter,
  207. GrpcPort: int(resp.VolumeLocation.GrpcPort),
  208. }
  209. for _, newVid := range resp.VolumeLocation.NewVids {
  210. glog.V(1).Infof("%s.%s: %s masterClient adds volume %d", mc.FilerGroup, mc.clientType, loc.Url, newVid)
  211. mc.addLocation(newVid, loc)
  212. }
  213. for _, deletedVid := range resp.VolumeLocation.DeletedVids {
  214. glog.V(1).Infof("%s.%s: %s masterClient removes volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedVid)
  215. mc.deleteLocation(deletedVid, loc)
  216. }
  217. for _, newEcVid := range resp.VolumeLocation.NewEcVids {
  218. glog.V(1).Infof("%s.%s: %s masterClient adds ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, newEcVid)
  219. mc.addEcLocation(newEcVid, loc)
  220. }
  221. for _, deletedEcVid := range resp.VolumeLocation.DeletedEcVids {
  222. glog.V(1).Infof("%s.%s: %s masterClient removes ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedEcVid)
  223. mc.deleteEcLocation(deletedEcVid, loc)
  224. }
  225. }
  226. func (mc *MasterClient) WithClient(streamingMode bool, fn func(client master_pb.SeaweedClient) error) error {
  227. return util.Retry("master grpc", func() error {
  228. for mc.currentMaster == "" {
  229. time.Sleep(3 * time.Second)
  230. }
  231. return pb.WithMasterClient(streamingMode, mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  232. return fn(client)
  233. })
  234. })
  235. }