2023-06-22 14:55:30 +03:00
|
|
|
package writecachebadger
|
2022-01-18 15:47:16 +03:00
|
|
|
|
|
|
|
import (
|
2023-04-12 17:01:29 +03:00
|
|
|
"context"
|
2022-07-05 07:55:46 +03:00
|
|
|
"fmt"
|
2022-01-18 15:47:16 +03:00
|
|
|
"time"
|
2022-03-17 14:55:25 +03:00
|
|
|
|
2023-04-12 17:35:10 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 16:38:26 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2023-05-31 12:24:04 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-04-12 17:01:29 +03:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-01-18 15:47:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2022-07-05 07:55:46 +03:00
|
|
|
func (c *cache) SetMode(m mode.Mode) error {
|
2023-04-12 17:01:29 +03:00
|
|
|
ctx, span := tracing.StartSpanFromContext(context.TODO(), "writecache.SetMode",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("mode", m.String()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-08-11 16:43:23 +03:00
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
|
|
|
|
2023-05-18 17:19:41 +03:00
|
|
|
err := c.setMode(ctx, m)
|
|
|
|
if err == nil {
|
|
|
|
c.metrics.SetMode(m)
|
|
|
|
}
|
|
|
|
return err
|
2023-02-15 17:53:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setMode applies new mode. Must be called with cache.modeMtx lock taken.
|
2023-04-12 17:01:29 +03:00
|
|
|
func (c *cache) setMode(ctx context.Context, m mode.Mode) error {
|
2023-02-15 17:53:42 +03:00
|
|
|
var err error
|
|
|
|
turnOffMeta := m.NoMetabase()
|
|
|
|
|
2023-04-20 18:51:16 +03:00
|
|
|
if turnOffMeta && !c.mode.NoMetabase() {
|
|
|
|
err = c.flush(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 07:55:46 +03:00
|
|
|
if c.db != nil {
|
2023-02-15 17:53:42 +03:00
|
|
|
if err = c.db.Close(); err != nil {
|
2022-07-05 07:55:46 +03:00
|
|
|
return fmt.Errorf("can't close write-cache database: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 15:47:16 +03:00
|
|
|
|
2022-07-07 15:52:40 +03:00
|
|
|
// 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 {
|
2023-04-12 17:35:10 +03:00
|
|
|
c.log.Info(logs.WritecacheWaitingForChannelsToFlush)
|
2022-01-18 15:47:16 +03:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2022-07-05 07:55:46 +03:00
|
|
|
|
2023-02-15 17:53:42 +03:00
|
|
|
if turnOffMeta {
|
2022-09-30 13:41:37 +03:00
|
|
|
c.mode = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:53:42 +03:00
|
|
|
if err = c.openStore(m.ReadOnly()); err != nil {
|
2022-07-05 07:55:46 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mode = m
|
|
|
|
return nil
|
2022-01-18 15:47:16 +03:00
|
|
|
}
|
2022-03-17 14:55:25 +03:00
|
|
|
|
|
|
|
// readOnly returns true if current mode is read-only.
|
|
|
|
// `c.modeMtx` must be taken.
|
|
|
|
func (c *cache) readOnly() bool {
|
2022-07-05 07:55:46 +03:00
|
|
|
return c.mode.ReadOnly()
|
2022-03-17 14:55:25 +03:00
|
|
|
}
|