better handle lock in case of exception

This commit is contained in:
Chris Lu
2020-03-24 18:41:25 -07:00
parent 4d5554b16f
commit e63a79ade8

View File

@@ -135,31 +135,52 @@ func (s *Store) addVolume(vid needle.VolumeId, collection string, needleMapKind
return fmt.Errorf("No more free space left") return fmt.Errorf("No more free space left")
} }
func (s *Store) VolumeInfos() []*VolumeInfo { func (s *Store) VolumeInfos() (allStats []*VolumeInfo) {
var stats []*VolumeInfo
for _, location := range s.Locations { for _, location := range s.Locations {
stats := collectStatsForOneLocation(location)
allStats = append(allStats, stats...)
}
sortVolumeInfos(allStats)
return allStats
}
func collectStatsForOneLocation(location *DiskLocation) (stats []*VolumeInfo) {
location.volumesLock.RLock() location.volumesLock.RLock()
defer location.volumesLock.RUnlock()
for k, v := range location.volumes { for k, v := range location.volumes {
s := &VolumeInfo{ s := collectStatForOneVolume(k, v)
Id: needle.VolumeId(k), stats = append(stats, s)
Size: v.ContentSize(), }
return stats
}
func collectStatForOneVolume(vid needle.VolumeId, v *Volume) (s *VolumeInfo) {
s = &VolumeInfo{
Id: vid,
Collection: v.Collection, Collection: v.Collection,
ReplicaPlacement: v.ReplicaPlacement, ReplicaPlacement: v.ReplicaPlacement,
Version: v.Version(), Version: v.Version(),
FileCount: int(v.FileCount()),
DeleteCount: int(v.DeletedCount()),
DeletedByteCount: v.DeletedSize(),
ReadOnly: v.IsReadOnly(), ReadOnly: v.IsReadOnly(),
Ttl: v.Ttl, Ttl: v.Ttl,
CompactRevision: uint32(v.CompactionRevision), CompactRevision: uint32(v.CompactionRevision),
} }
s.RemoteStorageName, s.RemoteStorageKey = v.RemoteStorageNameKey() s.RemoteStorageName, s.RemoteStorageKey = v.RemoteStorageNameKey()
stats = append(stats, s)
v.dataFileAccessLock.RLock()
defer v.dataFileAccessLock.RUnlock()
if v.nm == nil {
return
} }
location.volumesLock.RUnlock()
} s.FileCount = v.nm.FileCount()
sortVolumeInfos(stats) s.DeleteCount = v.nm.DeletedCount()
return stats s.DeletedByteCount = v.nm.DeletedSize()
s.Size = v.nm.ContentSize()
return
} }
func (s *Store) SetDataCenter(dataCenter string) { func (s *Store) SetDataCenter(dataCenter string) {