2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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-01-26 12:11:13 +00:00
|
|
|
Get(*addressSDK.Address) (*object.Object, error)
|
|
|
|
Head(*addressSDK.Address) (*object.Object, error)
|
|
|
|
Delete(*addressSDK.Address) error
|
2022-01-20 16:52:30 +00:00
|
|
|
Iterate(*IterationPrm) error
|
2021-04-06 10:56:06 +00:00
|
|
|
Put(*object.Object) error
|
2022-01-18 12:47:16 +00:00
|
|
|
SetMode(Mode)
|
2021-12-20 10:03:06 +00:00
|
|
|
DumpInfo() Info
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
Init() error
|
|
|
|
Open() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
options
|
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
// mtx protects mem field, statistics, counters and compressFlags.
|
2021-04-06 10:56:06 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
mem []objectInfo
|
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
mode Mode
|
|
|
|
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
|
|
|
// curMemSize is the current size of all objects cached in memory.
|
|
|
|
curMemSize uint64
|
|
|
|
|
|
|
|
// flushCh is a channel with objects to flush.
|
|
|
|
flushCh chan *object.Object
|
|
|
|
// directCh is a channel with objects to put directly to the main storage.
|
|
|
|
// it is prioritized over flushCh.
|
|
|
|
directCh chan *object.Object
|
|
|
|
// metaCh is a channel with objects for which only metadata needs to be written.
|
|
|
|
metaCh chan *object.Object
|
|
|
|
// closeCh is close channel.
|
|
|
|
closeCh chan struct{}
|
|
|
|
evictCh chan []byte
|
|
|
|
// 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 (
|
|
|
|
maxInMemorySizeBytes = 1024 * 1024 * 1024 // 1 GiB
|
|
|
|
maxObjectSize = 64 * 1024 * 1024 // 64 MiB
|
|
|
|
smallObjectSize = 32 * 1024 // 32 KiB
|
2021-09-08 09:32:20 +00:00
|
|
|
maxCacheSizeBytes = 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{
|
|
|
|
flushCh: make(chan *object.Object),
|
|
|
|
directCh: make(chan *object.Object),
|
|
|
|
metaCh: make(chan *object.Object),
|
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
evictCh: make(chan []byte),
|
2022-01-18 12:47:16 +00:00
|
|
|
mode: ModeReadWrite,
|
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(),
|
|
|
|
maxMemSize: maxInMemorySizeBytes,
|
|
|
|
maxObjectSize: maxObjectSize,
|
|
|
|
smallObjectSize: smallObjectSize,
|
|
|
|
workersCount: flushWorkersCount,
|
2021-09-08 09:32:20 +00:00
|
|
|
maxCacheSize: maxCacheSizeBytes,
|
2021-04-06 10:56:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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.
|
2021-04-06 10:56:06 +00:00
|
|
|
func (c *cache) Open() error {
|
2021-09-08 09:32:20 +00:00
|
|
|
err := c.openStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.objCounters == nil {
|
|
|
|
c.objCounters = &counters{
|
|
|
|
db: c.db,
|
|
|
|
fs: c.fsTree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.objCounters.Read()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init runs necessary services.
|
|
|
|
func (c *cache) Init() error {
|
|
|
|
go c.persistLoop()
|
|
|
|
go c.flushLoop()
|
|
|
|
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.
|
|
|
|
c.SetMode(ModeReadOnly)
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
close(c.closeCh)
|
2021-09-08 09:32:20 +00:00
|
|
|
c.objCounters.FlushAndClose()
|
2021-04-06 10:56:06 +00:00
|
|
|
return c.db.Close()
|
|
|
|
}
|