2012-08-24 05:33:37 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
2012-09-03 08:50:04 +00:00
|
|
|
_ "fmt"
|
2012-09-02 21:33:48 +00:00
|
|
|
"math/rand"
|
2012-08-28 08:04:39 +00:00
|
|
|
"pkg/storage"
|
2012-08-24 05:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Topology struct {
|
2012-09-02 21:33:48 +00:00
|
|
|
NodeImpl
|
2012-08-31 08:35:11 +00:00
|
|
|
}
|
2012-08-28 08:04:39 +00:00
|
|
|
|
2012-09-02 21:33:48 +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()
|
2012-09-02 21:33:48 +00:00
|
|
|
ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid)
|
2012-09-01 09:43:43 +00:00
|
|
|
return ret, node, vid
|
2012-08-28 08:04:39 +00:00
|
|
|
}
|
2012-08-29 07:58:03 +00:00
|
|
|
|
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 {
|
2012-09-02 21:33:48 +00:00
|
|
|
vid := t.GetMaxVolumeId()
|
2012-08-29 08:37:40 +00:00
|
|
|
return vid.Next()
|
2012-08-28 08:04:39 +00:00
|
|
|
}
|