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.

126 lines
2.8 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. 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.Lock()
  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. }
  59. dn.Unlock()
  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) GetVolumesById(id storage.VolumeId) (storage.VolumeInfo, error) {
  74. dn.RLock()
  75. defer dn.RUnlock()
  76. v_info, ok := dn.volumes[id]
  77. if ok {
  78. return v_info, nil
  79. } else {
  80. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  81. }
  82. }
  83. func (dn *DataNode) GetDataCenter() *DataCenter {
  84. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  85. }
  86. func (dn *DataNode) GetRack() *Rack {
  87. return dn.Parent().(*NodeImpl).value.(*Rack)
  88. }
  89. func (dn *DataNode) GetTopology() *Topology {
  90. p := dn.Parent()
  91. for p.Parent() != nil {
  92. p = p.Parent()
  93. }
  94. t := p.(*Topology)
  95. return t
  96. }
  97. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  98. return dn.Ip == ip && dn.Port == port
  99. }
  100. func (dn *DataNode) Url() string {
  101. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  102. }
  103. func (dn *DataNode) ToMap() interface{} {
  104. ret := make(map[string]interface{})
  105. ret["Url"] = dn.Url()
  106. ret["Volumes"] = dn.GetVolumeCount()
  107. ret["Max"] = dn.GetMaxVolumeCount()
  108. ret["Free"] = dn.FreeSpace()
  109. ret["PublicUrl"] = dn.PublicUrl
  110. return ret
  111. }