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.

180 lines
5.6 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/spf13/viper"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "golang.org/x/net/context"
  13. )
  14. func (vs *VolumeServer) GetMaster() string {
  15. return vs.currentMaster
  16. }
  17. func (vs *VolumeServer) heartbeat() {
  18. glog.V(0).Infof("Volume server start with masters: %v", vs.MasterNodes)
  19. vs.store.SetDataCenter(vs.dataCenter)
  20. vs.store.SetRack(vs.rack)
  21. grpcDialOption := security.LoadClientTLS(viper.Sub("grpc"), "volume")
  22. var err error
  23. var newLeader string
  24. for {
  25. for _, master := range vs.MasterNodes {
  26. if newLeader != "" {
  27. master = newLeader
  28. }
  29. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(master)
  30. if parseErr != nil {
  31. glog.V(0).Infof("failed to parse master grpc %v: %v", masterGrpcAddress, parseErr)
  32. continue
  33. }
  34. newLeader, err = vs.doHeartbeat(context.Background(), master, masterGrpcAddress, grpcDialOption, time.Duration(vs.pulseSeconds)*time.Second)
  35. if err != nil {
  36. glog.V(0).Infof("heartbeat error: %v", err)
  37. time.Sleep(time.Duration(vs.pulseSeconds) * time.Second)
  38. newLeader = ""
  39. }
  40. }
  41. }
  42. }
  43. func (vs *VolumeServer) doHeartbeat(ctx context.Context, masterNode, masterGrpcAddress string, grpcDialOption grpc.DialOption, sleepInterval time.Duration) (newLeader string, err error) {
  44. grpcConection, err := util.GrpcDial(ctx, masterGrpcAddress, grpcDialOption)
  45. if err != nil {
  46. return "", fmt.Errorf("fail to dial %s : %v", masterNode, err)
  47. }
  48. defer grpcConection.Close()
  49. client := master_pb.NewSeaweedClient(grpcConection)
  50. stream, err := client.SendHeartbeat(ctx)
  51. if err != nil {
  52. glog.V(0).Infof("SendHeartbeat to %s: %v", masterNode, err)
  53. return "", err
  54. }
  55. glog.V(0).Infof("Heartbeat to: %v", masterNode)
  56. vs.currentMaster = masterNode
  57. vs.store.Client = stream
  58. defer func() { vs.store.Client = nil }()
  59. doneChan := make(chan error, 1)
  60. go func() {
  61. for {
  62. in, err := stream.Recv()
  63. if err != nil {
  64. doneChan <- err
  65. return
  66. }
  67. if in.GetVolumeSizeLimit() != 0 {
  68. vs.store.SetVolumeSizeLimit(in.GetVolumeSizeLimit())
  69. }
  70. if in.GetLeader() != "" && masterNode != in.GetLeader() && !isSameIP(in.GetLeader(), masterNode) {
  71. glog.V(0).Infof("Volume Server found a new master newLeader: %v instead of %v", in.GetLeader(), masterNode)
  72. newLeader = in.GetLeader()
  73. doneChan <- nil
  74. return
  75. }
  76. }
  77. }()
  78. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  79. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  80. return "", err
  81. }
  82. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  83. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  84. return "", err
  85. }
  86. volumeTickChan := time.Tick(sleepInterval)
  87. ecShardTickChan := time.Tick(17 * sleepInterval)
  88. for {
  89. select {
  90. case volumeMessage := <-vs.store.NewVolumesChan:
  91. deltaBeat := &master_pb.Heartbeat{
  92. NewVolumes: []*master_pb.VolumeShortInformationMessage{
  93. &volumeMessage,
  94. },
  95. }
  96. glog.V(1).Infof("volume server %s:%d adds volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  97. if err = stream.Send(deltaBeat); err != nil {
  98. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  99. return "", err
  100. }
  101. case ecShardMessage := <-vs.store.NewEcShardsChan:
  102. deltaBeat := &master_pb.Heartbeat{
  103. NewEcShards: []*master_pb.VolumeEcShardInformationMessage{
  104. &ecShardMessage,
  105. },
  106. }
  107. glog.V(1).Infof("volume server %s:%d adds ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id, ecShardMessage.EcIndex)
  108. if err = stream.Send(deltaBeat); err != nil {
  109. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  110. return "", err
  111. }
  112. case volumeMessage := <-vs.store.DeletedVolumesChan:
  113. deltaBeat := &master_pb.Heartbeat{
  114. DeletedVolumes: []*master_pb.VolumeShortInformationMessage{
  115. &volumeMessage,
  116. },
  117. }
  118. glog.V(1).Infof("volume server %s:%d deletes volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  119. if err = stream.Send(deltaBeat); err != nil {
  120. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  121. return "", err
  122. }
  123. case ecShardMessage := <-vs.store.DeletedEcShardsChan:
  124. deltaBeat := &master_pb.Heartbeat{
  125. DeletedEcShards: []*master_pb.VolumeEcShardInformationMessage{
  126. &ecShardMessage,
  127. },
  128. }
  129. glog.V(1).Infof("volume server %s:%d deletes ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id, ecShardMessage.EcIndex)
  130. if err = stream.Send(deltaBeat); err != nil {
  131. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  132. return "", err
  133. }
  134. case <-volumeTickChan:
  135. glog.V(4).Infof("volume server %s:%d heartbeat", vs.store.Ip, vs.store.Port)
  136. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  137. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  138. return "", err
  139. }
  140. case <-ecShardTickChan:
  141. glog.V(4).Infof("volume server %s:%d ec heartbeat", vs.store.Ip, vs.store.Port)
  142. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  143. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  144. return "", err
  145. }
  146. case err = <-doneChan:
  147. return
  148. }
  149. }
  150. }
  151. func isSameIP(ip string, host string) bool {
  152. ips, err := net.LookupIP(host)
  153. if err != nil {
  154. return false
  155. }
  156. for _, t := range ips {
  157. if ip == t.String() {
  158. return true
  159. }
  160. }
  161. return false
  162. }