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.

66 lines
1.4 KiB

13 years ago
13 years ago
13 years ago
  1. package topology
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. type Rack struct {
  7. NodeImpl
  8. }
  9. func NewRack(id string) *Rack {
  10. r := &Rack{}
  11. r.id = NodeId(id)
  12. r.nodeType = "Rack"
  13. r.children = make(map[NodeId]Node)
  14. r.NodeImpl.value = r
  15. return r
  16. }
  17. func (r *Rack) FindDataNode(ip string, port int) *DataNode {
  18. for _, c := range r.Children() {
  19. dn := c.(*DataNode)
  20. if dn.MatchLocation(ip, port) {
  21. return dn
  22. }
  23. }
  24. return nil
  25. }
  26. func (r *Rack) GetOrCreateDataNode(ip string, port, adminPort int, publicUrl string, maxVolumeCount int) *DataNode {
  27. for _, c := range r.Children() {
  28. dn := c.(*DataNode)
  29. if dn.MatchLocation(ip, port) {
  30. dn.LastSeen = time.Now().Unix()
  31. if dn.Dead {
  32. dn.Dead = false
  33. r.GetTopology().chanRecoveredDataNodes <- dn
  34. dn.UpAdjustMaxVolumeCountDelta(maxVolumeCount - dn.maxVolumeCount)
  35. }
  36. return dn
  37. }
  38. }
  39. dn := NewDataNode(ip + ":" + strconv.Itoa(port))
  40. dn.Ip = ip
  41. dn.Port = port
  42. dn.AdminPort = adminPort
  43. dn.PublicUrl = publicUrl
  44. dn.maxVolumeCount = maxVolumeCount
  45. dn.LastSeen = time.Now().Unix()
  46. r.LinkChildNode(dn)
  47. return dn
  48. }
  49. func (rack *Rack) ToMap() interface{} {
  50. m := make(map[string]interface{})
  51. m["Id"] = rack.Id()
  52. m["Max"] = rack.GetMaxVolumeCount()
  53. m["Free"] = rack.FreeSpace()
  54. var dns []interface{}
  55. for _, c := range rack.Children() {
  56. dn := c.(*DataNode)
  57. dns = append(dns, dn.ToMap())
  58. }
  59. m["DataNodes"] = dns
  60. return m
  61. }