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"
|
2013-01-04 21:49:40 +00:00
|
|
|
"sort"
|
2012-09-10 07:18:07 +00:00
|
|
|
)
|
|
|
|
|
2013-01-04 21:49:40 +00:00
|
|
|
type volumeIdList []storage.VolumeId
|
|
|
|
|
2012-09-10 07:18:07 +00:00
|
|
|
type VolumeLayout struct {
|
2012-09-17 00:31:15 +00:00
|
|
|
repType storage.ReplicationType
|
2012-09-19 23:48:04 +00:00
|
|
|
vid2location map[storage.VolumeId]*VolumeLocationList
|
2013-01-04 21:49:40 +00:00
|
|
|
writables volumeIdList // transient (sorted!) array of writable volume Ids
|
2012-09-10 07:18:07 +00:00
|
|
|
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-19 23:48:04 +00:00
|
|
|
vid2location: make(map[storage.VolumeId]*VolumeLocationList),
|
2013-01-04 21:49:40 +00:00
|
|
|
writables: make(volumeIdList, 0, 4),
|
2012-09-10 07:18:07 +00:00
|
|
|
pulse: pulse,
|
|
|
|
volumeSizeLimit: volumeSizeLimit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
|
|
|
|
if _, ok := vl.vid2location[v.Id]; !ok {
|
2012-09-19 23:48:04 +00:00
|
|
|
vl.vid2location[v.Id] = NewVolumeLocationList()
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
if vl.vid2location[v.Id].Add(dn) {
|
2012-09-17 06:18:47 +00:00
|
|
|
if len(vl.vid2location[v.Id].list) == v.RepType.GetCopyCount() {
|
2012-12-18 00:48:54 +00:00
|
|
|
if vl.isWritable(v) {
|
2012-09-17 00:31:15 +00:00
|
|
|
vl.writables = append(vl.writables, v.Id)
|
2013-01-04 21:49:40 +00:00
|
|
|
if len(vl.writables) > 1 {
|
|
|
|
vl.writables.Sort()
|
|
|
|
}
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2012-09-11 00:08:52 +00:00
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 21:49:40 +00:00
|
|
|
func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
|
2013-01-17 08:14:58 +00:00
|
|
|
return uint64(v.Size) < vl.volumeSizeLimit && v.Version == storage.CurrentVersion
|
2012-12-18 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:52 +00:00
|
|
|
func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
|
|
|
|
return vl.vid2location[vid].list
|
2012-09-23 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (vl *VolumeLayout) PickForWrite(count int) (*storage.VolumeId, int, *VolumeLocationList, 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
|
|
|
}
|
2013-01-04 21:49:40 +00:00
|
|
|
var vid storage.VolumeId
|
|
|
|
if len_writers == 1 {
|
|
|
|
vid = vl.writables[0]
|
|
|
|
} else {
|
|
|
|
// skew for lesser indices
|
|
|
|
vid = vl.writables[rand.Intn(len_writers+1)%len_writers]
|
|
|
|
}
|
2012-09-11 00:08:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
|
|
|
|
for i, v := range vl.writables {
|
|
|
|
if v == vid {
|
2012-12-04 04:28:12 +00:00
|
|
|
fmt.Println("Volume", vid, "becomes unwritable")
|
2012-09-19 23:48:04 +00:00
|
|
|
vl.writables = append(vl.writables[:i], vl.writables[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
|
|
|
|
for _, v := range vl.writables {
|
|
|
|
if v == vid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 04:28:12 +00:00
|
|
|
fmt.Println("Volume", vid, "becomes writable")
|
2012-09-19 23:48:04 +00:00
|
|
|
vl.writables = append(vl.writables, vid)
|
2013-01-04 21:49:40 +00:00
|
|
|
if len(vl.writables) > 1 {
|
|
|
|
vl.writables.Sort()
|
|
|
|
}
|
2012-09-19 23:48:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
|
2012-09-20 09:11:08 +00:00
|
|
|
if vl.vid2location[vid].Remove(dn) {
|
|
|
|
if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
|
|
|
|
fmt.Println("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
|
|
|
|
return vl.removeFromWritable(vid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2012-09-19 08:45:30 +00:00
|
|
|
}
|
2012-09-19 23:48:04 +00:00
|
|
|
func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
|
|
|
|
if vl.vid2location[vid].Add(dn) {
|
|
|
|
if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
|
|
|
|
return vl.setVolumeWritable(vid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2012-09-19 08:45:30 +00:00
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
|
|
|
|
return vl.removeFromWritable(vid)
|
2012-09-19 08:45:30 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
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
|
|
|
}
|
2013-01-04 21:49:40 +00:00
|
|
|
|
|
|
|
func (vls volumeIdList) Len() int { return len(vls) }
|
|
|
|
|
|
|
|
func (vls volumeIdList) Less(i, j int) bool {
|
|
|
|
return vls[i] < vls[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vls volumeIdList) Swap(i, j int) {
|
|
|
|
vls[i], vls[j] = vls[j], vls[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// convienence sorting
|
|
|
|
func (vls volumeIdList) Sort() {
|
|
|
|
sort.Sort(vls)
|
|
|
|
}
|