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

40 lines
866 B
Go
Raw Normal View History

2012-08-24 05:33:37 +00:00
package topology
import (
2012-09-03 08:50:04 +00:00
_ "fmt"
"math/rand"
"pkg/storage"
2012-08-24 05:33:37 +00:00
)
type Topology struct {
NodeImpl
2012-08-31 08:35:11 +00:00
}
func NewTopology(id string) *Topology {
t := &Topology{}
t.id = NodeId(id)
t.nodeType = "Topology"
t.children = make(map[NodeId]Node)
return t
}
func (t *Topology) RandomlyReserveOneVolume() (bool, *Server, storage.VolumeId) {
2012-09-03 08:50:04 +00:00
vid := t.NextVolumeId()
ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid)
return ret, node, vid
}
2012-09-03 08:50:04 +00:00
func (t *Topology) RandomlyReserveOneVolumeExcept(except []Node) (bool, *Server, storage.VolumeId) {
freeSpace := t.FreeSpace()
for _, node := range except {
freeSpace -= node.FreeSpace()
}
vid := t.NextVolumeId()
ret, node := t.ReserveOneVolume(rand.Intn(freeSpace), vid)
return ret, node, vid
}
func (t *Topology) NextVolumeId() storage.VolumeId {
vid := t.GetMaxVolumeId()
2012-08-29 08:37:40 +00:00
return vid.Next()
}