data_center.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. )
  5. type DataCenter struct {
  6. NodeImpl
  7. }
  8. func NewDataCenter(id string) *DataCenter {
  9. dc := &DataCenter{}
  10. dc.id = NodeId(id)
  11. dc.nodeType = "DataCenter"
  12. dc.diskUsages = newDiskUsages()
  13. dc.children = make(map[NodeId]Node)
  14. dc.NodeImpl.value = dc
  15. return dc
  16. }
  17. func (dc *DataCenter) GetOrCreateRack(rackName string) *Rack {
  18. for _, c := range dc.Children() {
  19. rack := c.(*Rack)
  20. if string(rack.Id()) == rackName {
  21. return rack
  22. }
  23. }
  24. rack := NewRack(rackName)
  25. dc.LinkChildNode(rack)
  26. return rack
  27. }
  28. func (dc *DataCenter) ToMap() interface{} {
  29. m := make(map[string]interface{})
  30. m["Id"] = dc.Id()
  31. var racks []interface{}
  32. for _, c := range dc.Children() {
  33. rack := c.(*Rack)
  34. racks = append(racks, rack.ToMap())
  35. }
  36. m["Racks"] = racks
  37. return m
  38. }
  39. func (dc *DataCenter) ToDataCenterInfo() *master_pb.DataCenterInfo {
  40. m := &master_pb.DataCenterInfo{
  41. Id: string(dc.Id()),
  42. DiskInfos: dc.diskUsages.ToDiskInfo(),
  43. }
  44. for _, c := range dc.Children() {
  45. rack := c.(*Rack)
  46. m.RackInfos = append(m.RackInfos, rack.ToRackInfo())
  47. }
  48. return m
  49. }