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.

159 lines
4.4 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
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. )
  11. type MasterClient struct {
  12. clientType string
  13. clientHost string
  14. grpcPort uint32
  15. currentMaster string
  16. masters []string
  17. grpcDialOption grpc.DialOption
  18. vidMap
  19. }
  20. func NewMasterClient(grpcDialOption grpc.DialOption, clientType string, clientHost string, clientGrpcPort uint32, masters []string) *MasterClient {
  21. return &MasterClient{
  22. clientType: clientType,
  23. clientHost: clientHost,
  24. grpcPort: clientGrpcPort,
  25. masters: masters,
  26. grpcDialOption: grpcDialOption,
  27. vidMap: newVidMap(),
  28. }
  29. }
  30. func (mc *MasterClient) GetMaster() string {
  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 string) (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. nextHintedLeader := ""
  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 string) (nextHintedLeader string) {
  82. glog.V(1).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, GrpcPort: mc.grpcPort}); 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 = volumeLocation.Leader
  107. return nil
  108. }
  109. // process new volume location
  110. loc := Location{
  111. Url: volumeLocation.Url,
  112. PublicUrl: volumeLocation.PublicUrl,
  113. }
  114. for _, newVid := range volumeLocation.NewVids {
  115. glog.V(1).Infof("%s: %s masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  116. mc.addLocation(newVid, loc)
  117. }
  118. for _, deletedVid := range volumeLocation.DeletedVids {
  119. glog.V(1).Infof("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  120. mc.deleteLocation(deletedVid, loc)
  121. }
  122. }
  123. })
  124. if gprcErr != nil {
  125. glog.V(1).Infof("%s masterClient failed to connect with master %v: %v", mc.clientType, master, gprcErr)
  126. }
  127. return
  128. }
  129. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  130. for mc.currentMaster == "" {
  131. time.Sleep(3 * time.Second)
  132. }
  133. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  134. return fn(client)
  135. })
  136. }