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.

164 lines
4.6 KiB

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