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.

115 lines
2.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package topology
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/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. dn.RLock()
  27. defer dn.RUnlock()
  28. 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)
  29. }
  30. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) {
  31. dn.Lock()
  32. defer dn.Unlock()
  33. if _, ok := dn.volumes[v.Id]; !ok {
  34. dn.volumes[v.Id] = v
  35. dn.UpAdjustVolumeCountDelta(1)
  36. if !v.ReadOnly {
  37. dn.UpAdjustActiveVolumeCountDelta(1)
  38. }
  39. dn.UpAdjustMaxVolumeId(v.Id)
  40. } else {
  41. dn.volumes[v.Id] = v
  42. }
  43. }
  44. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (deletedVolumes []storage.VolumeInfo) {
  45. actualVolumeMap := make(map[storage.VolumeId]storage.VolumeInfo)
  46. for _, v := range actualVolumes {
  47. actualVolumeMap[v.Id] = v
  48. }
  49. dn.RLock()
  50. for vid, v := range dn.volumes {
  51. if _, ok := actualVolumeMap[vid]; !ok {
  52. glog.V(0).Infoln("Deleting volume id:", vid)
  53. delete(dn.volumes, vid)
  54. deletedVolumes = append(deletedVolumes, v)
  55. dn.UpAdjustVolumeCountDelta(-1)
  56. dn.UpAdjustActiveVolumeCountDelta(-1)
  57. }
  58. } //TODO: adjust max volume id, if need to reclaim volume ids
  59. dn.RUnlock()
  60. for _, v := range actualVolumes {
  61. dn.AddOrUpdateVolume(v)
  62. }
  63. return
  64. }
  65. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  66. dn.RLock()
  67. for _, v := range dn.volumes {
  68. ret = append(ret, v)
  69. }
  70. dn.RUnlock()
  71. return ret
  72. }
  73. func (dn *DataNode) GetDataCenter() *DataCenter {
  74. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  75. }
  76. func (dn *DataNode) GetRack() *Rack {
  77. return dn.Parent().(*NodeImpl).value.(*Rack)
  78. }
  79. func (dn *DataNode) GetTopology() *Topology {
  80. p := dn.Parent()
  81. for p.Parent() != nil {
  82. p = p.Parent()
  83. }
  84. t := p.(*Topology)
  85. return t
  86. }
  87. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  88. return dn.Ip == ip && dn.Port == port
  89. }
  90. func (dn *DataNode) Url() string {
  91. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  92. }
  93. func (dn *DataNode) ToMap() interface{} {
  94. ret := make(map[string]interface{})
  95. ret["Url"] = dn.Url()
  96. ret["Volumes"] = dn.GetVolumeCount()
  97. ret["Max"] = dn.GetMaxVolumeCount()
  98. ret["Free"] = dn.FreeSpace()
  99. ret["PublicUrl"] = dn.PublicUrl
  100. return ret
  101. }