2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2022-01-20 15:53:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
// flushLoop periodically flushes changes from the database to memory.
|
|
|
|
func (c *cache) flushLoop() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < c.workersCount; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
c.flushWorker(i)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
c.flushBigObjects()
|
|
|
|
}()
|
|
|
|
|
2022-05-31 10:56:35 +00:00
|
|
|
tt := time.NewTimer(defaultFlushInterval)
|
|
|
|
defer tt.Stop()
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
2022-05-31 10:56:35 +00:00
|
|
|
case <-tt.C:
|
2021-04-06 10:56:06 +00:00
|
|
|
c.flush()
|
2022-05-31 10:56:35 +00:00
|
|
|
tt.Reset(defaultFlushInterval)
|
2021-04-06 10:56:06 +00:00
|
|
|
case <-c.closeCh:
|
|
|
|
c.log.Debug("waiting for workers to quit")
|
|
|
|
wg.Wait()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) flush() {
|
|
|
|
lastKey := []byte{}
|
|
|
|
var m []objectInfo
|
|
|
|
for {
|
|
|
|
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),
|
|
|
|
data: cloneBytes(v),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.evictObjects(len(m))
|
|
|
|
for i := range m {
|
|
|
|
c.flushed.Add(m[i].addr, true)
|
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
c.log.Debug("flushed items from write-cache",
|
|
|
|
zap.Int("count", len(m)),
|
|
|
|
zap.String("start", base58.Encode(lastKey)))
|
|
|
|
|
|
|
|
if len(m) > 0 {
|
|
|
|
lastKey = append([]byte(m[len(m)-1].addr), 0)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) flushBigObjects() {
|
|
|
|
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
|
|
|
|
|
|
|
var prm fstree.IterationPrm
|
|
|
|
prm.WithHandler(func(addr oid.Address, data []byte) 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-01-11 11:33:04 +00:00
|
|
|
c.mtx.Lock()
|
|
|
|
_, compress := c.compressFlags[sAddr]
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
if _, err := c.blobstor.PutRaw(addr, data, compress); 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
|
|
|
|
c.store.flushed.Add(sAddr, false)
|
|
|
|
|
2021-12-09 12:30:45 +00:00
|
|
|
evictNum++
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
2022-05-23 14:15:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
_ = c.fsTree.Iterate(prm)
|
2021-12-09 12:30:45 +00:00
|
|
|
|
|
|
|
// evict objects which were successfully written to BlobStor
|
|
|
|
c.evictObjects(evictNum)
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
case <-c.closeCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flushWorker runs in a separate goroutine and write objects to the main storage.
|
|
|
|
// If flushFirst is true, flushing objects from cache database takes priority over
|
|
|
|
// putting new objects.
|
|
|
|
func (c *cache) flushWorker(num int) {
|
|
|
|
priorityCh := c.directCh
|
|
|
|
switch num % 3 {
|
|
|
|
case 0:
|
|
|
|
priorityCh = c.flushCh
|
|
|
|
case 1:
|
|
|
|
priorityCh = c.metaCh
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj *object.Object
|
|
|
|
for {
|
|
|
|
metaOnly := false
|
|
|
|
|
|
|
|
// Give priority to direct put.
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO(fyrchik): #1150 do this once in N iterations depending on load
|
2021-04-06 10:56:06 +00:00
|
|
|
select {
|
|
|
|
case obj = <-priorityCh:
|
2022-01-19 08:47:52 +00:00
|
|
|
metaOnly = num%3 == 1
|
2021-04-06 10:56:06 +00:00
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case obj = <-c.directCh:
|
|
|
|
case obj = <-c.flushCh:
|
|
|
|
case obj = <-c.metaCh:
|
|
|
|
metaOnly = true
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.writeObject(obj, metaOnly)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("can't flush object to the main storage", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeObject is used to write object directly to the main storage.
|
|
|
|
func (c *cache) writeObject(obj *object.Object, metaOnly bool) error {
|
|
|
|
var id *blobovnicza.ID
|
|
|
|
|
|
|
|
if !metaOnly {
|
2022-05-23 14:15:16 +00:00
|
|
|
var prm blobstor.PutPrm
|
2021-04-06 10:56:06 +00:00
|
|
|
prm.SetObject(obj)
|
2022-05-23 14:15:16 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err := c.blobstor.Put(prm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id = res.BlobovniczaID()
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta.Put(c.metabase, obj, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneBytes(a []byte) []byte {
|
|
|
|
b := make([]byte, len(a))
|
|
|
|
copy(b, a)
|
|
|
|
return b
|
|
|
|
}
|