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.

92 lines
2.1 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "strconv"
  6. )
  7. type DataNode struct {
  8. NodeImpl
  9. volumes map[storage.VolumeId]storage.VolumeInfo
  10. Ip string
  11. Port int
  12. PublicUrl string
  13. LastSeen int64 // unix time in seconds
  14. Dead bool
  15. }
  16. func NewDataNode(id string) *DataNode {
  17. s := &DataNode{}
  18. s.id = NodeId(id)
  19. s.nodeType = "DataNode"
  20. s.volumes = make(map[storage.VolumeId]storage.VolumeInfo)
  21. s.NodeImpl.value = s
  22. return s
  23. }
  24. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) {
  25. if _, ok := dn.volumes[v.Id]; !ok {
  26. dn.volumes[v.Id] = v
  27. dn.UpAdjustVolumeCountDelta(1)
  28. if !v.ReadOnly {
  29. dn.UpAdjustActiveVolumeCountDelta(1)
  30. }
  31. dn.UpAdjustMaxVolumeId(v.Id)
  32. } else {
  33. dn.volumes[v.Id] = v
  34. }
  35. }
  36. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) {
  37. actualVolumeMap := make(map[storage.VolumeId]storage.VolumeInfo)
  38. for _, v := range actualVolumes {
  39. actualVolumeMap[v.Id] = v
  40. }
  41. for vid, _ := range dn.volumes {
  42. if _, ok := actualVolumeMap[vid]; !ok {
  43. glog.V(0).Infoln("Deleting volume id:", vid)
  44. delete(dn.volumes, vid)
  45. dn.UpAdjustVolumeCountDelta(-1)
  46. dn.UpAdjustActiveVolumeCountDelta(-1)
  47. }
  48. } //TODO: adjust max volume id, if need to reclaim volume ids
  49. for _, v := range actualVolumes {
  50. dn.AddOrUpdateVolume(v)
  51. }
  52. }
  53. func (dn *DataNode) GetDataCenter() *DataCenter {
  54. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  55. }
  56. func (dn *DataNode) GetRack() *Rack {
  57. return dn.Parent().(*NodeImpl).value.(*Rack)
  58. }
  59. func (dn *DataNode) GetTopology() *Topology {
  60. p := dn.Parent()
  61. for p.Parent() != nil {
  62. p = p.Parent()
  63. }
  64. t := p.(*Topology)
  65. return t
  66. }
  67. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  68. return dn.Ip == ip && dn.Port == port
  69. }
  70. func (dn *DataNode) Url() string {
  71. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  72. }
  73. func (dn *DataNode) ToMap() interface{} {
  74. ret := make(map[string]interface{})
  75. ret["Url"] = dn.Url()
  76. ret["Volumes"] = dn.GetVolumeCount()
  77. ret["Max"] = dn.GetMaxVolumeCount()
  78. ret["Free"] = dn.FreeSpace()
  79. ret["PublicUrl"] = dn.PublicUrl
  80. return ret
  81. }