diff --git a/weed/storage/disk_location.go b/weed/storage/disk_location.go index 02f5f5923..aac824318 100644 --- a/weed/storage/disk_location.go +++ b/weed/storage/disk_location.go @@ -386,6 +386,19 @@ func (l *DiskLocation) VolumesLen() int { return len(l.volumes) } +func (l *DiskLocation) LocalVolumesLen() int { + l.volumesLock.RLock() + defer l.volumesLock.RUnlock() + + count := 0 + for _, v := range l.volumes { + if !v.HasRemoteFile() { + count++ + } + } + return count +} + func (l *DiskLocation) SetStopping() { l.volumesLock.Lock() for _, v := range l.volumes { diff --git a/weed/storage/store.go b/weed/storage/store.go index 1d625dd69..df8d9add4 100644 --- a/weed/storage/store.go +++ b/weed/storage/store.go @@ -165,14 +165,18 @@ func (s *Store) addVolume(vid needle.VolumeId, collection string, needleMapKind return fmt.Errorf("Volume Id %d already exists!", vid) } - // Find location and its index + // Find location with lowest local volume count (load balancing) var location *DiskLocation var diskId uint32 + var minVolCount int = -1 for i, loc := range s.Locations { if loc.DiskType == diskType && s.hasFreeDiskLocation(loc) { - location = loc - diskId = uint32(i) - break + volCount := loc.LocalVolumesLen() + if location == nil || volCount < minVolCount { + location = loc + diskId = uint32(i) + minVolCount = volCount + } } }