rack.go 2.0 KB

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