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.

66 lines
1.6 KiB

7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "time"
  9. )
  10. func (fs *Filer) GetMaster() string {
  11. return fs.currentMaster
  12. }
  13. func (fs *Filer) KeepConnectedToMaster() {
  14. glog.V(0).Infof("Filer bootstraps with masters %v", fs.masters)
  15. for {
  16. fs.tryAllMasters()
  17. time.Sleep(time.Second)
  18. }
  19. }
  20. func (fs *Filer) tryAllMasters() {
  21. for _, master := range fs.masters {
  22. glog.V(0).Infof("Connecting to %v", master)
  23. withMasterClient(master, func(client master_pb.SeaweedClient) error {
  24. stream, err := client.KeepConnected(context.Background())
  25. if err != nil {
  26. glog.V(0).Infof("failed to keep connected to %s: %v", master, err)
  27. return err
  28. }
  29. glog.V(0).Infof("Connected to %v", master)
  30. fs.currentMaster = master
  31. if err = stream.Send(&master_pb.ClientListenRequest{Name: "filer"}); err != nil {
  32. glog.V(0).Infof("failed to send to %s: %v", master, err)
  33. return err
  34. }
  35. for {
  36. if volumeLocation, err := stream.Recv(); err != nil {
  37. glog.V(0).Infof("failed to receive from %s: %v", master, err)
  38. return err
  39. } else {
  40. glog.V(0).Infof("volume location: %+v", volumeLocation)
  41. }
  42. }
  43. })
  44. fs.currentMaster = ""
  45. }
  46. }
  47. func withMasterClient(master string, fn func(client master_pb.SeaweedClient) error) error {
  48. grpcConnection, err := util.GrpcDial(master)
  49. if err != nil {
  50. return fmt.Errorf("fail to dial %s: %v", master, err)
  51. }
  52. defer grpcConnection.Close()
  53. client := master_pb.NewSeaweedClient(grpcConnection)
  54. return fn(client)
  55. }