2022-01-18 12:47:16 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2022-07-05 04:55:46 +00:00
|
|
|
"fmt"
|
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"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2022-01-18 12:47:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrReadOnly is returned when Put/Write is performed in a read-only mode.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrReadOnly = logicerr.New("write-cache is in read-only mode")
|
2022-01-18 12:47:16 +00:00
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
// ErrNotInitialized is returned when write-cache is initializing.
|
|
|
|
var ErrNotInitialized = logicerr.New("write-cache is not initialized yet")
|
|
|
|
|
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.
|
2022-07-05 04:55:46 +00:00
|
|
|
func (c *cache) SetMode(m mode.Mode) error {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
2022-07-05 04:55:46 +00:00
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
return c.setMode(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMode applies new mode. Must be called with cache.modeMtx lock taken.
|
|
|
|
func (c *cache) setMode(m mode.Mode) error {
|
|
|
|
var err error
|
|
|
|
turnOffMeta := m.NoMetabase()
|
|
|
|
|
|
|
|
if turnOffMeta && !c.mode.NoMetabase() {
|
|
|
|
err = c.flush(true)
|
2022-09-30 10:41:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
if !c.initialized.Load() {
|
|
|
|
close(c.stopInitCh)
|
|
|
|
|
|
|
|
c.initWG.Wait()
|
|
|
|
c.stopInitCh = make(chan struct{})
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil && !turnOffMeta {
|
|
|
|
c.initFlushMarks()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-07-05 04:55:46 +00:00
|
|
|
if c.db != nil {
|
2023-02-15 14:53:42 +00:00
|
|
|
if err = c.db.Close(); err != nil {
|
2022-07-05 04:55:46 +00:00
|
|
|
return fmt.Errorf("can't close write-cache database: %w", err)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Info(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
|
|
|
|
}
|
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
if err = c.openStore(m.ReadOnly()); 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
|
|
|
|
|
|
|
// 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
|
|
|
}
|