2012-08-24 11:56:09 +08:00
|
|
|
package topology
|
|
|
|
|
2012-08-24 13:56:14 +08:00
|
|
|
import (
|
2012-08-31 16:35:11 +08:00
|
|
|
"fmt"
|
2012-08-29 15:58:03 +08:00
|
|
|
"pkg/storage"
|
2012-08-24 13:56:14 +08:00
|
|
|
)
|
2012-08-24 11:56:09 +08:00
|
|
|
|
2012-08-28 16:04:39 +08:00
|
|
|
type NodeId string
|
2012-09-03 05:33:48 +08:00
|
|
|
type Node interface {
|
|
|
|
Id() NodeId
|
|
|
|
String() string
|
|
|
|
FreeSpace() int
|
|
|
|
ReserveOneVolume(r int, vid storage.VolumeId) (bool, *Server)
|
|
|
|
UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int)
|
|
|
|
UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int)
|
|
|
|
UpAdjustMaxVolumeId(vid storage.VolumeId)
|
|
|
|
GetActiveVolumeCount() int
|
|
|
|
GetMaxVolumeCount() int
|
|
|
|
GetMaxVolumeId() storage.VolumeId
|
|
|
|
setParent(Node)
|
|
|
|
LinkChildNode(node Node)
|
|
|
|
UnlinkChildNode(nodeId NodeId)
|
|
|
|
|
|
|
|
IsServer() bool
|
2012-08-24 11:56:09 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
type NodeImpl struct {
|
|
|
|
id NodeId
|
|
|
|
activeVolumeCount int
|
|
|
|
maxVolumeCount int
|
|
|
|
parent Node
|
|
|
|
children map[NodeId]Node
|
|
|
|
maxVolumeId storage.VolumeId
|
2012-08-29 15:58:03 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
//for rack, data center, topology
|
|
|
|
nodeType string
|
2012-09-01 17:20:59 +08:00
|
|
|
}
|
2012-08-31 16:35:11 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) IsServer() bool {
|
|
|
|
return n.nodeType == "Server"
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) IsRack() bool {
|
|
|
|
return n.nodeType == "Rack"
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) IsDataCenter() bool {
|
|
|
|
return n.nodeType == "DataCenter"
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) String() string {
|
|
|
|
if n.parent != nil {
|
|
|
|
return n.parent.String() + ":" + string(n.id)
|
|
|
|
}
|
|
|
|
return string(n.id)
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) Id() NodeId {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) FreeSpace() int {
|
|
|
|
return n.maxVolumeCount - n.activeVolumeCount
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) setParent(node Node) {
|
|
|
|
n.parent = node
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) ReserveOneVolume(r int, vid storage.VolumeId) (bool, *Server) {
|
|
|
|
ret := false
|
|
|
|
var assignedNode *Server
|
2012-08-29 15:58:03 +08:00
|
|
|
for _, node := range n.children {
|
2012-09-03 05:33:48 +08:00
|
|
|
freeSpace := node.FreeSpace()
|
|
|
|
fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
|
|
|
|
if freeSpace <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2012-09-01 17:20:59 +08:00
|
|
|
if r >= freeSpace {
|
2012-08-29 15:58:03 +08:00
|
|
|
r -= freeSpace
|
|
|
|
} else {
|
2012-09-03 05:33:48 +08:00
|
|
|
if node.IsServer() && node.FreeSpace()>0 {
|
|
|
|
fmt.Println("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
|
|
|
|
return true, node.(*Server)
|
2012-09-01 17:20:59 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
ret, assignedNode = node.ReserveOneVolume(r, vid)
|
|
|
|
if ret {
|
|
|
|
break
|
|
|
|
}
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 17:43:43 +08:00
|
|
|
return ret, assignedNode
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int) { //can be negative
|
|
|
|
n.maxVolumeCount += maxVolumeCountDelta
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta)
|
2012-08-31 16:35:11 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
}
|
|
|
|
func (n *NodeImpl) UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int) { //can be negative
|
|
|
|
n.activeVolumeCount += activeVolumeCountDelta
|
2012-08-29 15:58:03 +08:00
|
|
|
if n.parent != nil {
|
2012-09-03 05:33:48 +08:00
|
|
|
n.parent.UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta)
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
2012-08-28 04:52:02 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UpAdjustMaxVolumeId(vid storage.VolumeId) { //can be negative
|
|
|
|
if n.maxVolumeId < vid {
|
|
|
|
n.maxVolumeId = vid
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustMaxVolumeId(vid)
|
|
|
|
}
|
|
|
|
}
|
2012-09-01 17:20:59 +08:00
|
|
|
}
|
2012-08-29 15:58:03 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) GetMaxVolumeId() storage.VolumeId {
|
2012-08-31 16:35:11 +08:00
|
|
|
return n.maxVolumeId
|
2012-08-29 16:37:40 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) GetActiveVolumeCount() int {
|
|
|
|
return n.activeVolumeCount
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) GetMaxVolumeCount() int {
|
|
|
|
return n.maxVolumeCount
|
|
|
|
}
|
2012-08-29 16:37:40 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) LinkChildNode(node Node) {
|
|
|
|
if n.children[node.Id()] == nil {
|
|
|
|
n.children[node.Id()] = node
|
|
|
|
n.activeVolumeCount += node.GetActiveVolumeCount()
|
|
|
|
n.maxVolumeCount += node.GetMaxVolumeCount()
|
|
|
|
node.setParent(n)
|
|
|
|
if n.maxVolumeId < node.GetMaxVolumeId() {
|
|
|
|
n.maxVolumeId = node.GetMaxVolumeId()
|
|
|
|
}
|
|
|
|
fmt.Println(n, "adds", node, "volumeCount =", n.activeVolumeCount)
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UnlinkChildNode(nodeId NodeId) {
|
2012-09-01 17:20:59 +08:00
|
|
|
node := n.children[nodeId]
|
2012-09-03 05:33:48 +08:00
|
|
|
node.setParent(nil)
|
2012-09-01 17:20:59 +08:00
|
|
|
if node != nil {
|
2012-09-03 05:33:48 +08:00
|
|
|
delete(n.children, node.Id())
|
|
|
|
n.UpAdjustActiveVolumeCountDelta(-node.GetActiveVolumeCount())
|
|
|
|
n.UpAdjustMaxVolumeCountDelta(-node.GetMaxVolumeCount())
|
|
|
|
fmt.Println(n, "removes", node, "volumeCount =", n.activeVolumeCount)
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
2012-08-28 04:52:02 +08:00
|
|
|
}
|