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.

65 lines
1.3 KiB

13 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 DataCenter struct {
  7. NodeImpl
  8. }
  9. func NewDataCenter(id string) *DataCenter {
  10. dc := &DataCenter{}
  11. dc.id = NodeId(id)
  12. dc.nodeType = "DataCenter"
  13. dc.diskUsages = newDiskUsages()
  14. dc.children = make(map[NodeId]Node)
  15. dc.NodeImpl.value = dc
  16. return dc
  17. }
  18. func (dc *DataCenter) GetOrCreateRack(rackName string) *Rack {
  19. for _, c := range dc.Children() {
  20. rack := c.(*Rack)
  21. if string(rack.Id()) == rackName {
  22. return rack
  23. }
  24. }
  25. rack := NewRack(rackName)
  26. dc.LinkChildNode(rack)
  27. return rack
  28. }
  29. type DataCenterMap struct {
  30. Id NodeId `json:"Id"`
  31. Racks []RackMap `json:"Racks"`
  32. }
  33. func (dc *DataCenter) ToMap() DataCenterMap {
  34. m := DataCenterMap{}
  35. m.Id = dc.Id()
  36. var racks []RackMap
  37. for _, c := range dc.Children() {
  38. rack := c.(*Rack)
  39. racks = append(racks, rack.ToMap())
  40. }
  41. slices.SortFunc(racks, func(a, b RackMap) bool {
  42. return a.Id < b.Id
  43. })
  44. m.Racks = racks
  45. return m
  46. }
  47. func (dc *DataCenter) ToDataCenterInfo() *master_pb.DataCenterInfo {
  48. m := &master_pb.DataCenterInfo{
  49. Id: string(dc.Id()),
  50. DiskInfos: dc.diskUsages.ToDiskInfo(),
  51. }
  52. for _, c := range dc.Children() {
  53. rack := c.(*Rack)
  54. m.RackInfos = append(m.RackInfos, rack.ToRackInfo())
  55. }
  56. return m
  57. }