Skip to content

Commit

Permalink
[nspcc-dev#1825] writecache: Flush cache when moving to the DEGRADED …
Browse files Browse the repository at this point in the history
…mode

Degraded mode allows us to operate without an SSD,
thus writecache should be unavailable in this mode.

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
  • Loading branch information
fyrchik committed Oct 4, 2022
1 parent 236414d commit 8b3b16f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changelog for NeoFS Node
### Changed

- Allow to evacuate shard data with `EvacuateShard` control RPC (#1800)
- Flush write-cache when moving shard to DEGRADED mode (#1825)

### Fixed
- Description of command `netmap nodeinfo` (#1821)
Expand Down
5 changes: 3 additions & 2 deletions pkg/local_object_storage/shard/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ func (s *Shard) Put(prm PutPrm) (PutRes, error) {

// exist check are not performed there, these checks should be executed
// ahead of `Put` by storage engine
if s.hasWriteCache() {
tryCache := s.hasWriteCache() && !m.NoMetabase()
if tryCache {
res, err = s.writeCache.Put(putPrm)
}
if err != nil || !s.hasWriteCache() {
if err != nil || !tryCache {
if err != nil {
s.log.Debug("can't put object to the write-cache, trying blobstor",
zap.String("err", err.Error()))
Expand Down
8 changes: 6 additions & 2 deletions pkg/local_object_storage/writecache/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *cache) runFlushLoop() {
for {
select {
case <-tt.C:
c.flush()
c.flushDB()
tt.Reset(defaultFlushInterval)
case <-c.closeCh:
return
Expand All @@ -59,7 +59,7 @@ func (c *cache) runFlushLoop() {
}()
}

func (c *cache) flush() {
func (c *cache) flushDB() {
lastKey := []byte{}
var m []objectInfo
for {
Expand Down Expand Up @@ -241,6 +241,10 @@ func (c *cache) Flush(ignoreErrors bool) error {
return errMustBeReadOnly
}

return c.flush(ignoreErrors)
}

func (c *cache) flush(ignoreErrors bool) error {
var prm common.IteratePrm
prm.IgnoreErrors = ignoreErrors
prm.LazyHandler = func(addr oid.Address, f func() ([]byte, error)) error {
Expand Down
34 changes: 30 additions & 4 deletions pkg/local_object_storage/writecache/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ func TestFlush(t *testing.T) {
wc, bs, mb := newCache(t)
objects := putObjects(t, wc)

t.Run("must be read-only", func(t *testing.T) {
require.ErrorIs(t, wc.Flush(false), errMustBeReadOnly)
})

require.NoError(t, wc.SetMode(mode.ReadOnly))
require.NoError(t, bs.SetMode(mode.ReadWrite))
require.NoError(t, mb.SetMode(mode.ReadWrite))
Expand All @@ -135,6 +131,36 @@ func TestFlush(t *testing.T) {
check(t, mb, bs, objects[2:])
})

t.Run("flush on moving to degraded mode", func(t *testing.T) {
wc, bs, mb := newCache(t)
objects := putObjects(t, wc)

// Blobstor is read-only, so we expect en error from `flush` here.
require.Error(t, wc.SetMode(mode.Degraded))

// First move to read-only mode to close background workers.
require.NoError(t, wc.SetMode(mode.ReadOnly))
require.NoError(t, bs.SetMode(mode.ReadWrite))
require.NoError(t, mb.SetMode(mode.ReadWrite))

wc.(*cache).flushed.Add(objects[0].addr.EncodeToString(), true)
wc.(*cache).flushed.Add(objects[1].addr.EncodeToString(), false)

require.NoError(t, wc.SetMode(mode.Degraded))

for i := 0; i < 2; i++ {
var mPrm meta.GetPrm
mPrm.SetAddress(objects[i].addr)
_, err := mb.Get(mPrm)
require.Error(t, err)

_, err = bs.Get(common.GetPrm{Address: objects[i].addr})
require.Error(t, err)
}

check(t, mb, bs, objects[2:])
})

t.Run("ignore errors", func(t *testing.T) {
testIgnoreErrors := func(t *testing.T, f func(*cache)) {
wc, bs, mb := newCache(t)
Expand Down
13 changes: 10 additions & 3 deletions pkg/local_object_storage/writecache/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func (c *cache) SetMode(m mode.Mode) error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()

if m.ReadOnly() == c.readOnly() {
c.mode = m
return nil
if m.NoMetabase() && !c.mode.NoMetabase() {
err := c.flush(true)
if err != nil {
return err
}
}

if c.db != nil {
Expand All @@ -37,6 +39,11 @@ func (c *cache) SetMode(m mode.Mode) error {
time.Sleep(time.Second)
}

if m.NoMetabase() {
c.mode = m
return nil
}

if err := c.openStore(m.ReadOnly()); err != nil {
return err
}
Expand Down

0 comments on commit 8b3b16f

Please sign in to comment.