Alejandro Lopez
345a1a69a2
All checks were successful
DCO action / DCO (pull_request) Successful in 2m48s
Build / Build Components (1.21) (pull_request) Successful in 4m59s
Build / Build Components (1.20) (pull_request) Successful in 5m5s
Vulncheck / Vulncheck (pull_request) Successful in 4m55s
Tests and linters / Staticcheck (pull_request) Successful in 6m51s
Tests and linters / Tests (1.21) (pull_request) Successful in 7m21s
Tests and linters / Tests (1.20) (pull_request) Successful in 7m53s
Tests and linters / Lint (pull_request) Successful in 8m35s
Tests and linters / Tests with -race (pull_request) Successful in 9m57s
Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package writecachebadger
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
"github.com/dgraph-io/badger/v4"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// store represents persistent storage with in-memory LRU cache
|
|
// for flushed items on top of it.
|
|
type store struct {
|
|
db *badger.DB
|
|
}
|
|
|
|
type internalKey [len(cid.ID{}) + len(oid.ID{})]byte
|
|
|
|
func (k internalKey) address() oid.Address {
|
|
var addr oid.Address
|
|
var cnr cid.ID
|
|
var obj oid.ID
|
|
copy(cnr[:], k[:len(cnr)])
|
|
copy(obj[:], k[len(cnr):])
|
|
addr.SetContainer(cnr)
|
|
addr.SetObject(obj)
|
|
return addr
|
|
}
|
|
|
|
func addr2key(addr oid.Address) internalKey {
|
|
var key internalKey
|
|
cnr, obj := addr.Container(), addr.Object()
|
|
copy(key[:len(cnr)], cnr[:])
|
|
copy(key[len(cnr):], obj[:])
|
|
return key
|
|
}
|
|
|
|
const dbName = "small.badger"
|
|
|
|
func (c *cache) openStore(readOnly bool) error {
|
|
err := util.MkdirAllX(c.path, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.db, err = OpenDB(filepath.Join(c.path, dbName), readOnly, c.log)
|
|
if err != nil {
|
|
return fmt.Errorf("could not open database: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *cache) deleteFromDB(keys []internalKey) []internalKey {
|
|
if len(keys) == 0 {
|
|
return keys
|
|
}
|
|
|
|
wb := c.db.NewWriteBatch()
|
|
|
|
var errorIndex int
|
|
for errorIndex = range keys {
|
|
if err := wb.Delete(keys[errorIndex][:]); err != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
for i := 0; i < errorIndex; i++ {
|
|
c.objCounters.DecDB()
|
|
c.metrics.Evict(writecache.StorageTypeDB)
|
|
storagelog.Write(c.log,
|
|
storagelog.AddressField(keys[i]),
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
storagelog.OpField("db DELETE"),
|
|
)
|
|
}
|
|
|
|
if err := wb.Flush(); err != nil {
|
|
c.log.Error(logs.WritecacheCantRemoveObjectsFromTheDatabase, zap.Error(err))
|
|
}
|
|
|
|
copy(keys, keys[errorIndex:])
|
|
return keys[:len(keys)-errorIndex]
|
|
}
|