node.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package topology
  2. import (
  3. "errors"
  4. "math/rand"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. )
  12. type NodeId string
  13. type Node interface {
  14. Id() NodeId
  15. String() string
  16. FreeSpace() int64
  17. ReserveOneVolume(r int64) (*DataNode, error)
  18. UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int64)
  19. UpAdjustVolumeCountDelta(volumeCountDelta int64)
  20. UpAdjustRemoteVolumeCountDelta(remoteVolumeCountDelta int64)
  21. UpAdjustEcShardCountDelta(ecShardCountDelta int64)
  22. UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int64)
  23. UpAdjustMaxVolumeId(vid needle.VolumeId)
  24. GetVolumeCount() int64
  25. GetEcShardCount() int64
  26. GetActiveVolumeCount() int64
  27. GetRemoteVolumeCount() int64
  28. GetMaxVolumeCount() int64
  29. GetMaxVolumeId() needle.VolumeId
  30. SetParent(Node)
  31. LinkChildNode(node Node)
  32. UnlinkChildNode(nodeId NodeId)
  33. CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64)
  34. IsDataNode() bool
  35. IsRack() bool
  36. IsDataCenter() bool
  37. Children() []Node
  38. Parent() Node
  39. GetValue() interface{} //get reference to the topology,dc,rack,datanode
  40. }
  41. type NodeImpl struct {
  42. volumeCount int64
  43. remoteVolumeCount int64
  44. activeVolumeCount int64
  45. ecShardCount int64
  46. maxVolumeCount int64
  47. id NodeId
  48. parent Node
  49. sync.RWMutex // lock children
  50. children map[NodeId]Node
  51. maxVolumeId needle.VolumeId
  52. //for rack, data center, topology
  53. nodeType string
  54. value interface{}
  55. }
  56. // the first node must satisfy filterFirstNodeFn(), the rest nodes must have one free slot
  57. func (n *NodeImpl) PickNodesByWeight(numberOfNodes int, filterFirstNodeFn func(dn Node) error) (firstNode Node, restNodes []Node, err error) {
  58. var totalWeights int64
  59. var errs []string
  60. n.RLock()
  61. candidates := make([]Node, 0, len(n.children))
  62. candidatesWeights := make([]int64, 0, len(n.children))
  63. //pick nodes which has enough free volumes as candidates, and use free volumes number as node weight.
  64. for _, node := range n.children {
  65. if node.FreeSpace() <= 0 {
  66. continue
  67. }
  68. totalWeights += node.FreeSpace()
  69. candidates = append(candidates, node)
  70. candidatesWeights = append(candidatesWeights, node.FreeSpace())
  71. }
  72. n.RUnlock()
  73. if len(candidates) < numberOfNodes {
  74. glog.V(2).Infoln(n.Id(), "failed to pick", numberOfNodes, "from ", len(candidates), "node candidates")
  75. return nil, nil, errors.New("No enough data node found!")
  76. }
  77. //pick nodes randomly by weights, the node picked earlier has higher final weights
  78. sortedCandidates := make([]Node, 0, len(candidates))
  79. for i := 0; i < len(candidates); i++ {
  80. weightsInterval := rand.Int63n(totalWeights)
  81. lastWeights := int64(0)
  82. for k, weights := range candidatesWeights {
  83. if (weightsInterval >= lastWeights) && (weightsInterval < lastWeights+weights) {
  84. sortedCandidates = append(sortedCandidates, candidates[k])
  85. candidatesWeights[k] = 0
  86. totalWeights -= weights
  87. break
  88. }
  89. lastWeights += weights
  90. }
  91. }
  92. restNodes = make([]Node, 0, numberOfNodes-1)
  93. ret := false
  94. n.RLock()
  95. for k, node := range sortedCandidates {
  96. if err := filterFirstNodeFn(node); err == nil {
  97. firstNode = node
  98. if k >= numberOfNodes-1 {
  99. restNodes = sortedCandidates[:numberOfNodes-1]
  100. } else {
  101. restNodes = append(restNodes, sortedCandidates[:k]...)
  102. restNodes = append(restNodes, sortedCandidates[k+1:numberOfNodes]...)
  103. }
  104. ret = true
  105. break
  106. } else {
  107. errs = append(errs, string(node.Id())+":"+err.Error())
  108. }
  109. }
  110. n.RUnlock()
  111. if !ret {
  112. return nil, nil, errors.New("No matching data node found! \n" + strings.Join(errs, "\n"))
  113. }
  114. return
  115. }
  116. func (n *NodeImpl) IsDataNode() bool {
  117. return n.nodeType == "DataNode"
  118. }
  119. func (n *NodeImpl) IsRack() bool {
  120. return n.nodeType == "Rack"
  121. }
  122. func (n *NodeImpl) IsDataCenter() bool {
  123. return n.nodeType == "DataCenter"
  124. }
  125. func (n *NodeImpl) String() string {
  126. if n.parent != nil {
  127. return n.parent.String() + ":" + string(n.id)
  128. }
  129. return string(n.id)
  130. }
  131. func (n *NodeImpl) Id() NodeId {
  132. return n.id
  133. }
  134. func (n *NodeImpl) FreeSpace() int64 {
  135. freeVolumeSlotCount := n.maxVolumeCount + n.remoteVolumeCount - n.volumeCount
  136. if n.ecShardCount > 0 {
  137. freeVolumeSlotCount = freeVolumeSlotCount - n.ecShardCount/erasure_coding.DataShardsCount - 1
  138. }
  139. return freeVolumeSlotCount
  140. }
  141. func (n *NodeImpl) SetParent(node Node) {
  142. n.parent = node
  143. }
  144. func (n *NodeImpl) Children() (ret []Node) {
  145. n.RLock()
  146. defer n.RUnlock()
  147. for _, c := range n.children {
  148. ret = append(ret, c)
  149. }
  150. return ret
  151. }
  152. func (n *NodeImpl) Parent() Node {
  153. return n.parent
  154. }
  155. func (n *NodeImpl) GetValue() interface{} {
  156. return n.value
  157. }
  158. func (n *NodeImpl) ReserveOneVolume(r int64) (assignedNode *DataNode, err error) {
  159. n.RLock()
  160. defer n.RUnlock()
  161. for _, node := range n.children {
  162. freeSpace := node.FreeSpace()
  163. // fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
  164. if freeSpace <= 0 {
  165. continue
  166. }
  167. if r >= freeSpace {
  168. r -= freeSpace
  169. } else {
  170. if node.IsDataNode() && node.FreeSpace() > 0 {
  171. // fmt.Println("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
  172. return node.(*DataNode), nil
  173. }
  174. assignedNode, err = node.ReserveOneVolume(r)
  175. if err == nil {
  176. return
  177. }
  178. }
  179. }
  180. return nil, errors.New("No free volume slot found!")
  181. }
  182. func (n *NodeImpl) UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int64) { //can be negative
  183. atomic.AddInt64(&n.maxVolumeCount, maxVolumeCountDelta)
  184. if n.parent != nil {
  185. n.parent.UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta)
  186. }
  187. }
  188. func (n *NodeImpl) UpAdjustVolumeCountDelta(volumeCountDelta int64) { //can be negative
  189. atomic.AddInt64(&n.volumeCount, volumeCountDelta)
  190. if n.parent != nil {
  191. n.parent.UpAdjustVolumeCountDelta(volumeCountDelta)
  192. }
  193. }
  194. func (n *NodeImpl) UpAdjustRemoteVolumeCountDelta(remoteVolumeCountDelta int64) { //can be negative
  195. atomic.AddInt64(&n.remoteVolumeCount, remoteVolumeCountDelta)
  196. if n.parent != nil {
  197. n.parent.UpAdjustRemoteVolumeCountDelta(remoteVolumeCountDelta)
  198. }
  199. }
  200. func (n *NodeImpl) UpAdjustEcShardCountDelta(ecShardCountDelta int64) { //can be negative
  201. atomic.AddInt64(&n.ecShardCount, ecShardCountDelta)
  202. if n.parent != nil {
  203. n.parent.UpAdjustEcShardCountDelta(ecShardCountDelta)
  204. }
  205. }
  206. func (n *NodeImpl) UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int64) { //can be negative
  207. atomic.AddInt64(&n.activeVolumeCount, activeVolumeCountDelta)
  208. if n.parent != nil {
  209. n.parent.UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta)
  210. }
  211. }
  212. func (n *NodeImpl) UpAdjustMaxVolumeId(vid needle.VolumeId) { //can be negative
  213. if n.maxVolumeId < vid {
  214. n.maxVolumeId = vid
  215. if n.parent != nil {
  216. n.parent.UpAdjustMaxVolumeId(vid)
  217. }
  218. }
  219. }
  220. func (n *NodeImpl) GetMaxVolumeId() needle.VolumeId {
  221. return n.maxVolumeId
  222. }
  223. func (n *NodeImpl) GetVolumeCount() int64 {
  224. return n.volumeCount
  225. }
  226. func (n *NodeImpl) GetEcShardCount() int64 {
  227. return n.ecShardCount
  228. }
  229. func (n *NodeImpl) GetRemoteVolumeCount() int64 {
  230. return n.remoteVolumeCount
  231. }
  232. func (n *NodeImpl) GetActiveVolumeCount() int64 {
  233. return n.activeVolumeCount
  234. }
  235. func (n *NodeImpl) GetMaxVolumeCount() int64 {
  236. return n.maxVolumeCount
  237. }
  238. func (n *NodeImpl) LinkChildNode(node Node) {
  239. n.Lock()
  240. defer n.Unlock()
  241. if n.children[node.Id()] == nil {
  242. n.children[node.Id()] = node
  243. n.UpAdjustMaxVolumeCountDelta(node.GetMaxVolumeCount())
  244. n.UpAdjustMaxVolumeId(node.GetMaxVolumeId())
  245. n.UpAdjustVolumeCountDelta(node.GetVolumeCount())
  246. n.UpAdjustRemoteVolumeCountDelta(node.GetRemoteVolumeCount())
  247. n.UpAdjustEcShardCountDelta(node.GetEcShardCount())
  248. n.UpAdjustActiveVolumeCountDelta(node.GetActiveVolumeCount())
  249. node.SetParent(n)
  250. glog.V(0).Infoln(n, "adds child", node.Id())
  251. }
  252. }
  253. func (n *NodeImpl) UnlinkChildNode(nodeId NodeId) {
  254. n.Lock()
  255. defer n.Unlock()
  256. node := n.children[nodeId]
  257. if node != nil {
  258. node.SetParent(nil)
  259. delete(n.children, node.Id())
  260. n.UpAdjustVolumeCountDelta(-node.GetVolumeCount())
  261. n.UpAdjustRemoteVolumeCountDelta(-node.GetRemoteVolumeCount())
  262. n.UpAdjustEcShardCountDelta(-node.GetEcShardCount())
  263. n.UpAdjustActiveVolumeCountDelta(-node.GetActiveVolumeCount())
  264. n.UpAdjustMaxVolumeCountDelta(-node.GetMaxVolumeCount())
  265. glog.V(0).Infoln(n, "removes", node.Id())
  266. }
  267. }
  268. func (n *NodeImpl) CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64) {
  269. if n.IsRack() {
  270. for _, c := range n.Children() {
  271. dn := c.(*DataNode) //can not cast n to DataNode
  272. for _, v := range dn.GetVolumes() {
  273. if uint64(v.Size) >= volumeSizeLimit {
  274. //fmt.Println("volume",v.Id,"size",v.Size,">",volumeSizeLimit)
  275. n.GetTopology().chanFullVolumes <- v
  276. }
  277. }
  278. }
  279. } else {
  280. for _, c := range n.Children() {
  281. c.CollectDeadNodeAndFullVolumes(freshThreshHold, volumeSizeLimit)
  282. }
  283. }
  284. }
  285. func (n *NodeImpl) GetTopology() *Topology {
  286. var p Node
  287. p = n
  288. for p.Parent() != nil {
  289. p = p.Parent()
  290. }
  291. return p.GetValue().(*Topology)
  292. }