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.

106 lines
2.7 KiB

9 years ago
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "golang.org/x/exp/slices"
  5. )
  6. type TopologyMap struct {
  7. Max int64 `json:"Max"`
  8. Free int64 `json:"Free"`
  9. DataCenters []DataCenterMap `json:"DataCenters"`
  10. Layouts []interface{} `json:"Layouts"`
  11. }
  12. func (t *Topology) ToMap() interface{} {
  13. m := TopologyMap{}
  14. m.Max = t.diskUsages.GetMaxVolumeCount()
  15. m.Free = t.diskUsages.FreeSpace()
  16. var dcs []DataCenterMap
  17. for _, c := range t.Children() {
  18. dc := c.(*DataCenter)
  19. dcs = append(dcs, dc.ToMap())
  20. }
  21. slices.SortFunc(dcs, func(a, b DataCenterMap) bool {
  22. return a.Id < b.Id
  23. })
  24. m.DataCenters = dcs
  25. var layouts []interface{}
  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).ToMap()
  31. tmp["collection"] = c.Name
  32. layouts = append(layouts, tmp)
  33. }
  34. }
  35. }
  36. m.Layouts = layouts
  37. return m
  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. }
  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. }