2012-08-24 13:33:37 +08:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
2012-08-29 15:58:03 +08:00
|
|
|
"math/rand"
|
2012-08-28 16:04:39 +08:00
|
|
|
"pkg/storage"
|
2012-09-02 04:17:30 +08:00
|
|
|
_ "fmt"
|
2012-08-24 13:33:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Topology struct {
|
2012-08-29 15:58:03 +08:00
|
|
|
Node
|
2012-08-24 13:33:37 +08:00
|
|
|
}
|
2012-08-31 16:35:11 +08:00
|
|
|
func NewTopology(id NodeId) *Topology{
|
|
|
|
t := &Topology{}
|
|
|
|
t.Node = *NewNode()
|
|
|
|
t.Node.Id = id
|
|
|
|
return t
|
|
|
|
}
|
2012-08-28 16:04:39 +08:00
|
|
|
|
2012-09-01 17:43:43 +08:00
|
|
|
func (t *Topology) RandomlyReserveOneVolume() (bool, *Node, storage.VolumeId) {
|
2012-09-02 01:51:07 +08:00
|
|
|
slots := t.Node.maxVolumeCount-t.Node.activeVolumeCount
|
2012-09-01 17:20:59 +08:00
|
|
|
r := rand.Intn(slots)
|
2012-08-28 16:04:39 +08:00
|
|
|
vid := t.nextVolumeId()
|
2012-09-01 17:43:43 +08:00
|
|
|
ret, node := t.Node.ReserveOneVolume(r,vid)
|
|
|
|
return ret, node, vid
|
2012-08-28 16:04:39 +08:00
|
|
|
}
|
2012-08-29 15:58:03 +08:00
|
|
|
|
2012-08-28 16:04:39 +08:00
|
|
|
func (t *Topology) nextVolumeId() storage.VolumeId {
|
2012-08-29 16:37:40 +08:00
|
|
|
vid := t.Node.GetMaxVolumeId()
|
|
|
|
return vid.Next()
|
2012-08-28 16:04:39 +08:00
|
|
|
}
|