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.

88 lines
1.9 KiB

13 years ago
13 years ago
13 years ago
  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. 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 RackInfo struct {
  53. Id NodeId `json:"Id"`
  54. DataNodes []DataNodeInfo `json:"DataNodes"`
  55. }
  56. func (r *Rack) ToInfo() (info RackInfo) {
  57. info.Id = r.Id()
  58. var dns []DataNodeInfo
  59. for _, c := range r.Children() {
  60. dn := c.(*DataNode)
  61. dns = append(dns, dn.ToInfo())
  62. }
  63. slices.SortFunc(dns, func(a, b DataNodeInfo) bool {
  64. return a.Url < b.Url
  65. })
  66. info.DataNodes = dns
  67. return
  68. }
  69. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  70. m := &master_pb.RackInfo{
  71. Id: string(r.Id()),
  72. DiskInfos: r.diskUsages.ToDiskInfo(),
  73. }
  74. for _, c := range r.Children() {
  75. dn := c.(*DataNode)
  76. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  77. }
  78. return m
  79. }