rack.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. RemoteVolumeCount: uint64(r.GetRemoteVolumeCount()),
  65. }
  66. for _, c := range r.Children() {
  67. dn := c.(*DataNode)
  68. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  69. }
  70. return m
  71. }