2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-08-16 08:12:19 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Open opens an internal database at the configured path with the configured permissions.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// If the database file does not exist, it will be created automatically.
|
2023-08-16 08:12:19 +00:00
|
|
|
// If blobovnizca is already open, does nothing.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Open() error {
|
2023-08-16 08:12:19 +00:00
|
|
|
b.controlMtx.Lock()
|
|
|
|
defer b.controlMtx.Unlock()
|
|
|
|
|
|
|
|
if b.opened {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaCreatingDirectoryForBoltDB,
|
2020-11-26 14:26:53 +00:00
|
|
|
zap.String("path", b.path),
|
2021-09-14 14:44:07 +00:00
|
|
|
zap.Bool("ro", b.boltOptions.ReadOnly),
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
2021-09-14 14:44:07 +00:00
|
|
|
var err error
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2021-09-14 14:44:07 +00:00
|
|
|
if !b.boltOptions.ReadOnly {
|
2022-02-02 13:28:08 +00:00
|
|
|
err = util.MkdirAllX(filepath.Dir(b.path), b.perm)
|
2021-09-14 14:44:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaOpeningBoltDB,
|
2021-09-14 14:44:07 +00:00
|
|
|
zap.String("path", b.path),
|
|
|
|
zap.Stringer("permissions", b.perm),
|
|
|
|
)
|
|
|
|
|
|
|
|
b.boltDB, err = bbolt.Open(b.path, b.perm, b.boltOptions)
|
2023-06-20 08:23:17 +00:00
|
|
|
if err == nil {
|
2023-08-16 08:12:19 +00:00
|
|
|
b.opened = true
|
2023-06-09 09:14:32 +00:00
|
|
|
b.metrics.IncOpenBlobovnizcaCount()
|
2023-06-20 08:23:17 +00:00
|
|
|
}
|
2021-09-14 14:44:07 +00:00
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes internal database structure.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// If Blobovnicza is already initialized, no action is taken.
|
2023-08-16 08:12:19 +00:00
|
|
|
// Blobovnizca must be open, otherwise an error will return.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Init() error {
|
2023-08-16 08:12:19 +00:00
|
|
|
b.controlMtx.Lock()
|
|
|
|
defer b.controlMtx.Unlock()
|
|
|
|
|
|
|
|
if !b.opened {
|
|
|
|
return errors.New("blobovnizca is not open")
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaInitializing,
|
2020-11-26 14:26:53 +00:00
|
|
|
zap.Uint64("object size limit", b.objSizeLimit),
|
|
|
|
zap.Uint64("storage size limit", b.fullSizeLimit),
|
|
|
|
)
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
if size := b.dataSize.Load(); size != 0 {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaAlreadyInitialized, zap.Uint64("size", size))
|
2022-01-19 13:17:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:53:38 +00:00
|
|
|
if !b.boltOptions.ReadOnly {
|
|
|
|
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
|
2023-08-16 08:12:19 +00:00
|
|
|
return b.iterateBucketKeys(true, func(lower, upper uint64, key []byte) (bool, error) {
|
2023-08-15 13:53:38 +00:00
|
|
|
// create size range bucket
|
|
|
|
|
|
|
|
rangeStr := stringifyBounds(lower, upper)
|
|
|
|
b.log.Debug(logs.BlobovniczaCreatingBucketForSizeRange,
|
|
|
|
zap.String("range", rangeStr))
|
|
|
|
|
|
|
|
_, err := tx.CreateBucketIfNotExists(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("(%T) could not create bucket for bounds %s: %w",
|
|
|
|
b, rangeStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
2020-11-26 14:26:53 +00:00
|
|
|
})
|
2023-08-15 13:53:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-01 10:20:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
2022-06-01 10:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't determine DB size: %w", err)
|
|
|
|
}
|
2023-08-16 08:12:19 +00:00
|
|
|
b.dataSize.Store(size)
|
2023-08-16 08:58:47 +00:00
|
|
|
b.metrics.AddOpenBlobovnizcaSize(size)
|
2023-08-16 08:12:19 +00:00
|
|
|
return nil
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close releases all internal database resources.
|
2023-08-16 08:12:19 +00:00
|
|
|
//
|
|
|
|
// If blobovnizca is already closed, does nothing.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Close() error {
|
2023-08-16 08:12:19 +00:00
|
|
|
b.controlMtx.Lock()
|
|
|
|
defer b.controlMtx.Unlock()
|
|
|
|
|
|
|
|
if !b.opened {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaClosingBoltDB,
|
2020-12-11 07:59:28 +00:00
|
|
|
zap.String("path", b.path),
|
|
|
|
)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
if err := b.boltDB.Close(); err != nil {
|
|
|
|
return err
|
2023-06-20 08:23:17 +00:00
|
|
|
}
|
2023-08-16 08:12:19 +00:00
|
|
|
|
|
|
|
b.metrics.DecOpenBlobovnizcaCount()
|
2023-08-16 08:58:47 +00:00
|
|
|
b.metrics.SubOpenBlobovnizcaSize(b.dataSize.Load())
|
2023-08-16 08:12:19 +00:00
|
|
|
b.dataSize.Store(0)
|
|
|
|
|
|
|
|
b.opened = false
|
|
|
|
|
|
|
|
return nil
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|