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.

148 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
9 years ago
9 years ago
6 years ago
9 years ago
6 years ago
  1. package topology
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  5. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/storage"
  9. )
  10. type DataNode struct {
  11. NodeImpl
  12. volumes map[needle.VolumeId]storage.VolumeInfo
  13. Ip string
  14. Port int
  15. PublicUrl string
  16. LastSeen int64 // unix time in seconds
  17. }
  18. func NewDataNode(id string) *DataNode {
  19. s := &DataNode{}
  20. s.id = NodeId(id)
  21. s.nodeType = "DataNode"
  22. s.volumes = make(map[needle.VolumeId]storage.VolumeInfo)
  23. s.NodeImpl.value = s
  24. return s
  25. }
  26. func (dn *DataNode) String() string {
  27. dn.RLock()
  28. defer dn.RUnlock()
  29. return fmt.Sprintf("Node:%s, volumes:%v, Ip:%s, Port:%d, PublicUrl:%s", dn.NodeImpl.String(), dn.volumes, dn.Ip, dn.Port, dn.PublicUrl)
  30. }
  31. func (dn *DataNode) AddOrUpdateVolume(v storage.VolumeInfo) (isNew bool) {
  32. dn.Lock()
  33. defer dn.Unlock()
  34. if _, ok := dn.volumes[v.Id]; !ok {
  35. dn.volumes[v.Id] = v
  36. dn.UpAdjustVolumeCountDelta(1)
  37. if !v.ReadOnly {
  38. dn.UpAdjustActiveVolumeCountDelta(1)
  39. }
  40. dn.UpAdjustMaxVolumeId(v.Id)
  41. isNew = true
  42. } else {
  43. dn.volumes[v.Id] = v
  44. }
  45. return
  46. }
  47. func (dn *DataNode) UpdateVolumes(actualVolumes []storage.VolumeInfo) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  48. actualVolumeMap := make(map[needle.VolumeId]storage.VolumeInfo)
  49. for _, v := range actualVolumes {
  50. actualVolumeMap[v.Id] = v
  51. }
  52. dn.Lock()
  53. for vid, v := range dn.volumes {
  54. if _, ok := actualVolumeMap[vid]; !ok {
  55. glog.V(0).Infoln("Deleting volume id:", vid)
  56. delete(dn.volumes, vid)
  57. deletedVolumes = append(deletedVolumes, v)
  58. dn.UpAdjustVolumeCountDelta(-1)
  59. dn.UpAdjustActiveVolumeCountDelta(-1)
  60. }
  61. }
  62. dn.Unlock()
  63. for _, v := range actualVolumes {
  64. isNew := dn.AddOrUpdateVolume(v)
  65. if isNew {
  66. newVolumes = append(newVolumes, v)
  67. }
  68. }
  69. return
  70. }
  71. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  72. dn.RLock()
  73. for _, v := range dn.volumes {
  74. ret = append(ret, v)
  75. }
  76. dn.RUnlock()
  77. return ret
  78. }
  79. func (dn *DataNode) GetVolumesById(id needle.VolumeId) (storage.VolumeInfo, error) {
  80. dn.RLock()
  81. defer dn.RUnlock()
  82. vInfo, ok := dn.volumes[id]
  83. if ok {
  84. return vInfo, nil
  85. } else {
  86. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  87. }
  88. }
  89. func (dn *DataNode) GetDataCenter() *DataCenter {
  90. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  91. }
  92. func (dn *DataNode) GetRack() *Rack {
  93. return dn.Parent().(*NodeImpl).value.(*Rack)
  94. }
  95. func (dn *DataNode) GetTopology() *Topology {
  96. p := dn.Parent()
  97. for p.Parent() != nil {
  98. p = p.Parent()
  99. }
  100. t := p.(*Topology)
  101. return t
  102. }
  103. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  104. return dn.Ip == ip && dn.Port == port
  105. }
  106. func (dn *DataNode) Url() string {
  107. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  108. }
  109. func (dn *DataNode) ToMap() interface{} {
  110. ret := make(map[string]interface{})
  111. ret["Url"] = dn.Url()
  112. ret["Volumes"] = dn.GetVolumeCount()
  113. ret["Max"] = dn.GetMaxVolumeCount()
  114. ret["Free"] = dn.FreeSpace()
  115. ret["PublicUrl"] = dn.PublicUrl
  116. return ret
  117. }
  118. func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo {
  119. m := &master_pb.DataNodeInfo{
  120. Id: string(dn.Id()),
  121. VolumeCount: uint64(dn.GetVolumeCount()),
  122. MaxVolumeCount: uint64(dn.GetMaxVolumeCount()),
  123. FreeVolumeCount: uint64(dn.FreeSpace()),
  124. ActiveVolumeCount: uint64(dn.GetActiveVolumeCount()),
  125. }
  126. for _, v := range dn.GetVolumes() {
  127. m.VolumeInfos = append(m.VolumeInfos, v.ToVolumeInformationMessage())
  128. }
  129. return m
  130. }