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.

95 lines
2.2 KiB

  1. package topology
  2. import (
  3. "strconv"
  4. "github.com/chrislusf/weed-fs/go/glog"
  5. "github.com/chrislusf/weed-fs/go/storage"
  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) (deletedVolumes []storage.VolumeInfo) {
  37. actualVolumeMap := make(map[storage.VolumeId]storage.VolumeInfo)
  38. for _, v := range actualVolumes {
  39. actualVolumeMap[v.Id] = v
  40. }
  41. for vid, v := range dn.volumes {
  42. if _, ok := actualVolumeMap[vid]; !ok {
  43. glog.V(0).Infoln("Deleting volume id:", vid)
  44. delete(dn.volumes, vid)
  45. deletedVolumes = append(deletedVolumes, v)
  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. return
  54. }
  55. func (dn *DataNode) GetDataCenter() *DataCenter {
  56. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  57. }
  58. func (dn *DataNode) GetRack() *Rack {
  59. return dn.Parent().(*NodeImpl).value.(*Rack)
  60. }
  61. func (dn *DataNode) GetTopology() *Topology {
  62. p := dn.Parent()
  63. for p.Parent() != nil {
  64. p = p.Parent()
  65. }
  66. t := p.(*Topology)
  67. return t
  68. }
  69. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  70. return dn.Ip == ip && dn.Port == port
  71. }
  72. func (dn *DataNode) Url() string {
  73. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  74. }
  75. func (dn *DataNode) ToMap() interface{} {
  76. ret := make(map[string]interface{})
  77. ret["Url"] = dn.Url()
  78. ret["Volumes"] = dn.GetVolumeCount()
  79. ret["Max"] = dn.GetMaxVolumeCount()
  80. ret["Free"] = dn.FreeSpace()
  81. ret["PublicUrl"] = dn.PublicUrl
  82. return ret
  83. }