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.

118 lines
2.9 KiB

7 years ago
6 years ago
6 years ago
7 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(0).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(0).Infof("Connecting to master %v", master)
  47. gprcErr := withMasterClient(master, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  48. stream, err := client.KeepConnected(context.Background())
  49. if err != nil {
  50. glog.V(0).Infof("failed to keep connected to %s: %v", master, err)
  51. return err
  52. }
  53. if err = stream.Send(&master_pb.ClientListenRequest{Name: mc.name}); err != nil {
  54. glog.V(0).Infof("failed to send to %s: %v", master, err)
  55. return err
  56. }
  57. if mc.currentMaster == "" {
  58. glog.V(0).Infof("Connected to %v", master)
  59. mc.currentMaster = master
  60. }
  61. for {
  62. if volumeLocation, err := stream.Recv(); err != nil {
  63. glog.V(0).Infof("failed to receive from %s: %v", 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. mc.addLocation(newVid, loc)
  72. }
  73. for _, deletedVid := range volumeLocation.DeletedVids {
  74. mc.deleteLocation(deletedVid, loc)
  75. }
  76. }
  77. }
  78. })
  79. if gprcErr != nil {
  80. glog.V(0).Infof("%s failed to connect with master %v: %v", mc.name, master, gprcErr)
  81. }
  82. mc.currentMaster = ""
  83. }
  84. }
  85. func withMasterClient(master string, grpcDialOption grpc.DialOption, fn func(client master_pb.SeaweedClient) error) error {
  86. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master, 0)
  87. if parseErr != nil {
  88. return fmt.Errorf("failed to parse master grpc %v", master)
  89. }
  90. grpcConnection, err := util.GrpcDial(masterGrpcAddress, grpcDialOption)
  91. if err != nil {
  92. return fmt.Errorf("fail to dial %s: %v", master, err)
  93. }
  94. defer grpcConnection.Close()
  95. client := master_pb.NewSeaweedClient(grpcConnection)
  96. return fn(client)
  97. }