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.

140 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 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. }
  53. }
  54. func (mc *MasterClient) tryConnectToMaster(master string) (nextHintedLeader string) {
  55. glog.V(1).Infof("%s Connecting to master %v", mc.name, master)
  56. gprcErr := withMasterClient(context.Background(), master, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  57. stream, err := client.KeepConnected(ctx)
  58. if err != nil {
  59. glog.V(0).Infof("%s failed to keep connected to %s: %v", mc.name, master, err)
  60. return err
  61. }
  62. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.name}); err != nil {
  63. glog.V(0).Infof("%s failed to send to %s: %v", mc.name, master, err)
  64. return err
  65. }
  66. if mc.currentMaster == "" {
  67. glog.V(1).Infof("%s Connected to %v", mc.name, master)
  68. mc.currentMaster = master
  69. }
  70. for {
  71. volumeLocation, err := stream.Recv()
  72. if err != nil {
  73. glog.V(0).Infof("%s failed to receive from %s: %v", mc.name, master, err)
  74. return err
  75. }
  76. // maybe the leader is changed
  77. if volumeLocation.Leader != "" {
  78. glog.V(1).Infof("redirected to leader %v", volumeLocation.Leader)
  79. nextHintedLeader = volumeLocation.Leader
  80. return nil
  81. }
  82. // process new volume location
  83. loc := Location{
  84. Url: volumeLocation.Url,
  85. PublicUrl: volumeLocation.PublicUrl,
  86. }
  87. for _, newVid := range volumeLocation.NewVids {
  88. glog.V(1).Infof("%s: %s adds volume %d", mc.name, loc.Url, newVid)
  89. mc.addLocation(newVid, loc)
  90. }
  91. for _, deletedVid := range volumeLocation.DeletedVids {
  92. glog.V(1).Infof("%s: %s removes volume %d", mc.name, loc.Url, deletedVid)
  93. mc.deleteLocation(deletedVid, loc)
  94. }
  95. }
  96. })
  97. if gprcErr != nil {
  98. glog.V(0).Infof("%s failed to connect with master %v: %v", mc.name, master, gprcErr)
  99. }
  100. return
  101. }
  102. func withMasterClient(ctx context.Context, master string, grpcDialOption grpc.DialOption, fn func(ctx context.Context, client master_pb.SeaweedClient) error) error {
  103. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master)
  104. if parseErr != nil {
  105. return fmt.Errorf("failed to parse master grpc %v: %v", master, parseErr)
  106. }
  107. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  108. client := master_pb.NewSeaweedClient(grpcConnection)
  109. return fn(ctx, client)
  110. }, masterGrpcAddress, grpcDialOption)
  111. }
  112. func (mc *MasterClient) WithClient(ctx context.Context, fn func(client master_pb.SeaweedClient) error) error {
  113. return withMasterClient(ctx, mc.currentMaster, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  114. return fn(client)
  115. })
  116. }