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.

60 lines
1.5 KiB

  1. package filer2
  2. import (
  3. "fmt"
  4. "context"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  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 _, master := range fs.masters {
  16. glog.V(0).Infof("Connecting to %v", master)
  17. withMasterClient(master, func(client master_pb.SeaweedClient) error {
  18. stream, err := client.KeepConnected(context.Background())
  19. if err != nil {
  20. glog.V(0).Infof("failed to keep connected to %s: %v", master, err)
  21. return err
  22. }
  23. glog.V(0).Infof("Connected to %v", master)
  24. fs.currentMaster = master
  25. for {
  26. time.Sleep(time.Duration(float32(10*1e3)*0.25) * time.Millisecond)
  27. if err = stream.Send(&master_pb.Empty{}); err != nil {
  28. glog.V(0).Infof("failed to send to %s: %v", master, err)
  29. return err
  30. }
  31. if _, err = stream.Recv(); err != nil {
  32. glog.V(0).Infof("failed to receive from %s: %v", master, err)
  33. return err
  34. }
  35. }
  36. })
  37. fs.currentMaster = ""
  38. }
  39. }
  40. func withMasterClient(master string, fn func(client master_pb.SeaweedClient) error) error {
  41. grpcConnection, err := util.GrpcDial(master)
  42. if err != nil {
  43. return fmt.Errorf("fail to dial %s: %v", master, err)
  44. }
  45. defer grpcConnection.Close()
  46. client := master_pb.NewSeaweedClient(grpcConnection)
  47. return fn(client)
  48. }