volume_grpc_client_to_master.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. grpcConection, err := pb.GrpcDial(context.Background(), masterGrpcAddress, grpcDialOption)
  51. if err != nil {
  52. return "", fmt.Errorf("fail to dial %s : %v", masterNode, err)
  53. }
  54. defer grpcConection.Close()
  55. client := master_pb.NewSeaweedClient(grpcConection)
  56. stream, err := client.SendHeartbeat(context.Background())
  57. if err != nil {
  58. glog.V(0).Infof("SendHeartbeat to %s: %v", masterNode, err)
  59. return "", err
  60. }
  61. glog.V(0).Infof("Heartbeat to: %v", masterNode)
  62. vs.currentMaster = masterNode
  63. doneChan := make(chan error, 1)
  64. go func() {
  65. for {
  66. in, err := stream.Recv()
  67. if err != nil {
  68. doneChan <- err
  69. return
  70. }
  71. if in.GetVolumeSizeLimit() != 0 && vs.store.GetVolumeSizeLimit() != in.GetVolumeSizeLimit() {
  72. vs.store.SetVolumeSizeLimit(in.GetVolumeSizeLimit())
  73. if vs.store.MaybeAdjustVolumeMax() {
  74. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  75. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", vs.currentMaster, err)
  76. }
  77. }
  78. }
  79. if in.GetLeader() != "" && vs.currentMaster != in.GetLeader() {
  80. glog.V(0).Infof("Volume Server found a new master newLeader: %v instead of %v", in.GetLeader(), vs.currentMaster)
  81. newLeader = in.GetLeader()
  82. doneChan <- nil
  83. return
  84. }
  85. if in.GetMetricsAddress() != "" && vs.MetricsAddress != in.GetMetricsAddress() {
  86. vs.MetricsAddress = in.GetMetricsAddress()
  87. vs.MetricsIntervalSec = int(in.GetMetricsIntervalSeconds())
  88. }
  89. if len(in.StorageBackends) > 0 {
  90. backend.LoadFromPbStorageBackends(in.StorageBackends)
  91. }
  92. }
  93. }()
  94. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  95. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  96. return "", err
  97. }
  98. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  99. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  100. return "", err
  101. }
  102. volumeTickChan := time.Tick(sleepInterval)
  103. ecShardTickChan := time.Tick(17 * sleepInterval)
  104. for {
  105. select {
  106. case volumeMessage := <-vs.store.NewVolumesChan:
  107. deltaBeat := &master_pb.Heartbeat{
  108. NewVolumes: []*master_pb.VolumeShortInformationMessage{
  109. &volumeMessage,
  110. },
  111. }
  112. glog.V(1).Infof("volume server %s:%d adds volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  113. if err = stream.Send(deltaBeat); err != nil {
  114. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  115. return "", err
  116. }
  117. case ecShardMessage := <-vs.store.NewEcShardsChan:
  118. deltaBeat := &master_pb.Heartbeat{
  119. NewEcShards: []*master_pb.VolumeEcShardInformationMessage{
  120. &ecShardMessage,
  121. },
  122. }
  123. glog.V(1).Infof("volume server %s:%d adds ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  124. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  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 volumeMessage := <-vs.store.DeletedVolumesChan:
  130. deltaBeat := &master_pb.Heartbeat{
  131. DeletedVolumes: []*master_pb.VolumeShortInformationMessage{
  132. &volumeMessage,
  133. },
  134. }
  135. glog.V(1).Infof("volume server %s:%d deletes volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  136. if err = stream.Send(deltaBeat); err != nil {
  137. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  138. return "", err
  139. }
  140. case ecShardMessage := <-vs.store.DeletedEcShardsChan:
  141. deltaBeat := &master_pb.Heartbeat{
  142. DeletedEcShards: []*master_pb.VolumeEcShardInformationMessage{
  143. &ecShardMessage,
  144. },
  145. }
  146. glog.V(1).Infof("volume server %s:%d deletes ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  147. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  148. if err = stream.Send(deltaBeat); err != nil {
  149. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  150. return "", err
  151. }
  152. case <-volumeTickChan:
  153. glog.V(5).Infof("volume server %s:%d heartbeat", vs.store.Ip, vs.store.Port)
  154. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  155. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  156. return "", err
  157. }
  158. case <-ecShardTickChan:
  159. glog.V(5).Infof("volume server %s:%d ec heartbeat", vs.store.Ip, vs.store.Port)
  160. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  161. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  162. return "", err
  163. }
  164. case err = <-doneChan:
  165. return
  166. }
  167. }
  168. }