2023-06-22 11:55:30 +00:00
|
|
|
package writecachebbolt
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2021-09-14 16:20:31 +00:00
|
|
|
"fmt"
|
2023-08-11 09:51:41 +00:00
|
|
|
"math"
|
2021-04-06 10:56:06 +00:00
|
|
|
"os"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
2023-06-22 11:55:30 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-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 {
|
2023-05-10 14:43:49 +00:00
|
|
|
db *bbolt.DB
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:38:44 +00:00
|
|
|
c.db, err = OpenDB(c.path, readOnly, c.openFile)
|
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
|
|
|
}
|
|
|
|
|
2022-10-28 10:09:38 +00:00
|
|
|
c.fsTree = fstree.New(
|
|
|
|
fstree.WithPath(c.path),
|
|
|
|
fstree.WithPerm(os.ModePerm),
|
|
|
|
fstree.WithDepth(1),
|
|
|
|
fstree.WithDirNameLen(1),
|
2023-08-11 08:32:43 +00:00
|
|
|
fstree.WithNoSync(c.noSync),
|
|
|
|
fstree.WithFileCounter(&c.objCounters),
|
|
|
|
)
|
2022-11-17 08:58:56 +00:00
|
|
|
if err := c.fsTree.Open(readOnly); err != nil {
|
|
|
|
return fmt.Errorf("could not open FSTree: %w", err)
|
|
|
|
}
|
2023-08-11 08:32:43 +00:00
|
|
|
if err := c.fsTree.Init(); err != nil {
|
|
|
|
return fmt.Errorf("could not init FSTree: %w", err)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 10:31:56 +00:00
|
|
|
func (c *cache) deleteFromDB(key string) {
|
2023-08-11 09:51:41 +00:00
|
|
|
var recordDeleted bool
|
2022-08-31 09:20:43 +00:00
|
|
|
err := c.db.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
2023-08-11 09:51:41 +00:00
|
|
|
key := []byte(key)
|
2023-10-27 06:53:12 +00:00
|
|
|
recordDeleted = b.Get(key) != nil
|
2023-08-11 09:51:41 +00:00
|
|
|
return b.Delete(key)
|
2021-04-06 10:56:06 +00:00
|
|
|
})
|
2023-08-07 10:31:56 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2023-06-22 11:55:30 +00:00
|
|
|
c.metrics.Evict(writecache.StorageTypeDB)
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
2023-08-07 10:31:56 +00:00
|
|
|
storagelog.AddressField(key),
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("db DELETE"),
|
|
|
|
)
|
2023-08-11 09:51:41 +00:00
|
|
|
if recordDeleted {
|
|
|
|
c.objCounters.cDB.Add(math.MaxUint64)
|
|
|
|
c.estimateCacheSize()
|
|
|
|
}
|
2023-08-07 10:31:56 +00:00
|
|
|
} else {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Error(logs.WritecacheCantRemoveObjectsFromTheDatabase, zap.Error(err))
|
2022-08-31 09:20:43 +00:00
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 14:41:13 +00:00
|
|
|
func (c *cache) deleteFromDisk(ctx context.Context, addr oid.Address) {
|
|
|
|
_, err := c.fsTree.Delete(ctx, common.DeletePrm{Address: addr})
|
|
|
|
if err != nil && !client.IsErrObjectNotFound(err) {
|
|
|
|
c.log.Error(logs.WritecacheCantRemoveObjectFromWritecache, zap.Error(err))
|
|
|
|
} else if err == nil {
|
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(addr.EncodeToString()),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("fstree DELETE"),
|
|
|
|
)
|
|
|
|
c.metrics.Evict(writecache.StorageTypeFSTree)
|
|
|
|
// counter changed by fstree
|
|
|
|
c.estimateCacheSize()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|