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.

117 lines
2.7 KiB

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