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,33 +135,54 @@ 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 {
location.volumesLock.RLock() stats := collectStatsForOneLocation(location)
for k, v := range location.volumes { allStats = append(allStats, stats...)
s := &VolumeInfo{ }
Id: needle.VolumeId(k), sortVolumeInfos(allStats)
Size: v.ContentSize(), return allStats
Collection: v.Collection, }
ReplicaPlacement: v.ReplicaPlacement,
Version: v.Version(), func collectStatsForOneLocation(location *DiskLocation) (stats []*VolumeInfo) {
FileCount: int(v.FileCount()), location.volumesLock.RLock()
DeleteCount: int(v.DeletedCount()), defer location.volumesLock.RUnlock()
DeletedByteCount: v.DeletedSize(),
ReadOnly: v.IsReadOnly(), for k, v := range location.volumes {
Ttl: v.Ttl, s := collectStatForOneVolume(k, v)
CompactRevision: uint32(v.CompactionRevision), stats = append(stats, s)
}
s.RemoteStorageName, s.RemoteStorageKey = v.RemoteStorageNameKey()
stats = append(stats, s)
}
location.volumesLock.RUnlock()
} }
sortVolumeInfos(stats)
return stats return stats
} }
func collectStatForOneVolume(vid needle.VolumeId, v *Volume) (s *VolumeInfo) {
s = &VolumeInfo{
Id: vid,
Collection: v.Collection,
ReplicaPlacement: v.ReplicaPlacement,
Version: v.Version(),
ReadOnly: v.IsReadOnly(),
Ttl: v.Ttl,
CompactRevision: uint32(v.CompactionRevision),
}
s.RemoteStorageName, s.RemoteStorageKey = v.RemoteStorageNameKey()
v.dataFileAccessLock.RLock()
defer v.dataFileAccessLock.RUnlock()
if v.nm == nil {
return
}
s.FileCount = v.nm.FileCount()
s.DeleteCount = v.nm.DeletedCount()
s.DeletedByteCount = v.nm.DeletedSize()
s.Size = v.nm.ContentSize()
return
}
func (s *Store) SetDataCenter(dataCenter string) { func (s *Store) SetDataCenter(dataCenter string) {
s.dataCenter = dataCenter s.dataCenter = dataCenter
} }