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.

139 lines
3.8 KiB

7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "google.golang.org/grpc"
  11. )
  12. type MasterClient struct {
  13. ctx context.Context
  14. name string
  15. currentMaster string
  16. masters []string
  17. grpcDialOption grpc.DialOption
  18. vidMap
  19. }
  20. func NewMasterClient(ctx context.Context, grpcDialOption grpc.DialOption, clientName string, masters []string) *MasterClient {
  21. return &MasterClient{
  22. ctx: ctx,
  23. name: clientName,
  24. masters: masters,
  25. grpcDialOption: grpcDialOption,
  26. vidMap: newVidMap(),
  27. }
  28. }
  29. func (mc *MasterClient) GetMaster() string {
  30. return mc.currentMaster
  31. }
  32. func (mc *MasterClient) WaitUntilConnected() {
  33. for mc.currentMaster == "" {
  34. time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
  35. }
  36. }
  37. func (mc *MasterClient) KeepConnectedToMaster() {
  38. glog.V(1).Infof("%s bootstraps with masters %v", mc.name, mc.masters)
  39. for {
  40. mc.tryAllMasters()
  41. time.Sleep(time.Second)
  42. }
  43. }
  44. func (mc *MasterClient) tryAllMasters() {
  45. nextHintedLeader := ""
  46. for _, master := range mc.masters {
  47. nextHintedLeader = mc.tryConnectToMaster(master)
  48. for nextHintedLeader != "" {
  49. nextHintedLeader = mc.tryConnectToMaster(nextHintedLeader)
  50. }
  51. mc.currentMaster = ""
  52. mc.vidMap = newVidMap()
  53. }
  54. }
  55. func (mc *MasterClient) tryConnectToMaster(master string) (nextHintedLeader string) {
  56. glog.V(1).Infof("%s Connecting to master %v", mc.name, master)
  57. gprcErr := withMasterClient(context.Background(), master, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  58. stream, err := client.KeepConnected(ctx)
  59. if err != nil {
  60. glog.V(0).Infof("%s failed to keep connected to %s: %v", mc.name, master, err)
  61. return err
  62. }
  63. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.name}); err != nil {
  64. glog.V(0).Infof("%s failed to send to %s: %v", mc.name, master, err)
  65. return err
  66. }
  67. glog.V(1).Infof("%s Connected to %v", mc.name, master)
  68. mc.currentMaster = master
  69. for {
  70. volumeLocation, err := stream.Recv()
  71. if err != nil {
  72. glog.V(0).Infof("%s failed to receive from %s: %v", mc.name, master, err)
  73. return err
  74. }
  75. // maybe the leader is changed
  76. if volumeLocation.Leader != "" {
  77. glog.V(0).Infof("redirected to leader %v", volumeLocation.Leader)
  78. nextHintedLeader = volumeLocation.Leader
  79. return nil
  80. }
  81. // process new volume location
  82. loc := Location{
  83. Url: volumeLocation.Url,
  84. PublicUrl: volumeLocation.PublicUrl,
  85. }
  86. for _, newVid := range volumeLocation.NewVids {
  87. glog.V(1).Infof("%s: %s adds volume %d", mc.name, loc.Url, newVid)
  88. mc.addLocation(newVid, loc)
  89. }
  90. for _, deletedVid := range volumeLocation.DeletedVids {
  91. glog.V(1).Infof("%s: %s removes volume %d", mc.name, loc.Url, deletedVid)
  92. mc.deleteLocation(deletedVid, loc)
  93. }
  94. }
  95. })
  96. if gprcErr != nil {
  97. glog.V(0).Infof("%s failed to connect with master %v: %v", mc.name, master, gprcErr)
  98. }
  99. return
  100. }
  101. func withMasterClient(ctx context.Context, master string, grpcDialOption grpc.DialOption, fn func(ctx context.Context, client master_pb.SeaweedClient) error) error {
  102. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master)
  103. if parseErr != nil {
  104. return fmt.Errorf("failed to parse master grpc %v: %v", master, parseErr)
  105. }
  106. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  107. client := master_pb.NewSeaweedClient(grpcConnection)
  108. return fn(ctx, client)
  109. }, masterGrpcAddress, grpcDialOption)
  110. }
  111. func (mc *MasterClient) WithClient(ctx context.Context, fn func(client master_pb.SeaweedClient) error) error {
  112. return withMasterClient(ctx, mc.currentMaster, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  113. return fn(client)
  114. })
  115. }