2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
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-18 08:14:10 +00:00
|
|
|
// If blobovnicza 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
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), 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
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), 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-08-18 08:14:10 +00:00
|
|
|
b.metrics.IncOpenBlobovniczaCount()
|
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-18 08:14:10 +00:00
|
|
|
// Blobovnicza 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 {
|
2023-08-18 08:14:10 +00:00
|
|
|
return errors.New("blobovnicza is not open")
|
2023-08-16 08:12:19 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), 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-18 10:01:27 +00:00
|
|
|
size := b.dataSize.Load()
|
|
|
|
items := b.itemsCount.Load()
|
|
|
|
if size != 0 || items != 0 {
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), logs.BlobovniczaAlreadyInitialized, zap.Uint64("size", size), zap.Uint64("items", items))
|
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)
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), logs.BlobovniczaCreatingBucketForSizeRange,
|
2023-08-15 13:53:38 +00:00
|
|
|
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-18 10:01:27 +00:00
|
|
|
return b.initializeCounters()
|
2023-08-16 08:12:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 14:36:26 +00:00
|
|
|
func (b *Blobovnicza) ObjectsCount() uint64 {
|
|
|
|
return b.itemsCount.Load()
|
|
|
|
}
|
|
|
|
|
2023-08-18 10:01:27 +00:00
|
|
|
func (b *Blobovnicza) initializeCounters() error {
|
2023-08-16 08:12:19 +00:00
|
|
|
var size uint64
|
2023-08-18 10:01:27 +00:00
|
|
|
var items uint64
|
2023-10-12 12:25:16 +00:00
|
|
|
var sizeExists bool
|
|
|
|
var itemsCountExists bool
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
err := b.boltDB.View(func(tx *bbolt.Tx) error {
|
2023-10-12 12:25:16 +00:00
|
|
|
size, sizeExists = hasDataSize(tx)
|
|
|
|
items, itemsCountExists = hasItemsCount(tx)
|
|
|
|
|
|
|
|
if sizeExists && itemsCountExists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:11:49 +00:00
|
|
|
return b.iterateAllDataBuckets(tx, func(_, _ uint64, b *bbolt.Bucket) (bool, error) {
|
2023-10-12 18:35:08 +00:00
|
|
|
return false, b.ForEach(func(k, v []byte) error {
|
|
|
|
size += uint64(len(k) + len(v))
|
|
|
|
items++
|
|
|
|
return nil
|
|
|
|
})
|
2023-08-16 08:12:19 +00:00
|
|
|
})
|
|
|
|
})
|
2022-06-01 10:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't determine DB size: %w", err)
|
|
|
|
}
|
2023-10-12 12:25:16 +00:00
|
|
|
if (!sizeExists || !itemsCountExists) && !b.boltOptions.ReadOnly {
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), logs.BlobovniczaSavingCountersToMeta, zap.Uint64("size", size), zap.Uint64("items", items))
|
2023-10-12 12:25:16 +00:00
|
|
|
if err := b.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
if err := saveDataSize(tx, size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return saveItemsCount(tx, items)
|
|
|
|
}); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), logs.BlobovniczaSavingCountersToMetaFailed, zap.Uint64("size", size), zap.Uint64("items", items))
|
2023-10-12 12:25:16 +00:00
|
|
|
return fmt.Errorf("can't save blobovnicza's size and items count: %w", err)
|
|
|
|
}
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), logs.BlobovniczaSavingCountersToMetaSuccess, zap.Uint64("size", size), zap.Uint64("items", items))
|
2023-10-12 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
b.dataSize.Store(size)
|
2023-08-18 10:01:27 +00:00
|
|
|
b.itemsCount.Store(items)
|
2023-08-18 08:14:10 +00:00
|
|
|
b.metrics.AddOpenBlobovniczaSize(size)
|
2023-08-18 10:01:27 +00:00
|
|
|
b.metrics.AddOpenBlobovniczaItems(items)
|
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
|
|
|
//
|
2023-08-18 08:14:10 +00:00
|
|
|
// If blobovnicza 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
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(context.Background(), 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
|
|
|
|
2023-08-18 08:14:10 +00:00
|
|
|
b.metrics.DecOpenBlobovniczaCount()
|
|
|
|
b.metrics.SubOpenBlobovniczaSize(b.dataSize.Load())
|
2023-08-18 10:01:27 +00:00
|
|
|
b.metrics.SubOpenBlobovniczaItems(b.itemsCount.Load())
|
2023-08-16 08:12:19 +00:00
|
|
|
b.dataSize.Store(0)
|
2023-08-18 10:01:27 +00:00
|
|
|
b.itemsCount.Store(0)
|
2023-08-16 08:12:19 +00:00
|
|
|
|
|
|
|
b.opened = false
|
|
|
|
|
|
|
|
return nil
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|