reserve a volume

add VolumeId.Next()
This commit is contained in:
Chris Lu 2012-08-29 01:37:40 -07:00
parent f7adf1687f
commit 317e12644a
4 changed files with 15 additions and 4 deletions

View file

@ -22,7 +22,7 @@ type VolumeGrowth struct {
func (vg *VolumeGrowth) GrowVolumeCopy(copyLevel int, topo topology.Topology) {
if copyLevel == 1 {
for i := 0; i <vg.copy1factor; i++ {
topo.RandomlyCreateOneVolume()
topo.RandomlyReserveOneVolume()
}
}

View file

@ -12,3 +12,6 @@ func NewVolumeId(vid string) (VolumeId,error) {
func (vid *VolumeId) String() string{
return strconv.FormatUint(uint64(*vid), 10)
}
func (vid *VolumeId) Next() VolumeId{
return VolumeId(uint32(*vid)+1)
}

View file

@ -12,7 +12,7 @@ type Node struct {
maxVolumeCount int
parent *Node
children map[NodeId]*Node
isLeaf bool
maxVolumeId storage.VolumeId
}
func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool {
@ -33,6 +33,9 @@ func (n *Node) ReserveOneVolume(r int, vid storage.VolumeId) bool {
}
func (n *Node) AddVolume(v *storage.VolumeInfo) {
if n.maxVolumeId < v.Id {
n.maxVolumeId = v.Id
}
n.countVolumeCount++
if n.reservedVolumeCount > 0 { //if reserved
n.reservedVolumeCount--
@ -42,6 +45,10 @@ func (n *Node) AddVolume(v *storage.VolumeInfo) {
}
}
func (n *Node) GetMaxVolumeId() storage.VolumeId {
return n.maxVolumeId
}
func (n *Node) AddNode(node *Node) {
n.children[node.id] = node
n.countVolumeCount += node.countVolumeCount

View file

@ -9,12 +9,13 @@ type Topology struct {
Node
}
func (t *Topology) RandomlyCreateOneVolume() (bool,storage.VolumeId) {
func (t *Topology) RandomlyReserveOneVolume() (bool,storage.VolumeId) {
r := rand.Intn(t.Node.maxVolumeCount-t.Node.countVolumeCount-t.Node.reservedVolumeCount)
vid := t.nextVolumeId()
return t.Node.ReserveOneVolume(r,vid), vid
}
func (t *Topology) nextVolumeId() storage.VolumeId {
return storage.VolumeId(0)
vid := t.Node.GetMaxVolumeId()
return vid.Next()
}