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.

100 lines
2.4 KiB

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