topology_info.go 2.8 KB

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