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.

122 lines
3.4 KiB

7 years ago
6 years ago
6 years ago
7 years ago
6 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. for _, master := range mc.masters {
  46. glog.V(1).Infof("%s Connecting to master %v", mc.name, master)
  47. gprcErr := withMasterClient(context.Background(), master, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  48. stream, err := client.KeepConnected(ctx)
  49. if err != nil {
  50. glog.V(0).Infof("%s failed to keep connected to %s: %v", mc.name, master, err)
  51. return err
  52. }
  53. if err = stream.Send(&master_pb.ClientListenRequest{Name: mc.name}); err != nil {
  54. glog.V(0).Infof("%s failed to send to %s: %v", mc.name, master, err)
  55. return err
  56. }
  57. if mc.currentMaster == "" {
  58. glog.V(1).Infof("%s Connected to %v", mc.name, master)
  59. mc.currentMaster = master
  60. }
  61. for {
  62. if volumeLocation, err := stream.Recv(); err != nil {
  63. glog.V(0).Infof("%s failed to receive from %s: %v", mc.name, master, err)
  64. return err
  65. } else {
  66. loc := Location{
  67. Url: volumeLocation.Url,
  68. PublicUrl: volumeLocation.PublicUrl,
  69. }
  70. for _, newVid := range volumeLocation.NewVids {
  71. glog.V(1).Infof("%s: %s adds volume %d", mc.name, loc.Url, newVid)
  72. mc.addLocation(newVid, loc)
  73. }
  74. for _, deletedVid := range volumeLocation.DeletedVids {
  75. glog.V(1).Infof("%s: %s removes volume %d", mc.name, loc.Url, deletedVid)
  76. mc.deleteLocation(deletedVid, loc)
  77. }
  78. }
  79. }
  80. })
  81. if gprcErr != nil {
  82. glog.V(0).Infof("%s failed to connect with master %v: %v", mc.name, master, gprcErr)
  83. }
  84. mc.currentMaster = ""
  85. }
  86. }
  87. func withMasterClient(ctx context.Context, master string, grpcDialOption grpc.DialOption, fn func(ctx context.Context, client master_pb.SeaweedClient) error) error {
  88. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master)
  89. if parseErr != nil {
  90. return fmt.Errorf("failed to parse master grpc %v: %v", master, parseErr)
  91. }
  92. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  93. client := master_pb.NewSeaweedClient(grpcConnection)
  94. return fn(ctx, client)
  95. }, masterGrpcAddress, grpcDialOption)
  96. }
  97. func (mc *MasterClient) WithClient(ctx context.Context, fn func(client master_pb.SeaweedClient) error) error {
  98. return withMasterClient(ctx, mc.currentMaster, mc.grpcDialOption, func(ctx context.Context, client master_pb.SeaweedClient) error {
  99. return fn(client)
  100. })
  101. }