maintain layout correctness when changing volumes

This commit is contained in:
Chris Lu 2019-04-21 13:32:36 -07:00
parent 0302b9496c
commit ebe94be1a1

View file

@ -50,7 +50,7 @@ func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
vl.accessLock.Lock() vl.accessLock.Lock()
defer vl.accessLock.Unlock() defer vl.accessLock.Unlock()
if _, ok := vl.vid2location[v.Id]; !ok || vl.vid2location[v.Id] == nil { if _, ok := vl.vid2location[v.Id]; !ok {
vl.vid2location[v.Id] = NewVolumeLocationList() vl.vid2location[v.Id] = NewVolumeLocationList()
} }
vl.vid2location[v.Id].Set(dn) vl.vid2location[v.Id].Set(dn)
@ -72,17 +72,13 @@ func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
return return
} }
} }
if vl.vid2location[v.Id].Length() == vl.rp.GetCopyCount() && vl.isWritable(v) {
if _, ok := vl.oversizedVolumes[v.Id]; !ok { vl.rememberOversizedVolume(v)
vl.addToWritable(v.Id) vl.ensureCorrectWritables(v)
}
} else {
vl.rememberOversizedVolumne(v)
vl.removeFromWritable(v.Id)
}
} }
func (vl *VolumeLayout) rememberOversizedVolumne(v *storage.VolumeInfo) { func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo) {
if vl.isOversized(v) { if vl.isOversized(v) {
vl.oversizedVolumes[v.Id] = true vl.oversizedVolumes[v.Id] = true
} }
@ -92,9 +88,31 @@ func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
vl.accessLock.Lock() vl.accessLock.Lock()
defer vl.accessLock.Unlock() defer vl.accessLock.Unlock()
// remove from vid2location map
location, ok := vl.vid2location[v.Id]
if !ok {
return
}
if location.Remove(dn) {
vl.ensureCorrectWritables(v)
if location.Length() == 0 {
delete(vl.vid2location, v.Id)
}
}
}
func (vl *VolumeLayout) ensureCorrectWritables(v *storage.VolumeInfo) {
if vl.vid2location[v.Id].Length() == vl.rp.GetCopyCount() && vl.isWritable(v) {
if _, ok := vl.oversizedVolumes[v.Id]; !ok {
vl.addToWritable(v.Id)
}
} else {
vl.removeFromWritable(v.Id) vl.removeFromWritable(v.Id)
delete(vl.vid2location, v.Id) // somehow this line does not work as expected }
// vl.vid2location[v.Id] = nil
} }
func (vl *VolumeLayout) addToWritable(vid needle.VolumeId) { func (vl *VolumeLayout) addToWritable(vid needle.VolumeId) {
@ -252,7 +270,7 @@ func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId) bo
defer vl.accessLock.Unlock() defer vl.accessLock.Unlock()
vl.vid2location[vid].Set(dn) vl.vid2location[vid].Set(dn)
if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() { if vl.vid2location[vid].Length() == vl.rp.GetCopyCount() {
return vl.setVolumeWritable(vid) return vl.setVolumeWritable(vid)
} }
return false return false