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.

89 lines
1.9 KiB

13 years ago
13 years ago
13 years ago
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "github.com/chrislusf/seaweedfs/weed/storage/types"
  5. "github.com/chrislusf/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. for _, c := range r.Children() {
  32. dn := c.(*DataNode)
  33. if dn.MatchLocation(ip, port) {
  34. dn.LastSeen = time.Now().Unix()
  35. return dn
  36. }
  37. }
  38. dn := NewDataNode(util.JoinHostPort(ip, port))
  39. dn.Ip = ip
  40. dn.Port = port
  41. dn.GrpcPort = grpcPort
  42. dn.PublicUrl = publicUrl
  43. dn.LastSeen = time.Now().Unix()
  44. r.LinkChildNode(dn)
  45. for diskType, maxVolumeCount := range maxVolumeCounts {
  46. disk := NewDisk(diskType)
  47. disk.diskUsages.getOrCreateDisk(types.ToDiskType(diskType)).maxVolumeCount = int64(maxVolumeCount)
  48. dn.LinkChildNode(disk)
  49. }
  50. return dn
  51. }
  52. type RackMap struct {
  53. Id NodeId `json:"Id"`
  54. DataNodes []DataNodeMap `json:"DataNodes"`
  55. }
  56. func (r *Rack) ToMap() RackMap {
  57. m := RackMap{}
  58. m.Id = r.Id()
  59. var dns []DataNodeMap
  60. for _, c := range r.Children() {
  61. dn := c.(*DataNode)
  62. dns = append(dns, dn.ToMap())
  63. }
  64. slices.SortFunc(dns, func(a, b DataNodeMap) bool {
  65. return a.Url < b.Url
  66. })
  67. m.DataNodes = dns
  68. return m
  69. }
  70. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  71. m := &master_pb.RackInfo{
  72. Id: string(r.Id()),
  73. DiskInfos: r.diskUsages.ToDiskInfo(),
  74. }
  75. for _, c := range r.Children() {
  76. dn := c.(*DataNode)
  77. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  78. }
  79. return m
  80. }