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.

88 lines
2.1 KiB

  1. package weed_server
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/chrislusf/seaweedfs/weed/storage"
  9. "golang.org/x/net/context"
  10. "google.golang.org/grpc"
  11. )
  12. func (vs *VolumeServer) heartbeat() {
  13. glog.V(0).Infof("Volume server bootstraps with master %s", vs.GetMasterNode())
  14. vs.masterNodes = storage.NewMasterNodes(vs.masterNode)
  15. vs.store.SetDataCenter(vs.dataCenter)
  16. vs.store.SetRack(vs.rack)
  17. for {
  18. err := vs.doHeartbeat(time.Duration(vs.pulseSeconds) * time.Second)
  19. if err != nil {
  20. glog.V(0).Infof("heartbeat error: %v", err)
  21. time.Sleep(time.Duration(3*vs.pulseSeconds) * time.Second)
  22. }
  23. }
  24. }
  25. func (vs *VolumeServer) doHeartbeat(sleepInterval time.Duration) error {
  26. masterNode, err := vs.masterNodes.FindMaster()
  27. if err != nil {
  28. return fmt.Errorf("No master found: %v", err)
  29. }
  30. grpcConection, err := grpc.Dial(masterNode, grpc.WithInsecure())
  31. if err != nil {
  32. return fmt.Errorf("fail to dial: %v", err)
  33. }
  34. defer grpcConection.Close()
  35. client := pb.NewSeaweedClient(grpcConection)
  36. stream, err := client.SendHeartbeat(context.Background())
  37. if err != nil {
  38. glog.V(0).Infof("%v.SendHeartbeat(_) = _, %v", client, err)
  39. return err
  40. }
  41. vs.SetMasterNode(masterNode)
  42. glog.V(0).Infof("Heartbeat to %s", masterNode)
  43. vs.store.Client = stream
  44. defer func() { vs.store.Client = nil }()
  45. doneChan := make(chan error, 1)
  46. go func() {
  47. for {
  48. in, err := stream.Recv()
  49. if err != nil {
  50. doneChan <- err
  51. return
  52. }
  53. vs.store.VolumeSizeLimit = in.GetVolumeSizeLimit()
  54. vs.guard.SecretKey = security.Secret(in.GetSecretKey())
  55. }
  56. }()
  57. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  58. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  59. return err
  60. }
  61. tickChan := time.NewTimer(sleepInterval).C
  62. for {
  63. select {
  64. case <-tickChan:
  65. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  66. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  67. return err
  68. }
  69. case err := <-doneChan:
  70. return err
  71. }
  72. }
  73. }