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.

60 lines
1.2 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. return dn
  32. }
  33. }
  34. dn := NewDataNode(ip + ":" + strconv.Itoa(port))
  35. dn.Ip = ip
  36. dn.Port = port
  37. dn.PublicUrl = publicUrl
  38. dn.maxVolumeCount = maxVolumeCount
  39. dn.LastSeen = time.Now().Unix()
  40. r.LinkChildNode(dn)
  41. return dn
  42. }
  43. func (r *Rack) ToMap() interface{} {
  44. m := make(map[string]interface{})
  45. m["Id"] = r.Id()
  46. m["Max"] = r.GetMaxVolumeCount()
  47. m["Free"] = r.FreeSpace()
  48. var dns []interface{}
  49. for _, c := range r.Children() {
  50. dn := c.(*DataNode)
  51. dns = append(dns, dn.ToMap())
  52. }
  53. m["DataNodes"] = dns
  54. return m
  55. }