2022-09-21 10:43:31 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2022-09-21 10:43:31 +00:00
|
|
|
"errors"
|
2023-04-12 14:01:29 +00:00
|
|
|
|
2024-07-31 13:30:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2024-08-06 10:20:33 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
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"
|
2024-07-31 13:30:07 +00:00
|
|
|
"go.uber.org/zap"
|
2022-09-21 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
2024-07-31 13:30:07 +00:00
|
|
|
var (
|
|
|
|
dummyCancel = &writecacheSealCanceler{cancel: func() {}}
|
|
|
|
notInitializedCancel = &writecacheSealCanceler{cancel: func() {}}
|
|
|
|
errWriteCacheSealing = errors.New("writecache is already sealing or shard is not initialized")
|
|
|
|
)
|
|
|
|
|
|
|
|
type writecacheSealCanceler struct {
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
2022-09-26 08:54:21 +00:00
|
|
|
// FlushWriteCachePrm represents parameters of a `FlushWriteCache` operation.
|
|
|
|
type FlushWriteCachePrm struct {
|
|
|
|
ignoreErrors bool
|
2023-12-27 05:20:15 +00:00
|
|
|
seal bool
|
2022-09-26 08:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetIgnoreErrors sets the flag to ignore read-errors during flush.
|
|
|
|
func (p *FlushWriteCachePrm) SetIgnoreErrors(ignore bool) {
|
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
// SetSeal sets the flag to left writecache in read-only mode after flush.
|
|
|
|
func (p *FlushWriteCachePrm) SetSeal(v bool) {
|
|
|
|
p.seal = v
|
|
|
|
}
|
|
|
|
|
2022-09-21 10:43:31 +00:00
|
|
|
// errWriteCacheDisabled is returned when an operation on write-cache is performed,
|
|
|
|
// but write-cache is disabled.
|
|
|
|
var errWriteCacheDisabled = errors.New("write-cache is disabled")
|
|
|
|
|
2022-11-08 12:20:58 +00:00
|
|
|
// FlushWriteCache flushes all data from the write-cache.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) FlushWriteCache(ctx context.Context, p FlushWriteCachePrm) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.FlushWriteCache",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.Bool("ignore_errors", p.ignoreErrors),
|
2023-12-27 05:20:15 +00:00
|
|
|
attribute.Bool("seal", p.seal),
|
2023-04-12 14:01:29 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-09-21 10:43:31 +00:00
|
|
|
if !s.hasWriteCache() {
|
|
|
|
return errWriteCacheDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
// To write data to the blobstor we need to write to the blobstor and the metabase.
|
|
|
|
if s.info.Mode.ReadOnly() {
|
|
|
|
return ErrReadOnlyMode
|
|
|
|
}
|
|
|
|
if s.info.Mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
return s.writeCache.Flush(ctx, p.ignoreErrors, p.seal)
|
2022-09-21 10:43:31 +00:00
|
|
|
}
|
2023-12-27 11:37:22 +00:00
|
|
|
|
|
|
|
type SealWriteCachePrm struct {
|
|
|
|
IgnoreErrors bool
|
2024-07-31 13:30:07 +00:00
|
|
|
Async bool
|
2024-08-06 10:20:33 +00:00
|
|
|
RestoreMode bool
|
2024-08-06 12:45:53 +00:00
|
|
|
Shrink bool
|
2023-12-27 11:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SealWriteCache flushes all data from the write-cache and moves it to degraded read only mode.
|
|
|
|
func (s *Shard) SealWriteCache(ctx context.Context, p SealWriteCachePrm) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.SealWriteCache",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.Bool("ignore_errors", p.IgnoreErrors),
|
2024-08-06 10:20:33 +00:00
|
|
|
attribute.Bool("restore_mode", p.RestoreMode),
|
2023-12-27 11:37:22 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if !s.hasWriteCache() {
|
|
|
|
return errWriteCacheDisabled
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:30:07 +00:00
|
|
|
if p.Async {
|
|
|
|
ctx = context.WithoutCancel(ctx)
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
canceler := &writecacheSealCanceler{cancel: cancel}
|
|
|
|
if !s.writecacheSealCancel.CompareAndSwap(dummyCancel, canceler) {
|
|
|
|
return errWriteCacheSealing
|
|
|
|
}
|
2023-12-27 11:37:22 +00:00
|
|
|
s.m.RLock()
|
2024-07-31 13:30:07 +00:00
|
|
|
cleanup := func() {
|
|
|
|
s.m.RUnlock()
|
|
|
|
s.writecacheSealCancel.Store(dummyCancel)
|
|
|
|
}
|
2023-12-27 11:37:22 +00:00
|
|
|
|
|
|
|
if s.info.Mode.ReadOnly() {
|
2024-07-31 13:30:07 +00:00
|
|
|
cleanup()
|
2023-12-27 11:37:22 +00:00
|
|
|
return ErrReadOnlyMode
|
|
|
|
}
|
|
|
|
if s.info.Mode.NoMetabase() {
|
2024-07-31 13:30:07 +00:00
|
|
|
cleanup()
|
2023-12-27 11:37:22 +00:00
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:30:07 +00:00
|
|
|
if !p.Async {
|
|
|
|
defer cleanup()
|
|
|
|
}
|
|
|
|
prm := writecache.SealPrm{IgnoreErrors: p.IgnoreErrors, RestoreMode: p.RestoreMode, Shrink: p.Shrink}
|
|
|
|
if p.Async {
|
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
close(started)
|
|
|
|
defer cleanup()
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Info(ctx, logs.StartedWritecacheSealAsync)
|
2024-07-31 13:30:07 +00:00
|
|
|
if err := s.writeCache.Seal(ctx, prm); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Warn(ctx, logs.FailedToSealWritecacheAsync, zap.Error(err))
|
2024-07-31 13:30:07 +00:00
|
|
|
return
|
|
|
|
}
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Info(ctx, logs.WritecacheSealCompletedAsync)
|
2024-07-31 13:30:07 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-started:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.writeCache.Seal(ctx, prm)
|
2023-12-27 11:37:22 +00:00
|
|
|
}
|