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.

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