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.

263 lines
7.3 KiB

7 years ago
7 years ago
6 years ago
5 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
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/chrislusf/raft"
  7. "google.golang.org/grpc/peer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. )
  14. func (ms *MasterServer) SendHeartbeat(stream master_pb.Seaweed_SendHeartbeatServer) error {
  15. var dn *topology.DataNode
  16. t := ms.Topo
  17. defer func() {
  18. if dn != nil {
  19. glog.V(0).Infof("unregister disconnected volume server %s:%d", dn.Ip, dn.Port)
  20. t.UnRegisterDataNode(dn)
  21. message := &master_pb.VolumeLocation{
  22. Url: dn.Url(),
  23. PublicUrl: dn.PublicUrl,
  24. }
  25. for _, v := range dn.GetVolumes() {
  26. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  27. }
  28. for _, s := range dn.GetEcShards() {
  29. message.DeletedVids = append(message.DeletedVids, uint32(s.VolumeId))
  30. }
  31. if len(message.DeletedVids) > 0 {
  32. ms.clientChansLock.RLock()
  33. for _, ch := range ms.clientChans {
  34. ch <- message
  35. }
  36. ms.clientChansLock.RUnlock()
  37. }
  38. }
  39. }()
  40. for {
  41. heartbeat, err := stream.Recv()
  42. if err != nil {
  43. if dn != nil {
  44. glog.Warningf("SendHeartbeat.Recv server %s:%d : %v", dn.Ip, dn.Port, err)
  45. } else {
  46. glog.Warningf("SendHeartbeat.Recv: %v", err)
  47. }
  48. return err
  49. }
  50. t.Sequence.SetMax(heartbeat.MaxFileKey)
  51. if dn == nil {
  52. dcName, rackName := t.Configuration.Locate(heartbeat.Ip, heartbeat.DataCenter, heartbeat.Rack)
  53. dc := t.GetOrCreateDataCenter(dcName)
  54. rack := dc.GetOrCreateRack(rackName)
  55. dn = rack.GetOrCreateDataNode(heartbeat.Ip,
  56. int(heartbeat.Port), heartbeat.PublicUrl,
  57. int64(heartbeat.MaxVolumeCount))
  58. glog.V(0).Infof("added volume server %v:%d", heartbeat.GetIp(), heartbeat.GetPort())
  59. if err := stream.Send(&master_pb.HeartbeatResponse{
  60. VolumeSizeLimit: uint64(ms.option.VolumeSizeLimitMB) * 1024 * 1024,
  61. MetricsAddress: ms.option.MetricsAddress,
  62. MetricsIntervalSeconds: uint32(ms.option.MetricsIntervalSec),
  63. StorageBackends: backend.ToPbStorageBackends(),
  64. }); err != nil {
  65. glog.Warningf("SendHeartbeat.Send volume size to %s:%d %v", dn.Ip, dn.Port, err)
  66. return err
  67. }
  68. }
  69. glog.V(4).Infof("master received heartbeat %s", heartbeat.String())
  70. message := &master_pb.VolumeLocation{
  71. Url: dn.Url(),
  72. PublicUrl: dn.PublicUrl,
  73. }
  74. if len(heartbeat.NewVolumes) > 0 || len(heartbeat.DeletedVolumes) > 0 {
  75. // process delta volume ids if exists for fast volume id updates
  76. for _, volInfo := range heartbeat.NewVolumes {
  77. message.NewVids = append(message.NewVids, volInfo.Id)
  78. }
  79. for _, volInfo := range heartbeat.DeletedVolumes {
  80. message.DeletedVids = append(message.DeletedVids, volInfo.Id)
  81. }
  82. // update master internal volume layouts
  83. t.IncrementalSyncDataNodeRegistration(heartbeat.NewVolumes, heartbeat.DeletedVolumes, dn)
  84. }
  85. if len(heartbeat.Volumes) > 0 || heartbeat.HasNoVolumes {
  86. // process heartbeat.Volumes
  87. newVolumes, deletedVolumes := t.SyncDataNodeRegistration(heartbeat.Volumes, dn)
  88. for _, v := range newVolumes {
  89. glog.V(0).Infof("master see new volume %d from %s", uint32(v.Id), dn.Url())
  90. message.NewVids = append(message.NewVids, uint32(v.Id))
  91. }
  92. for _, v := range deletedVolumes {
  93. glog.V(0).Infof("master see deleted volume %d from %s", uint32(v.Id), dn.Url())
  94. message.DeletedVids = append(message.DeletedVids, uint32(v.Id))
  95. }
  96. }
  97. if len(heartbeat.NewEcShards) > 0 || len(heartbeat.DeletedEcShards) > 0 {
  98. // update master internal volume layouts
  99. t.IncrementalSyncDataNodeEcShards(heartbeat.NewEcShards, heartbeat.DeletedEcShards, dn)
  100. for _, s := range heartbeat.NewEcShards {
  101. message.NewVids = append(message.NewVids, s.Id)
  102. }
  103. for _, s := range heartbeat.DeletedEcShards {
  104. if dn.HasVolumesById(needle.VolumeId(s.Id)) {
  105. continue
  106. }
  107. message.DeletedVids = append(message.DeletedVids, s.Id)
  108. }
  109. }
  110. if len(heartbeat.EcShards) > 0 || heartbeat.HasNoEcShards {
  111. glog.V(1).Infof("master recieved ec shards from %s: %+v", dn.Url(), heartbeat.EcShards)
  112. newShards, deletedShards := t.SyncDataNodeEcShards(heartbeat.EcShards, dn)
  113. // broadcast the ec vid changes to master clients
  114. for _, s := range newShards {
  115. message.NewVids = append(message.NewVids, uint32(s.VolumeId))
  116. }
  117. for _, s := range deletedShards {
  118. if dn.HasVolumesById(s.VolumeId) {
  119. continue
  120. }
  121. message.DeletedVids = append(message.DeletedVids, uint32(s.VolumeId))
  122. }
  123. }
  124. if len(message.NewVids) > 0 || len(message.DeletedVids) > 0 {
  125. ms.clientChansLock.RLock()
  126. for host, ch := range ms.clientChans {
  127. glog.V(0).Infof("master send to %s: %s", host, message.String())
  128. ch <- message
  129. }
  130. ms.clientChansLock.RUnlock()
  131. }
  132. // tell the volume servers about the leader
  133. newLeader, err := t.Leader()
  134. if err != nil {
  135. glog.Warningf("SendHeartbeat find leader: %v", err)
  136. return err
  137. }
  138. if err := stream.Send(&master_pb.HeartbeatResponse{
  139. Leader: newLeader,
  140. }); err != nil {
  141. glog.Warningf("SendHeartbeat.Send response to to %s:%d %v", dn.Ip, dn.Port, err)
  142. return err
  143. }
  144. }
  145. }
  146. // KeepConnected keep a stream gRPC call to the master. Used by clients to know the master is up.
  147. // And clients gets the up-to-date list of volume locations
  148. func (ms *MasterServer) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error {
  149. req, err := stream.Recv()
  150. if err != nil {
  151. return err
  152. }
  153. if !ms.Topo.IsLeader() {
  154. return ms.informNewLeader(stream)
  155. }
  156. // remember client address
  157. ctx := stream.Context()
  158. // fmt.Printf("FromContext %+v\n", ctx)
  159. pr, ok := peer.FromContext(ctx)
  160. if !ok {
  161. glog.Error("failed to get peer from ctx")
  162. return fmt.Errorf("failed to get peer from ctx")
  163. }
  164. if pr.Addr == net.Addr(nil) {
  165. glog.Error("failed to get peer address")
  166. return fmt.Errorf("failed to get peer address")
  167. }
  168. clientName := req.Name + pr.Addr.String()
  169. glog.V(0).Infof("+ client %v", clientName)
  170. messageChan := make(chan *master_pb.VolumeLocation)
  171. stopChan := make(chan bool)
  172. ms.clientChansLock.Lock()
  173. ms.clientChans[clientName] = messageChan
  174. ms.clientChansLock.Unlock()
  175. defer func() {
  176. glog.V(0).Infof("- client %v", clientName)
  177. ms.clientChansLock.Lock()
  178. delete(ms.clientChans, clientName)
  179. ms.clientChansLock.Unlock()
  180. }()
  181. for _, message := range ms.Topo.ToVolumeLocations() {
  182. if err := stream.Send(message); err != nil {
  183. return err
  184. }
  185. }
  186. go func() {
  187. for {
  188. _, err := stream.Recv()
  189. if err != nil {
  190. glog.V(2).Infof("- client %v: %v", clientName, err)
  191. stopChan <- true
  192. break
  193. }
  194. }
  195. }()
  196. ticker := time.NewTicker(5 * time.Second)
  197. for {
  198. select {
  199. case message := <-messageChan:
  200. if err := stream.Send(message); err != nil {
  201. glog.V(0).Infof("=> client %v: %+v", clientName, message)
  202. return err
  203. }
  204. case <-ticker.C:
  205. if !ms.Topo.IsLeader() {
  206. return ms.informNewLeader(stream)
  207. }
  208. case <-stopChan:
  209. return nil
  210. }
  211. }
  212. return nil
  213. }
  214. func (ms *MasterServer) informNewLeader(stream master_pb.Seaweed_KeepConnectedServer) error {
  215. leader, err := ms.Topo.Leader()
  216. if err != nil {
  217. glog.Errorf("topo leader: %v", err)
  218. return raft.NotLeaderError
  219. }
  220. if err := stream.Send(&master_pb.VolumeLocation{
  221. Leader: leader,
  222. }); err != nil {
  223. return err
  224. }
  225. return nil
  226. }