data_center.go 1.3 KB

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