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.

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