2022-07-05 13:47:39 +00:00
|
|
|
package blobovniczatree
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-01 11:43:26 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2024-07-04 06:18:17 +00:00
|
|
|
"os"
|
2020-11-30 08:11:37 +00:00
|
|
|
"strconv"
|
2023-09-19 15:08:38 +00:00
|
|
|
"strings"
|
2023-09-22 10:07:32 +00:00
|
|
|
"sync"
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/compression"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// Blobovniczas represents the storage of the "small" objects.
|
2020-11-30 08:11:37 +00:00
|
|
|
//
|
|
|
|
// Each object is stored in Blobovnicza's (B-s).
|
|
|
|
// B-s are structured in a multilevel directory hierarchy
|
|
|
|
// with fixed depth and width (configured by BlobStor).
|
|
|
|
//
|
|
|
|
// Example (width = 4, depth = 3):
|
|
|
|
//
|
|
|
|
// x===============================x
|
|
|
|
// |[0] [1] [2] [3]|
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// | \ / |
|
|
|
|
// |[0] [1] [2] [3]|
|
|
|
|
// | | / |
|
|
|
|
// | | / |
|
|
|
|
// | | / |
|
|
|
|
// | | / |
|
|
|
|
// |[0](F) [1](A) [X] [X]|
|
|
|
|
// x===============================x
|
|
|
|
//
|
|
|
|
// Elements of the deepest level are B-s.
|
|
|
|
// B-s are allocated dynamically. At each moment of the time there is
|
|
|
|
// an active B (ex. A), set of already filled B-s (ex. F) and
|
2022-04-21 11:28:05 +00:00
|
|
|
// a list of not yet initialized B-s (ex. X). After filling the active B
|
2020-11-30 08:11:37 +00:00
|
|
|
// it becomes full, and next B becomes initialized and active.
|
|
|
|
//
|
|
|
|
// Active B and some of the full B-s are cached (LRU). All cached
|
|
|
|
// B-s are intitialized and opened.
|
|
|
|
//
|
|
|
|
// Object is saved as follows:
|
2022-08-19 14:29:53 +00:00
|
|
|
// 1. at each level, according to HRW, the next one is selected and
|
|
|
|
// dives into it until we reach the deepest;
|
|
|
|
// 2. at the B-s level object is saved to the active B. If active B
|
|
|
|
// is full, next B is opened, initialized and cached. If there
|
|
|
|
// is no more X candidates, goto 1 and process next level.
|
2020-11-30 08:11:37 +00:00
|
|
|
//
|
|
|
|
// After the object is saved in B, path concatenation is returned
|
|
|
|
// in system path format as B identifier (ex. "0/1/1" or "3/2/1").
|
2022-07-05 13:47:39 +00:00
|
|
|
type Blobovniczas struct {
|
|
|
|
cfg
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2023-09-27 13:25:15 +00:00
|
|
|
commondbManager *dbManager
|
|
|
|
activeDBManager *activeDBManager
|
|
|
|
dbCache *dbCache
|
|
|
|
deleteProtectedObjects *addressMap
|
|
|
|
dbFilesGuard *sync.RWMutex
|
|
|
|
rebuildGuard *sync.RWMutex
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
var _ common.Storage = (*Blobovniczas)(nil)
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
var errPutFailed = errors.New("could not save the object in any blobovnicza")
|
|
|
|
|
2023-09-19 15:08:38 +00:00
|
|
|
const (
|
|
|
|
dbExtension = ".db"
|
|
|
|
)
|
|
|
|
|
2023-08-18 08:14:10 +00:00
|
|
|
// NewBlobovniczaTree returns new instance of blobovniczas tree.
|
2024-03-01 11:43:26 +00:00
|
|
|
func NewBlobovniczaTree(ctx context.Context, opts ...Option) (blz *Blobovniczas) {
|
2022-07-05 13:47:39 +00:00
|
|
|
blz = new(Blobovniczas)
|
|
|
|
initConfig(&blz.cfg)
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&blz.cfg)
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
blz.commondbManager = newDBManager(blz.rootPath, blz.blzOpts, blz.readOnly, blz.metrics.Blobovnicza(), blz.log)
|
2024-07-04 06:18:17 +00:00
|
|
|
blz.activeDBManager = newActiveDBManager(blz.commondbManager, blz.rootPath)
|
2024-03-01 11:43:26 +00:00
|
|
|
blz.dbCache = newDBCache(ctx, blz.openedCacheSize,
|
|
|
|
blz.openedCacheTTL, blz.openedCacheExpInterval, blz.commondbManager)
|
2023-09-27 13:25:15 +00:00
|
|
|
blz.deleteProtectedObjects = newAddressMap()
|
2023-09-22 10:07:32 +00:00
|
|
|
blz.dbFilesGuard = &sync.RWMutex{}
|
|
|
|
blz.rebuildGuard = &sync.RWMutex{}
|
2022-07-05 13:47:39 +00:00
|
|
|
|
|
|
|
return blz
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns hash of the object address.
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressHash(addr *oid.Address, path string) uint64 {
|
2020-11-30 08:11:37 +00:00
|
|
|
var a string
|
|
|
|
|
|
|
|
if addr != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
a = addr.EncodeToString()
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 12:39:16 +00:00
|
|
|
return hrw.StringHash(a + path)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func u64ToHexString(ind uint64) string {
|
|
|
|
return strconv.FormatUint(ind, 16)
|
|
|
|
}
|
|
|
|
|
2023-09-19 15:08:38 +00:00
|
|
|
func u64ToHexStringExt(ind uint64) string {
|
|
|
|
return strconv.FormatUint(ind, 16) + dbExtension
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
func u64FromHexString(str string) uint64 {
|
2023-09-19 15:08:38 +00:00
|
|
|
v, err := strconv.ParseUint(strings.TrimSuffix(str, dbExtension), 16, 64)
|
2020-11-30 08:11:37 +00:00
|
|
|
if err != nil {
|
2024-03-11 14:55:50 +00:00
|
|
|
panic("blobovnicza name is not an index " + str)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2022-07-08 11:33:49 +00:00
|
|
|
|
2024-07-04 06:18:17 +00:00
|
|
|
func getBlobovniczaMaxIndex(directory string) (bool, uint64, error) {
|
|
|
|
entries, err := os.ReadDir(directory)
|
|
|
|
if os.IsNotExist(err) { // non initialized tree
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
var hasDBs bool
|
|
|
|
var maxIdx uint64
|
|
|
|
for _, e := range entries {
|
2024-08-27 12:51:55 +00:00
|
|
|
if e.IsDir() || strings.HasSuffix(e.Name(), rebuildSuffix) {
|
2024-07-04 06:18:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasDBs = true
|
|
|
|
maxIdx = max(u64FromHexString(e.Name()), maxIdx)
|
|
|
|
}
|
|
|
|
return hasDBs, maxIdx, nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 12:42:56 +00:00
|
|
|
// Type is blobovniczatree storage type used in logs and configuration.
|
|
|
|
const Type = "blobovnicza"
|
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
// Type implements common.Storage.
|
|
|
|
func (b *Blobovniczas) Type() string {
|
2022-09-20 12:42:56 +00:00
|
|
|
return Type
|
2022-07-08 11:33:49 +00:00
|
|
|
}
|
2022-07-11 12:34:17 +00:00
|
|
|
|
2022-10-05 12:11:12 +00:00
|
|
|
// Path implements common.Storage.
|
|
|
|
func (b *Blobovniczas) Path() string {
|
|
|
|
return b.rootPath
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
// SetCompressor implements common.Storage.
|
2022-08-19 14:29:53 +00:00
|
|
|
func (b *Blobovniczas) SetCompressor(cc *compression.Config) {
|
|
|
|
b.compression = cc
|
2022-07-11 12:34:17 +00:00
|
|
|
}
|
2022-11-09 10:59:24 +00:00
|
|
|
|
2023-08-09 12:54:08 +00:00
|
|
|
func (b *Blobovniczas) Compressor() *compression.Config {
|
|
|
|
return b.compression
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:59:24 +00:00
|
|
|
// SetReportErrorFunc implements common.Storage.
|
|
|
|
func (b *Blobovniczas) SetReportErrorFunc(f func(string, error)) {
|
|
|
|
b.reportError = f
|
|
|
|
}
|
2023-06-07 11:39:03 +00:00
|
|
|
|
|
|
|
func (b *Blobovniczas) SetParentID(parentID string) {
|
|
|
|
b.metrics.SetParentID(parentID)
|
|
|
|
}
|