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"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
storagelog "github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util"
|
|
|
|
apistatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-12-30 09:50:05 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
|
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
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 {
|
2022-09-01 06:33:56 +00:00
|
|
|
maxFlushedMarksCount int
|
|
|
|
maxRemoveBatchSize int
|
|
|
|
|
2022-12-30 09:50:05 +00:00
|
|
|
flushed simplelru.LRUCache[string, bool]
|
2021-04-06 10:56:06 +00:00
|
|
|
db *bbolt.DB
|
2022-08-31 09:20:43 +00:00
|
|
|
|
|
|
|
dbKeysToRemove []string
|
|
|
|
fsKeysToRemove []string
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
fstree.WithNoSync(c.noSync))
|
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)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-21 13:26:25 +00:00
|
|
|
// Write-cache can be opened multiple times during `SetMode`.
|
|
|
|
// flushed map must not be re-created in this case.
|
|
|
|
if c.flushed == nil {
|
2022-12-30 09:50:05 +00:00
|
|
|
c.flushed, _ = lru.NewWithEvict[string, bool](c.maxFlushedMarksCount, c.removeFlushed)
|
2022-07-21 13:26:25 +00:00
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
// removeFlushed removes an object from the writecache.
|
|
|
|
// To minimize interference with the client operations, the actual removal
|
|
|
|
// is done in batches.
|
|
|
|
// It is not thread-safe and is used only as an evict callback to LRU cache.
|
2022-12-30 09:50:05 +00:00
|
|
|
func (c *cache) removeFlushed(key string, value bool) {
|
|
|
|
fromDatabase := value
|
2022-08-31 09:20:43 +00:00
|
|
|
if fromDatabase {
|
2022-12-30 09:50:05 +00:00
|
|
|
c.dbKeysToRemove = append(c.dbKeysToRemove, key)
|
2022-08-31 09:20:43 +00:00
|
|
|
} else {
|
2022-12-30 09:50:05 +00:00
|
|
|
c.fsKeysToRemove = append(c.fsKeysToRemove, key)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 06:33:56 +00:00
|
|
|
if len(c.dbKeysToRemove)+len(c.fsKeysToRemove) >= c.maxRemoveBatchSize {
|
2022-08-31 09:20:43 +00:00
|
|
|
c.dbKeysToRemove = c.deleteFromDB(c.dbKeysToRemove)
|
|
|
|
c.fsKeysToRemove = c.deleteFromDisk(c.fsKeysToRemove)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
func (c *cache) deleteFromDB(keys []string) []string {
|
2021-04-06 10:56:06 +00:00
|
|
|
if len(keys) == 0 {
|
2022-08-31 09:20:43 +00:00
|
|
|
return keys
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-03-17 08:03:58 +00:00
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
var errorIndex int
|
|
|
|
err := c.db.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
for errorIndex = range keys {
|
|
|
|
if err := b.Delete([]byte(keys[errorIndex])); err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-08-31 09:20:43 +00:00
|
|
|
for i := 0; i < errorIndex; i++ {
|
2021-12-08 10:14:57 +00:00
|
|
|
c.objCounters.DecDB()
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(keys[i]),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("db DELETE"),
|
|
|
|
)
|
2021-12-08 10:14:57 +00:00
|
|
|
}
|
2022-08-31 09:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Error("can't remove objects from the database", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(keys, keys[errorIndex:])
|
|
|
|
return keys[:len(keys)-errorIndex]
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
func (c *cache) deleteFromDisk(keys []string) []string {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return keys
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
var copyIndex int
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := range keys {
|
2022-08-31 09:20:43 +00:00
|
|
|
if err := addr.DecodeString(keys[i]); err != nil {
|
|
|
|
c.log.Error("can't parse address", zap.String("address", keys[i]))
|
2021-04-06 10:56:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
_, err := c.fsTree.Delete(common.DeletePrm{Address: addr})
|
|
|
|
if err != nil && !errors.As(err, new(apistatus.ObjectNotFound)) {
|
2021-04-06 10:56:06 +00:00
|
|
|
c.log.Error("can't remove object from write-cache", zap.Error(err))
|
2022-08-31 09:20:43 +00:00
|
|
|
|
|
|
|
// Save the key for the next iteration.
|
|
|
|
keys[copyIndex] = keys[i]
|
|
|
|
copyIndex++
|
2021-04-06 10:56:06 +00:00
|
|
|
continue
|
2021-09-06 14:28:55 +00:00
|
|
|
} else if err == nil {
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(keys[i]),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("fstree DELETE"),
|
|
|
|
)
|
2021-09-08 09:32:20 +00:00
|
|
|
c.objCounters.DecFS()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 09:20:43 +00:00
|
|
|
return keys[:copyIndex]
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|