2012-09-10 15:18:07 +08:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
2013-08-13 07:39:49 +08:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-02-27 14:54:22 +08:00
|
|
|
"code.google.com/p/weed-fs/go/storage"
|
2012-09-11 08:08:52 +08:00
|
|
|
"errors"
|
2012-09-10 15:18:07 +08:00
|
|
|
"math/rand"
|
2013-07-16 12:34:43 +08:00
|
|
|
"sync"
|
2012-09-10 15:18:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VolumeLayout struct {
|
2012-09-17 08:31:15 +08:00
|
|
|
repType storage.ReplicationType
|
2012-09-20 07:48:04 +08:00
|
|
|
vid2location map[storage.VolumeId]*VolumeLocationList
|
2013-01-17 16:15:13 +08:00
|
|
|
writables []storage.VolumeId // transient array of writable volume id
|
2012-09-10 15:18:07 +08:00
|
|
|
pulse int64
|
|
|
|
volumeSizeLimit uint64
|
2013-07-16 12:34:43 +08:00
|
|
|
accessLock sync.Mutex
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 08:31:15 +08:00
|
|
|
func NewVolumeLayout(repType storage.ReplicationType, volumeSizeLimit uint64, pulse int64) *VolumeLayout {
|
2012-09-10 15:18:07 +08:00
|
|
|
return &VolumeLayout{
|
2012-09-17 08:31:15 +08:00
|
|
|
repType: repType,
|
2012-09-20 07:48:04 +08:00
|
|
|
vid2location: make(map[storage.VolumeId]*VolumeLocationList),
|
2013-01-17 16:15:13 +08:00
|
|
|
writables: *new([]storage.VolumeId),
|
2012-09-10 15:18:07 +08:00
|
|
|
pulse: pulse,
|
|
|
|
volumeSizeLimit: volumeSizeLimit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
|
2013-07-16 12:34:43 +08:00
|
|
|
vl.accessLock.Lock()
|
|
|
|
defer vl.accessLock.Unlock()
|
|
|
|
|
2012-09-10 15:18:07 +08:00
|
|
|
if _, ok := vl.vid2location[v.Id]; !ok {
|
2012-09-20 07:48:04 +08:00
|
|
|
vl.vid2location[v.Id] = NewVolumeLocationList()
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
2012-09-17 08:31:15 +08:00
|
|
|
if vl.vid2location[v.Id].Add(dn) {
|
2012-09-17 14:18:47 +08:00
|
|
|
if len(vl.vid2location[v.Id].list) == v.RepType.GetCopyCount() {
|
2012-12-18 08:48:54 +08:00
|
|
|
if vl.isWritable(v) {
|
2012-09-17 08:31:15 +08:00
|
|
|
vl.writables = append(vl.writables, v.Id)
|
2013-08-13 07:39:49 +08:00
|
|
|
} else {
|
|
|
|
vl.removeFromWritable(v.Id)
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
2012-09-11 08:08:52 +08:00
|
|
|
}
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 19:40:04 +08:00
|
|
|
func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
|
2013-04-15 10:30:26 +08:00
|
|
|
return uint64(v.Size) < vl.volumeSizeLimit &&
|
|
|
|
v.Version == storage.CurrentVersion &&
|
|
|
|
!v.ReadOnly
|
2012-12-18 08:48:54 +08:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:52 +08:00
|
|
|
func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
|
2013-04-15 10:30:26 +08:00
|
|
|
if location := vl.vid2location[vid]; location != nil {
|
|
|
|
return location.list
|
|
|
|
}
|
2013-03-20 16:23:15 +08:00
|
|
|
return nil
|
2012-09-24 06:45:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 09:10:38 +08:00
|
|
|
func (vl *VolumeLayout) PickForWrite(count int, dataCenter string) (*storage.VolumeId, int, *VolumeLocationList, error) {
|
2012-09-11 08:08:52 +08:00
|
|
|
len_writers := len(vl.writables)
|
|
|
|
if len_writers <= 0 {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("No more writable volumes!")
|
2012-09-17 08:31:15 +08:00
|
|
|
return nil, 0, nil, errors.New("No more writable volumes!")
|
2012-09-11 08:08:52 +08:00
|
|
|
}
|
2013-06-20 09:10:38 +08:00
|
|
|
if dataCenter == "" {
|
|
|
|
vid := vl.writables[rand.Intn(len_writers)]
|
|
|
|
locationList := vl.vid2location[vid]
|
|
|
|
if locationList != nil {
|
|
|
|
return &vid, count, locationList, nil
|
|
|
|
}
|
|
|
|
return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
|
|
|
|
} else {
|
|
|
|
var vid storage.VolumeId
|
|
|
|
var locationList *VolumeLocationList
|
|
|
|
counter := 0
|
|
|
|
for _, v := range vl.writables {
|
|
|
|
volumeLocationList := vl.vid2location[v]
|
|
|
|
for _, dn := range volumeLocationList.list {
|
|
|
|
if dn.GetDataCenter().Id() == NodeId(dataCenter) {
|
|
|
|
counter++
|
|
|
|
if rand.Intn(counter) < 1 {
|
|
|
|
vid, locationList = v, volumeLocationList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-17 08:31:15 +08:00
|
|
|
return &vid, count, locationList, nil
|
2012-09-11 08:08:52 +08:00
|
|
|
}
|
2013-06-20 09:10:38 +08:00
|
|
|
return nil, 0, nil, errors.New("Strangely This Should Never Have Happened!")
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 09:10:38 +08:00
|
|
|
func (vl *VolumeLayout) GetActiveVolumeCount(dataCenter string) int {
|
|
|
|
if dataCenter == "" {
|
|
|
|
return len(vl.writables)
|
|
|
|
}
|
|
|
|
counter := 0
|
|
|
|
for _, v := range vl.writables {
|
|
|
|
for _, dn := range vl.vid2location[v].list {
|
|
|
|
if dn.GetDataCenter().Id() == NodeId(dataCenter) {
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counter
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
|
|
|
|
2012-09-20 07:48:04 +08:00
|
|
|
func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
|
2013-08-13 07:39:49 +08:00
|
|
|
toDeleteIndex := -1
|
|
|
|
for k, id := range vl.writables {
|
|
|
|
if id == vid {
|
|
|
|
toDeleteIndex = k
|
|
|
|
break
|
2012-09-20 07:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-13 07:39:49 +08:00
|
|
|
if toDeleteIndex >= 0 {
|
|
|
|
glog.V(0).Infoln("Volume", vid, "becomes unwritable")
|
|
|
|
vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
2012-09-20 07:48:04 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
|
|
|
|
for _, v := range vl.writables {
|
|
|
|
if v == vid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Volume", vid, "becomes writable")
|
2012-09-20 07:48:04 +08:00
|
|
|
vl.writables = append(vl.writables, vid)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
|
2013-07-16 12:34:43 +08:00
|
|
|
vl.accessLock.Lock()
|
|
|
|
defer vl.accessLock.Unlock()
|
|
|
|
|
2012-09-20 17:11:08 +08:00
|
|
|
if vl.vid2location[vid].Remove(dn) {
|
|
|
|
if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
|
2012-09-20 17:11:08 +08:00
|
|
|
return vl.removeFromWritable(vid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2012-09-19 16:45:30 +08:00
|
|
|
}
|
2012-09-20 07:48:04 +08:00
|
|
|
func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
|
2013-07-16 12:34:43 +08:00
|
|
|
vl.accessLock.Lock()
|
|
|
|
defer vl.accessLock.Unlock()
|
|
|
|
|
2012-09-20 07:48:04 +08:00
|
|
|
if vl.vid2location[vid].Add(dn) {
|
|
|
|
if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
|
|
|
|
return vl.setVolumeWritable(vid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2012-09-19 16:45:30 +08:00
|
|
|
|
2012-09-20 07:48:04 +08:00
|
|
|
func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
|
2013-07-16 12:34:43 +08:00
|
|
|
vl.accessLock.Lock()
|
|
|
|
defer vl.accessLock.Unlock()
|
|
|
|
|
2013-08-09 14:57:22 +08:00
|
|
|
// glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
|
2012-09-20 07:48:04 +08:00
|
|
|
return vl.removeFromWritable(vid)
|
2012-09-19 16:45:30 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 08:31:15 +08: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 15:18:07 +08:00
|
|
|
}
|