master_grpc_server.go 7.4 KB

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