data_center.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. dc.Lock()
  20. defer dc.Unlock()
  21. for _, c := range dc.children {
  22. rack := c.(*Rack)
  23. if string(rack.Id()) == rackName {
  24. return rack
  25. }
  26. }
  27. rack := NewRack(rackName)
  28. dc.doLinkChildNode(rack)
  29. return rack
  30. }
  31. type DataCenterInfo struct {
  32. Id NodeId `json:"Id"`
  33. Racks []RackInfo `json:"Racks"`
  34. }
  35. func (dc *DataCenter) ToInfo() (info DataCenterInfo) {
  36. info.Id = dc.Id()
  37. var racks []RackInfo
  38. for _, c := range dc.Children() {
  39. rack := c.(*Rack)
  40. racks = append(racks, rack.ToInfo())
  41. }
  42. slices.SortFunc(racks, func(a, b RackInfo) bool {
  43. return a.Id < b.Id
  44. })
  45. info.Racks = racks
  46. return
  47. }
  48. func (dc *DataCenter) ToDataCenterInfo() *master_pb.DataCenterInfo {
  49. m := &master_pb.DataCenterInfo{
  50. Id: string(dc.Id()),
  51. DiskInfos: dc.diskUsages.ToDiskInfo(),
  52. }
  53. for _, c := range dc.Children() {
  54. rack := c.(*Rack)
  55. m.RackInfos = append(m.RackInfos, rack.ToRackInfo())
  56. }
  57. return m
  58. }