[#1298] writecache: Add shrink
flag for Seal command
All checks were successful
DCO action / DCO (pull_request) Successful in 1m23s
Vulncheck / Vulncheck (pull_request) Successful in 1m48s
Tests and linters / Run gofumpt (pull_request) Successful in 1m54s
Build / Build Components (1.22) (pull_request) Successful in 2m23s
Build / Build Components (1.21) (pull_request) Successful in 2m28s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m53s
Tests and linters / Tests (1.21) (pull_request) Successful in 2m53s
Tests and linters / Staticcheck (pull_request) Successful in 2m48s
Tests and linters / Tests (1.22) (pull_request) Successful in 2m54s
Tests and linters / Tests with -race (pull_request) Successful in 2m58s
Tests and linters / Lint (pull_request) Successful in 3m24s
Tests and linters / gopls check (pull_request) Successful in 3m36s
All checks were successful
DCO action / DCO (pull_request) Successful in 1m23s
Vulncheck / Vulncheck (pull_request) Successful in 1m48s
Tests and linters / Run gofumpt (pull_request) Successful in 1m54s
Build / Build Components (1.22) (pull_request) Successful in 2m23s
Build / Build Components (1.21) (pull_request) Successful in 2m28s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m53s
Tests and linters / Tests (1.21) (pull_request) Successful in 2m53s
Tests and linters / Staticcheck (pull_request) Successful in 2m48s
Tests and linters / Tests (1.22) (pull_request) Successful in 2m54s
Tests and linters / Tests with -race (pull_request) Successful in 2m58s
Tests and linters / Lint (pull_request) Successful in 3m24s
Tests and linters / gopls check (pull_request) Successful in 3m36s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
5c01bd5be8
commit
36efccd862
12 changed files with 255 additions and 187 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue