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.

200 lines
5.2 KiB

13 years ago
13 years ago
13 years ago
  1. package topology
  2. import (
  3. "fmt"
  4. "code.google.com/p/weed-fs/weed/storage"
  5. )
  6. type NodeId string
  7. type Node interface {
  8. Id() NodeId
  9. String() string
  10. FreeSpace() int
  11. ReserveOneVolume(r int, vid storage.VolumeId) (bool, *DataNode)
  12. UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int)
  13. UpAdjustVolumeCountDelta(volumeCountDelta int)
  14. UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int)
  15. UpAdjustMaxVolumeId(vid storage.VolumeId)
  16. GetVolumeCount() int
  17. GetActiveVolumeCount() int
  18. GetMaxVolumeCount() int
  19. GetMaxVolumeId() storage.VolumeId
  20. SetParent(Node)
  21. LinkChildNode(node Node)
  22. UnlinkChildNode(nodeId NodeId)
  23. CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64)
  24. IsDataNode() bool
  25. Children() map[NodeId]Node
  26. Parent() Node
  27. GetValue() interface{} //get reference to the topology,dc,rack,datanode
  28. }
  29. type NodeImpl struct {
  30. id NodeId
  31. volumeCount int
  32. activeVolumeCount int
  33. maxVolumeCount int
  34. parent Node
  35. children map[NodeId]Node
  36. maxVolumeId storage.VolumeId
  37. //for rack, data center, topology
  38. nodeType string
  39. value interface{}
  40. }
  41. func (n *NodeImpl) IsDataNode() bool {
  42. return n.nodeType == "DataNode"
  43. }
  44. func (n *NodeImpl) IsRack() bool {
  45. return n.nodeType == "Rack"
  46. }
  47. func (n *NodeImpl) IsDataCenter() bool {
  48. return n.nodeType == "DataCenter"
  49. }
  50. func (n *NodeImpl) String() string {
  51. if n.parent != nil {
  52. return n.parent.String() + ":" + string(n.id)
  53. }
  54. return string(n.id)
  55. }
  56. func (n *NodeImpl) Id() NodeId {
  57. return n.id
  58. }
  59. func (n *NodeImpl) FreeSpace() int {
  60. return n.maxVolumeCount - n.volumeCount
  61. }
  62. func (n *NodeImpl) SetParent(node Node) {
  63. n.parent = node
  64. }
  65. func (n *NodeImpl) Children() map[NodeId]Node {
  66. return n.children
  67. }
  68. func (n *NodeImpl) Parent() Node {
  69. return n.parent
  70. }
  71. func (n *NodeImpl) GetValue() interface{} {
  72. return n.value
  73. }
  74. func (n *NodeImpl) ReserveOneVolume(r int, vid storage.VolumeId) (bool, *DataNode) {
  75. ret := false
  76. var assignedNode *DataNode
  77. for _, node := range n.children {
  78. freeSpace := node.FreeSpace()
  79. //fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
  80. if freeSpace <= 0 {
  81. continue
  82. }
  83. if r >= freeSpace {
  84. r -= freeSpace
  85. } else {
  86. if node.IsDataNode() && node.FreeSpace() > 0 {
  87. //fmt.Println("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
  88. return true, node.(*DataNode)
  89. }
  90. ret, assignedNode = node.ReserveOneVolume(r, vid)
  91. if ret {
  92. break
  93. }
  94. }
  95. }
  96. return ret, assignedNode
  97. }
  98. func (n *NodeImpl) UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int) { //can be negative
  99. n.maxVolumeCount += maxVolumeCountDelta
  100. if n.parent != nil {
  101. n.parent.UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta)
  102. }
  103. }
  104. func (n *NodeImpl) UpAdjustVolumeCountDelta(volumeCountDelta int) { //can be negative
  105. n.volumeCount += volumeCountDelta
  106. if n.parent != nil {
  107. n.parent.UpAdjustVolumeCountDelta(volumeCountDelta)
  108. }
  109. }
  110. func (n *NodeImpl) UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int) { //can be negative
  111. n.activeVolumeCount += activeVolumeCountDelta
  112. if n.parent != nil {
  113. n.parent.UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta)
  114. }
  115. }
  116. func (n *NodeImpl) UpAdjustMaxVolumeId(vid storage.VolumeId) { //can be negative
  117. if n.maxVolumeId < vid {
  118. n.maxVolumeId = vid
  119. if n.parent != nil {
  120. n.parent.UpAdjustMaxVolumeId(vid)
  121. }
  122. }
  123. }
  124. func (n *NodeImpl) GetMaxVolumeId() storage.VolumeId {
  125. return n.maxVolumeId
  126. }
  127. func (n *NodeImpl) GetVolumeCount() int {
  128. return n.volumeCount
  129. }
  130. func (n *NodeImpl) GetActiveVolumeCount() int {
  131. return n.activeVolumeCount
  132. }
  133. func (n *NodeImpl) GetMaxVolumeCount() int {
  134. return n.maxVolumeCount
  135. }
  136. func (n *NodeImpl) LinkChildNode(node Node) {
  137. if n.children[node.Id()] == nil {
  138. n.children[node.Id()] = node
  139. n.UpAdjustMaxVolumeCountDelta(node.GetMaxVolumeCount())
  140. n.UpAdjustMaxVolumeId(node.GetMaxVolumeId())
  141. n.UpAdjustVolumeCountDelta(node.GetVolumeCount())
  142. n.UpAdjustActiveVolumeCountDelta(node.GetActiveVolumeCount())
  143. node.SetParent(n)
  144. fmt.Println(n, "adds child", node.Id())
  145. }
  146. }
  147. func (n *NodeImpl) UnlinkChildNode(nodeId NodeId) {
  148. node := n.children[nodeId]
  149. node.SetParent(nil)
  150. if node != nil {
  151. delete(n.children, node.Id())
  152. n.UpAdjustVolumeCountDelta(-node.GetVolumeCount())
  153. n.UpAdjustActiveVolumeCountDelta(-node.GetActiveVolumeCount())
  154. n.UpAdjustMaxVolumeCountDelta(-node.GetMaxVolumeCount())
  155. fmt.Println(n, "removes", node, "volumeCount =", n.activeVolumeCount)
  156. }
  157. }
  158. func (n *NodeImpl) CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64) {
  159. if n.IsRack() {
  160. for _, c := range n.Children() {
  161. dn := c.(*DataNode) //can not cast n to DataNode
  162. if dn.LastSeen < freshThreshHold {
  163. if !dn.Dead {
  164. dn.Dead = true
  165. n.GetTopology().chanDeadDataNodes <- dn
  166. }
  167. }
  168. for _, v := range dn.volumes {
  169. if uint64(v.Size) >= volumeSizeLimit {
  170. //fmt.Println("volume",v.Id,"size",v.Size,">",volumeSizeLimit)
  171. n.GetTopology().chanFullVolumes <- v
  172. }
  173. }
  174. }
  175. } else {
  176. for _, c := range n.Children() {
  177. c.CollectDeadNodeAndFullVolumes(freshThreshHold, volumeSizeLimit)
  178. }
  179. }
  180. }
  181. func (n *NodeImpl) GetTopology() *Topology {
  182. var p Node
  183. p = n
  184. for p.Parent() != nil {
  185. p = p.Parent()
  186. }
  187. return p.GetValue().(*Topology)
  188. }