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.

130 lines
3.6 KiB

6 years ago
6 years ago
5 years ago
7 years ago
5 years ago
7 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. stream, err := client.KeepConnected(context.Background())
  60. if err != nil {
  61. glog.V(0).Infof("%s masterClient failed to keep connected to %s: %v", mc.clientType, master, err)
  62. return err
  63. }
  64. if err = stream.Send(&master_pb.KeepConnectedRequest{Name: mc.clientType, GrpcPort: mc.grpcPort}); err != nil {
  65. glog.V(0).Infof("%s masterClient failed to send to %s: %v", mc.clientType, master, err)
  66. return err
  67. }
  68. glog.V(1).Infof("%s masterClient Connected to %v", mc.clientType, master)
  69. mc.currentMaster = master
  70. for {
  71. volumeLocation, err := stream.Recv()
  72. if err != nil {
  73. glog.V(0).Infof("%s masterClient failed to receive from %s: %v", mc.clientType, master, err)
  74. return err
  75. }
  76. // maybe the leader is changed
  77. if volumeLocation.Leader != "" {
  78. glog.V(0).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 masterClient adds volume %d", mc.clientType, loc.Url, newVid)
  89. mc.addLocation(newVid, loc)
  90. }
  91. for _, deletedVid := range volumeLocation.DeletedVids {
  92. glog.V(1).Infof("%s: %s masterClient removes volume %d", mc.clientType, loc.Url, deletedVid)
  93. mc.deleteLocation(deletedVid, loc)
  94. }
  95. }
  96. })
  97. if gprcErr != nil {
  98. glog.V(0).Infof("%s masterClient failed to connect with master %v: %v", mc.clientType, master, gprcErr)
  99. }
  100. return
  101. }
  102. func (mc *MasterClient) WithClient(fn func(client master_pb.SeaweedClient) error) error {
  103. for mc.currentMaster == "" {
  104. time.Sleep(3 * time.Second)
  105. }
  106. return pb.WithMasterClient(mc.currentMaster, mc.grpcDialOption, func(client master_pb.SeaweedClient) error {
  107. return fn(client)
  108. })
  109. }