fixed potential bug in quota calculation

This commit is contained in:
ston1th 2022-03-30 23:16:26 +02:00
commit 23528fe28e

View file

@ -126,12 +126,16 @@ func (mh *MetadataHandler) DeleteOldest() (s int64) {
if m != nil { if m != nil {
log := mh.log.WithValues("file", m.name) log := mh.log.WithValues("file", m.name)
log.Info("deleting oldest file") log.Info("deleting oldest file")
cs := m.Size
if !m.FullyCached() {
cs = m.ChunkSize()
}
err := m.Delete() err := m.Delete()
if err != nil { if err != nil {
log.Error(err, "error deleting oldest file") log.Error(err, "error deleting oldest file")
return return
} }
s = m.Size s = cs
} }
return return
} }
@ -140,7 +144,7 @@ func (mh *MetadataHandler) Size() (s int64) {
mh.mu.RLock() mh.mu.RLock()
defer mh.mu.RUnlock() defer mh.mu.RUnlock()
for _, md := range mh.md { for _, md := range mh.md {
s += md.Chunks.Size() s += md.ChunkSize()
} }
return return
} }
@ -152,7 +156,10 @@ func (mh *MetadataHandler) CacheStatus(name string) int {
if !ok { if !ok {
return -1 return -1
} }
size := md.Chunks.Size() if md.FullyCached() {
return 100
}
size := md.ChunkSize()
if size == 0 { if size == 0 {
return -1 return -1
} }
@ -321,10 +328,7 @@ func (md *Metadata) Delete() error {
func (md *Metadata) FullyCached() bool { func (md *Metadata) FullyCached() bool {
md.mu.RLock() md.mu.RLock()
defer md.mu.RUnlock() defer md.mu.RUnlock()
if len(md.Chunks) == 0 { return len(md.Chunks) == 1 && md.Size == md.Chunks[0][1]
return false
}
return md.Size == md.Chunks[0][1]
} }
func (md *Metadata) HasChunk(off int64, n int) bool { func (md *Metadata) HasChunk(off int64, n int) bool {
@ -333,6 +337,12 @@ func (md *Metadata) HasChunk(off int64, n int) bool {
return md.Chunks.Exists(off, n, md.Size) return md.Chunks.Exists(off, n, md.Size)
} }
func (md *Metadata) ChunkSize() int64 {
md.mu.RLock()
defer md.mu.RUnlock()
return md.Chunks.Size()
}
func (md *Metadata) AddChunk(off int64, n int) { func (md *Metadata) AddChunk(off int64, n int) {
md.mu.Lock() md.mu.Lock()
defer md.mu.Unlock() defer md.mu.Unlock()