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.

186 lines
5.9 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. if in.GetMetricsAddress() != "" && vs.MetricsAddress != in.GetMetricsAddress() {
  78. vs.MetricsAddress = in.GetMetricsAddress()
  79. vs.MetricsIntervalSec = int(in.GetMetricsIntervalSeconds())
  80. }
  81. }
  82. }()
  83. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  84. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  85. return "", err
  86. }
  87. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  88. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  89. return "", err
  90. }
  91. volumeTickChan := time.Tick(sleepInterval)
  92. ecShardTickChan := time.Tick(17 * sleepInterval)
  93. for {
  94. select {
  95. case volumeMessage := <-vs.store.NewVolumesChan:
  96. deltaBeat := &master_pb.Heartbeat{
  97. NewVolumes: []*master_pb.VolumeShortInformationMessage{
  98. &volumeMessage,
  99. },
  100. }
  101. glog.V(1).Infof("volume server %s:%d adds volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  102. if err = stream.Send(deltaBeat); err != nil {
  103. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  104. return "", err
  105. }
  106. case ecShardMessage := <-vs.store.NewEcShardsChan:
  107. deltaBeat := &master_pb.Heartbeat{
  108. NewEcShards: []*master_pb.VolumeEcShardInformationMessage{
  109. &ecShardMessage,
  110. },
  111. }
  112. glog.V(1).Infof("volume server %s:%d adds ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  113. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  114. if err = stream.Send(deltaBeat); err != nil {
  115. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  116. return "", err
  117. }
  118. case volumeMessage := <-vs.store.DeletedVolumesChan:
  119. deltaBeat := &master_pb.Heartbeat{
  120. DeletedVolumes: []*master_pb.VolumeShortInformationMessage{
  121. &volumeMessage,
  122. },
  123. }
  124. glog.V(1).Infof("volume server %s:%d deletes volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  125. if err = stream.Send(deltaBeat); err != nil {
  126. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  127. return "", err
  128. }
  129. case ecShardMessage := <-vs.store.DeletedEcShardsChan:
  130. deltaBeat := &master_pb.Heartbeat{
  131. DeletedEcShards: []*master_pb.VolumeEcShardInformationMessage{
  132. &ecShardMessage,
  133. },
  134. }
  135. glog.V(1).Infof("volume server %s:%d deletes ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  136. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  137. if err = stream.Send(deltaBeat); err != nil {
  138. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  139. return "", err
  140. }
  141. case <-volumeTickChan:
  142. glog.V(4).Infof("volume server %s:%d heartbeat", vs.store.Ip, vs.store.Port)
  143. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  144. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  145. return "", err
  146. }
  147. case <-ecShardTickChan:
  148. glog.V(4).Infof("volume server %s:%d ec heartbeat", vs.store.Ip, vs.store.Port)
  149. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  150. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  151. return "", err
  152. }
  153. case err = <-doneChan:
  154. return
  155. }
  156. }
  157. }
  158. func isSameIP(ip string, host string) bool {
  159. ips, err := net.LookupIP(host)
  160. if err != nil {
  161. return false
  162. }
  163. for _, t := range ips {
  164. if ip == t.String() {
  165. return true
  166. }
  167. }
  168. return false
  169. }