2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2023-03-21 10:38:44 +00:00
|
|
|
"os"
|
2021-04-06 10:56:06 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-16 15:21:10 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
// Info groups the information about write-cache.
|
|
|
|
type Info struct {
|
|
|
|
// Full path to the write-cache.
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// Cache represents write-cache for objects.
|
|
|
|
type Cache interface {
|
2023-03-13 11:37:35 +00:00
|
|
|
Get(ctx context.Context, address oid.Address) (*object.Object, error)
|
|
|
|
Head(context.Context, oid.Address) (*object.Object, error)
|
2022-10-28 10:55:21 +00:00
|
|
|
// Delete removes object referenced by the given oid.Address from the
|
|
|
|
// Cache. Returns any error encountered that prevented the object to be
|
|
|
|
// removed.
|
|
|
|
//
|
|
|
|
// Returns apistatus.ObjectNotFound if object is missing in the Cache.
|
|
|
|
// Returns ErrReadOnly if the Cache is currently in the read-only mode.
|
2022-05-31 17:00:41 +00:00
|
|
|
Delete(oid.Address) error
|
2022-05-20 15:43:05 +00:00
|
|
|
Iterate(IterationPrm) error
|
2022-07-07 12:52:40 +00:00
|
|
|
Put(common.PutPrm) (common.PutRes, error)
|
2022-07-05 04:55:46 +00:00
|
|
|
SetMode(mode.Mode) error
|
2022-09-28 07:41:01 +00:00
|
|
|
SetLogger(*logger.Logger)
|
2021-12-20 10:03:06 +00:00
|
|
|
DumpInfo() Info
|
2022-09-26 08:54:21 +00:00
|
|
|
Flush(bool) error
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
Init() error
|
2022-06-28 13:42:50 +00:00
|
|
|
Open(readOnly bool) error
|
2021-04-06 10:56:06 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
options
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// mtx protects statistics, counters and compressFlags.
|
2021-04-06 10:56:06 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2023-05-10 14:43:49 +00:00
|
|
|
mode mode.Mode
|
|
|
|
modeMtx sync.RWMutex
|
2022-01-18 12:47:16 +00:00
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
// compressFlags maps address of a big object to boolean value indicating
|
|
|
|
// whether object should be compressed.
|
|
|
|
compressFlags map[string]struct{}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// flushCh is a channel with objects to flush.
|
|
|
|
flushCh chan *object.Object
|
2023-02-15 14:53:42 +00:00
|
|
|
// closeCh is close channel, protected by modeMtx.
|
2021-04-06 10:56:06 +00:00
|
|
|
closeCh chan struct{}
|
2022-07-07 12:52:40 +00:00
|
|
|
// wg is a wait group for flush workers.
|
|
|
|
wg sync.WaitGroup
|
2021-04-06 10:56:06 +00:00
|
|
|
// store contains underlying database.
|
|
|
|
store
|
|
|
|
// fsTree contains big files stored directly on file-system.
|
|
|
|
fsTree *fstree.FSTree
|
|
|
|
}
|
|
|
|
|
2022-12-20 12:29:05 +00:00
|
|
|
// wcStorageType is used for write-cache operations logging.
|
|
|
|
const wcStorageType = "write-cache"
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
type objectInfo struct {
|
|
|
|
addr string
|
|
|
|
data []byte
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-09-01 06:08:28 +00:00
|
|
|
defaultMaxObjectSize = 64 * 1024 * 1024 // 64 MiB
|
|
|
|
defaultSmallObjectSize = 32 * 1024 // 32 KiB
|
|
|
|
defaultMaxCacheSize = 1 << 30 // 1 GiB
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultBucket = []byte{0}
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates new writecache instance.
|
|
|
|
func New(opts ...Option) Cache {
|
|
|
|
c := &cache{
|
2023-05-10 14:43:49 +00:00
|
|
|
flushCh: make(chan *object.Object),
|
|
|
|
mode: mode.ReadWrite,
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
compressFlags: make(map[string]struct{}),
|
2021-04-06 10:56:06 +00:00
|
|
|
options: options{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.NewNop()},
|
2022-09-01 06:08:28 +00:00
|
|
|
maxObjectSize: defaultMaxObjectSize,
|
|
|
|
smallObjectSize: defaultSmallObjectSize,
|
2022-05-31 11:15:31 +00:00
|
|
|
workersCount: defaultFlushWorkersCount,
|
2022-09-01 06:08:28 +00:00
|
|
|
maxCacheSize: defaultMaxCacheSize,
|
2022-06-16 15:21:10 +00:00
|
|
|
maxBatchSize: bbolt.DefaultMaxBatchSize,
|
|
|
|
maxBatchDelay: bbolt.DefaultMaxBatchDelay,
|
2023-03-21 10:38:44 +00:00
|
|
|
openFile: os.OpenFile,
|
2021-04-06 10:56:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:27:12 +00:00
|
|
|
// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
|
2022-09-28 07:41:01 +00:00
|
|
|
func (c *cache) SetLogger(l *logger.Logger) {
|
2022-07-19 11:27:12 +00:00
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
func (c *cache) DumpInfo() Info {
|
|
|
|
return Info{
|
|
|
|
Path: c.path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
// Open opens and initializes database. Reads object counters from the ObjectCounters instance.
|
2022-06-28 13:42:50 +00:00
|
|
|
func (c *cache) Open(readOnly bool) error {
|
|
|
|
err := c.openStore(readOnly)
|
2021-09-08 09:32:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 11:40:03 +00:00
|
|
|
// Opening after Close is done during maintenance mode,
|
|
|
|
// thus we need to create a channel here.
|
|
|
|
c.closeCh = make(chan struct{})
|
|
|
|
|
2022-09-01 05:56:21 +00:00
|
|
|
return c.initCounters()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init runs necessary services.
|
|
|
|
func (c *cache) Init() error {
|
2022-07-07 12:52:40 +00:00
|
|
|
c.runFlushLoop()
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
// Close closes db connection and stops services. Executes ObjectCounters.FlushAndClose op.
|
2021-04-06 10:56:06 +00:00
|
|
|
func (c *cache) Close() error {
|
2023-05-10 14:43:49 +00:00
|
|
|
// We cannot lock mutex for the whole operation duration
|
|
|
|
// because it is taken by some background workers, so `wg.Wait()` is done without modeMtx.
|
2023-02-15 14:53:42 +00:00
|
|
|
c.modeMtx.Lock()
|
2022-08-22 11:40:03 +00:00
|
|
|
if c.closeCh != nil {
|
|
|
|
close(c.closeCh)
|
|
|
|
}
|
2023-05-10 14:43:49 +00:00
|
|
|
c.mode = mode.DegradedReadOnly // prevent new operations from being processed
|
|
|
|
c.modeMtx.Unlock()
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Wait()
|
|
|
|
|
2023-05-10 14:43:49 +00:00
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
2023-02-15 14:53:42 +00:00
|
|
|
|
2023-05-10 14:43:49 +00:00
|
|
|
c.closeCh = nil
|
2022-08-22 11:40:03 +00:00
|
|
|
var err error
|
2022-07-05 04:55:46 +00:00
|
|
|
if c.db != nil {
|
2022-08-22 11:40:03 +00:00
|
|
|
err = c.db.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.db = nil
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|