diff --git a/pkg/local_object_storage/engine/writecache.go b/pkg/local_object_storage/engine/writecache.go new file mode 100644 index 0000000000..3dd3c67a46 --- /dev/null +++ b/pkg/local_object_storage/engine/writecache.go @@ -0,0 +1,33 @@ +package engine + +import ( + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" +) + +// FlushWriteCachePrm groups the parameters of FlushWriteCache operation. +type FlushWriteCachePrm struct { + shardID *shard.ID +} + +// SetShardID is an option to set shard ID. +// +// Option is required. +func (p *FlushWriteCachePrm) SetShardID(id *shard.ID) { + p.shardID = id +} + +// FlushWriteCacheRes groups the resulting values of FlushWriteCache operation. +type FlushWriteCacheRes struct{} + +// FlushWriteCache flushes write-cache on a single shard. +func (e *StorageEngine) FlushWriteCache(p FlushWriteCachePrm) (FlushWriteCacheRes, error) { + e.mtx.RLock() + sh, ok := e.shards[p.shardID.String()] + e.mtx.RUnlock() + + if !ok { + return FlushWriteCacheRes{}, errShardNotFound + } + + return FlushWriteCacheRes{}, sh.FlushWriteCache() +} diff --git a/pkg/local_object_storage/shard/writecache.go b/pkg/local_object_storage/shard/writecache.go new file mode 100644 index 0000000000..37de9d788a --- /dev/null +++ b/pkg/local_object_storage/shard/writecache.go @@ -0,0 +1,36 @@ +package shard + +import ( + "errors" + + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" +) + +// errWriteCacheDisabled is returned when an operation on write-cache is performed, +// but write-cache is disabled. +var errWriteCacheDisabled = errors.New("write-cache is disabled") + +// FlushWriteCache moves writecache in read-only mode and flushes all data from it. +// After the operation writecache will remain read-only mode. +func (s *Shard) FlushWriteCache() error { + 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 + } + + if err := s.writeCache.SetMode(mode.ReadOnly); err != nil { + return err + } + + return s.writeCache.Flush() +}