[#1298] writecache: Add shrink flag for Seal command

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2024-08-06 15:45:53 +03:00
parent 5c01bd5be8
commit 36efccd862
12 changed files with 68 additions and 13 deletions

View file

@ -9,7 +9,10 @@ import (
"github.com/spf13/cobra"
)
const restoreModeFlag = "restore-mode"
const (
restoreModeFlag = "restore-mode"
shrinkFlag = "shrink"
)
var writecacheShardCmd = &cobra.Command{
Use: "writecache",
@ -29,11 +32,13 @@ func sealWritecache(cmd *cobra.Command, _ []string) {
ignoreErrors, _ := cmd.Flags().GetBool(ignoreErrorsFlag)
restoreMode, _ := cmd.Flags().GetBool(restoreModeFlag)
shrink, _ := cmd.Flags().GetBool(shrinkFlag)
req := &control.SealWriteCacheRequest{Body: &control.SealWriteCacheRequest_Body{
Shard_ID: getShardIDList(cmd),
IgnoreErrors: ignoreErrors,
RestoreMode: restoreMode,
Shrink: shrink,
}}
signRequest(cmd, pk, req)
@ -73,6 +78,7 @@ func initControlShardsWritecacheCmd() {
ff.Bool(shardAllFlag, false, "Process all shards")
ff.Bool(ignoreErrorsFlag, true, "Skip invalid/unreadable objects")
ff.Bool(restoreModeFlag, false, "Restore writecache's mode after sealing")
ff.Bool(shrinkFlag, false, "Shrink writecache's internal storage")
sealWritecacheShardCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
}

View file

@ -539,4 +539,5 @@ const (
PolicerCouldNotGetChunk = "could not get EC chunk"
PolicerCouldNotGetChunks = "could not get EC chunks"
AuditEventLogRecord = "audit event log record"
WritecacheShrinkSkippedNotEmpty = "writecache shrink skipped: database is not empty"
)

View file

@ -71,6 +71,7 @@ type SealWriteCachePrm struct {
ShardIDs []*shard.ID
IgnoreErrors bool
RestoreMode bool
Shrink bool
}
type ShardSealResult struct {
@ -116,7 +117,7 @@ func (e *StorageEngine) SealWriteCache(ctx context.Context, prm SealWriteCachePr
return nil
}
err := sh.SealWriteCache(egCtx, shard.SealWriteCachePrm{IgnoreErrors: prm.IgnoreErrors, RestoreMode: prm.RestoreMode})
err := sh.SealWriteCache(egCtx, shard.SealWriteCachePrm{IgnoreErrors: prm.IgnoreErrors, RestoreMode: prm.RestoreMode, Shrink: prm.Shrink})
resGuard.Lock()
defer resGuard.Unlock()

View file

@ -61,6 +61,7 @@ func (s *Shard) FlushWriteCache(ctx context.Context, p FlushWriteCachePrm) error
type SealWriteCachePrm struct {
IgnoreErrors bool
RestoreMode bool
Shrink bool
}
// SealWriteCache flushes all data from the write-cache and moves it to degraded read only mode.
@ -87,5 +88,5 @@ func (s *Shard) SealWriteCache(ctx context.Context, p SealWriteCachePrm) error {
return ErrDegradedMode
}
return s.writeCache.Seal(ctx, writecache.SealPrm{IgnoreErrors: p.IgnoreErrors, RestoreMode: p.RestoreMode})
return s.writeCache.Seal(ctx, writecache.SealPrm{IgnoreErrors: p.IgnoreErrors, RestoreMode: p.RestoreMode, Shrink: p.Shrink})
}

View file

@ -291,7 +291,7 @@ func (c *cache) Flush(ctx context.Context, ignoreErrors, seal bool) error {
if seal {
m := c.mode | mode.ReadOnly
if err := c.setMode(ctx, m, ignoreErrors); err != nil {
if err := c.setMode(ctx, m, setModePrm{ignoreErrors: ignoreErrors}); err != nil {
return err
}
c.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))

View file

@ -2,16 +2,25 @@ package writecache
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
"go.etcd.io/bbolt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type setModePrm struct {
ignoreErrors bool
shrink bool
}
// SetMode sets write-cache mode of operation.
// When shard is put in read-only mode all objects in memory are flushed to disk
// and all background jobs are suspended.
@ -25,7 +34,7 @@ func (c *cache) SetMode(m mode.Mode) error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
err := c.setMode(ctx, m, true)
err := c.setMode(ctx, m, setModePrm{ignoreErrors: true})
if err == nil {
c.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))
}
@ -33,21 +42,19 @@ func (c *cache) SetMode(m mode.Mode) error {
}
// setMode applies new mode. Must be called with cache.modeMtx lock taken.
func (c *cache) setMode(ctx context.Context, m mode.Mode, ignoreErrors bool) error {
func (c *cache) setMode(ctx context.Context, m mode.Mode, prm setModePrm) error {
var err error
turnOffMeta := m.NoMetabase()
if turnOffMeta && !c.mode.NoMetabase() {
err = c.flush(ctx, ignoreErrors)
err = c.flush(ctx, prm.ignoreErrors)
if err != nil {
return err
}
}
if c.db != nil {
if err = c.db.Close(); err != nil {
return fmt.Errorf("can't close write-cache database: %w", err)
}
if err := c.closeDB(prm.shrink); err != nil {
return err
}
// Suspend producers to ensure there are channel send operations in fly.
@ -71,6 +78,40 @@ func (c *cache) setMode(ctx context.Context, m mode.Mode, ignoreErrors bool) err
return nil
}
func (c *cache) closeDB(shrink bool) error {
if c.db == nil {
return nil
}
if !shrink {
if err := c.db.Close(); err != nil {
return fmt.Errorf("can't close write-cache database: %w", err)
}
return nil
}
var empty bool
err := c.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(defaultBucket)
empty = b == nil || b.Stats().KeyN == 0
return nil
})
if err != nil && !errors.Is(err, bbolt.ErrDatabaseNotOpen) {
return fmt.Errorf("failed to check DB items: %w", err)
}
if err := c.db.Close(); err != nil {
return fmt.Errorf("can't close write-cache database: %w", err)
}
if empty {
err := os.Remove(filepath.Join(c.path, dbName))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove DB file: %w", err)
}
} else {
c.log.Info(logs.WritecacheShrinkSkippedNotEmpty)
}
return nil
}
// readOnly returns true if current mode is read-only.
// `c.modeMtx` must be taken.
func (c *cache) readOnly() bool {

View file

@ -22,13 +22,13 @@ func (c *cache) Seal(ctx context.Context, prm SealPrm) error {
sourceMode := c.mode
// flush will be done by setMode
err := c.setMode(ctx, mode.DegradedReadOnly, prm.IgnoreErrors)
err := c.setMode(ctx, mode.DegradedReadOnly, setModePrm{ignoreErrors: prm.IgnoreErrors, shrink: prm.Shrink})
if err != nil {
return err
}
c.metrics.SetMode(mode.ComponentDisabled)
if prm.RestoreMode {
err = c.setMode(ctx, sourceMode, prm.IgnoreErrors)
err = c.setMode(ctx, sourceMode, setModePrm{ignoreErrors: prm.IgnoreErrors})
if err == nil {
c.metrics.SetMode(mode.ConvertToComponentMode(sourceMode))
}

View file

@ -23,6 +23,7 @@ type Info struct {
type SealPrm struct {
IgnoreErrors bool
RestoreMode bool
Shrink bool
}
// Cache represents write-cache for objects.

View file

@ -20,6 +20,7 @@ func (s *Server) SealWriteCache(ctx context.Context, req *control.SealWriteCache
ShardIDs: s.getShardIDList(req.GetBody().GetShard_ID()),
IgnoreErrors: req.GetBody().GetIgnoreErrors(),
RestoreMode: req.GetBody().GetRestoreMode(),
Shrink: req.GetBody().GetShrink(),
}
res, err := s.s.SealWriteCache(ctx, prm)

Binary file not shown.

View file

@ -658,6 +658,9 @@ message SealWriteCacheRequest {
// If true, then writecache will be sealed, but mode will be restored to the current one.
bool restore_mode = 4;
// If true, then writecache will shrink internal storage.
bool shrink = 5;
}
Body body = 1;

Binary file not shown.