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.

66 lines
1.3 KiB

13 years ago
2 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. 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. }