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.

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