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.

170 lines
4.1 KiB

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