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.

64 lines
1.3 KiB

13 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 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 DataCenterInfo struct {
  30. Id NodeId `json:"Id"`
  31. Racks []RackInfo `json:"Racks"`
  32. }
  33. func (dc *DataCenter) ToInfo() (info DataCenterInfo) {
  34. info.Id = dc.Id()
  35. var racks []RackInfo
  36. for _, c := range dc.Children() {
  37. rack := c.(*Rack)
  38. racks = append(racks, rack.ToInfo())
  39. }
  40. slices.SortFunc(racks, func(a, b RackInfo) bool {
  41. return a.Id < b.Id
  42. })
  43. info.Racks = racks
  44. return
  45. }
  46. func (dc *DataCenter) ToDataCenterInfo() *master_pb.DataCenterInfo {
  47. m := &master_pb.DataCenterInfo{
  48. Id: string(dc.Id()),
  49. DiskInfos: dc.diskUsages.ToDiskInfo(),
  50. }
  51. for _, c := range dc.Children() {
  52. rack := c.(*Rack)
  53. m.RackInfos = append(m.RackInfos, rack.ToRackInfo())
  54. }
  55. return m
  56. }