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.

238 lines
7.8 KiB

5 years ago
3 years ago
5 years ago
3 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/operation"
  5. "time"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  10. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  11. "golang.org/x/net/context"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. func (vs *VolumeServer) GetMaster() string {
  17. return vs.currentMaster
  18. }
  19. func (vs *VolumeServer) checkWithMaster() (err error) {
  20. for {
  21. for _, master := range vs.SeedMasterNodes {
  22. err = operation.WithMasterServerClient(master, vs.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  23. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  24. if err != nil {
  25. return fmt.Errorf("get master %s configuration: %v", master, err)
  26. }
  27. vs.metricsAddress, vs.metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  28. backend.LoadFromPbStorageBackends(resp.StorageBackends)
  29. return nil
  30. })
  31. if err == nil {
  32. return
  33. } else {
  34. glog.V(0).Infof("checkWithMaster %s: %v", master, err)
  35. }
  36. }
  37. time.Sleep(1790 * time.Millisecond)
  38. }
  39. }
  40. func (vs *VolumeServer) heartbeat() {
  41. glog.V(0).Infof("Volume server start with seed master nodes: %v", vs.SeedMasterNodes)
  42. vs.store.SetDataCenter(vs.dataCenter)
  43. vs.store.SetRack(vs.rack)
  44. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.volume")
  45. var err error
  46. var newLeader string
  47. for vs.isHeartbeating {
  48. for _, master := range vs.SeedMasterNodes {
  49. if newLeader != "" {
  50. // the new leader may actually is the same master
  51. // need to wait a bit before adding itself
  52. time.Sleep(3 * time.Second)
  53. master = newLeader
  54. }
  55. masterGrpcAddress, parseErr := pb.ParseServerToGrpcAddress(master)
  56. if parseErr != nil {
  57. glog.V(0).Infof("failed to parse master grpc %v: %v", masterGrpcAddress, parseErr)
  58. continue
  59. }
  60. vs.store.MasterAddress = master
  61. newLeader, err = vs.doHeartbeat(master, masterGrpcAddress, grpcDialOption, time.Duration(vs.pulseSeconds)*time.Second)
  62. if err != nil {
  63. glog.V(0).Infof("heartbeat error: %v", err)
  64. time.Sleep(time.Duration(vs.pulseSeconds) * time.Second)
  65. newLeader = ""
  66. vs.store.MasterAddress = ""
  67. }
  68. if !vs.isHeartbeating {
  69. break
  70. }
  71. }
  72. }
  73. }
  74. func (vs *VolumeServer) StopHeartbeat() (isAlreadyStopping bool) {
  75. if !vs.isHeartbeating {
  76. return true
  77. }
  78. vs.isHeartbeating = false
  79. close(vs.stopChan)
  80. return false
  81. }
  82. func (vs *VolumeServer) doHeartbeat(masterNode, masterGrpcAddress string, grpcDialOption grpc.DialOption, sleepInterval time.Duration) (newLeader string, err error) {
  83. ctx, cancel := context.WithCancel(context.Background())
  84. defer cancel()
  85. grpcConection, err := pb.GrpcDial(ctx, masterGrpcAddress, grpcDialOption)
  86. if err != nil {
  87. return "", fmt.Errorf("fail to dial %s : %v", masterNode, err)
  88. }
  89. defer grpcConection.Close()
  90. client := master_pb.NewSeaweedClient(grpcConection)
  91. stream, err := client.SendHeartbeat(ctx)
  92. if err != nil {
  93. glog.V(0).Infof("SendHeartbeat to %s: %v", masterNode, err)
  94. return "", err
  95. }
  96. glog.V(0).Infof("Heartbeat to: %v", masterNode)
  97. vs.currentMaster = masterNode
  98. doneChan := make(chan error, 1)
  99. go func() {
  100. for {
  101. in, err := stream.Recv()
  102. if err != nil {
  103. doneChan <- err
  104. return
  105. }
  106. if in.GetVolumeSizeLimit() != 0 && vs.store.GetVolumeSizeLimit() != in.GetVolumeSizeLimit() {
  107. vs.store.SetVolumeSizeLimit(in.GetVolumeSizeLimit())
  108. if vs.store.MaybeAdjustVolumeMax() {
  109. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  110. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", vs.currentMaster, err)
  111. return
  112. }
  113. }
  114. }
  115. if in.GetLeader() != "" && vs.currentMaster != in.GetLeader() {
  116. glog.V(0).Infof("Volume Server found a new master newLeader: %v instead of %v", in.GetLeader(), vs.currentMaster)
  117. newLeader = in.GetLeader()
  118. doneChan <- nil
  119. return
  120. }
  121. }
  122. }()
  123. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  124. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  125. return "", err
  126. }
  127. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  128. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  129. return "", err
  130. }
  131. volumeTickChan := time.Tick(sleepInterval)
  132. ecShardTickChan := time.Tick(17 * sleepInterval)
  133. for {
  134. select {
  135. case volumeMessage := <-vs.store.NewVolumesChan:
  136. deltaBeat := &master_pb.Heartbeat{
  137. NewVolumes: []*master_pb.VolumeShortInformationMessage{
  138. &volumeMessage,
  139. },
  140. }
  141. glog.V(1).Infof("volume server %s:%d adds volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  142. if err = stream.Send(deltaBeat); err != nil {
  143. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  144. return "", err
  145. }
  146. case ecShardMessage := <-vs.store.NewEcShardsChan:
  147. deltaBeat := &master_pb.Heartbeat{
  148. NewEcShards: []*master_pb.VolumeEcShardInformationMessage{
  149. &ecShardMessage,
  150. },
  151. }
  152. glog.V(1).Infof("volume server %s:%d adds ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  153. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  154. if err = stream.Send(deltaBeat); err != nil {
  155. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  156. return "", err
  157. }
  158. case volumeMessage := <-vs.store.DeletedVolumesChan:
  159. deltaBeat := &master_pb.Heartbeat{
  160. DeletedVolumes: []*master_pb.VolumeShortInformationMessage{
  161. &volumeMessage,
  162. },
  163. }
  164. glog.V(1).Infof("volume server %s:%d deletes volume %d", vs.store.Ip, vs.store.Port, volumeMessage.Id)
  165. if err = stream.Send(deltaBeat); err != nil {
  166. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  167. return "", err
  168. }
  169. case ecShardMessage := <-vs.store.DeletedEcShardsChan:
  170. deltaBeat := &master_pb.Heartbeat{
  171. DeletedEcShards: []*master_pb.VolumeEcShardInformationMessage{
  172. &ecShardMessage,
  173. },
  174. }
  175. glog.V(1).Infof("volume server %s:%d deletes ec shard %d:%d", vs.store.Ip, vs.store.Port, ecShardMessage.Id,
  176. erasure_coding.ShardBits(ecShardMessage.EcIndexBits).ShardIds())
  177. if err = stream.Send(deltaBeat); err != nil {
  178. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  179. return "", err
  180. }
  181. case <-volumeTickChan:
  182. glog.V(4).Infof("volume server %s:%d heartbeat", vs.store.Ip, vs.store.Port)
  183. vs.store.MaybeAdjustVolumeMax()
  184. if err = stream.Send(vs.store.CollectHeartbeat()); err != nil {
  185. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  186. return "", err
  187. }
  188. case <-ecShardTickChan:
  189. glog.V(4).Infof("volume server %s:%d ec heartbeat", vs.store.Ip, vs.store.Port)
  190. if err = stream.Send(vs.store.CollectErasureCodingHeartbeat()); err != nil {
  191. glog.V(0).Infof("Volume Server Failed to talk with master %s: %v", masterNode, err)
  192. return "", err
  193. }
  194. case err = <-doneChan:
  195. return
  196. case <-vs.stopChan:
  197. var volumeMessages []*master_pb.VolumeInformationMessage
  198. emptyBeat := &master_pb.Heartbeat{
  199. Ip: vs.store.Ip,
  200. Port: uint32(vs.store.Port),
  201. PublicUrl: vs.store.PublicUrl,
  202. MaxFileKey: uint64(0),
  203. DataCenter: vs.store.GetDataCenter(),
  204. Rack: vs.store.GetRack(),
  205. Volumes: volumeMessages,
  206. HasNoVolumes: len(volumeMessages) == 0,
  207. }
  208. glog.V(1).Infof("volume server %s:%d stops and deletes all volumes", vs.store.Ip, vs.store.Port)
  209. if err = stream.Send(emptyBeat); err != nil {
  210. glog.V(0).Infof("Volume Server Failed to update to master %s: %v", masterNode, err)
  211. return "", err
  212. }
  213. return
  214. }
  215. }
  216. }