2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2022-09-21 06:33:58 +00:00
|
|
|
"errors"
|
2021-04-06 10:56:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2022-07-07 12:56:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2022-08-31 09:20:43 +00:00
|
|
|
objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-07-06 13:41:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2021-04-06 10:56:06 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
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"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// flushBatchSize is amount of keys which will be read from cache to be flushed
|
|
|
|
// to the main storage. It is used to reduce contention between cache put
|
|
|
|
// and cache persist.
|
|
|
|
flushBatchSize = 512
|
2022-05-31 11:15:31 +00:00
|
|
|
// defaultFlushWorkersCount is number of workers for putting objects in main storage.
|
|
|
|
defaultFlushWorkersCount = 20
|
2021-04-06 10:56:06 +00:00
|
|
|
// defaultFlushInterval is default time interval between successive flushes.
|
|
|
|
defaultFlushInterval = time.Second
|
|
|
|
)
|
|
|
|
|
2022-09-21 06:33:58 +00:00
|
|
|
// errMustBeReadOnly is returned when write-cache must be
|
|
|
|
// in read-only mode to perform an operation.
|
|
|
|
var errMustBeReadOnly = errors.New("write-cache must be in read-only mode")
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// runFlushLoop starts background workers which periodically flush objects to the blobstor.
|
|
|
|
func (c *cache) runFlushLoop() {
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := 0; i < c.workersCount; i++ {
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
go c.flushWorker(i)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
go c.flushBigObjects()
|
|
|
|
|
|
|
|
c.wg.Add(1)
|
2021-04-06 10:56:06 +00:00
|
|
|
go func() {
|
2022-07-07 12:52:40 +00:00
|
|
|
defer c.wg.Done()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
tt := time.NewTimer(defaultFlushInterval)
|
|
|
|
defer tt.Stop()
|
2022-05-31 10:56:35 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tt.C:
|
|
|
|
c.flush()
|
|
|
|
tt.Reset(defaultFlushInterval)
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
}()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) flush() {
|
|
|
|
lastKey := []byte{}
|
|
|
|
var m []objectInfo
|
|
|
|
for {
|
2022-07-07 12:52:40 +00:00
|
|
|
select {
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
m = m[:0]
|
|
|
|
sz := 0
|
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
2022-03-17 11:55:25 +00:00
|
|
|
if c.readOnly() {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// We put objects in batches of fixed size to not interfere with main put cycle a lot.
|
|
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
cs := b.Cursor()
|
|
|
|
for k, v := cs.Seek(lastKey); k != nil && len(m) < flushBatchSize; k, v = cs.Next() {
|
|
|
|
if _, ok := c.flushed.Peek(string(k)); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sz += len(k) + len(v)
|
|
|
|
m = append(m, objectInfo{
|
|
|
|
addr: string(k),
|
2022-07-07 12:56:05 +00:00
|
|
|
data: slice.Copy(v),
|
2021-04-06 10:56:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := range m {
|
|
|
|
obj := object.New()
|
|
|
|
if err := obj.Unmarshal(m[i].data); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case c.flushCh <- obj:
|
|
|
|
case <-c.closeCh:
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:39:40 +00:00
|
|
|
if len(m) == 0 {
|
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
c.log.Debug("tried to flush items from write-cache",
|
2021-04-06 10:56:06 +00:00
|
|
|
zap.Int("count", len(m)),
|
|
|
|
zap.String("start", base58.Encode(lastKey)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) flushBigObjects() {
|
2022-07-07 12:52:40 +00:00
|
|
|
defer c.wg.Done()
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
tick := time.NewTicker(defaultFlushInterval * 10)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
2022-03-17 11:55:25 +00:00
|
|
|
if c.readOnly() {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
break
|
|
|
|
}
|
2021-12-09 12:30:45 +00:00
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
evictNum := 0
|
2022-05-23 14:15:16 +00:00
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
var prm common.IteratePrm
|
|
|
|
prm.LazyHandler = func(addr oid.Address, f func() ([]byte, error)) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
sAddr := addr.EncodeToString()
|
2021-12-09 12:29:31 +00:00
|
|
|
|
|
|
|
if _, ok := c.store.flushed.Peek(sAddr); ok {
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:11:48 +00:00
|
|
|
data, err := f()
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("can't read a file", zap.Stringer("address", addr))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
c.mtx.Lock()
|
|
|
|
_, compress := c.compressFlags[sAddr]
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Address = addr
|
|
|
|
prm.RawData = data
|
2022-07-08 07:09:48 +00:00
|
|
|
prm.DontCompress = !compress
|
2022-07-06 13:41:35 +00:00
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
if _, err := c.blobstor.Put(prm); err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
c.log.Error("cant flush object to blobstor", zap.Error(err))
|
2021-12-09 12:29:31 +00:00
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2021-12-09 12:29:31 +00:00
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
if compress {
|
|
|
|
c.mtx.Lock()
|
|
|
|
delete(c.compressFlags, sAddr)
|
|
|
|
c.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-12-09 12:29:31 +00:00
|
|
|
// mark object as flushed
|
2022-08-31 09:20:43 +00:00
|
|
|
c.flushed.Add(sAddr, false)
|
2021-12-09 12:29:31 +00:00
|
|
|
|
2021-12-09 12:30:45 +00:00
|
|
|
evictNum++
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
2022-05-23 14:15:16 +00:00
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
_, _ = c.fsTree.Iterate(prm)
|
2021-12-09 12:30:45 +00:00
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
case <-c.closeCh:
|
2022-07-07 12:52:40 +00:00
|
|
|
return
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 06:14:47 +00:00
|
|
|
// flushWorker writes objects to the main storage.
|
2022-07-07 12:52:40 +00:00
|
|
|
func (c *cache) flushWorker(_ int) {
|
|
|
|
defer c.wg.Done()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
var obj *object.Object
|
|
|
|
for {
|
|
|
|
// Give priority to direct put.
|
|
|
|
select {
|
2022-07-07 12:52:40 +00:00
|
|
|
case obj = <-c.flushCh:
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
err := c.flushObject(obj)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Error("can't flush object to the main storage", zap.Error(err))
|
2022-08-31 09:20:43 +00:00
|
|
|
} else {
|
|
|
|
c.flushed.Add(objectCore.AddressOf(obj).EncodeToString(), true)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// flushObject is used to write object directly to the main storage.
|
|
|
|
func (c *cache) flushObject(obj *object.Object) error {
|
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Object = obj
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
res, err := c.blobstor.Put(prm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
var pPrm meta.PutPrm
|
2022-07-12 14:59:37 +00:00
|
|
|
pPrm.SetObject(obj)
|
2022-07-07 12:52:40 +00:00
|
|
|
pPrm.SetStorageID(res.StorageID)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
_, err = c.metabase.Put(pPrm)
|
2022-07-12 14:42:55 +00:00
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
|
|
|
|
// Flush flushes all objects from the write-cache to the main storage.
|
|
|
|
// Write-cache must be in readonly mode to ensure correctness of an operation and
|
|
|
|
// to prevent interference with background flush workers.
|
|
|
|
func (c *cache) Flush() error {
|
|
|
|
c.modeMtx.RLock()
|
|
|
|
defer c.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if !c.mode.ReadOnly() {
|
|
|
|
return errMustBeReadOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm common.IteratePrm
|
|
|
|
prm.LazyHandler = func(addr oid.Address, f func() ([]byte, error)) error {
|
|
|
|
_, ok := c.flushed.Peek(addr.EncodeToString())
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := f()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj object.Object
|
|
|
|
err = obj.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.flushObject(&obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.fsTree.Iterate(prm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
cs := b.Cursor()
|
|
|
|
for k, data := cs.Seek(nil); k != nil; k, data = cs.Next() {
|
|
|
|
sa := string(k)
|
|
|
|
if _, ok := c.flushed.Peek(sa); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addr.DecodeString(sa); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj object.Object
|
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.flushObject(&obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|