[#1806] engine: Allow to flush write-cache

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-09-21 13:43:31 +03:00 committed by fyrchik
parent 0b4c867ef1
commit 3d882e9f47
2 changed files with 69 additions and 0 deletions

View file

@ -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()
}

View file

@ -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()
}