seaweedfs/go/topology/node_list.go

84 lines
1.9 KiB
Go
Raw Normal View History

2012-09-03 08:50:04 +00:00
package topology
import (
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/storage"
2012-09-03 08:50:04 +00:00
"math/rand"
)
type NodeList struct {
nodes map[NodeId]Node
except map[string]Node
}
func NewNodeList(nodes map[NodeId]Node, except map[string]Node) *NodeList {
m := make(map[NodeId]Node, len(nodes)-len(except))
for _, n := range nodes {
if except[n.String()] == nil {
m[n.Id()] = n
}
}
nl := &NodeList{nodes: m}
return nl
}
func (nl *NodeList) FreeSpace() int {
freeSpace := 0
for _, n := range nl.nodes {
freeSpace += n.FreeSpace()
}
return freeSpace
}
func (nl *NodeList) RandomlyPickN(count int, minSpace int, firstNodeName string) ([]Node, bool) {
2012-09-03 08:50:04 +00:00
var list []Node
var preferredNode *Node
if firstNodeName != "" {
for _, n := range nl.nodes {
if n.Id() == NodeId(firstNodeName) && n.FreeSpace() >= minSpace {
preferredNode = &n
break
}
}
if preferredNode == nil {
return list, false
}
}
2012-09-03 08:50:04 +00:00
for _, n := range nl.nodes {
if n.FreeSpace() >= minSpace && n.Id() != NodeId(firstNodeName) {
2012-09-03 08:50:04 +00:00
list = append(list, n)
}
}
if count > len(list) || count == len(list) && firstNodeName != "" {
2013-01-17 08:56:56 +00:00
return nil, false
2012-09-03 08:50:04 +00:00
}
for i := len(list); i > 0; i-- {
2013-01-17 08:56:56 +00:00
r := rand.Intn(i)
list[r], list[i-1] = list[i-1], list[r]
}
if firstNodeName != "" {
list[0] = *preferredNode
2012-09-03 08:50:04 +00:00
}
return list[:count], true
2012-09-03 08:50:04 +00:00
}
2012-09-08 23:25:44 +00:00
func (nl *NodeList) ReserveOneVolume(randomVolumeIndex int, vid storage.VolumeId) (bool, *DataNode) {
2012-09-03 08:50:04 +00:00
for _, node := range nl.nodes {
freeSpace := node.FreeSpace()
if randomVolumeIndex >= freeSpace {
randomVolumeIndex -= freeSpace
} else {
2012-09-08 23:25:44 +00:00
if node.IsDataNode() && node.FreeSpace() > 0 {
glog.V(0).Infoln("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
2012-09-08 23:25:44 +00:00
return true, node.(*DataNode)
2012-09-03 08:50:04 +00:00
}
children := node.Children()
newNodeList := NewNodeList(children, nl.except)
return newNodeList.ReserveOneVolume(randomVolumeIndex, vid)
}
}
return false, nil
}