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.

193 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/raft"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/topology"
  11. "google.golang.org/grpc/peer"
  12. )
  13. func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServer) error {
  14. var dn *topology.DataNode
  15. t := ms.Topo
  16. defer func() {
  17. if dn != nil {
  18. glog.V(0).Infof("unregister disconnected volume server %s:%d", dn.Ip, dn.Port)
  19. t.UnRegisterDataNode(dn)
  20. message := &master_pb.VolumeLocation{
  21. Url: dn.Url(),
  22. PublicUrl: dn.PublicUrl,
  23. }
  24. for _, v := range dn.GetVolumes() {
  25. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  26. }
  27. if len(message.DeletedVids) > 0 {
  28. ms.clientChansLock.RLock()
  29. for _, ch := range ms.clientChans {
  30. ch <- message
  31. }
  32. ms.clientChansLock.RUnlock()
  33. }
  34. }
  35. }()
  36. for {
  37. heartbeat, err := stream.Recv()
  38. if err != nil {
  39. return err
  40. }
  41. if dn == nil {
  42. t.Sequence.SetMax(heartbeat.MaxFileKey)
  43. if heartbeat.Ip == "" {
  44. if pr, ok := peer.FromContext(stream.Context()); ok {
  45. if pr.Addr != net.Addr(nil) {
  46. heartbeat.Ip = pr.Addr.String()[0:strings.LastIndex(pr.Addr.String(), ":")]
  47. glog.V(0).Infof("remote IP address is detected as %v", heartbeat.Ip)
  48. }
  49. }
  50. }
  51. dcName, rackName := t.Configuration.Locate(heartbeat.Ip, heartbeat.DataCenter, heartbeat.Rack)
  52. dc := t.GetOrCreateDataCenter(dcName)
  53. rack := dc.GetOrCreateRack(rackName)
  54. dn = rack.GetOrCreateDataNode(heartbeat.Ip,
  55. int(heartbeat.Port), heartbeat.PublicUrl,
  56. int(heartbeat.MaxVolumeCount))
  57. glog.V(0).Infof("added volume server %v:%d", heartbeat.GetIp(), heartbeat.GetPort())
  58. if err := stream.Send(&master_pb.HeartbeatResponse{
  59. VolumeSizeLimit: uint64(ms.volumeSizeLimitMB) * 1024 * 1024,
  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. ticker := time.NewTicker(5 * time.Second)
  151. for {
  152. select {
  153. case message := <-messageChan:
  154. if err := stream.Send(message); err != nil {
  155. glog.V(0).Infof("=> client %v: %+v", clientName, message)
  156. return err
  157. }
  158. case <-ticker.C:
  159. if !ms.Topo.IsLeader() {
  160. return raft.NotLeaderError
  161. }
  162. case <-stopChan:
  163. return nil
  164. }
  165. }
  166. return nil
  167. }