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.

65 lines
1.3 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 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.PublicUrl = publicUrl
  43. dn.maxVolumeCount = maxVolumeCount
  44. dn.LastSeen = time.Now().Unix()
  45. r.LinkChildNode(dn)
  46. return dn
  47. }
  48. func (r *Rack) ToMap() interface{} {
  49. m := make(map[string]interface{})
  50. m["Id"] = r.Id()
  51. m["Max"] = r.GetMaxVolumeCount()
  52. m["Free"] = r.FreeSpace()
  53. var dns []interface{}
  54. for _, c := range r.Children() {
  55. dn := c.(*DataNode)
  56. dns = append(dns, dn.ToMap())
  57. }
  58. m["DataNodes"] = dns
  59. return m
  60. }