data_center.go 1.3 KB

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