forked from TrueCloudLab/frostfs-node
[#1367] writecache: Add background flushing objects limiter
To limit memory usage by background flush. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
8a6e3025a0
commit
e39378b1c3
12 changed files with 184 additions and 35 deletions
|
@ -18,7 +18,7 @@ import (
|
|||
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
||||
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
||||
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
||||
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
||||
"go.etcd.io/bbolt"
|
||||
|
@ -41,19 +41,25 @@ func (c *cache) runFlushLoop(ctx context.Context) {
|
|||
if c.disableBackgroundFlush {
|
||||
return
|
||||
}
|
||||
fl := newFlushLimiter(c.flushSizeLimit)
|
||||
c.wg.Add(1)
|
||||
go func() {
|
||||
defer c.wg.Done()
|
||||
c.pushToFlushQueue(ctx)
|
||||
c.pushToFlushQueue(ctx, fl)
|
||||
}()
|
||||
|
||||
for range c.workersCount {
|
||||
c.wg.Add(1)
|
||||
go c.workerFlush(ctx)
|
||||
go c.workerFlush(ctx, fl)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cache) pushToFlushQueue(ctx context.Context) {
|
||||
func (c *cache) pushToFlushQueue(ctx context.Context, fl *flushLimiter) {
|
||||
stopf := context.AfterFunc(ctx, func() {
|
||||
fl.close()
|
||||
})
|
||||
defer stopf()
|
||||
|
||||
tick := time.NewTicker(defaultFlushInterval)
|
||||
for {
|
||||
select {
|
||||
|
@ -65,6 +71,9 @@ func (c *cache) pushToFlushQueue(ctx context.Context) {
|
|||
}
|
||||
|
||||
err := c.fsTree.IterateInfo(ctx, func(oi fstree.ObjectInfo) error {
|
||||
if err := fl.acquire(oi.DataSize); err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case c.flushCh <- objectInfo{
|
||||
addr: oi.Address,
|
||||
|
@ -72,6 +81,7 @@ func (c *cache) pushToFlushQueue(ctx context.Context) {
|
|||
}:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
fl.release(oi.DataSize)
|
||||
return ctx.Err()
|
||||
}
|
||||
})
|
||||
|
@ -86,37 +96,42 @@ func (c *cache) pushToFlushQueue(ctx context.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *cache) workerFlush(ctx context.Context) {
|
||||
func (c *cache) workerFlush(ctx context.Context, fl *flushLimiter) {
|
||||
defer c.wg.Done()
|
||||
|
||||
var objInfo objectInfo
|
||||
for {
|
||||
select {
|
||||
case objInfo = <-c.flushCh:
|
||||
c.flushIfAnObjectExistsWorker(ctx, objInfo, fl)
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
|
||||
res, err := c.fsTree.Get(ctx, common.GetPrm{
|
||||
Address: objInfo.addr,
|
||||
})
|
||||
if err != nil {
|
||||
if !errors.As(err, new(*apistatus.ObjectNotFound)) {
|
||||
c.reportFlushError(logs.WritecacheCantGetObject, objInfo.addr.EncodeToString(), metaerr.Wrap(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = c.flushObject(ctx, res.Object, res.RawData, StorageTypeFSTree)
|
||||
if err != nil {
|
||||
// Error is handled in flushObject.
|
||||
continue
|
||||
}
|
||||
|
||||
c.deleteFromDisk(ctx, objInfo.addr)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cache) flushIfAnObjectExistsWorker(ctx context.Context, objInfo objectInfo, fl *flushLimiter) {
|
||||
defer fl.release(objInfo.size)
|
||||
|
||||
res, err := c.fsTree.Get(ctx, common.GetPrm{
|
||||
Address: objInfo.addr,
|
||||
})
|
||||
if err != nil {
|
||||
if !client.IsErrObjectNotFound(err) {
|
||||
c.reportFlushError(logs.WritecacheCantGetObject, objInfo.addr.EncodeToString(), metaerr.Wrap(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = c.flushObject(ctx, res.Object, res.RawData, StorageTypeFSTree)
|
||||
if err != nil {
|
||||
// Error is handled in flushObject.
|
||||
return
|
||||
}
|
||||
|
||||
c.deleteFromDisk(ctx, objInfo.addr)
|
||||
}
|
||||
|
||||
func (c *cache) reportFlushError(msg string, addr string, err error) {
|
||||
if c.reportError != nil {
|
||||
c.reportError(msg, err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue