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.6 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
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 string
  15. grpcPort uint32
  16. currentMaster string
  17. masters []string
  18. grpcDialOption grpc.DialOption
  19. vidMap
  20. }
  21. func NewMasterClient(grpcDialOption grpc.DialOption, clientType string, clientHost string, clientGrpcPort uint32, clientDataCenter string, masters []string) *MasterClient {
  22. return &MasterClient{
  23. clientType: clientType,
  24. clientHost: clientHost,
  25. grpcPort: clientGrpcPort,
  26. masters: masters,
  27. grpcDialOption: grpcDialOption,
  28. vidMap: newVidMap(clientDataCenter),
  29. }
  30. }
  31. func (mc *MasterClient) GetMaster() string {
  32. return mc.currentMaster
  33. }
  34. func (mc *MasterClient) WaitUntilConnected() {
  35. for mc.currentMaster == "" {
  36. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  37. }
  38. }
  39. func (mc *MasterClient) KeepConnectedToMaster() {
  40. glog.V(1).Infof("%s masterClient bootstraps with masters %v", mc.clientType, mc.masters)
  41. for {
  42. mc.tryAllMasters()
  43. time.Sleep(time.Second)
  44. }
  45. }
  46. func (mc *MasterClient) FindLeaderFromOtherPeers(myMasterAddress string) (leader string) {
  47. for _, master := range mc.masters {
  48. if master == myMasterAddress {
  49. continue
  50. }
  51. if grpcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  52. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Millisecond)
  53. defer cancel()
  54. resp, err := client.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  55. if err != nil {
  56. return err
  57. }
  58. leader = resp.Leader
  59. return nil
  60. }); grpcErr != nil {
  61. glog.V(0).Infof("connect to %s: %v", master, grpcErr)
  62. }
  63. if leader != "" {
  64. glog.V(0).Infof("existing leader is %s", leader)
  65. return
  66. }
  67. }
  68. glog.V(0).Infof("No existing leader found!")
  69. return
  70. }
  71. func (mc *MasterClient) tryAllMasters() {
  72. nextHintedLeader := ""
  73. for _, master := range mc.masters {
  74. nextHintedLeader = mc.tryConnectToMaster(master)
  75. for nextHintedLeader != "" {
  76. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  77. }
  78. mc.currentMaster = ""
  79. mc.vidMap = newVidMap("")
  80. }
  81. }
  82. func (mc *MasterClient) tryConnectToMaster(master string) (nextHintedLeader string) {
  83. glog.V(1).Infof("%s masterClient Connecting to master %v", mc.clientType, master)
  84. gprcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  85. ctx, cancel := context.WithCancel(context.Background())
  86. defer cancel()
  87. stream, err := client.KeepConnected(ctx)
  88. if err != nil {
  89. glog.V(1).Infof("%s masterClient failed to keep connected to %s: %v", mc.clientType, master, err)
  90. return err
  91. }
  92. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.clientType, GrpcPort: mc.grpcPort}); err != nil {
  93. glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  94. return err
  95. }
  96. glog.V(1).Infof("%s masterClient Connected to %v", mc.clientType, master)
  97. mc.currentMaster = master
  98. for {
  99. volumeLocation, err := stream.Recv()
  100. if err != nil {
  101. glog.V(0).Infof("%s masterClient failed to receive from %s: %v", mc.clientType, master, err)
  102. return err
  103. }
  104. // maybe the leader is changed
  105. if volumeLocation.Leader != "" {
  106. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  107. nextHintedLeader = volumeLocation.Leader
  108. return nil
  109. }
  110. // process new volume location
  111. loc := Location{
  112. Url: volumeLocation.Url,
  113. PublicUrl: volumeLocation.PublicUrl,
  114. DataCenter: volumeLocation.DataCenter,
  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. }