seaweedfs/weed-fs/src/pkg/topology/node.go

108 lines
2.4 KiB
Go
Raw Normal View History

2012-08-24 03:56:09 +00:00
package topology
2012-08-24 05:56:14 +00:00
import (
2012-08-31 08:35:11 +00:00
"fmt"
"pkg/storage"
2012-08-24 05:56:14 +00:00
)
2012-08-24 03:56:09 +00:00
type NodeId string
2012-08-24 03:56:09 +00:00
type Node struct {
2012-08-31 08:35:11 +00:00
Id NodeId
2012-09-01 09:20:59 +00:00
activeVolumeCount int
reservedVolumeCount int
maxVolumeCount int
parent *Node
children map[NodeId]*Node
2012-08-29 08:37:40 +00:00
maxVolumeId storage.VolumeId
2012-08-24 03:56:09 +00:00
}
2012-08-31 08:35:11 +00:00
func NewNode() *Node {
n := &Node{}
n.children = make(map[NodeId]*Node)
return n
}
2012-09-01 09:20:59 +00:00
func (n *Node) String() string {
if n.parent!=nil {
return n.parent.String()+":"+string(n.Id)
}
return string(n.Id)
}
2012-08-31 08:35:11 +00:00
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool {
2012-09-01 09:20:59 +00:00
ret := false
if n.children == nil {
return true
}
for _, node := range n.children {
2012-09-01 09:20:59 +00:00
freeSpace := node.maxVolumeCount - node.activeVolumeCount - node.reservedVolumeCount
fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
if freeSpace <= 0 {
continue
}
if r >= freeSpace {
r -= freeSpace
} else {
2012-09-01 09:20:59 +00:00
ret = node.ReserveOneVolume(r, vid)
if ret {
break
}
}
}
2012-09-01 09:20:59 +00:00
if ret {
n.reservedVolumeCount++
}
return ret
}
func (n *Node) AddVolume(v *storage.VolumeInfo) {
2012-08-31 08:35:11 +00:00
if n.maxVolumeId < v.Id {
n.maxVolumeId = v.Id
}
2012-09-01 09:20:59 +00:00
n.activeVolumeCount++
fmt.Println(n.Id, "adds 1, volumeCount =", n.activeVolumeCount)
if n.reservedVolumeCount > 0 { //if reserved
n.reservedVolumeCount--
}
if n.parent != nil {
n.parent.AddVolume(v)
}
}
2012-09-01 09:20:59 +00:00
func (n *Node) AddMaxVolumeCount(maxVolumeCount int) {//can be negative
n.maxVolumeCount += maxVolumeCount
if n.parent != nil {
n.parent.AddMaxVolumeCount(maxVolumeCount)
}
}
2012-08-29 08:37:40 +00:00
func (n *Node) GetMaxVolumeId() storage.VolumeId {
2012-08-31 08:35:11 +00:00
return n.maxVolumeId
2012-08-29 08:37:40 +00:00
}
func (n *Node) AddNode(node *Node) {
2012-08-31 08:35:11 +00:00
if n.children[node.Id] == nil {
n.children[node.Id] = node
2012-09-01 09:20:59 +00:00
n.activeVolumeCount += node.activeVolumeCount
n.reservedVolumeCount += node.reservedVolumeCount
2012-08-31 08:35:11 +00:00
n.maxVolumeCount += node.maxVolumeCount
2012-09-01 09:20:59 +00:00
fmt.Println(n.Id, "adds", node.Id, "volumeCount =", n.activeVolumeCount)
}
}
2012-09-01 09:20:59 +00:00
func (n *Node) RemoveNode(nodeId NodeId) {
node := n.children[nodeId]
if node != nil {
2012-08-31 08:35:11 +00:00
delete(n.children, node.Id)
2012-09-01 09:20:59 +00:00
n.activeVolumeCount -= node.activeVolumeCount
2012-08-31 08:35:11 +00:00
n.maxVolumeCount -= node.maxVolumeCount
2012-09-01 09:20:59 +00:00
n.reservedVolumeCount -= node.reservedVolumeCount
p := n.parent
for p != nil {
p.activeVolumeCount -= node.activeVolumeCount
p.maxVolumeCount -= node.maxVolumeCount
p.reservedVolumeCount -= node.reservedVolumeCount
p = p.parent
}
fmt.Println(n.Id, "removes", node.Id, "volumeCount =", n.activeVolumeCount)
}
}