Dmitrii Stepanov
8180a0664f
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 6m1s
Build / Build Components (1.21) (pull_request) Successful in 7m37s
Build / Build Components (1.20) (pull_request) Successful in 7m52s
Tests and linters / Staticcheck (pull_request) Successful in 8m56s
Tests and linters / Lint (pull_request) Successful in 9m26s
Tests and linters / Tests (1.21) (pull_request) Successful in 15m5s
Tests and linters / Tests with -race (pull_request) Successful in 15m7s
DCO action / DCO (pull_request) Successful in 1m1s
Tests and linters / Tests (1.20) (pull_request) Successful in 4m1s
Badger implementation isn't tested and works not well, but requires human resources to maintain. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package writecache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// 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.
|
|
func (c *cache) SetMode(m mode.Mode) error {
|
|
ctx, span := tracing.StartSpanFromContext(context.TODO(), "writecache.SetMode",
|
|
trace.WithAttributes(
|
|
attribute.String("mode", m.String()),
|
|
))
|
|
defer span.End()
|
|
|
|
c.modeMtx.Lock()
|
|
defer c.modeMtx.Unlock()
|
|
|
|
err := c.setMode(ctx, m)
|
|
if err == nil {
|
|
c.metrics.SetMode(m)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// setMode applies new mode. Must be called with cache.modeMtx lock taken.
|
|
func (c *cache) setMode(ctx context.Context, m mode.Mode) error {
|
|
var err error
|
|
turnOffMeta := m.NoMetabase()
|
|
|
|
if turnOffMeta && !c.mode.NoMetabase() {
|
|
err = c.flush(ctx, true)
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Suspend producers to ensure there are channel send operations in fly.
|
|
// flushCh is populated by `flush` with `modeMtx` taken, thus waiting until it is empty
|
|
// guarantees that there are no in-fly operations.
|
|
for len(c.flushCh) != 0 {
|
|
c.log.Info(logs.WritecacheWaitingForChannelsToFlush)
|
|
time.Sleep(time.Second)
|
|
}
|
|
|
|
if turnOffMeta {
|
|
c.mode = m
|
|
return nil
|
|
}
|
|
|
|
if err = c.openStore(m.ReadOnly()); err != nil {
|
|
return err
|
|
}
|
|
|
|
c.mode = m
|
|
return nil
|
|
}
|
|
|
|
// readOnly returns true if current mode is read-only.
|
|
// `c.modeMtx` must be taken.
|
|
func (c *cache) readOnly() bool {
|
|
return c.mode.ReadOnly()
|
|
}
|