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.

123 lines
3.4 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/security"
  5. "github.com/spf13/viper"
  6. "google.golang.org/grpc"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "golang.org/x/net/context"
  12. )
  13. func (vs *VolumeServer) GetMaster() string {
  14. return vs.currentMaster
  15. }
  16. func (vs *VolumeServer) heartbeat() {
  17. glog.V(0).Infof("Volume server start with masters: %v", vs.MasterNodes)
  18. vs.store.SetDataCenter(vs.dataCenter)
  19. vs.store.SetRack(vs.rack)
  20. grpcDialOption := security.LoadClientTLS(viper.Sub("grpc"), "volume")
  21. var err error
  22. var newLeader string
  23. for {
  24. for _, master := range vs.MasterNodes {
  25. if newLeader != "" {
  26. master = newLeader
  27. }
  28. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master, 0)
  29. if parseErr != nil {
  30. glog.V(0).Infof("failed to parse master grpc %v", masterGrpcAddress)
  31. continue
  32. }
  33. newLeader, err = vs.doHeartbeat(master, masterGrpcAddress, grpcDialOption, time.Duration(vs.pulseSeconds)*time.Second)
  34. if err != nil {
  35. glog.V(0).Infof("heartbeat error: %v", err)
  36. time.Sleep(time.Duration(vs.pulseSeconds) * time.Second)
  37. }
  38. }
  39. }
  40. }
  41. func (vs *VolumeServer) doHeartbeat(masterNode, masterGrpcAddress string, grpcDialOption grpc.DialOption, sleepInterval time.Duration) (newLeader string, err error) {
  42. grpcConection, err := util.GrpcDial(masterGrpcAddress, grpcDialOption)
  43. if err != nil {
  44. return "", fmt.Errorf("fail to dial %s : %v", masterNode, err)
  45. }
  46. defer grpcConection.Close()
  47. client := master_pb.NewSeaweedClient(grpcConection)
  48. stream, err := client.SendHeartbeat(context.Background())
  49. if err != nil {
  50. glog.V(0).Infof("SendHeartbeat to %s: %v", masterNode, err)
  51. return "", err
  52. }
  53. glog.V(0).Infof("Heartbeat to: %v", masterNode)
  54. vs.currentMaster = masterNode
  55. vs.store.Client = stream
  56. defer func() { vs.store.Client = nil }()
  57. doneChan := make(chan error, 1)
  58. go func() {
  59. for {
  60. in, err := stream.Recv()
  61. if err != nil {
  62. doneChan <- err
  63. return
  64. }
  65. if in.GetVolumeSizeLimit() != 0 {
  66. vs.store.VolumeSizeLimit = in.GetVolumeSizeLimit()
  67. }
  68. if in.GetLeader() != "" && masterNode != in.GetLeader() {
  69. glog.V(0).Infof("Volume Server found a new master newLeader: %v instead of %v", in.GetLeader(), masterNode)
  70. newLeader = in.GetLeader()
  71. doneChan <- nil
  72. return
  73. }
  74. }
  75. }()
  76. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  77. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  78. return "", err
  79. }
  80. tickChan := time.Tick(sleepInterval)
  81. for {
  82. select {
  83. case vid := <-vs.store.NewVolumeIdChan:
  84. deltaBeat := &master_pb.Heartbeat{
  85. NewVids: []uint32{uint32(vid)},
  86. }
  87. if err = stream.Send(deltaBeat); err != nil {
  88. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  89. return "", err
  90. }
  91. case vid := <-vs.store.DeletedVolumeIdChan:
  92. deltaBeat := &master_pb.Heartbeat{
  93. DeletedVids: []uint32{uint32(vid)},
  94. }
  95. if err = stream.Send(deltaBeat); err != nil {
  96. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  97. return "", err
  98. }
  99. case <-tickChan:
  100. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  101. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  102. return "", err
  103. }
  104. case err = <-doneChan:
  105. return
  106. }
  107. }
  108. }