rack.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "strings"
  8. "time"
  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.NodeImpl.value = r
  20. return r
  21. }
  22. func (r *Rack) FindDataNode(ip string, port int) *DataNode {
  23. for _, c := range r.Children() {
  24. dn := c.(*DataNode)
  25. if dn.MatchLocation(ip, port) {
  26. return dn
  27. }
  28. }
  29. return nil
  30. }
  31. func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl string, maxVolumeCounts map[string]uint32) *DataNode {
  32. r.Lock()
  33. defer r.Unlock()
  34. for _, c := range r.children {
  35. dn := c.(*DataNode)
  36. if dn.MatchLocation(ip, port) {
  37. dn.LastSeen = time.Now().Unix()
  38. return dn
  39. }
  40. }
  41. dn := NewDataNode(util.JoinHostPort(ip, port))
  42. dn.Ip = ip
  43. dn.Port = port
  44. dn.GrpcPort = grpcPort
  45. dn.PublicUrl = publicUrl
  46. dn.LastSeen = time.Now().Unix()
  47. r.doLinkChildNode(dn)
  48. for diskType, maxVolumeCount := range maxVolumeCounts {
  49. disk := NewDisk(diskType)
  50. disk.diskUsages.getOrCreateDisk(types.ToDiskType(diskType)).maxVolumeCount = int64(maxVolumeCount)
  51. dn.LinkChildNode(disk)
  52. }
  53. return dn
  54. }
  55. type RackInfo struct {
  56. Id NodeId `json:"Id"`
  57. DataNodes []DataNodeInfo `json:"DataNodes"`
  58. }
  59. func (r *Rack) ToInfo() (info RackInfo) {
  60. info.Id = r.Id()
  61. var dns []DataNodeInfo
  62. for _, c := range r.Children() {
  63. dn := c.(*DataNode)
  64. dns = append(dns, dn.ToInfo())
  65. }
  66. slices.SortFunc(dns, func(a, b DataNodeInfo) int {
  67. return strings.Compare(a.Url, b.Url)
  68. })
  69. info.DataNodes = dns
  70. return
  71. }
  72. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  73. m := &master_pb.RackInfo{
  74. Id: string(r.Id()),
  75. DiskInfos: r.diskUsages.ToDiskInfo(),
  76. }
  77. for _, c := range r.Children() {
  78. dn := c.(*DataNode)
  79. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  80. }
  81. return m
  82. }