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.

204 lines
5.3 KiB

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