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.

81 lines
2.2 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "net"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/topology"
  8. "google.golang.org/grpc/peer"
  9. )
  10. func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServer) error {
  11. var dn *topology.DataNode
  12. t := ms.Topo
  13. defer func() {
  14. if dn != nil {
  15. glog.V(0).Infof("unregister disconnected volume server %s:%d", dn.Ip, dn.Port)
  16. t.UnRegisterDataNode(dn)
  17. }
  18. }()
  19. for {
  20. heartbeat, err := stream.Recv()
  21. if err == nil {
  22. if dn == nil {
  23. t.Sequence.SetMax(heartbeat.MaxFileKey)
  24. if heartbeat.Ip == "" {
  25. if pr, ok := peer.FromContext(stream.Context()); ok {
  26. if pr.Addr != net.Addr(nil) {
  27. heartbeat.Ip = pr.Addr.String()[0:strings.LastIndex(pr.Addr.String(), ":")]
  28. glog.V(0).Infof("remote IP address is detected as %v", heartbeat.Ip)
  29. }
  30. }
  31. }
  32. dcName, rackName := t.Configuration.Locate(heartbeat.Ip, heartbeat.DataCenter, heartbeat.Rack)
  33. dc := t.GetOrCreateDataCenter(dcName)
  34. rack := dc.GetOrCreateRack(rackName)
  35. dn = rack.GetOrCreateDataNode(heartbeat.Ip,
  36. int(heartbeat.Port), heartbeat.PublicUrl,
  37. int(heartbeat.MaxVolumeCount))
  38. glog.V(0).Infof("added volume server %v:%d", heartbeat.GetIp(), heartbeat.GetPort())
  39. if err := stream.Send(&master_pb.HeartbeatResponse{
  40. VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024,
  41. SecretKey: string(ms.guard.SecretKey),
  42. }); err != nil {
  43. return err
  44. }
  45. }
  46. t.SyncDataNodeRegistration(heartbeat.Volumes, dn)
  47. } else {
  48. return err
  49. }
  50. // tell the volume servers about the leader
  51. newLeader, err := t.Leader()
  52. if err == nil {
  53. if err := stream.Send(&master_pb.HeartbeatResponse{
  54. Leader: newLeader,
  55. }); err != nil {
  56. return err
  57. }
  58. }
  59. }
  60. }
  61. // KeepConnected keep a stream gRPC call to the master. Used by filer to know the master is up.
  62. func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error {
  63. for {
  64. _, err := stream.Recv()
  65. if err != nil {
  66. return err
  67. }
  68. if err := stream.Send(&master_pb.Empty{}); err != nil {
  69. return err
  70. }
  71. }
  72. }