2022-08-19 12:36:37 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2022-08-25 15:58:18 +00:00
|
|
|
"fmt"
|
2022-08-19 12:36:37 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-08-19 12:36:37 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
var objectPhyCounterKey = []byte("phy_counter")
|
|
|
|
var objectLogicCounterKey = []byte("logic_counter")
|
2022-08-19 12:36:37 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
type objectType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ objectType = iota
|
|
|
|
phy
|
|
|
|
logical
|
|
|
|
)
|
|
|
|
|
|
|
|
// ObjectCounters groups object counter
|
|
|
|
// according to metabase state.
|
|
|
|
type ObjectCounters struct {
|
|
|
|
logic uint64
|
|
|
|
phy uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logic returns logical object counter.
|
|
|
|
func (o ObjectCounters) Logic() uint64 {
|
|
|
|
return o.logic
|
|
|
|
}
|
|
|
|
|
|
|
|
// Phy returns physical object counter.
|
|
|
|
func (o ObjectCounters) Phy() uint64 {
|
|
|
|
return o.phy
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectCounters returns object counters that metabase has
|
2022-08-19 12:36:37 +00:00
|
|
|
// tracked since it was opened and initialized.
|
|
|
|
//
|
|
|
|
// Returns only the errors that do not allow reading counter
|
|
|
|
// in Bolt database.
|
2022-09-09 11:33:38 +00:00
|
|
|
func (db *DB) ObjectCounters() (cc ObjectCounters, err error) {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ObjectCounters{}, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:36:37 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
data := b.Get(objectPhyCounterKey)
|
|
|
|
if len(data) == 8 {
|
|
|
|
cc.phy = binary.LittleEndian.Uint64(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = b.Get(objectLogicCounterKey)
|
2022-08-19 12:36:37 +00:00
|
|
|
if len(data) == 8 {
|
2022-09-09 11:33:38 +00:00
|
|
|
cc.logic = binary.LittleEndian.Uint64(data)
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return cc, metaerr.Wrap(err)
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateCounter updates the object counter. Tx MUST be writable.
|
|
|
|
// If inc == `true`, increases the counter, decreases otherwise.
|
2022-09-09 11:33:38 +00:00
|
|
|
func (db *DB) updateCounter(tx *bbolt.Tx, typ objectType, delta uint64, inc bool) error {
|
2022-08-19 12:36:37 +00:00
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var counter uint64
|
2022-09-09 11:33:38 +00:00
|
|
|
var counterKey []byte
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case phy:
|
|
|
|
counterKey = objectPhyCounterKey
|
|
|
|
case logical:
|
|
|
|
counterKey = objectLogicCounterKey
|
|
|
|
default:
|
|
|
|
panic("unknown object type counter")
|
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
data := b.Get(counterKey)
|
2022-08-19 12:36:37 +00:00
|
|
|
if len(data) == 8 {
|
|
|
|
counter = binary.LittleEndian.Uint64(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inc {
|
|
|
|
counter += delta
|
|
|
|
} else if counter <= delta {
|
|
|
|
counter = 0
|
|
|
|
} else {
|
|
|
|
counter -= delta
|
|
|
|
}
|
|
|
|
|
|
|
|
newCounter := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(newCounter, counter)
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
return b.Put(counterKey, newCounter)
|
2022-08-25 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// syncCounter updates object counters according to metabase state:
|
|
|
|
// it counts all the physically/logically stored objects using internal
|
|
|
|
// indexes. Tx MUST be writable.
|
2022-08-25 15:58:18 +00:00
|
|
|
//
|
2022-09-09 11:33:38 +00:00
|
|
|
// Does nothing if counters are not empty and force is false. If force is
|
|
|
|
// true, updates the counters anyway.
|
2022-09-12 17:35:44 +00:00
|
|
|
func syncCounter(tx *bbolt.Tx, force bool) error {
|
2022-08-25 15:58:18 +00:00
|
|
|
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get shard info bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
if !force && len(b.Get(objectPhyCounterKey)) == 8 && len(b.Get(objectLogicCounterKey)) == 8 {
|
|
|
|
// the counters are already inited
|
2022-08-25 15:58:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
var addr oid.Address
|
|
|
|
var phyCounter uint64
|
|
|
|
var logicCounter uint64
|
|
|
|
|
2022-09-12 17:35:44 +00:00
|
|
|
graveyardBKT := tx.Bucket(graveyardBucketName)
|
|
|
|
garbageBKT := tx.Bucket(garbageBucketName)
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, addressKeySize)
|
2022-09-12 17:35:44 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
err = iteratePhyObjects(tx, func(cnr cid.ID, obj oid.ID) error {
|
|
|
|
phyCounter++
|
|
|
|
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(obj)
|
|
|
|
|
2022-09-12 17:35:44 +00:00
|
|
|
// check if an object is available: not with GCMark
|
|
|
|
// and not covered with a tombstone
|
2022-09-08 11:54:21 +00:00
|
|
|
if inGraveyardWithKey(addressKey(addr, key), graveyardBKT, garbageBKT) == 0 {
|
2022-09-09 11:33:38 +00:00
|
|
|
logicCounter++
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:58:18 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return fmt.Errorf("could not iterate objects: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(data, phyCounter)
|
|
|
|
|
|
|
|
err = b.Put(objectPhyCounterKey, data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not update phy object counter: %w", err)
|
2022-08-25 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, 8)
|
2022-09-09 11:33:38 +00:00
|
|
|
binary.LittleEndian.PutUint64(data, logicCounter)
|
2022-08-25 15:58:18 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
err = b.Put(objectLogicCounterKey, data)
|
2022-08-25 15:58:18 +00:00
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return fmt.Errorf("could not update logic object counter: %w", err)
|
2022-08-25 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|