volume_grpc_client_to_master.go 6.1 KB

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