forked from TrueCloudLab/frostfs-node
129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package writecache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
"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"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/kvio"
|
|
bboltrepo "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/kvio/bbolt"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"go.etcd.io/bbolt"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (c *cache) openStore(readOnly bool) error {
|
|
err := util.MkdirAllX(c.path, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// It's possible to use other Repository implementations here, such as the badger-backed one:
|
|
//
|
|
// opts := badger.DefaultOptions(filepath.Join(c.path, "small.badger")).WithReadOnly(readOnly)
|
|
// c.repo, err = badgerrepo.Open(opts)
|
|
|
|
c.repo, err = bboltrepo.Open(filepath.Join(c.path, "small.bolt"), &bboltrepo.Options{
|
|
Options: bbolt.Options{
|
|
NoFreelistSync: true,
|
|
ReadOnly: readOnly,
|
|
Timeout: 100 * time.Millisecond,
|
|
OpenFile: c.openFile,
|
|
},
|
|
MaxBatchSize: c.maxBatchSize,
|
|
MaxBatchDelay: c.maxBatchDelay,
|
|
})
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("could not open database: %w", err)
|
|
}
|
|
|
|
c.fsTree = fstree.New(
|
|
fstree.WithPath(c.path),
|
|
fstree.WithPerm(os.ModePerm),
|
|
fstree.WithDepth(1),
|
|
fstree.WithDirNameLen(1),
|
|
fstree.WithNoSync(c.noSync))
|
|
if err := c.fsTree.Open(readOnly); err != nil {
|
|
return fmt.Errorf("could not open FSTree: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *cache) deleteFromDB(keys []string) []string {
|
|
if len(keys) == 0 {
|
|
return keys
|
|
}
|
|
|
|
var errorIndex int
|
|
err := c.repo.Write(func(tx kvio.WriteOnlyTx) error {
|
|
for errorIndex = range keys {
|
|
if err := tx.Delete([]byte(keys[errorIndex])); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
for i := 0; i < errorIndex; i++ {
|
|
c.objCounters.DecDB()
|
|
c.metrics.Evict(StorageTypeDB)
|
|
storagelog.Write(c.log,
|
|
storagelog.AddressField(keys[i]),
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
storagelog.OpField("db DELETE"),
|
|
)
|
|
}
|
|
if err != nil {
|
|
c.log.Error(logs.WritecacheCantRemoveObjectsFromTheDatabase, zap.Error(err))
|
|
}
|
|
|
|
copy(keys, keys[errorIndex:])
|
|
return keys[:len(keys)-errorIndex]
|
|
}
|
|
|
|
func (c *cache) deleteFromDisk(ctx context.Context, keys []string) []string {
|
|
if len(keys) == 0 {
|
|
return keys
|
|
}
|
|
|
|
var copyIndex int
|
|
var addr oid.Address
|
|
|
|
for i := range keys {
|
|
if err := addr.DecodeString(keys[i]); err != nil {
|
|
c.log.Error(logs.WritecacheCantParseAddress, zap.String("address", keys[i]))
|
|
continue
|
|
}
|
|
|
|
_, err := c.fsTree.Delete(ctx, common.DeletePrm{Address: addr})
|
|
if err != nil && !errors.As(err, new(apistatus.ObjectNotFound)) {
|
|
c.log.Error(logs.WritecacheCantRemoveObjectFromWritecache, zap.Error(err))
|
|
|
|
// Save the key for the next iteration.
|
|
keys[copyIndex] = keys[i]
|
|
copyIndex++
|
|
continue
|
|
} else if err == nil {
|
|
storagelog.Write(c.log,
|
|
storagelog.AddressField(keys[i]),
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
storagelog.OpField("fstree DELETE"),
|
|
)
|
|
c.metrics.Evict(StorageTypeFSTree)
|
|
c.objCounters.DecFS()
|
|
}
|
|
}
|
|
|
|
return keys[:copyIndex]
|
|
}
|