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.

193 lines
6.5 KiB

5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "time"
  5. "google.golang.org/grpc"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  9. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  10. "golang.org/x/net/context"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  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(util.GetViper(), "grpc.volume")
  23. var err error
  24. var newLeader string
  25. for {
  26. for _, master := range vs.SeedMasterNodes {
  27. if newLeader != "" {
  28. // the new leader may actually is the same master
  29. // need to wait a bit before adding itself
  30. time.Sleep(3 * time.Second)
  31. master = newLeader
  32. }
  33. masterGrpcAddress, parseErr := pb.ParseServerToGrpcAddress(master)
  34. if parseErr != nil {
  35. glog.V(0).Infof("failed to parse master grpc %v: %v", masterGrpcAddress, parseErr)
  36. continue
  37. }
  38. vs.store.MasterAddress = master
  39. newLeader, err = vs.doHeartbeat(master, masterGrpcAddress, grpcDialOption, time.Duration(vs.pulseSeconds)*time.Second)
  40. if err != nil {
  41. glog.V(0).Infof("heartbeat error: %v", err)
  42. time.Sleep(time.Duration(vs.pulseSeconds) * time.Second)
  43. newLeader = ""
  44. vs.store.MasterAddress = ""
  45. }
  46. }
  47. }
  48. }
  49. func (vs *VolumeServer) doHeartbeat(masterNode, masterGrpcAddress string, grpcDialOption grpc.DialOption, sleepInterval time.Duration) (newLeader string, err error) {
  50. ctx, cancel := context.WithCancel(context.Background())
  51. defer cancel()
  52. grpcConection, err := pb.GrpcDial(ctx, masterGrpcAddress, grpcDialOption)
  53. if err != nil {
  54. return "", fmt.Errorf("fail to dial %s : %v", masterNode, err)
  55. }
  56. defer grpcConection.Close()
  57. client := master_pb.NewSeaweedClient(grpcConection)
  58. stream, err := client.SendHeartbeat(ctx)
  59. if err != nil {
  60. glog.V(0).Infof("SendHeartbeat to %s: %v", masterNode, err)
  61. return "", err
  62. }
  63. glog.V(0).Infof("Heartbeat to: %v", masterNode)
  64. vs.currentMaster = masterNode
  65. doneChan := make(chan error, 1)
  66. go func() {
  67. for {
  68. in, err := stream.Recv()
  69. if err != nil {
  70. doneChan <- err
  71. return
  72. }
  73. if in.GetVolumeSizeLimit() != 0 && vs.store.GetVolumeSizeLimit() != in.GetVolumeSizeLimit() {
  74. vs.store.SetVolumeSizeLimit(in.GetVolumeSizeLimit())
  75. if vs.store.MaybeAdjustVolumeMax() {
  76. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  77. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", vs.currentMaster, err)
  78. }
  79. }
  80. }
  81. if in.GetLeader() != "" && vs.currentMaster != in.GetLeader() {
  82. glog.V(0).Infof("Volume Server found a new master newLeader: %v instead of %v", in.GetLeader(), vs.currentMaster)
  83. newLeader = in.GetLeader()
  84. doneChan <- nil
  85. return
  86. }
  87. if in.GetMetricsAddress() != "" && vs.MetricsAddress != in.GetMetricsAddress() {
  88. vs.MetricsAddress = in.GetMetricsAddress()
  89. vs.MetricsIntervalSec = int(in.GetMetricsIntervalSeconds())
  90. }
  91. if len(in.StorageBackends) > 0 {
  92. backend.LoadFromPbStorageBackends(in.StorageBackends)
  93. }
  94. }
  95. }()
  96. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  97. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  98. return "", err
  99. }
  100. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  101. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  102. return "", err
  103. }
  104. volumeTickChan := time.Tick(sleepInterval)
  105. ecShardTickChan := time.Tick(17 * sleepInterval)
  106. for {
  107. select {
  108. case volumeMessage := <-vs.store.NewVolumesChan:
  109. deltaBeat := &master_pb.Heartbeat{
  110. NewVolumes: []*master_pb.VolumeShortInformationMessage{
  111. &volumeMessage,
  112. },
  113. }
  114. glog.V(1).Infof("volume server %s:%d adds volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  115. if err = stream.Send(deltaBeat); err != nil {
  116. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  117. return "", err
  118. }
  119. case ecShardMessage := <-vs.store.NewEcShardsChan:
  120. deltaBeat := &master_pb.Heartbeat{
  121. NewEcShards: []*master_pb.VolumeEcShardInformationMessage{
  122. &ecShardMessage,
  123. },
  124. }
  125. glog.V(1).Infof("volume server %s:%d adds ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  126. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  127. if err = stream.Send(deltaBeat); err != nil {
  128. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  129. return "", err
  130. }
  131. case volumeMessage := <-vs.store.DeletedVolumesChan:
  132. deltaBeat := &master_pb.Heartbeat{
  133. DeletedVolumes: []*master_pb.VolumeShortInformationMessage{
  134. &volumeMessage,
  135. },
  136. }
  137. glog.V(1).Infof("volume server %s:%d deletes volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  138. if err = stream.Send(deltaBeat); err != nil {
  139. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  140. return "", err
  141. }
  142. case ecShardMessage := <-vs.store.DeletedEcShardsChan:
  143. deltaBeat := &master_pb.Heartbeat{
  144. DeletedEcShards: []*master_pb.VolumeEcShardInformationMessage{
  145. &ecShardMessage,
  146. },
  147. }
  148. glog.V(1).Infof("volume server %s:%d deletes ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  149. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  150. if err = stream.Send(deltaBeat); err != nil {
  151. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  152. return "", err
  153. }
  154. case <-volumeTickChan:
  155. if vs.SendHeartbeat {
  156. glog.V(4).Infof("volume server %s:%d heartbeat", vs.store.Ip, vs.store.Port)
  157. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  158. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  159. return "", err
  160. }
  161. } else {
  162. glog.V(4).Infof("volume server %s:%d skip send heartbeat", vs.store.Ip, vs.store.Port)
  163. }
  164. case <-ecShardTickChan:
  165. glog.V(4).Infof("volume server %s:%d ec heartbeat", vs.store.Ip, vs.store.Port)
  166. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  167. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  168. return "", err
  169. }
  170. case err = <-doneChan:
  171. return
  172. }
  173. }
  174. }