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.

188 lines
4.7 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. message := &master_pb.VolumeLocation{
  65. Url: dn.Url(),
  66. PublicUrl: dn.PublicUrl,
  67. }
  68. if len(heartbeat.NewVids) > 0 || len(heartbeat.DeletedVids) > 0 {
  69. // process delta volume ids if exists for fast volume id updates
  70. message.NewVids = append(message.NewVids, heartbeat.NewVids...)
  71. message.DeletedVids = append(message.DeletedVids, heartbeat.DeletedVids...)
  72. } else {
  73. // process heartbeat.Volumes
  74. newVolumes, deletedVolumes := t.SyncDataNodeRegistration(heartbeat.Volumes, dn)
  75. for _, v := range newVolumes {
  76. message.NewVids = append(message.NewVids, uint32(v.Id))
  77. }
  78. for _, v := range deletedVolumes {
  79. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  80. }
  81. }
  82. if len(message.NewVids) > 0 || len(message.DeletedVids) > 0 {
  83. ms.clientChansLock.RLock()
  84. for _, ch := range ms.clientChans {
  85. ch <- message
  86. }
  87. ms.clientChansLock.RUnlock()
  88. }
  89. // tell the volume servers about the leader
  90. newLeader, err := t.Leader()
  91. if err == nil {
  92. if err := stream.Send(&master_pb.HeartbeatResponse{
  93. Leader: newLeader,
  94. }); err != nil {
  95. return err
  96. }
  97. }
  98. }
  99. }
  100. // KeepConnected keep a stream gRPC call to the master. Used by clients to know the master is up.
  101. // And clients gets the up-to-date list of volume locations
  102. func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error {
  103. req, err := stream.Recv()
  104. if err != nil {
  105. return err
  106. }
  107. if !ms.Topo.IsLeader() {
  108. return raft.NotLeaderError
  109. }
  110. // remember client address
  111. ctx := stream.Context()
  112. // fmt.Printf("FromContext %+v\n", ctx)
  113. pr, ok := peer.FromContext(ctx)
  114. if !ok {
  115. glog.Error("failed to get peer from ctx")
  116. return fmt.Errorf("failed to get peer from ctx")
  117. }
  118. if pr.Addr == net.Addr(nil) {
  119. glog.Error("failed to get peer address")
  120. return fmt.Errorf("failed to get peer address")
  121. }
  122. clientName := req.Name + pr.Addr.String()
  123. glog.V(0).Infof("+ client %v", clientName)
  124. messageChan := make(chan *master_pb.VolumeLocation)
  125. stopChan := make(chan bool)
  126. ms.clientChansLock.Lock()
  127. ms.clientChans[clientName] = messageChan
  128. ms.clientChansLock.Unlock()
  129. defer func() {
  130. glog.V(0).Infof("- client %v", clientName)
  131. ms.clientChansLock.Lock()
  132. delete(ms.clientChans, clientName)
  133. ms.clientChansLock.Unlock()
  134. }()
  135. for _, message := range ms.Topo.ToVolumeLocations() {
  136. if err := stream.Send(message); err != nil {
  137. return err
  138. }
  139. }
  140. go func() {
  141. for {
  142. _, err := stream.Recv()
  143. if err != nil {
  144. glog.V(2).Infof("- client %v: %v", clientName, err)
  145. stopChan <- true
  146. break
  147. }
  148. }
  149. }()
  150. for {
  151. select {
  152. case message := <-messageChan:
  153. if err := stream.Send(message); err != nil {
  154. glog.V(0).Infof("=> client %v: %+v", clientName, message)
  155. return err
  156. }
  157. case <-stopChan:
  158. return nil
  159. }
  160. }
  161. return nil
  162. }