2012-09-10 07:18:07 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
2012-09-11 00:08:52 +00:00
|
|
|
"errors"
|
2012-09-10 07:18:07 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"pkg/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type VolumeLayout struct {
|
2012-09-17 00:31:15 +00:00
|
|
|
repType storage.ReplicationType
|
2012-09-10 07:18:07 +00:00
|
|
|
vid2location map[storage.VolumeId]*DataNodeLocationList
|
|
|
|
writables []storage.VolumeId // transient array of writable volume id
|
|
|
|
pulse int64
|
|
|
|
volumeSizeLimit uint64
|
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
func NewVolumeLayout(repType storage.ReplicationType, volumeSizeLimit uint64, pulse int64) *VolumeLayout {
|
2012-09-10 07:18:07 +00:00
|
|
|
return &VolumeLayout{
|
2012-09-17 00:31:15 +00:00
|
|
|
repType: repType,
|
2012-09-10 07:18:07 +00:00
|
|
|
vid2location: make(map[storage.VolumeId]*DataNodeLocationList),
|
|
|
|
writables: *new([]storage.VolumeId),
|
|
|
|
pulse: pulse,
|
|
|
|
volumeSizeLimit: volumeSizeLimit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
|
|
|
|
if _, ok := vl.vid2location[v.Id]; !ok {
|
|
|
|
vl.vid2location[v.Id] = NewDataNodeLocationList()
|
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
if vl.vid2location[v.Id].Add(dn) {
|
|
|
|
if len(vl.vid2location[v.Id].list) == storage.GetCopyCount(v.RepType) {
|
|
|
|
if uint64(v.Size) < vl.volumeSizeLimit {
|
|
|
|
vl.writables = append(vl.writables, v.Id)
|
|
|
|
}
|
2012-09-11 00:08:52 +00:00
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
func (vl *VolumeLayout) PickForWrite(count int) (*storage.VolumeId, int, *DataNodeLocationList, error) {
|
2012-09-11 00:08:52 +00:00
|
|
|
len_writers := len(vl.writables)
|
|
|
|
if len_writers <= 0 {
|
|
|
|
fmt.Println("No more writable volumes!")
|
2012-09-17 00:31:15 +00:00
|
|
|
return nil, 0, nil, errors.New("No more writable volumes!")
|
2012-09-11 00:08:52 +00:00
|
|
|
}
|
|
|
|
vid := vl.writables[rand.Intn(len_writers)]
|
|
|
|
locationList := vl.vid2location[vid]
|
|
|
|
if locationList != nil {
|
2012-09-17 00:31:15 +00:00
|
|
|
return &vid, count, locationList, nil
|
2012-09-11 00:08:52 +00:00
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) GetActiveVolumeCount() int {
|
|
|
|
return len(vl.writables)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) ToMap() interface{} {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["replication"] = vl.repType.String()
|
|
|
|
m["writables"] = vl.writables
|
|
|
|
//m["locations"] = vl.vid2location
|
|
|
|
return m
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|