rack.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "strconv"
  5. "time"
  6. )
  7. type Rack struct {
  8. NodeImpl
  9. }
  10. func NewRack(id string) *Rack {
  11. r := &Rack{}
  12. r.id = NodeId(id)
  13. r.nodeType = "Rack"
  14. r.children = make(map[NodeId]Node)
  15. r.NodeImpl.value = r
  16. return r
  17. }
  18. func (r *Rack) FindDataNode(ip string, port int) *DataNode {
  19. for _, c := range r.Children() {
  20. dn := c.(*DataNode)
  21. if dn.MatchLocation(ip, port) {
  22. return dn
  23. }
  24. }
  25. return nil
  26. }
  27. func (r *Rack) GetOrCreateDataNode(ip string, port int, publicUrl string, maxVolumeCount int64) *DataNode {
  28. for _, c := range r.Children() {
  29. dn := c.(*DataNode)
  30. if dn.MatchLocation(ip, port) {
  31. dn.LastSeen = time.Now().Unix()
  32. return dn
  33. }
  34. }
  35. dn := NewDataNode(ip + ":" + strconv.Itoa(port))
  36. dn.Ip = ip
  37. dn.Port = port
  38. dn.PublicUrl = publicUrl
  39. dn.maxVolumeCount = maxVolumeCount
  40. dn.LastSeen = time.Now().Unix()
  41. r.LinkChildNode(dn)
  42. return dn
  43. }
  44. func (r *Rack) ToMap() interface{} {
  45. m := make(map[string]interface{})
  46. m["Id"] = r.Id()
  47. m["Max"] = r.GetMaxVolumeCount()
  48. m["Free"] = r.FreeSpace()
  49. var dns []interface{}
  50. for _, c := range r.Children() {
  51. dn := c.(*DataNode)
  52. dns = append(dns, dn.ToMap())
  53. }
  54. m["DataNodes"] = dns
  55. return m
  56. }
  57. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  58. m := &master_pb.RackInfo{
  59. Id: string(r.Id()),
  60. VolumeCount: uint64(r.GetVolumeCount()),
  61. MaxVolumeCount: uint64(r.GetMaxVolumeCount()),
  62. FreeVolumeCount: uint64(r.FreeSpace()),
  63. ActiveVolumeCount: uint64(r.GetActiveVolumeCount()),
  64. }
  65. for _, c := range r.Children() {
  66. dn := c.(*DataNode)
  67. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  68. }
  69. return m
  70. }