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.

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