add locks for location.volumes

fix https://github.com/chrislusf/seaweedfs/issues/392
This commit is contained in:
Chris Lu
2016-11-06 20:55:22 -08:00
parent df49692dff
commit 36f9633223
2 changed files with 47 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ package storage
import (
"io/ioutil"
"strings"
"sync"
"github.com/chrislusf/seaweedfs/weed/glog"
)
@@ -11,6 +12,7 @@ type DiskLocation struct {
Directory string
MaxVolumeCount int
volumes map[VolumeId]*Volume
sync.RWMutex
}
func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
@@ -20,6 +22,8 @@ func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
}
func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
l.Lock()
defer l.Unlock()
if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
for _, dir := range dirs {
@@ -48,6 +52,9 @@ func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
}
func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
l.Lock()
defer l.Unlock()
for k, v := range l.volumes {
if v.Collection == collection {
e = l.deleteVolumeById(k)
@@ -71,3 +78,35 @@ func (l *DiskLocation) deleteVolumeById(vid VolumeId) (e error) {
delete(l.volumes, vid)
return
}
func (l *DiskLocation) SetVolume(vid VolumeId, volume *Volume) {
l.Lock()
defer l.Unlock()
l.volumes[vid] = volume
}
func (l *DiskLocation) FindVolume(vid VolumeId) (*Volume, bool) {
l.RLock()
defer l.RUnlock()
v, ok := l.volumes[vid]
return v, ok
}
func (l *DiskLocation) VolumesLen() int {
l.RLock()
defer l.RUnlock()
return len(l.volumes)
}
func (l *DiskLocation) Close() {
l.Lock()
defer l.Unlock()
for _, v := range l.volumes {
v.Close()
}
return
}