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.

145 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
  1. package topology
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  5. "strconv"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. )
  9. type DataNode struct {
  10. NodeImpl
  11. volumes map[storage.VolumeId]storage.VolumeInfo
  12. Ip string
  13. Port int
  14. PublicUrl string
  15. LastSeen int64 // unix time in seconds
  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", dn.NodeImpl.String(), dn.volumes, dn.Ip, dn.Port, dn.PublicUrl)
  29. }
  30. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) (isNew bool) {
  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. isNew = true
  41. } else {
  42. dn.volumes[v.Id] = v
  43. }
  44. return
  45. }
  46. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  47. actualVolumeMap := make(map[storage.VolumeId]storage.VolumeInfo)
  48. for _, v := range actualVolumes {
  49. actualVolumeMap[v.Id] = v
  50. }
  51. dn.Lock()
  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. }
  61. dn.Unlock()
  62. for _, v := range actualVolumes {
  63. isNew := dn.AddOrUpdateVolume(v)
  64. if isNew {
  65. newVolumes = append(newVolumes, v)
  66. }
  67. }
  68. return
  69. }
  70. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  71. dn.RLock()
  72. for _, v := range dn.volumes {
  73. ret = append(ret, v)
  74. }
  75. dn.RUnlock()
  76. return ret
  77. }
  78. func (dn *DataNode) GetVolumesById(id storage.VolumeId) (storage.VolumeInfo, error) {
  79. dn.RLock()
  80. defer dn.RUnlock()
  81. vInfo, ok := dn.volumes[id]
  82. if ok {
  83. return vInfo, nil
  84. } else {
  85. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  86. }
  87. }
  88. func (dn *DataNode) GetDataCenter() *DataCenter {
  89. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  90. }
  91. func (dn *DataNode) GetRack() *Rack {
  92. return dn.Parent().(*NodeImpl).value.(*Rack)
  93. }
  94. func (dn *DataNode) GetTopology() *Topology {
  95. p := dn.Parent()
  96. for p.Parent() != nil {
  97. p = p.Parent()
  98. }
  99. t := p.(*Topology)
  100. return t
  101. }
  102. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  103. return dn.Ip == ip && dn.Port == port
  104. }
  105. func (dn *DataNode) Url() string {
  106. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  107. }
  108. func (dn *DataNode) ToMap() interface{} {
  109. ret := make(map[string]interface{})
  110. ret["Url"] = dn.Url()
  111. ret["Volumes"] = dn.GetVolumeCount()
  112. ret["Max"] = dn.GetMaxVolumeCount()
  113. ret["Free"] = dn.FreeSpace()
  114. ret["PublicUrl"] = dn.PublicUrl
  115. return ret
  116. }
  117. func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo {
  118. m := &master_pb.DataNodeInfo{
  119. Id: string(dn.Id()),
  120. VolumeCount: uint64(dn.GetVolumeCount()),
  121. MaxVolumeCount: uint64(dn.GetMaxVolumeCount()),
  122. FreeVolumeCount: uint64(dn.FreeSpace()),
  123. ActiveVolumeCount: uint64(dn.GetActiveVolumeCount()),
  124. }
  125. for _, v := range dn.GetVolumes() {
  126. m.VolumeInfos = append(m.VolumeInfos, v.ToVolumeInformationMessage())
  127. }
  128. return m
  129. }