topology_info.go 3.3 KB

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