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.

247 lines
8.1 KiB

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