2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultPersistInterval = time.Second
|
|
|
|
|
|
|
|
// persistLoop persists object accumulated in memory to the database.
|
|
|
|
func (c *cache) persistLoop() {
|
|
|
|
tick := time.NewTicker(defaultPersistInterval)
|
|
|
|
defer tick.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
c.mtx.RLock()
|
|
|
|
m := c.mem
|
|
|
|
c.mtx.RUnlock()
|
|
|
|
|
|
|
|
sort.Slice(m, func(i, j int) bool { return m[i].addr < m[j].addr })
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
c.persistObjects(m)
|
|
|
|
c.log.Debug("persisted items to disk",
|
|
|
|
zap.Duration("took", time.Since(start)),
|
|
|
|
zap.Int("total", len(m)))
|
|
|
|
|
2021-09-06 14:28:55 +00:00
|
|
|
for i := range m {
|
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(m[i].addr),
|
|
|
|
storagelog.OpField("in-mem DELETE persist"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
c.mtx.Lock()
|
2021-06-21 09:42:09 +00:00
|
|
|
c.curMemSize = 0
|
2021-04-06 10:56:06 +00:00
|
|
|
n := copy(c.mem, c.mem[len(m):])
|
|
|
|
c.mem = c.mem[:n]
|
|
|
|
for i := range c.mem {
|
|
|
|
c.curMemSize += uint64(len(c.mem[i].data))
|
|
|
|
}
|
|
|
|
c.mtx.Unlock()
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) persistToCache(objs []objectInfo) []int {
|
|
|
|
var (
|
2021-09-08 09:32:20 +00:00
|
|
|
failMem []int // some index is negative => all objects starting from it will overflow the cache
|
2021-04-06 10:56:06 +00:00
|
|
|
doneMem []int
|
|
|
|
)
|
2021-06-21 09:42:09 +00:00
|
|
|
var sz uint64
|
|
|
|
err := c.db.Update(func(tx *bbolt.Tx) error {
|
2021-04-06 10:56:06 +00:00
|
|
|
b := tx.Bucket(defaultBucket)
|
2021-09-08 09:32:20 +00:00
|
|
|
cacheSz := c.estimateCacheSize()
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := range objs {
|
|
|
|
if uint64(len(objs[i].data)) >= c.smallObjectSize {
|
|
|
|
failMem = append(failMem, i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
// check if object will overflow write-cache size limit
|
|
|
|
updCacheSz := c.incSizeDB(cacheSz)
|
|
|
|
if updCacheSz > c.maxCacheSize {
|
|
|
|
// set negative index. We decrement index to cover 0 val (overflow is practically impossible)
|
|
|
|
failMem = append(failMem, -i-1)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
err := b.Put([]byte(objs[i].addr), objs[i].data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-21 09:42:09 +00:00
|
|
|
sz += uint64(len(objs[i].data))
|
2021-04-06 10:56:06 +00:00
|
|
|
doneMem = append(doneMem, i)
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog.Write(c.log, storagelog.AddressField(objs[i].addr), storagelog.OpField("db PUT"))
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
// update cache size
|
|
|
|
cacheSz = updCacheSz
|
|
|
|
c.objCounters.IncDB()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-06-21 09:42:09 +00:00
|
|
|
if err == nil {
|
|
|
|
c.dbSize.Add(sz)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
if len(doneMem) > 0 {
|
|
|
|
c.evictObjects(len(doneMem))
|
|
|
|
for _, i := range doneMem {
|
|
|
|
c.flushed.Add(objs[i].addr, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var failDisk []int
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
cacheSz := c.estimateCacheSize()
|
|
|
|
|
|
|
|
for _, objInd := range failMem {
|
|
|
|
var (
|
|
|
|
updCacheSz uint64
|
|
|
|
overflowInd = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
if objInd < 0 {
|
|
|
|
// actually, since the overflow was detected in DB tx, the required space could well have been freed,
|
|
|
|
// but it is easier to consider the entire method atomic
|
|
|
|
overflowInd = -objInd - 1 // subtract 1 since we decremented index above
|
|
|
|
} else {
|
|
|
|
// check if object will overflow write-cache size limit
|
|
|
|
if updCacheSz = c.incSizeFS(cacheSz); updCacheSz > c.maxCacheSize {
|
|
|
|
overflowInd = objInd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if overflowInd >= 0 {
|
|
|
|
loop:
|
|
|
|
for j := range objs[overflowInd:] {
|
|
|
|
// exclude objects which are already stored in DB
|
|
|
|
for _, doneMemInd := range doneMem {
|
|
|
|
if j == doneMemInd {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
failDisk = append(failDisk, j)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(len(objs[objInd].data)) > c.maxObjectSize {
|
|
|
|
failDisk = append(failDisk, objInd)
|
2021-04-06 10:56:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
err := c.fsTree.Put(objs[objInd].obj.Address(), objs[objInd].data)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2021-09-08 09:32:20 +00:00
|
|
|
failDisk = append(failDisk, objInd)
|
2021-09-06 14:28:55 +00:00
|
|
|
} else {
|
2021-09-08 09:32:20 +00:00
|
|
|
storagelog.Write(c.log, storagelog.AddressField(objs[objInd].addr), storagelog.OpField("fstree PUT"))
|
|
|
|
|
|
|
|
// update cache size
|
|
|
|
cacheSz = updCacheSz
|
|
|
|
c.objCounters.IncFS()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return failDisk
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistObjects tries to write objects from memory to the persistent storage.
|
|
|
|
// If tryCache is false, writing skips cache and is done directly to the main storage.
|
|
|
|
func (c *cache) persistObjects(objs []objectInfo) {
|
|
|
|
toDisk := c.persistToCache(objs)
|
|
|
|
j := 0
|
|
|
|
|
|
|
|
for i := range objs {
|
|
|
|
ch := c.metaCh
|
|
|
|
if j < len(toDisk) {
|
|
|
|
if i == toDisk[j] {
|
|
|
|
ch = c.directCh
|
|
|
|
} else {
|
|
|
|
for ; j < len(toDisk) && i > toDisk[j]; j++ {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- objs[j].obj:
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|