fix(flashnode): delete pre-allocated size when return from InitForCacheRead

close:#1000012153

Signed-off-by: chihe <chihe@oppo.com>
This commit is contained in:
chihe 2025-03-21 09:25:12 +08:00 committed by zhumingze1108
parent 1118f934ee
commit 7c026a4daf
4 changed files with 20 additions and 11 deletions

View File

@ -561,9 +561,9 @@ func (cb *CacheBlock) GetRootPath() string {
return cb.rootPath return cb.rootPath
} }
func (cb *CacheBlock) InitOnceForCacheRead(engine *CacheEngine, sources []*proto.DataSource) { func (cb *CacheBlock) InitOnceForCacheRead(engine *CacheEngine, sources []*proto.DataSource, done chan struct{}) {
cb.initOnce.Do(func() { cb.initOnce.Do(func() {
cb.InitForCacheRead(sources, engine.readDataNodeTimeout) cb.InitForCacheRead(sources, engine.readDataNodeTimeout, done)
select { select {
case <-cb.closeCh: case <-cb.closeCh:
engine.deleteCacheBlock(cb.blockKey) engine.deleteCacheBlock(cb.blockKey)
@ -573,7 +573,7 @@ func (cb *CacheBlock) InitOnceForCacheRead(engine *CacheEngine, sources []*proto
}) })
} }
func (cb *CacheBlock) InitForCacheRead(sources []*proto.DataSource, readDataNodeTimeout int) { func (cb *CacheBlock) InitForCacheRead(sources []*proto.DataSource, readDataNodeTimeout int, done chan struct{}) {
var err error var err error
var file *os.File var file *os.File
bgTime := stat.BeginStat() bgTime := stat.BeginStat()
@ -586,8 +586,13 @@ func (cb *CacheBlock) InitForCacheRead(sources []*proto.DataSource, readDataNode
} }
cb.notifyClose() cb.notifyClose()
} }
if value, ok := cb.cacheEngine.lruCacheMap.Load(cb.rootPath); ok {
cacheItem := value.(*lruCacheItem)
cacheItem.lruCache.FreePreAllocatedSize(cb.blockKey)
}
stat.EndStat("MissCacheRead:InitForCacheRead", err, bgTime, 1) stat.EndStat("MissCacheRead:InitForCacheRead", err, bgTime, 1)
metric.SetWithLabels(err, map[string]string{exporter.Vol: cb.volume}) metric.SetWithLabels(err, map[string]string{exporter.Vol: cb.volume})
close(done)
}() }()
sb := strings.Builder{} sb := strings.Builder{}
for _, s := range sources { for _, s := range sources {

View File

@ -185,14 +185,16 @@ func (f *FlashNode) opCacheRead(conn net.Conn, p *proto.Packet) (err error) {
} }
} }
bgTime2 := stat.BeginStat() bgTime2 := stat.BeginStat()
taskDone := make(chan struct{})
missCacheMetric := exporter.NewTPCnt("MissCacheRead") missCacheMetric := exporter.NewTPCnt("MissCacheRead")
// try to cache more miss data, but reply to client more quickly // try to cache more miss data, but reply to client more quickly
if writable := f.limitWrite.TryRunWithContext(ctx, int(req.Size_), func() { if writable := f.limitWrite.TryRunWithContext(ctx, int(req.Size_), func() {
if block2, err := f.cacheEngine.CreateBlock(cr, conn.RemoteAddr().String(), false); err != nil { if block2, err := f.cacheEngine.CreateBlock(cr, conn.RemoteAddr().String(), false); err != nil {
log.LogWarnf("opCacheRead: CreateBlock failed, req(%v) err(%v)", req, err) log.LogWarnf("opCacheRead: CreateBlock failed, req(%v) err(%v)", req, err)
close(taskDone)
return return
} else { } else {
block2.InitOnceForCacheRead(f.cacheEngine, cr.Sources) block2.InitOnceForCacheRead(f.cacheEngine, cr.Sources, taskDone)
} }
}); !writable { }); !writable {
err = fmt.Errorf("create block cache limited") err = fmt.Errorf("create block cache limited")
@ -205,7 +207,7 @@ func (f *FlashNode) opCacheRead(conn net.Conn, p *proto.Packet) (err error) {
stat.EndStat("MissCacheReadCancel", ctx.Err(), bgTime2, 1) stat.EndStat("MissCacheReadCancel", ctx.Err(), bgTime2, 1)
missCacheMetric.SetWithLabels(err, map[string]string{exporter.Vol: volume}) missCacheMetric.SetWithLabels(err, map[string]string{exporter.Vol: volume})
return ctx.Err() return ctx.Err()
default: case <-taskDone:
block, err = f.cacheEngine.GetCacheBlockForRead(volume, cr.Inode, cr.FixedFileOffset, cr.Version, req.Size_) block, err = f.cacheEngine.GetCacheBlockForRead(volume, cr.Inode, cr.FixedFileOffset, cr.Version, req.Size_)
} }
stat.EndStat("MissCacheRead", err, bgTime2, 1) stat.EndStat("MissCacheRead", err, bgTime2, 1)

View File

@ -109,14 +109,14 @@ func (l *IoLimiter) Run(size int, allowHang bool, taskFn func()) (err error) {
func (l *IoLimiter) RunNoWait(size int, allowHang bool, taskFn func()) (err error) { func (l *IoLimiter) RunNoWait(size int, allowHang bool, taskFn func()) (err error) {
if size > 0 && l.limit > 0 { if size > 0 && l.limit > 0 {
if !l.flow.AllowN(time.Now(), size) { if !l.flow.AllowN(time.Now(), size) {
return fmt.Errorf("run limited") return fmt.Errorf("flow limited")
} }
} }
return l.getIO().Run(taskFn, allowHang) return l.getIO().Run(taskFn, allowHang)
} }
func (l *IoLimiter) TryRun(size int, taskFn func()) bool { func (l *IoLimiter) TryRun(size int, taskFn func()) bool {
if ok := l.getIO().TryRun(taskFn); !ok { if ok := l.getIO().TryRun(taskFn, false); !ok {
return false return false
} }
if size > 0 { if size > 0 {
@ -135,7 +135,7 @@ func (l *IoLimiter) TryRunWithContext(ctx context.Context, size int, taskFn func
return false return false
} }
} }
if ok := l.getIO().TryRun(taskFn); !ok { if ok := l.getIO().TryRun(taskFn, true); !ok {
return false return false
} }
return true return true
@ -286,7 +286,7 @@ func (q *ioQueue) Run(taskFn func(), allowHang bool) (err error) {
return return
} }
func (q *ioQueue) TryRun(taskFn func()) bool { func (q *ioQueue) TryRun(taskFn func(), async bool) bool {
if q.concurrency <= 0 { if q.concurrency <= 0 {
taskFn() taskFn()
return true return true
@ -305,7 +305,9 @@ func (q *ioQueue) TryRun(taskFn func()) bool {
taskFn() taskFn()
return true return true
case q.queue <- task: case q.queue <- task:
<-task.done if !async {
<-task.done
}
return true return true
default: default:
return false return false

View File

@ -74,7 +74,7 @@ func TestLimitIOBase(t *testing.T) {
q := l.getIO() q := l.getIO()
l.Close() l.Close()
q.Run(f, true) q.Run(f, true)
require.True(t, q.TryRun(f)) require.True(t, q.TryRun(f, false))
t.Logf("closed status: %+v", q.Status()) t.Logf("closed status: %+v", q.Status())
} }
} }