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.

308 lines
10 KiB

7 years ago
5 years ago
3 years ago
7 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
4 years ago
3 years ago
2 years ago
2 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/stats"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "google.golang.org/grpc"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  14. )
  15. type MasterClient struct {
  16. FilerGroup string
  17. clientType string
  18. clientHost pb.ServerAddress
  19. rack string
  20. currentMaster pb.ServerAddress
  21. masters map[string]pb.ServerAddress
  22. grpcDialOption grpc.DialOption
  23. vidMap
  24. vidMapCacheSize int
  25. OnPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time)
  26. OnPeerUpdateLock sync.RWMutex
  27. }
  28. func NewMasterClient(grpcDialOption grpc.DialOption, filerGroup string, clientType string, clientHost pb.ServerAddress, clientDataCenter string, rack string, masters map[string]pb.ServerAddress) *MasterClient {
  29. return &MasterClient{
  30. FilerGroup: filerGroup,
  31. clientType: clientType,
  32. clientHost: clientHost,
  33. rack: rack,
  34. masters: masters,
  35. grpcDialOption: grpcDialOption,
  36. vidMap: newVidMap(clientDataCenter),
  37. vidMapCacheSize: 5,
  38. }
  39. }
  40. func (mc *MasterClient) SetOnPeerUpdateFn(onPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time)) {
  41. mc.OnPeerUpdateLock.Lock()
  42. mc.OnPeerUpdate = onPeerUpdate
  43. mc.OnPeerUpdateLock.Unlock()
  44. }
  45. func (mc *MasterClient) GetLookupFileIdFunction() LookupFileIdFunctionType {
  46. return mc.LookupFileIdWithFallback
  47. }
  48. func (mc *MasterClient) LookupFileIdWithFallback(fileId string) (fullUrls []string, err error) {
  49. fullUrls, err = mc.vidMap.LookupFileId(fileId)
  50. if err == nil && len(fullUrls) > 0 {
  51. return
  52. }
  53. err = pb.WithMasterClient(false, mc.currentMaster, mc.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  54. resp, err := client.LookupVolume(context.Background(), &master_pb.LookupVolumeRequest{
  55. VolumeOrFileIds: []string{fileId},
  56. })
  57. if err != nil {
  58. return fmt.Errorf("LookupVolume failed: %v", err)
  59. }
  60. for vid, vidLocation := range resp.VolumeIdLocations {
  61. for _, vidLoc := range vidLocation.Locations {
  62. loc := Location{
  63. Url: vidLoc.Url,
  64. PublicUrl: vidLoc.PublicUrl,
  65. GrpcPort: int(vidLoc.GrpcPort),
  66. DataCenter: vidLoc.DataCenter,
  67. }
  68. mc.vidMap.addLocation(uint32(vid), loc)
  69. httpUrl := "http://" + loc.Url + "/" + fileId
  70. // Prefer same data center
  71. if mc.DataCenter != "" && mc.DataCenter == loc.DataCenter {
  72. fullUrls = append([]string{httpUrl}, fullUrls...)
  73. } else {
  74. fullUrls = append(fullUrls, httpUrl)
  75. }
  76. }
  77. }
  78. return nil
  79. })
  80. return
  81. }
  82. func (mc *MasterClient) GetMaster() pb.ServerAddress {
  83. mc.WaitUntilConnected()
  84. return mc.currentMaster
  85. }
  86. func (mc *MasterClient) GetMasters() map[string]pb.ServerAddress {
  87. mc.WaitUntilConnected()
  88. return mc.masters
  89. }
  90. func (mc *MasterClient) WaitUntilConnected() {
  91. for {
  92. if mc.currentMaster != "" {
  93. return
  94. }
  95. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  96. }
  97. }
  98. func (mc *MasterClient) KeepConnectedToMaster() {
  99. glog.V(1).Infof("%s.%s masterClient bootstraps with masters %v", mc.FilerGroup, mc.clientType, mc.masters)
  100. for {
  101. mc.tryAllMasters()
  102. time.Sleep(time.Second)
  103. }
  104. }
  105. func (mc *MasterClient) FindLeaderFromOtherPeers(myMasterAddress pb.ServerAddress) (leader string) {
  106. for _, master := range mc.masters {
  107. if master == myMasterAddress {
  108. continue
  109. }
  110. if grpcErr := pb.WithMasterClient(false, master, mc.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  111. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Millisecond)
  112. defer cancel()
  113. resp, err := client.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  114. if err != nil {
  115. return err
  116. }
  117. leader = resp.Leader
  118. return nil
  119. }); grpcErr != nil {
  120. glog.V(0).Infof("connect to %s: %v", master, grpcErr)
  121. }
  122. if leader != "" {
  123. glog.V(0).Infof("existing leader is %s", leader)
  124. return
  125. }
  126. }
  127. glog.V(0).Infof("No existing leader found!")
  128. return
  129. }
  130. func (mc *MasterClient) tryAllMasters() {
  131. var nextHintedLeader pb.ServerAddress
  132. for _, master := range mc.masters {
  133. nextHintedLeader = mc.tryConnectToMaster(master)
  134. for nextHintedLeader != "" {
  135. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  136. }
  137. mc.currentMaster = ""
  138. }
  139. }
  140. func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedLeader pb.ServerAddress) {
  141. glog.V(1).Infof("%s.%s masterClient Connecting to master %v", mc.FilerGroup, mc.clientType, master)
  142. stats.MasterClientConnectCounter.WithLabelValues("total").Inc()
  143. gprcErr := pb.WithMasterClient(true, master, mc.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  144. ctx, cancel := context.WithCancel(context.Background())
  145. defer cancel()
  146. stream, err := client.KeepConnected(ctx)
  147. if err != nil {
  148. glog.V(1).Infof("%s.%s masterClient failed to keep connected to %s: %v", mc.FilerGroup, mc.clientType, master, err)
  149. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToKeepConnected).Inc()
  150. return err
  151. }
  152. if err = stream.Send(&master_pb.KeepConnectedRequest{
  153. FilerGroup: mc.FilerGroup,
  154. DataCenter: mc.DataCenter,
  155. Rack: mc.rack,
  156. ClientType: mc.clientType,
  157. ClientAddress: string(mc.clientHost),
  158. Version: util.Version(),
  159. }); err != nil {
  160. glog.V(0).Infof("%s.%s masterClient failed to send to %s: %v", mc.FilerGroup, mc.clientType, master, err)
  161. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToSend).Inc()
  162. return err
  163. }
  164. glog.V(1).Infof("%s.%s masterClient Connected to %v", mc.FilerGroup, mc.clientType, master)
  165. resp, err := stream.Recv()
  166. if err != nil {
  167. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  168. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  169. return err
  170. }
  171. // check if it is the leader to determine whether to reset the vidMap
  172. if resp.VolumeLocation != nil {
  173. if resp.VolumeLocation.Leader != "" && string(master) != resp.VolumeLocation.Leader {
  174. glog.V(0).Infof("master %v redirected to leader %v", master, resp.VolumeLocation.Leader)
  175. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  176. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToLeader).Inc()
  177. return nil
  178. }
  179. mc.resetVidMap()
  180. mc.updateVidMap(resp)
  181. } else {
  182. mc.resetVidMap()
  183. }
  184. mc.currentMaster = master
  185. for {
  186. resp, err := stream.Recv()
  187. if err != nil {
  188. glog.V(0).Infof("%s.%s masterClient failed to receive from %s: %v", mc.FilerGroup, mc.clientType, master, err)
  189. stats.MasterClientConnectCounter.WithLabelValues(stats.FailedToReceive).Inc()
  190. return err
  191. }
  192. if resp.VolumeLocation != nil {
  193. // maybe the leader is changed
  194. if resp.VolumeLocation.Leader != "" && string(mc.currentMaster) != resp.VolumeLocation.Leader {
  195. glog.V(0).Infof("currentMaster %v redirected to leader %v", mc.currentMaster, resp.VolumeLocation.Leader)
  196. nextHintedLeader = pb.ServerAddress(resp.VolumeLocation.Leader)
  197. stats.MasterClientConnectCounter.WithLabelValues(stats.RedirectedToLeader).Inc()
  198. return nil
  199. }
  200. mc.updateVidMap(resp)
  201. }
  202. if resp.ClusterNodeUpdate != nil {
  203. update := resp.ClusterNodeUpdate
  204. mc.OnPeerUpdateLock.RLock()
  205. if mc.OnPeerUpdate != nil {
  206. if update.FilerGroup == mc.FilerGroup {
  207. if update.IsAdd {
  208. glog.V(0).Infof("+ %s.%s %s leader:%v\n", update.FilerGroup, update.NodeType, update.Address, update.IsLeader)
  209. } else {
  210. glog.V(0).Infof("- %s.%s %s leader:%v\n", update.FilerGroup, update.NodeType, update.Address, update.IsLeader)
  211. }
  212. stats.MasterClientConnectCounter.WithLabelValues(stats.OnPeerUpdate).Inc()
  213. mc.OnPeerUpdate(update, time.Now())
  214. }
  215. }
  216. mc.OnPeerUpdateLock.RUnlock()
  217. }
  218. }
  219. })
  220. if gprcErr != nil {
  221. stats.MasterClientConnectCounter.WithLabelValues(stats.Failed).Inc()
  222. glog.V(1).Infof("%s.%s masterClient failed to connect with master %v: %v", mc.FilerGroup, mc.clientType, master, gprcErr)
  223. }
  224. return
  225. }
  226. func (mc *MasterClient) updateVidMap(resp *master_pb.KeepConnectedResponse) {
  227. // process new volume location
  228. glog.V(1).Infof("updateVidMap() resp.VolumeLocation.DataCenter %v", resp.VolumeLocation.DataCenter)
  229. loc := Location{
  230. Url: resp.VolumeLocation.Url,
  231. PublicUrl: resp.VolumeLocation.PublicUrl,
  232. DataCenter: resp.VolumeLocation.DataCenter,
  233. GrpcPort: int(resp.VolumeLocation.GrpcPort),
  234. }
  235. for _, newVid := range resp.VolumeLocation.NewVids {
  236. glog.V(1).Infof("%s.%s: %s masterClient adds volume %d", mc.FilerGroup, mc.clientType, loc.Url, newVid)
  237. mc.addLocation(newVid, loc)
  238. }
  239. for _, deletedVid := range resp.VolumeLocation.DeletedVids {
  240. glog.V(1).Infof("%s.%s: %s masterClient removes volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedVid)
  241. mc.deleteLocation(deletedVid, loc)
  242. }
  243. for _, newEcVid := range resp.VolumeLocation.NewEcVids {
  244. glog.V(1).Infof("%s.%s: %s masterClient adds ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, newEcVid)
  245. mc.addEcLocation(newEcVid, loc)
  246. }
  247. for _, deletedEcVid := range resp.VolumeLocation.DeletedEcVids {
  248. glog.V(1).Infof("%s.%s: %s masterClient removes ec volume %d", mc.FilerGroup, mc.clientType, loc.Url, deletedEcVid)
  249. mc.deleteEcLocation(deletedEcVid, loc)
  250. }
  251. }
  252. func (mc *MasterClient) WithClient(streamingMode bool, fn func(client master_pb.SeaweedClient) error) error {
  253. return util.Retry("master grpc", func() error {
  254. for mc.currentMaster == "" {
  255. time.Sleep(3 * time.Second)
  256. }
  257. return pb.WithMasterClient(streamingMode, mc.currentMaster, mc.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
  258. return fn(client)
  259. })
  260. })
  261. }
  262. func (mc *MasterClient) resetVidMap() {
  263. tail := &vidMap{
  264. vid2Locations: mc.vid2Locations,
  265. ecVid2Locations: mc.ecVid2Locations,
  266. DataCenter: mc.DataCenter,
  267. cache: mc.cache,
  268. }
  269. mc.vidMap = newVidMap(mc.DataCenter)
  270. mc.vidMap.cache = tail
  271. for i := 0; i < mc.vidMapCacheSize && tail.cache != nil; i++ {
  272. if i == mc.vidMapCacheSize-1 {
  273. tail.cache = nil
  274. } else {
  275. tail = tail.cache
  276. }
  277. }
  278. }