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.

130 lines
2.9 KiB

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. }
  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) String() string {
  25. dn.RLock()
  26. defer dn.RUnlock()
  27. return fmt.Sprintf("Node:%s, volumes:%v, Ip:%s, Port:%d, PublicUrl:%s", dn.NodeImpl.String(), dn.volumes, dn.Ip, dn.Port, dn.PublicUrl)
  28. }
  29. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) (isNew bool) {
  30. dn.Lock()
  31. defer dn.Unlock()
  32. if _, ok := dn.volumes[v.Id]; !ok {
  33. dn.volumes[v.Id] = v
  34. dn.UpAdjustVolumeCountDelta(1)
  35. if !v.ReadOnly {
  36. dn.UpAdjustActiveVolumeCountDelta(1)
  37. }
  38. dn.UpAdjustMaxVolumeId(v.Id)
  39. isNew = true
  40. } else {
  41. dn.volumes[v.Id] = v
  42. }
  43. return
  44. }
  45. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  46. actualVolumeMap := make(map[storage.VolumeId]storage.VolumeInfo)
  47. for _, v := range actualVolumes {
  48. actualVolumeMap[v.Id] = v
  49. }
  50. dn.Lock()
  51. for vid, v := range dn.volumes {
  52. if _, ok := actualVolumeMap[vid]; !ok {
  53. glog.V(0).Infoln("Deleting volume id:", vid)
  54. delete(dn.volumes, vid)
  55. deletedVolumes = append(deletedVolumes, v)
  56. dn.UpAdjustVolumeCountDelta(-1)
  57. dn.UpAdjustActiveVolumeCountDelta(-1)
  58. }
  59. }
  60. dn.Unlock()
  61. for _, v := range actualVolumes {
  62. isNew := dn.AddOrUpdateVolume(v)
  63. if isNew {
  64. newVolumes = append(newVolumes, v)
  65. }
  66. }
  67. return
  68. }
  69. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  70. dn.RLock()
  71. for _, v := range dn.volumes {
  72. ret = append(ret, v)
  73. }
  74. dn.RUnlock()
  75. return ret
  76. }
  77. func (dn *DataNode) GetVolumesById(id storage.VolumeId) (storage.VolumeInfo, error) {
  78. dn.RLock()
  79. defer dn.RUnlock()
  80. vInfo, ok := dn.volumes[id]
  81. if ok {
  82. return vInfo, nil
  83. } else {
  84. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  85. }
  86. }
  87. func (dn *DataNode) GetDataCenter() *DataCenter {
  88. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  89. }
  90. func (dn *DataNode) GetRack() *Rack {
  91. return dn.Parent().(*NodeImpl).value.(*Rack)
  92. }
  93. func (dn *DataNode) GetTopology() *Topology {
  94. p := dn.Parent()
  95. for p.Parent() != nil {
  96. p = p.Parent()
  97. }
  98. t := p.(*Topology)
  99. return t
  100. }
  101. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  102. return dn.Ip == ip && dn.Port == port
  103. }
  104. func (dn *DataNode) Url() string {
  105. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  106. }
  107. func (dn *DataNode) ToMap() interface{} {
  108. ret := make(map[string]interface{})
  109. ret["Url"] = dn.Url()
  110. ret["Volumes"] = dn.GetVolumeCount()
  111. ret["Max"] = dn.GetMaxVolumeCount()
  112. ret["Free"] = dn.FreeSpace()
  113. ret["PublicUrl"] = dn.PublicUrl
  114. return ret
  115. }