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.

89 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(vs.pulseSeconds) * time.Second)
  22. }
  23. }
  24. }
  25. func (vs *VolumeServer) doHeartbeat(sleepInterval time.Duration) error {
  26. vs.masterNodes.Reset()
  27. masterNode, err := vs.masterNodes.FindMaster()
  28. if err != nil {
  29. return fmt.Errorf("No master found: %v", err)
  30. }
  31. grpcConection, err := grpc.Dial(masterNode, grpc.WithInsecure())
  32. if err != nil {
  33. return fmt.Errorf("fail to dial: %v", err)
  34. }
  35. defer grpcConection.Close()
  36. client := pb.NewSeaweedClient(grpcConection)
  37. stream, err := client.SendHeartbeat(context.Background())
  38. if err != nil {
  39. glog.V(0).Infof("%v.SendHeartbeat(_) = _, %v", client, err)
  40. return err
  41. }
  42. vs.SetMasterNode(masterNode)
  43. glog.V(0).Infof("Heartbeat to %s", masterNode)
  44. vs.store.Client = stream
  45. defer func() { vs.store.Client = nil }()
  46. doneChan := make(chan error, 1)
  47. go func() {
  48. for {
  49. in, err := stream.Recv()
  50. if err != nil {
  51. doneChan <- err
  52. return
  53. }
  54. vs.store.VolumeSizeLimit = in.GetVolumeSizeLimit()
  55. vs.guard.SecretKey = security.Secret(in.GetSecretKey())
  56. }
  57. }()
  58. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  59. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  60. return err
  61. }
  62. tickChan := time.NewTimer(sleepInterval).C
  63. for {
  64. select {
  65. case <-tickChan:
  66. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  67. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  68. return err
  69. }
  70. case err := <-doneChan:
  71. return err
  72. }
  73. }
  74. }