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.

163 lines
4.7 KiB

6 years ago
6 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. )
  12. type MasterClient struct {
  13. clientType string
  14. clientHost pb.ServerAddress
  15. currentMaster pb.ServerAddress
  16. masters []pb.ServerAddress
  17. grpcDialOption grpc.DialOption
  18. vidMap
  19. }
  20. func NewMasterClient(grpcDialOption grpc.DialOption, clientType string, clientHost pb.ServerAddress, clientDataCenter string, masters []pb.ServerAddress) *MasterClient {
  21. return &MasterClient{
  22. clientType: clientType,
  23. clientHost: clientHost,
  24. masters: masters,
  25. grpcDialOption: grpcDialOption,
  26. vidMap: newVidMap(clientDataCenter),
  27. }
  28. }
  29. func (mc *MasterClient) GetMaster() pb.ServerAddress {
  30. mc.WaitUntilConnected()
  31. return mc.currentMaster
  32. }
  33. func (mc *MasterClient) WaitUntilConnected() {
  34. for mc.currentMaster == "" {
  35. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  36. }
  37. }
  38. func (mc *MasterClient) KeepConnectedToMaster() {
  39. glog.V(1).Infof("%s masterClient bootstraps with masters %v", mc.clientType, mc.masters)
  40. for {
  41. mc.tryAllMasters()
  42. time.Sleep(time.Second)
  43. }
  44. }
  45. func (mc *MasterClient) FindLeaderFromOtherPeers(myMasterAddress pb.ServerAddress) (leader string) {
  46. for _, master := range mc.masters {
  47. if master == myMasterAddress {
  48. continue
  49. }
  50. if grpcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  51. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Millisecond)
  52. defer cancel()
  53. resp, err := client.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  54. if err != nil {
  55. return err
  56. }
  57. leader = resp.Leader
  58. return nil
  59. }); grpcErr != nil {
  60. glog.V(0).Infof("connect to %s: %v", master, grpcErr)
  61. }
  62. if leader != "" {
  63. glog.V(0).Infof("existing leader is %s", leader)
  64. return
  65. }
  66. }
  67. glog.V(0).Infof("No existing leader found!")
  68. return
  69. }
  70. func (mc *MasterClient) tryAllMasters() {
  71. var nextHintedLeader pb.ServerAddress
  72. for _, master := range mc.masters {
  73. nextHintedLeader = mc.tryConnectToMaster(master)
  74. for nextHintedLeader != "" {
  75. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  76. }
  77. mc.currentMaster = ""
  78. mc.vidMap = newVidMap("")
  79. }
  80. }
  81. func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedLeader pb.ServerAddress) {
  82. glog.V(0).Infof("%s masterClient Connecting to master %v", mc.clientType, master)
  83. gprcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  84. ctx, cancel := context.WithCancel(context.Background())
  85. defer cancel()
  86. stream, err := client.KeepConnected(ctx)
  87. if err != nil {
  88. glog.V(1).Infof("%s masterClient failed to keep connected to %s: %v", mc.clientType, master, err)
  89. return err
  90. }
  91. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.clientType, ClientAddress: string(mc.clientHost)}); err != nil {
  92. glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  93. return err
  94. }
  95. glog.V(1).Infof("%s masterClient Connected to %v", mc.clientType, master)
  96. mc.currentMaster = master
  97. for {
  98. volumeLocation, err := stream.Recv()
  99. if err != nil {
  100. glog.V(0).Infof("%s masterClient failed to receive from %s: %v", mc.clientType, master, err)
  101. return err
  102. }
  103. // maybe the leader is changed
  104. if volumeLocation.Leader != "" {
  105. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  106. nextHintedLeader = pb.ServerAddress(volumeLocation.Leader)
  107. return nil
  108. }
  109. // process new volume location
  110. loc := Location{
  111. Url: volumeLocation.Url,
  112. PublicUrl: volumeLocation.PublicUrl,
  113. DataCenter: volumeLocation.DataCenter,
  114. GrpcPort: int(volumeLocation.GrpcPort),
  115. }
  116. for _, newVid := range volumeLocation.NewVids {
  117. glog.V(1).Infof("%s: %s masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  118. mc.addLocation(newVid, loc)
  119. }
  120. for _, deletedVid := range volumeLocation.DeletedVids {
  121. glog.V(1).Infof("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  122. mc.deleteLocation(deletedVid, loc)
  123. }
  124. }
  125. })
  126. if gprcErr != nil {
  127. glog.V(1).Infof("%s masterClient failed to connect with master %v: %v", mc.clientType, master, gprcErr)
  128. }
  129. return
  130. }
  131. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  132. return util.Retry("master grpc", func() error {
  133. for mc.currentMaster == "" {
  134. time.Sleep(3 * time.Second)
  135. }
  136. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  137. return fn(client)
  138. })
  139. })
  140. }