forked from TrueCloudLab/frostfs-node
[#602] blobovnicza: Fix size counter
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
2e49d7ea7e
commit
809e97626b
7 changed files with 99 additions and 52 deletions
|
@ -1,8 +1,8 @@
|
|||
package blobovnicza
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
||||
|
@ -14,7 +14,15 @@ import (
|
|||
// Open opens an internal database at the configured path with the configured permissions.
|
||||
//
|
||||
// If the database file does not exist, it will be created automatically.
|
||||
// If blobovnizca is already open, does nothing.
|
||||
func (b *Blobovnicza) Open() error {
|
||||
b.controlMtx.Lock()
|
||||
defer b.controlMtx.Unlock()
|
||||
|
||||
if b.opened {
|
||||
return nil
|
||||
}
|
||||
|
||||
b.log.Debug(logs.BlobovniczaCreatingDirectoryForBoltDB,
|
||||
zap.String("path", b.path),
|
||||
zap.Bool("ro", b.boltOptions.ReadOnly),
|
||||
|
@ -36,6 +44,7 @@ func (b *Blobovnicza) Open() error {
|
|||
|
||||
b.boltDB, err = bbolt.Open(b.path, b.perm, b.boltOptions)
|
||||
if err == nil {
|
||||
b.opened = true
|
||||
b.metrics.IncOpenBlobovnizcaCount()
|
||||
}
|
||||
|
||||
|
@ -45,20 +54,28 @@ func (b *Blobovnicza) Open() error {
|
|||
// Init initializes internal database structure.
|
||||
//
|
||||
// If Blobovnicza is already initialized, no action is taken.
|
||||
// Blobovnizca must be open, otherwise an error will return.
|
||||
func (b *Blobovnicza) Init() error {
|
||||
b.controlMtx.Lock()
|
||||
defer b.controlMtx.Unlock()
|
||||
|
||||
if !b.opened {
|
||||
return errors.New("blobovnizca is not open")
|
||||
}
|
||||
|
||||
b.log.Debug(logs.BlobovniczaInitializing,
|
||||
zap.Uint64("object size limit", b.objSizeLimit),
|
||||
zap.Uint64("storage size limit", b.fullSizeLimit),
|
||||
)
|
||||
|
||||
if size := b.filled.Load(); size != 0 {
|
||||
if size := b.dataSize.Load(); size != 0 {
|
||||
b.log.Debug(logs.BlobovniczaAlreadyInitialized, zap.Uint64("size", size))
|
||||
return nil
|
||||
}
|
||||
|
||||
if !b.boltOptions.ReadOnly {
|
||||
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
|
||||
return b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
|
||||
return b.iterateBucketKeys(true, func(lower, upper uint64, key []byte) (bool, error) {
|
||||
// create size range bucket
|
||||
|
||||
rangeStr := stringifyBounds(lower, upper)
|
||||
|
@ -79,27 +96,49 @@ func (b *Blobovnicza) Init() error {
|
|||
}
|
||||
}
|
||||
|
||||
info, err := os.Stat(b.path)
|
||||
return b.initializeSize()
|
||||
}
|
||||
|
||||
func (b *Blobovnicza) initializeSize() error {
|
||||
var size uint64
|
||||
err := b.boltDB.View(func(tx *bbolt.Tx) error {
|
||||
return b.iterateAllBuckets(tx, func(lower, upper uint64, b *bbolt.Bucket) (bool, error) {
|
||||
size += uint64(b.Stats().KeyN) * upper
|
||||
return false, nil
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't determine DB size: %w", err)
|
||||
}
|
||||
|
||||
sz := uint64(info.Size())
|
||||
b.filled.Store(sz)
|
||||
b.metrics.AddSize(sz)
|
||||
return err
|
||||
b.dataSize.Store(size)
|
||||
b.metrics.AddSize(size)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close releases all internal database resources.
|
||||
//
|
||||
// If blobovnizca is already closed, does nothing.
|
||||
func (b *Blobovnicza) Close() error {
|
||||
b.controlMtx.Lock()
|
||||
defer b.controlMtx.Unlock()
|
||||
|
||||
if !b.opened {
|
||||
return nil
|
||||
}
|
||||
|
||||
b.log.Debug(logs.BlobovniczaClosingBoltDB,
|
||||
zap.String("path", b.path),
|
||||
)
|
||||
|
||||
err := b.boltDB.Close()
|
||||
if err == nil {
|
||||
b.metrics.DecOpenBlobovnizcaCount()
|
||||
b.metrics.SubSize(b.filled.Load())
|
||||
if err := b.boltDB.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
|
||||
b.metrics.DecOpenBlobovnizcaCount()
|
||||
b.metrics.SubSize(b.dataSize.Load())
|
||||
b.dataSize.Store(0)
|
||||
|
||||
b.opened = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue