|
@ -1,12 +1,13 @@ |
|
|
package topology |
|
|
package topology |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"fmt" |
|
|
"pkg/storage" |
|
|
"pkg/storage" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type NodeId string |
|
|
type NodeId string |
|
|
type Node struct { |
|
|
type Node struct { |
|
|
id NodeId |
|
|
|
|
|
|
|
|
Id NodeId |
|
|
countVolumeCount int |
|
|
countVolumeCount int |
|
|
reservedVolumeCount int |
|
|
reservedVolumeCount int |
|
|
maxVolumeCount int |
|
|
maxVolumeCount int |
|
@ -15,6 +16,12 @@ type Node struct { |
|
|
maxVolumeId storage.VolumeId |
|
|
maxVolumeId storage.VolumeId |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewNode() *Node { |
|
|
|
|
|
n := &Node{} |
|
|
|
|
|
n.children = make(map[NodeId]*Node) |
|
|
|
|
|
return n |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool { |
|
|
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool { |
|
|
for _, node := range n.children { |
|
|
for _, node := range n.children { |
|
|
freeSpace := node.maxVolumeCount - node.countVolumeCount - node.reservedVolumeCount |
|
|
freeSpace := node.maxVolumeCount - node.countVolumeCount - node.reservedVolumeCount |
|
@ -37,6 +44,7 @@ func (n *Node) AddVolume(v *storage.VolumeInfo) { |
|
|
n.maxVolumeId = v.Id |
|
|
n.maxVolumeId = v.Id |
|
|
} |
|
|
} |
|
|
n.countVolumeCount++ |
|
|
n.countVolumeCount++ |
|
|
|
|
|
fmt.Println(n.Id, "adds 1, volumeCount =", n.countVolumeCount) |
|
|
if n.reservedVolumeCount > 0 { //if reserved
|
|
|
if n.reservedVolumeCount > 0 { //if reserved
|
|
|
n.reservedVolumeCount-- |
|
|
n.reservedVolumeCount-- |
|
|
} |
|
|
} |
|
@ -50,19 +58,19 @@ func (n *Node) GetMaxVolumeId() storage.VolumeId { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (n *Node) AddNode(node *Node) { |
|
|
func (n *Node) AddNode(node *Node) { |
|
|
n.children[node.id] = node |
|
|
|
|
|
|
|
|
if n.children[node.Id] == nil { |
|
|
|
|
|
n.children[node.Id] = node |
|
|
n.countVolumeCount += node.countVolumeCount |
|
|
n.countVolumeCount += node.countVolumeCount |
|
|
n.maxVolumeCount += node.maxVolumeCount |
|
|
n.maxVolumeCount += node.maxVolumeCount |
|
|
if n.parent != nil { |
|
|
|
|
|
n.parent.AddNode(node) |
|
|
|
|
|
|
|
|
fmt.Println(n.Id, "adds", node.Id, "volumeCount =", n.countVolumeCount) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (n *Node) RemoveNode(node *Node) { |
|
|
func (n *Node) RemoveNode(node *Node) { |
|
|
delete(n.children, node.id) |
|
|
|
|
|
|
|
|
if n.children[node.Id] != nil { |
|
|
|
|
|
delete(n.children, node.Id) |
|
|
n.countVolumeCount -= node.countVolumeCount |
|
|
n.countVolumeCount -= node.countVolumeCount |
|
|
n.maxVolumeCount -= node.maxVolumeCount |
|
|
n.maxVolumeCount -= node.maxVolumeCount |
|
|
if n.parent != nil { |
|
|
|
|
|
n.parent.RemoveNode(node) |
|
|
|
|
|
|
|
|
fmt.Println(n.Id, "removes", node.Id, "volumeCount =", n.countVolumeCount) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |