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.

83 lines
2.0 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. glog.V(2).Infoln("Checking volume id:", vid)
  43. if _, ok := actualVolumeMap[vid]; !ok {
  44. glog.V(0).Infoln("Deleting volume id:", vid)
  45. delete(dn.volumes, vid)
  46. dn.UpAdjustVolumeCountDelta(-1)
  47. dn.UpAdjustActiveVolumeCountDelta(-1)
  48. }
  49. } //TODO: adjust max volume id, if need to reclaim volume ids
  50. for _, v := range actualVolumes {
  51. dn.AddOrUpdateVolume(v)
  52. }
  53. }
  54. func (dn *DataNode) GetDataCenter() *DataCenter {
  55. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  56. }
  57. func (dn *DataNode) GetTopology() *Topology {
  58. p := dn.Parent()
  59. for p.Parent() != nil {
  60. p = p.Parent()
  61. }
  62. t := p.(*Topology)
  63. return t
  64. }
  65. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  66. return dn.Ip == ip && dn.Port == port
  67. }
  68. func (dn *DataNode) Url() string {
  69. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  70. }
  71. func (dn *DataNode) ToMap() interface{} {
  72. ret := make(map[string]interface{})
  73. ret["Url"] = dn.Url()
  74. ret["Volumes"] = dn.GetVolumeCount()
  75. ret["Max"] = dn.GetMaxVolumeCount()
  76. ret["Free"] = dn.FreeSpace()
  77. ret["PublicUrl"] = dn.PublicUrl
  78. return ret
  79. }