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.

81 lines
1.9 KiB

13 years ago
13 years ago
13 years ago
  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, maxSsdVolumeCount 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.maxSsdVolumeCount = maxSsdVolumeCount
  41. dn.LastSeen = time.Now().Unix()
  42. r.LinkChildNode(dn)
  43. return dn
  44. }
  45. func (r *Rack) ToMap() interface{} {
  46. m := make(map[string]interface{})
  47. m["Id"] = r.Id()
  48. m["Max"] = r.GetMaxVolumeCount()
  49. m["MaxSsd"] = r.GetMaxSsdVolumeCount()
  50. m["Free"] = r.FreeSpace()
  51. var dns []interface{}
  52. for _, c := range r.Children() {
  53. dn := c.(*DataNode)
  54. dns = append(dns, dn.ToMap())
  55. }
  56. m["DataNodes"] = dns
  57. return m
  58. }
  59. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  60. m := &master_pb.RackInfo{
  61. Id: string(r.Id()),
  62. VolumeCount: uint64(r.GetVolumeCount()),
  63. MaxVolumeCount: uint64(r.GetMaxVolumeCount()),
  64. MaxSsdVolumeCount: uint64(r.GetMaxSsdVolumeCount()),
  65. SsdVolumeCount: uint64(r.GetSsdVolumeCount()),
  66. FreeVolumeCount: uint64(r.FreeSpace()),
  67. ActiveVolumeCount: uint64(r.GetActiveVolumeCount()),
  68. RemoteVolumeCount: uint64(r.GetRemoteVolumeCount()),
  69. }
  70. for _, c := range r.Children() {
  71. dn := c.(*DataNode)
  72. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  73. }
  74. return m
  75. }