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.

133 lines
3.6 KiB

6 years ago
6 years ago
5 years ago
7 years ago
5 years ago
6 years ago
5 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) tryAllMasters() {
  46. nextHintedLeader := ""
  47. for _, master := range mc.masters {
  48. nextHintedLeader = mc.tryConnectToMaster(master)
  49. for nextHintedLeader != "" {
  50. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  51. }
  52. mc.currentMaster = ""
  53. mc.vidMap = newVidMap()
  54. }
  55. }
  56. func (mc *MasterClient) tryConnectToMaster(master string) (nextHintedLeader string) {
  57. glog.V(1).Infof("%s masterClient Connecting to master %v", mc.clientType, master)
  58. gprcErr := pb.WithMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  59. ctx, cancel := context.WithCancel(context.Background())
  60. defer cancel()
  61. stream, err := client.KeepConnected(ctx)
  62. if err != nil {
  63. glog.V(0).Infof("%s masterClient failed to keep connected to %s: %v", mc.clientType, master, err)
  64. return err
  65. }
  66. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.clientType, GrpcPort: mc.grpcPort}); err != nil {
  67. glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  68. return err
  69. }
  70. glog.V(1).Infof("%s masterClient Connected to %v", mc.clientType, master)
  71. mc.currentMaster = master
  72. for {
  73. volumeLocation, err := stream.Recv()
  74. if err != nil {
  75. glog.V(0).Infof("%s masterClient failed to receive from %s: %v", mc.clientType, master, err)
  76. return err
  77. }
  78. // maybe the leader is changed
  79. if volumeLocation.Leader != "" {
  80. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  81. nextHintedLeader = volumeLocation.Leader
  82. return nil
  83. }
  84. // process new volume location
  85. loc := Location{
  86. Url: volumeLocation.Url,
  87. PublicUrl: volumeLocation.PublicUrl,
  88. }
  89. for _, newVid := range volumeLocation.NewVids {
  90. glog.V(1).Infof("%s: %s masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  91. mc.addLocation(newVid, loc)
  92. }
  93. for _, deletedVid := range volumeLocation.DeletedVids {
  94. glog.V(1).Infof("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  95. mc.deleteLocation(deletedVid, loc)
  96. }
  97. }
  98. })
  99. if gprcErr != nil {
  100. glog.V(0).Infof("%s masterClient failed to connect with master %v: %v", mc.clientType, master, gprcErr)
  101. }
  102. return
  103. }
  104. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  105. for mc.currentMaster == "" {
  106. time.Sleep(3 * time.Second)
  107. }
  108. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  109. return fn(client)
  110. })
  111. }