2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-06-01 10:20:51 +00:00
|
|
|
"os"
|
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.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Open() error {
|
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 {
|
|
|
|
b.metrics.IncOpenCount()
|
|
|
|
}
|
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.
|
2021-09-14 14:44:07 +00:00
|
|
|
//
|
|
|
|
// Should not be called in read-only configuration.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Init() error {
|
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),
|
|
|
|
)
|
|
|
|
|
2022-01-19 13:17:36 +00:00
|
|
|
if size := b.filled.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
|
|
|
|
}
|
|
|
|
|
|
|
|
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
|
2020-11-26 14:26:53 +00:00
|
|
|
return b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
|
|
|
|
// create size range bucket
|
|
|
|
|
2022-01-19 13:17:36 +00:00
|
|
|
rangeStr := stringifyBounds(lower, upper)
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczaCreatingBucketForSizeRange,
|
2022-01-19 13:17:36 +00:00
|
|
|
zap.String("range", rangeStr))
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-06-01 10:20:51 +00:00
|
|
|
_, err := tx.CreateBucketIfNotExists(key)
|
2021-05-18 08:12:51 +00:00
|
|
|
if err != nil {
|
2022-01-19 13:17:36 +00:00
|
|
|
return false, fmt.Errorf("(%T) could not create bucket for bounds %s: %w",
|
|
|
|
b, rangeStr, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
2022-01-19 13:17:36 +00:00
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return false, nil
|
2020-11-26 14:26:53 +00:00
|
|
|
})
|
|
|
|
})
|
2022-06-01 10:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(b.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't determine DB size: %w", err)
|
|
|
|
}
|
2022-01-19 13:17:36 +00:00
|
|
|
|
2023-06-20 08:23:17 +00:00
|
|
|
sz := uint64(info.Size())
|
|
|
|
b.filled.Store(sz)
|
|
|
|
b.metrics.IncSize(sz)
|
2022-01-19 13:17:36 +00:00
|
|
|
return err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close releases all internal database resources.
|
|
|
|
func (b *Blobovnicza) Close() error {
|
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-06-20 08:23:17 +00:00
|
|
|
err := b.boltDB.Close()
|
|
|
|
if err == nil {
|
|
|
|
b.metrics.DecOpenCount()
|
|
|
|
b.metrics.DecSize(b.filled.Load())
|
|
|
|
}
|
|
|
|
return err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|