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.

117 lines
3.1 KiB

4 months ago
9 years ago
  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) ListVolumeLayouts() (volumeLayouts []*VolumeLayout) {
  40. for _, col := range t.collectionMap.Items() {
  41. for _, volumeLayout := range col.(*Collection).storageType2VolumeLayout.Items() {
  42. volumeLayouts = append(volumeLayouts, volumeLayout.(*VolumeLayout))
  43. }
  44. }
  45. return volumeLayouts
  46. }
  47. func (t *Topology) ToVolumeMap() interface{} {
  48. m := make(map[string]interface{})
  49. m["Max"] = t.diskUsages.GetMaxVolumeCount()
  50. m["Free"] = t.diskUsages.FreeSpace()
  51. dcs := make(map[NodeId]interface{})
  52. for _, c := range t.Children() {
  53. dc := c.(*DataCenter)
  54. racks := make(map[NodeId]interface{})
  55. for _, r := range dc.Children() {
  56. rack := r.(*Rack)
  57. dataNodes := make(map[NodeId]interface{})
  58. for _, d := range rack.Children() {
  59. dn := d.(*DataNode)
  60. var volumes []interface{}
  61. for _, v := range dn.GetVolumes() {
  62. volumes = append(volumes, v)
  63. }
  64. dataNodes[d.Id()] = volumes
  65. }
  66. racks[r.Id()] = dataNodes
  67. }
  68. dcs[dc.Id()] = racks
  69. }
  70. m["DataCenters"] = dcs
  71. return m
  72. }
  73. func (t *Topology) ToVolumeLocations() (volumeLocations []*master_pb.VolumeLocation) {
  74. for _, c := range t.Children() {
  75. dc := c.(*DataCenter)
  76. for _, r := range dc.Children() {
  77. rack := r.(*Rack)
  78. for _, d := range rack.Children() {
  79. dn := d.(*DataNode)
  80. volumeLocation := &master_pb.VolumeLocation{
  81. Url: dn.Url(),
  82. PublicUrl: dn.PublicUrl,
  83. DataCenter: dn.GetDataCenterId(),
  84. GrpcPort: uint32(dn.GrpcPort),
  85. }
  86. for _, v := range dn.GetVolumes() {
  87. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(v.Id))
  88. }
  89. for _, s := range dn.GetEcShards() {
  90. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(s.VolumeId))
  91. }
  92. volumeLocations = append(volumeLocations, volumeLocation)
  93. }
  94. }
  95. }
  96. return
  97. }
  98. func (t *Topology) ToTopologyInfo() *master_pb.TopologyInfo {
  99. m := &master_pb.TopologyInfo{
  100. Id: string(t.Id()),
  101. DiskInfos: t.diskUsages.ToDiskInfo(),
  102. }
  103. for _, c := range t.Children() {
  104. dc := c.(*DataCenter)
  105. m.DataCenterInfos = append(m.DataCenterInfos, dc.ToDataCenterInfo())
  106. }
  107. return m
  108. }