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.

162 lines
3.8 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) DeltaUpdateVolumes(newlVolumes, deletedVolumes []storage.VolumeInfo) {
  72. dn.Lock()
  73. for _, v := range deletedVolumes {
  74. delete(dn.volumes, v.Id)
  75. dn.UpAdjustVolumeCountDelta(-1)
  76. dn.UpAdjustActiveVolumeCountDelta(-1)
  77. }
  78. dn.Unlock()
  79. for _, v := range newlVolumes {
  80. dn.AddOrUpdateVolume(v)
  81. }
  82. return
  83. }
  84. func (dn *DataNode) GetVolumes() (ret []storage.VolumeInfo) {
  85. dn.RLock()
  86. for _, v := range dn.volumes {
  87. ret = append(ret, v)
  88. }
  89. dn.RUnlock()
  90. return ret
  91. }
  92. func (dn *DataNode) GetVolumesById(id needle.VolumeId) (storage.VolumeInfo, error) {
  93. dn.RLock()
  94. defer dn.RUnlock()
  95. vInfo, ok := dn.volumes[id]
  96. if ok {
  97. return vInfo, nil
  98. } else {
  99. return storage.VolumeInfo{}, fmt.Errorf("volumeInfo not found")
  100. }
  101. }
  102. func (dn *DataNode) GetDataCenter() *DataCenter {
  103. return dn.Parent().Parent().(*NodeImpl).value.(*DataCenter)
  104. }
  105. func (dn *DataNode) GetRack() *Rack {
  106. return dn.Parent().(*NodeImpl).value.(*Rack)
  107. }
  108. func (dn *DataNode) GetTopology() *Topology {
  109. p := dn.Parent()
  110. for p.Parent() != nil {
  111. p = p.Parent()
  112. }
  113. t := p.(*Topology)
  114. return t
  115. }
  116. func (dn *DataNode) MatchLocation(ip string, port int) bool {
  117. return dn.Ip == ip && dn.Port == port
  118. }
  119. func (dn *DataNode) Url() string {
  120. return dn.Ip + ":" + strconv.Itoa(dn.Port)
  121. }
  122. func (dn *DataNode) ToMap() interface{} {
  123. ret := make(map[string]interface{})
  124. ret["Url"] = dn.Url()
  125. ret["Volumes"] = dn.GetVolumeCount()
  126. ret["Max"] = dn.GetMaxVolumeCount()
  127. ret["Free"] = dn.FreeSpace()
  128. ret["PublicUrl"] = dn.PublicUrl
  129. return ret
  130. }
  131. func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo {
  132. m := &master_pb.DataNodeInfo{
  133. Id: string(dn.Id()),
  134. VolumeCount: uint64(dn.GetVolumeCount()),
  135. MaxVolumeCount: uint64(dn.GetMaxVolumeCount()),
  136. FreeVolumeCount: uint64(dn.FreeSpace()),
  137. ActiveVolumeCount: uint64(dn.GetActiveVolumeCount()),
  138. }
  139. for _, v := range dn.GetVolumes() {
  140. m.VolumeInfos = append(m.VolumeInfos, v.ToVolumeInformationMessage())
  141. }
  142. return m
  143. }