2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2022-01-18 12:47:16 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2024-08-06 12:45:53 +00:00
|
|
|
"errors"
|
2022-07-05 04:55:46 +00:00
|
|
|
"fmt"
|
2024-08-06 12:45:53 +00:00
|
|
|
"os"
|
2022-01-18 12:47:16 +00:00
|
|
|
"time"
|
2022-03-17 11:55:25 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2024-09-09 15:37:06 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-01-18 12:47:16 +00:00
|
|
|
)
|
|
|
|
|
2024-08-06 12:45:53 +00:00
|
|
|
type setModePrm struct {
|
|
|
|
ignoreErrors bool
|
|
|
|
shrink bool
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:47:16 +00: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.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (c *cache) SetMode(ctx context.Context, m mode.Mode) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.SetMode",
|
2023-04-12 14:01:29 +00:00
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("mode", m.String()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-08-11 13:43:23 +00:00
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
|
|
|
|
2024-08-06 12:45:53 +00:00
|
|
|
err := c.setMode(ctx, m, setModePrm{ignoreErrors: true})
|
2023-05-18 14:19:41 +00:00
|
|
|
if err == nil {
|
2024-06-04 13:28:47 +00:00
|
|
|
c.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))
|
2023-05-18 14:19:41 +00:00
|
|
|
}
|
|
|
|
return err
|
2023-02-15 14:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setMode applies new mode. Must be called with cache.modeMtx lock taken.
|
2024-08-06 12:45:53 +00:00
|
|
|
func (c *cache) setMode(ctx context.Context, m mode.Mode, prm setModePrm) error {
|
2023-02-15 14:53:42 +00:00
|
|
|
var err error
|
|
|
|
turnOffMeta := m.NoMetabase()
|
|
|
|
|
2023-04-20 15:51:16 +00:00
|
|
|
if turnOffMeta && !c.mode.NoMetabase() {
|
2024-08-06 12:45:53 +00:00
|
|
|
err = c.flush(ctx, prm.ignoreErrors)
|
2023-04-20 15:51:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 15:37:06 +00:00
|
|
|
if err := c.closeStorage(ctx, prm.shrink); err != nil {
|
2024-08-06 12:45:53 +00:00
|
|
|
return err
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
|
2022-07-07 12:52:40 +00: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 {
|
2024-10-21 07:22:54 +00:00
|
|
|
c.log.Info(ctx, logs.WritecacheWaitingForChannelsToFlush)
|
2022-01-18 12:47:16 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
if turnOffMeta {
|
2022-09-30 10:41:37 +00:00
|
|
|
c.mode = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:28:47 +00:00
|
|
|
if err = c.openStore(mode.ConvertToComponentModeDegraded(m)); err != nil {
|
2022-07-05 04:55:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mode = m
|
|
|
|
return nil
|
2022-01-18 12:47:16 +00:00
|
|
|
}
|
2022-03-17 11:55:25 +00:00
|
|
|
|
2024-09-09 15:37:06 +00:00
|
|
|
func (c *cache) closeStorage(ctx context.Context, shrink bool) error {
|
|
|
|
if c.fsTree == nil {
|
2024-08-06 12:45:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !shrink {
|
2024-10-21 13:27:28 +00:00
|
|
|
if err := c.fsTree.Close(ctx); err != nil {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("can't close write-cache storage: %w", err)
|
2024-08-06 12:45:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 15:37:06 +00:00
|
|
|
empty := true
|
|
|
|
_, err := c.fsTree.Iterate(ctx, common.IteratePrm{
|
|
|
|
Handler: func(common.IterationElement) error {
|
|
|
|
return errIterationCompleted
|
|
|
|
},
|
2024-08-06 12:45:53 +00:00
|
|
|
})
|
2024-09-09 15:37:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errIterationCompleted) {
|
|
|
|
empty = false
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("failed to check write-cache items: %w", err)
|
|
|
|
}
|
2024-08-06 12:45:53 +00:00
|
|
|
}
|
2024-10-21 13:27:28 +00:00
|
|
|
if err := c.fsTree.Close(ctx); err != nil {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("can't close write-cache storage: %w", err)
|
2024-08-06 12:45:53 +00:00
|
|
|
}
|
|
|
|
if empty {
|
2024-09-09 15:37:06 +00:00
|
|
|
err := os.RemoveAll(c.path)
|
2024-08-06 12:45:53 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("failed to remove write-cache files: %w", err)
|
2024-08-06 12:45:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-10-21 07:22:54 +00:00
|
|
|
c.log.Info(ctx, logs.WritecacheShrinkSkippedNotEmpty)
|
2024-08-06 12:45:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:55:25 +00:00
|
|
|
// readOnly returns true if current mode is read-only.
|
|
|
|
// `c.modeMtx` must be taken.
|
|
|
|
func (c *cache) readOnly() bool {
|
2022-07-05 04:55:46 +00:00
|
|
|
return c.mode.ReadOnly()
|
2022-03-17 11:55:25 +00:00
|
|
|
}
|
2024-02-20 14:24:57 +00:00
|
|
|
|
|
|
|
// noMetabase returns true if c is operating without the metabase.
|
|
|
|
// `c.modeMtx` must be taken.
|
|
|
|
func (c *cache) noMetabase() bool {
|
|
|
|
return c.mode.NoMetabase()
|
|
|
|
}
|