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.

107 lines
2.8 KiB

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) 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. }
  77. for _, v := range dn.GetVolumes() {
  78. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(v.Id))
  79. }
  80. for _, s := range dn.GetEcShards() {
  81. volumeLocation.NewVids = append(volumeLocation.NewVids, uint32(s.VolumeId))
  82. }
  83. volumeLocations = append(volumeLocations, volumeLocation)
  84. }
  85. }
  86. }
  87. return
  88. }
  89. func (t *Topology) ToTopologyInfo() *master_pb.TopologyInfo {
  90. m := &master_pb.TopologyInfo{
  91. Id: string(t.Id()),
  92. DiskInfos: t.diskUsages.ToDiskInfo(),
  93. }
  94. for _, c := range t.Children() {
  95. dc := c.(*DataCenter)
  96. m.DataCenterInfos = append(m.DataCenterInfos, dc.ToDataCenterInfo())
  97. }
  98. return m
  99. }