2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2021-04-06 10:56:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2022-06-28 14:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
Get(address oid.Address) (*object.Object, error)
|
|
|
|
Head(oid.Address) (*object.Object, error)
|
|
|
|
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-07-19 11:27:12 +00:00
|
|
|
SetLogger(*zap.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
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
mode mode.Mode
|
2022-01-18 12:47:16 +00:00
|
|
|
modeMtx sync.RWMutex
|
|
|
|
|
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
|
|
|
|
// closeCh is close channel.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2022-07-07 12:52:40 +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{
|
|
|
|
log: 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,
|
2021-04-06 10:56:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c.options)
|
|
|
|
}
|
|
|
|
|
2022-09-01 06:33:56 +00:00
|
|
|
// Make the LRU cache contain which take approximately 3/4 of the maximum space.
|
|
|
|
// Assume small and big objects are stored in 50-50 proportion.
|
|
|
|
c.maxFlushedMarksCount = int(c.maxCacheSize/c.maxObjectSize+c.maxCacheSize/c.smallObjectSize) / 2 * 3 / 4
|
|
|
|
// Trigger the removal when the cache is 7/8 full, so that new items can still arrive.
|
|
|
|
c.maxRemoveBatchSize = c.maxFlushedMarksCount / 8
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
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.
|
|
|
|
func (c *cache) SetLogger(l *zap.Logger) {
|
|
|
|
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-06-01 07:06:32 +00:00
|
|
|
c.initFlushMarks()
|
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 {
|
2022-01-18 14:27:07 +00:00
|
|
|
// Finish all in-progress operations.
|
2022-07-05 04:55:46 +00:00
|
|
|
if err := c.SetMode(mode.ReadOnly); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-18 14:27:07 +00:00
|
|
|
|
2022-08-22 11:40:03 +00:00
|
|
|
if c.closeCh != nil {
|
|
|
|
close(c.closeCh)
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Wait()
|
2022-08-22 11:40:03 +00:00
|
|
|
if c.closeCh != nil {
|
|
|
|
c.closeCh = nil
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
|
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
|
|
|
}
|