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.

180 lines
4.3 KiB

7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "github.com/chrislusf/raft"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/topology"
  10. "google.golang.org/grpc/peer"
  11. )
  12. func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServer) error {
  13. var dn *topology.DataNode
  14. t := ms.Topo
  15. defer func() {
  16. if dn != nil {
  17. glog.V(0).Infof("unregister disconnected volume server %s:%d", dn.Ip, dn.Port)
  18. t.UnRegisterDataNode(dn)
  19. message := &master_pb.VolumeLocation{
  20. Url: dn.Url(),
  21. PublicUrl: dn.PublicUrl,
  22. }
  23. for _, v := range dn.GetVolumes() {
  24. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  25. }
  26. if len(message.DeletedVids) > 0 {
  27. ms.clientChansLock.RLock()
  28. for _, ch := range ms.clientChans {
  29. ch <- message
  30. }
  31. ms.clientChansLock.RUnlock()
  32. }
  33. }
  34. }()
  35. for {
  36. heartbeat, err := stream.Recv()
  37. if err != nil {
  38. return err
  39. }
  40. if dn == nil {
  41. t.Sequence.SetMax(heartbeat.MaxFileKey)
  42. if heartbeat.Ip == "" {
  43. if pr, ok := peer.FromContext(stream.Context()); ok {
  44. if pr.Addr != net.Addr(nil) {
  45. heartbeat.Ip = pr.Addr.String()[0:strings.LastIndex(pr.Addr.String(), ":")]
  46. glog.V(0).Infof("remote IP address is detected as %v", heartbeat.Ip)
  47. }
  48. }
  49. }
  50. dcName, rackName := t.Configuration.Locate(heartbeat.Ip, heartbeat.DataCenter, heartbeat.Rack)
  51. dc := t.GetOrCreateDataCenter(dcName)
  52. rack := dc.GetOrCreateRack(rackName)
  53. dn = rack.GetOrCreateDataNode(heartbeat.Ip,
  54. int(heartbeat.Port), heartbeat.PublicUrl,
  55. int(heartbeat.MaxVolumeCount))
  56. glog.V(0).Infof("added volume server %v:%d", heartbeat.GetIp(), heartbeat.GetPort())
  57. if err := stream.Send(&master_pb.HeartbeatResponse{
  58. VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024,
  59. SecretKey: string(ms.guard.SecretKey),
  60. }); err != nil {
  61. return err
  62. }
  63. }
  64. newVolumes, deletedVolumes := t.SyncDataNodeRegistration(heartbeat.Volumes, dn)
  65. message := &master_pb.VolumeLocation{
  66. Url: dn.Url(),
  67. PublicUrl: dn.PublicUrl,
  68. }
  69. for _, v := range newVolumes {
  70. message.NewVids = append(message.NewVids, uint32(v.Id))
  71. }
  72. for _, v := range deletedVolumes {
  73. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  74. }
  75. if len(message.NewVids) > 0 || len(message.DeletedVids) > 0 {
  76. ms.clientChansLock.RLock()
  77. for _, ch := range ms.clientChans {
  78. ch <- message
  79. }
  80. ms.clientChansLock.RUnlock()
  81. }
  82. // tell the volume servers about the leader
  83. newLeader, err := t.Leader()
  84. if err == nil {
  85. if err := stream.Send(&master_pb.HeartbeatResponse{
  86. Leader: newLeader,
  87. }); err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. }
  93. // KeepConnected keep a stream gRPC call to the master. Used by clients to know the master is up.
  94. // And clients gets the up-to-date list of volume locations
  95. func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error {
  96. req, err := stream.Recv()
  97. if err != nil {
  98. return err
  99. }
  100. if !ms.Topo.IsLeader() {
  101. return raft.NotLeaderError
  102. }
  103. // remember client address
  104. ctx := stream.Context()
  105. // fmt.Printf("FromContext %+v\n", ctx)
  106. pr, ok := peer.FromContext(ctx)
  107. if !ok {
  108. glog.Error("failed to get peer from ctx")
  109. return fmt.Errorf("failed to get peer from ctx")
  110. }
  111. if pr.Addr == net.Addr(nil) {
  112. glog.Error("failed to get peer address")
  113. return fmt.Errorf("failed to get peer address")
  114. }
  115. clientName := req.Name + pr.Addr.String()
  116. glog.V(0).Infof("+ client %v", clientName)
  117. messageChan := make(chan *master_pb.VolumeLocation)
  118. stopChan := make(chan bool)
  119. ms.clientChansLock.Lock()
  120. ms.clientChans[clientName] = messageChan
  121. ms.clientChansLock.Unlock()
  122. defer func() {
  123. glog.V(0).Infof("- client %v", clientName)
  124. ms.clientChansLock.Lock()
  125. delete(ms.clientChans, clientName)
  126. ms.clientChansLock.Unlock()
  127. }()
  128. for _, message := range ms.Topo.ToVolumeLocations() {
  129. if err := stream.Send(message); err != nil {
  130. return err
  131. }
  132. }
  133. go func() {
  134. for {
  135. _, err := stream.Recv()
  136. if err != nil {
  137. glog.V(2).Infof("- client %v: %v", clientName, err)
  138. stopChan <- true
  139. break
  140. }
  141. }
  142. }()
  143. for {
  144. select {
  145. case message := <-messageChan:
  146. if err := stream.Send(message); err != nil {
  147. return err
  148. }
  149. case <-stopChan:
  150. return nil
  151. }
  152. }
  153. return nil
  154. }