rack.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package topology
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "golang.org/x/exp/slices"
  7. "time"
  8. )
  9. type Rack struct {
  10. NodeImpl
  11. }
  12. func NewRack(id string) *Rack {
  13. r := &Rack{}
  14. r.id = NodeId(id)
  15. r.nodeType = "Rack"
  16. r.diskUsages = newDiskUsages()
  17. r.children = make(map[NodeId]Node)
  18. r.NodeImpl.value = r
  19. return r
  20. }
  21. func (r *Rack) FindDataNode(ip string, port int) *DataNode {
  22. for _, c := range r.Children() {
  23. dn := c.(*DataNode)
  24. if dn.MatchLocation(ip, port) {
  25. return dn
  26. }
  27. }
  28. return nil
  29. }
  30. func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl string, maxVolumeCounts map[string]uint32) *DataNode {
  31. r.Lock()
  32. defer r.Unlock()
  33. for _, c := range r.children {
  34. dn := c.(*DataNode)
  35. if dn.MatchLocation(ip, port) {
  36. dn.LastSeen = time.Now().Unix()
  37. return dn
  38. }
  39. }
  40. dn := NewDataNode(util.JoinHostPort(ip, port))
  41. dn.Ip = ip
  42. dn.Port = port
  43. dn.GrpcPort = grpcPort
  44. dn.PublicUrl = publicUrl
  45. dn.LastSeen = time.Now().Unix()
  46. r.doLinkChildNode(dn)
  47. for diskType, maxVolumeCount := range maxVolumeCounts {
  48. disk := NewDisk(diskType)
  49. disk.diskUsages.getOrCreateDisk(types.ToDiskType(diskType)).maxVolumeCount = int64(maxVolumeCount)
  50. dn.LinkChildNode(disk)
  51. }
  52. return dn
  53. }
  54. type RackInfo struct {
  55. Id NodeId `json:"Id"`
  56. DataNodes []DataNodeInfo `json:"DataNodes"`
  57. }
  58. func (r *Rack) ToInfo() (info RackInfo) {
  59. info.Id = r.Id()
  60. var dns []DataNodeInfo
  61. for _, c := range r.Children() {
  62. dn := c.(*DataNode)
  63. dns = append(dns, dn.ToInfo())
  64. }
  65. slices.SortFunc(dns, func(a, b DataNodeInfo) bool {
  66. return a.Url < b.Url
  67. })
  68. info.DataNodes = dns
  69. return
  70. }
  71. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  72. m := &master_pb.RackInfo{
  73. Id: string(r.Id()),
  74. DiskInfos: r.diskUsages.ToDiskInfo(),
  75. }
  76. for _, c := range r.Children() {
  77. dn := c.(*DataNode)
  78. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  79. }
  80. return m
  81. }