topology_info.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package topology
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  4. "golang.org/x/exp/slices"
  5. )
  6. type TopologyInfo struct {
  7. Max int64 `json:"Max"`
  8. Free int64 `json:"Free"`
  9. DataCenters []DataCenterInfo `json:"DataCenters"`
  10. Layouts []VolumeLayoutInfo `json:"Layouts"`
  11. }
  12. func (t *Topology) ToInfo() (info TopologyInfo) {
  13. info.Max = t.diskUsages.GetMaxVolumeCount()
  14. info.Free = t.diskUsages.FreeSpace()
  15. var dcs []DataCenterInfo
  16. for _, c := range t.Children() {
  17. dc := c.(*DataCenter)
  18. dcs = append(dcs, dc.ToInfo())
  19. }
  20. slices.SortFunc(dcs, func(a, b DataCenterInfo) bool {
  21. return a.Id < b.Id
  22. })
  23. info.DataCenters = dcs
  24. var layouts []VolumeLayoutInfo
  25. for _, col := range t.collectionMap.Items() {
  26. c := col.(*Collection)
  27. for _, layout := range c.storageType2VolumeLayout.Items() {
  28. if layout != nil {
  29. tmp := layout.(*VolumeLayout).ToInfo()
  30. tmp.Collection = c.Name
  31. layouts = append(layouts, tmp)
  32. }
  33. }
  34. }
  35. info.Layouts = layouts
  36. return
  37. }
  38. func (t *Topology) ToVolumeMap() interface{} {
  39. m := make(map[string]interface{})
  40. m["Max"] = t.diskUsages.GetMaxVolumeCount()
  41. m["Free"] = t.diskUsages.FreeSpace()
  42. dcs := make(map[NodeId]interface{})
  43. for _, c := range t.Children() {
  44. dc := c.(*DataCenter)
  45. racks := make(map[NodeId]interface{})
  46. for _, r := range dc.Children() {
  47. rack := r.(*Rack)
  48. dataNodes := make(map[NodeId]interface{})
  49. for _, d := range rack.Children() {
  50. dn := d.(*DataNode)
  51. var volumes []interface{}
  52. for _, v := range dn.GetVolumes() {
  53. volumes = append(volumes, v)
  54. }
  55. dataNodes[d.Id()] = volumes
  56. }
  57. racks[r.Id()] = dataNodes
  58. }
  59. dcs[dc.Id()] = racks
  60. }
  61. m["DataCenters"] = dcs
  62. return m
  63. }
  64. func (t *Topology) ToVolumeLocations() (volumeLocations []*master_pb.VolumeLocation) {
  65. for _, c := range t.Children() {
  66. dc := c.(*DataCenter)
  67. for _, r := range dc.Children() {
  68. rack := r.(*Rack)
  69. for _, d := range rack.Children() {
  70. dn := d.(*DataNode)
  71. volumeLocation := &master_pb.VolumeLocation{
  72. Url: dn.Url(),
  73. PublicUrl: dn.PublicUrl,
  74. DataCenter: dn.GetDataCenterId(),
  75. }
  76. for _, v := range dn.GetVolumes() {
  77. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(v.Id))
  78. }
  79. for _, s := range dn.GetEcShards() {
  80. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(s.VolumeId))
  81. }
  82. volumeLocations = append(volumeLocations, volumeLocation)
  83. }
  84. }
  85. }
  86. return
  87. }
  88. func (t *Topology) ToTopologyInfo() *master_pb.TopologyInfo {
  89. m := &master_pb.TopologyInfo{
  90. Id: string(t.Id()),
  91. DiskInfos: t.diskUsages.ToDiskInfo(),
  92. }
  93. for _, c := range t.Children() {
  94. dc := c.(*DataCenter)
  95. m.DataCenterInfos = append(m.DataCenterInfos, dc.ToDataCenterInfo())
  96. }
  97. return m
  98. }