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.

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