2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-09-14 16:20:31 +00:00
|
|
|
"fmt"
|
2021-04-06 10:56:06 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/hashicorp/golang-lru/simplelru"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2021-07-28 16:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// store represents persistent storage with in-memory LRU cache
|
|
|
|
// for flushed items on top of it.
|
|
|
|
type store struct {
|
|
|
|
flushed simplelru.LRUCache
|
|
|
|
db *bbolt.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
const lruKeysCount = 256 * 1024 * 8
|
|
|
|
|
|
|
|
const dbName = "small.bolt"
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
func (c *cache) openStore(readOnly bool) error {
|
2021-09-14 16:20:31 +00:00
|
|
|
err := util.MkdirAllX(c.path, os.ModePerm)
|
|
|
|
if err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
c.db, err = OpenDB(c.path, readOnly)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2021-09-14 16:20:31 +00:00
|
|
|
return fmt.Errorf("could not open database: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:21:10 +00:00
|
|
|
c.db.MaxBatchSize = c.maxBatchSize
|
|
|
|
c.db.MaxBatchDelay = c.maxBatchDelay
|
|
|
|
|
2022-07-05 04:55:46 +00:00
|
|
|
if !readOnly {
|
|
|
|
err = c.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(defaultBucket)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create default bucket: %w", err)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.fsTree = &fstree.FSTree{
|
|
|
|
Info: fstree.Info{
|
|
|
|
Permissions: os.ModePerm,
|
|
|
|
RootPath: c.path,
|
|
|
|
},
|
|
|
|
Depth: 1,
|
|
|
|
DirNameLen: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.flushed, _ = lru.New(lruKeysCount)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) removeFlushedKeys(n int) ([][]byte, [][]byte) {
|
|
|
|
var keysMem, keysDisk [][]byte
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
k, v, ok := s.flushed.RemoveOldest()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.(bool) {
|
|
|
|
keysMem = append(keysMem, []byte(k.(string)))
|
|
|
|
} else {
|
|
|
|
keysDisk = append(keysDisk, []byte(k.(string)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keysMem, keysDisk
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) evictObjects(putCount int) {
|
|
|
|
sum := c.flushed.Len() + putCount
|
|
|
|
if sum <= lruKeysCount {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keysMem, keysDisk := c.store.removeFlushedKeys(sum - lruKeysCount)
|
|
|
|
|
|
|
|
if err := c.deleteFromDB(keysMem); err != nil {
|
|
|
|
c.log.Error("error while removing objects from write-cache (database)", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.deleteFromDisk(keysDisk); err != nil {
|
|
|
|
c.log.Error("error while removing objects from write-cache (disk)", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) deleteFromDB(keys [][]byte) error {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-21 09:42:09 +00:00
|
|
|
err := c.db.Update(func(tx *bbolt.Tx) error {
|
2021-04-06 10:56:06 +00:00
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
for i := range keys {
|
2021-06-21 09:42:09 +00:00
|
|
|
has := b.Get(keys[i])
|
|
|
|
if has == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return errNotFound
|
2021-06-21 09:42:09 +00:00
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
if err := b.Delete(keys[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog.Write(c.log, storagelog.AddressField(string(keys[i])), storagelog.OpField("db DELETE"))
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-06-21 09:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-08 10:14:57 +00:00
|
|
|
for range keys {
|
|
|
|
c.objCounters.DecDB()
|
|
|
|
}
|
2021-06-21 09:42:09 +00:00
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) deleteFromDisk(keys [][]byte) error {
|
|
|
|
var lastErr error
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := range keys {
|
|
|
|
addrStr := string(keys[i])
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if err := addr.DecodeString(addrStr); err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
c.log.Error("can't parse address", zap.String("address", addrStr))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.fsTree.Delete(addr); err != nil && !errors.Is(err, fstree.ErrFileNotFound) {
|
|
|
|
lastErr = err
|
|
|
|
c.log.Error("can't remove object from write-cache", zap.Error(err))
|
|
|
|
continue
|
2021-09-06 14:28:55 +00:00
|
|
|
} else if err == nil {
|
|
|
|
storagelog.Write(c.log, storagelog.AddressField(string(keys[i])), storagelog.OpField("fstree DELETE"))
|
2021-09-08 09:32:20 +00:00
|
|
|
c.objCounters.DecFS()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastErr
|
|
|
|
}
|