masterclient.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/util/log"
  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. log.Debugf("%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. log.Infof("connect to %s: %v", master, grpcErr)
  62. }
  63. if leader != "" {
  64. log.Infof("existing leader is %s", leader)
  65. return
  66. }
  67. }
  68. log.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. log.Debugf("%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. log.Debugf("%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. log.Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  94. return err
  95. }
  96. log.Debugf("%s masterClient Connected to %v", mc.clientType, master)
  97. mc.currentMaster = master
  98. for {
  99. volumeLocation, err := stream.Recv()
  100. if err != nil {
  101. log.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. log.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. log.Debugf("%s: %s masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  118. mc.addLocation(newVid, loc)
  119. }
  120. for _, deletedVid := range volumeLocation.DeletedVids {
  121. log.Debugf("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  122. mc.deleteLocation(deletedVid, loc)
  123. }
  124. }
  125. })
  126. if gprcErr != nil {
  127. log.Debugf("%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. }