2022-07-05 13:47:39 +00:00
|
|
|
package blobovniczatree
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-11-30 08:11:37 +00:00
|
|
|
"fmt"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-11-30 08:11:37 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
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/local_object_storage/blobovnicza"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/compression"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2022-12-30 10:01:01 +00:00
|
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
2020-11-30 08:11:37 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// cache of opened filled Blobovniczas
|
2022-12-30 10:01:01 +00:00
|
|
|
opened *simplelru.LRU[string, *blobovnicza.Blobovnicza]
|
2021-04-20 13:29:14 +00:00
|
|
|
// lruMtx protects opened cache.
|
|
|
|
// It isn't RWMutex because `Get` calls must
|
|
|
|
// lock this mutex on write, as LRU info is updated.
|
|
|
|
// It must be taken after activeMtx in case when eviction is possible
|
|
|
|
// i.e. `Add`, `Purge` and `Remove` calls.
|
|
|
|
lruMtx sync.Mutex
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2020-12-14 09:39:22 +00:00
|
|
|
// mutex to exclude parallel bbolt.Open() calls
|
|
|
|
// bbolt.Open() deadlocks if it tries to open already opened file
|
|
|
|
openMtx sync.Mutex
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// list of active (opened, non-filled) Blobovniczas
|
2020-11-30 08:11:37 +00:00
|
|
|
activeMtx sync.RWMutex
|
|
|
|
active map[string]blobovniczaWithIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
type blobovniczaWithIndex struct {
|
|
|
|
ind uint64
|
|
|
|
|
|
|
|
blz *blobovnicza.Blobovnicza
|
|
|
|
}
|
|
|
|
|
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-08-18 08:14:10 +00:00
|
|
|
// NewBlobovniczaTree returns new instance of blobovniczas tree.
|
2022-07-05 13:47:39 +00:00
|
|
|
func NewBlobovniczaTree(opts ...Option) (blz *Blobovniczas) {
|
|
|
|
blz = new(Blobovniczas)
|
|
|
|
initConfig(&blz.cfg)
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&blz.cfg)
|
|
|
|
}
|
|
|
|
|
2023-08-18 06:08:26 +00:00
|
|
|
if blz.blzLeafWidth == 0 {
|
|
|
|
blz.blzLeafWidth = blz.blzShallowWidth
|
|
|
|
}
|
|
|
|
|
2022-12-30 10:01:01 +00:00
|
|
|
cache, err := simplelru.NewLRU[string, *blobovnicza.Blobovnicza](blz.openedCacheSize, func(p string, value *blobovnicza.Blobovnicza) {
|
2022-12-26 11:47:14 +00:00
|
|
|
lvlPath := filepath.Dir(p)
|
|
|
|
if b, ok := blz.active[lvlPath]; ok && b.ind == u64FromHexString(filepath.Base(p)) {
|
|
|
|
// This branch is taken if we have recently updated active blobovnicza and remove
|
|
|
|
// it from opened cache.
|
2020-11-30 08:11:37 +00:00
|
|
|
return
|
2022-12-30 10:01:01 +00:00
|
|
|
} else if err := value.Close(); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
blz.log.Error(logs.BlobovniczatreeCouldNotCloseBlobovnicza,
|
2022-12-30 10:01:01 +00:00
|
|
|
zap.String("id", p),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
2023-04-12 14:35:10 +00:00
|
|
|
blz.log.Debug(logs.BlobovniczatreeBlobovniczaSuccessfullyClosedOnEvict,
|
2022-12-30 10:01:01 +00:00
|
|
|
zap.String("id", p),
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// occurs only if the size is not positive
|
2022-07-05 13:47:39 +00:00
|
|
|
panic(fmt.Errorf("could not create LRU cache of size %d: %w", blz.openedCacheSize, err))
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 06:08:26 +00:00
|
|
|
activeMapCapacity := uint64(1)
|
2022-07-05 13:47:39 +00:00
|
|
|
for i := uint64(0); i < blz.blzShallowDepth; i++ {
|
2023-08-18 06:08:26 +00:00
|
|
|
if i+1 == blz.blzShallowDepth {
|
|
|
|
activeMapCapacity *= blz.blzLeafWidth
|
|
|
|
} else {
|
|
|
|
activeMapCapacity *= blz.blzShallowWidth
|
|
|
|
}
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
blz.opened = cache
|
2023-08-18 06:08:26 +00:00
|
|
|
blz.active = make(map[string]blobovniczaWithIndex, activeMapCapacity)
|
2022-07-05 13:47:39 +00:00
|
|
|
|
|
|
|
return blz
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// activates and returns activated blobovnicza of p-level (dir).
|
|
|
|
//
|
|
|
|
// returns error if blobvnicza could not be activated.
|
2022-12-26 10:53:36 +00:00
|
|
|
func (b *Blobovniczas) getActivated(lvlPath string) (blobovniczaWithIndex, error) {
|
|
|
|
return b.updateAndGet(lvlPath, nil)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updates active blobovnicza of p-level (dir).
|
|
|
|
//
|
|
|
|
// if current active blobovnicza's index is not old, it remains unchanged.
|
2022-12-26 10:53:36 +00:00
|
|
|
func (b *Blobovniczas) updateActive(lvlPath string, old *uint64) error {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeUpdatingActiveBlobovnicza, zap.String("path", lvlPath))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2022-12-26 10:53:36 +00:00
|
|
|
_, err := b.updateAndGet(lvlPath, old)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeActiveBlobovniczaSuccessfullyUpdated, zap.String("path", lvlPath))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates and returns active blobovnicza of p-level (dir).
|
|
|
|
//
|
|
|
|
// if current active blobovnicza's index is not old, it is returned unchanged.
|
2022-12-26 10:53:36 +00:00
|
|
|
func (b *Blobovniczas) updateAndGet(lvlPath string, old *uint64) (blobovniczaWithIndex, error) {
|
2020-12-14 09:39:22 +00:00
|
|
|
b.activeMtx.RLock()
|
2022-12-26 10:53:36 +00:00
|
|
|
active, ok := b.active[lvlPath]
|
2020-12-14 09:39:22 +00:00
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
if ok {
|
|
|
|
if old != nil {
|
2023-08-18 06:08:26 +00:00
|
|
|
if active.ind == b.blzLeafWidth-1 {
|
2022-11-09 10:59:24 +00:00
|
|
|
return active, logicerr.New("no more Blobovniczas")
|
2020-11-30 08:11:37 +00:00
|
|
|
} else if active.ind != *old {
|
|
|
|
// sort of CAS in order to control concurrent
|
|
|
|
// updateActive calls
|
|
|
|
return active, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return active, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
active.ind++
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-12-26 10:53:36 +00:00
|
|
|
if active.blz, err = b.openBlobovnicza(filepath.Join(lvlPath, u64ToHexString(active.ind))); err != nil {
|
2020-11-30 08:11:37 +00:00
|
|
|
return active, err
|
|
|
|
}
|
|
|
|
|
2020-12-14 09:39:22 +00:00
|
|
|
b.activeMtx.Lock()
|
|
|
|
defer b.activeMtx.Unlock()
|
|
|
|
|
|
|
|
// check 2nd time to find out if it blobovnicza was activated while thread was locked
|
2022-12-26 10:53:36 +00:00
|
|
|
tryActive, ok := b.active[lvlPath]
|
2022-08-30 06:15:16 +00:00
|
|
|
if ok && tryActive.blz == active.blz {
|
2020-12-14 09:39:22 +00:00
|
|
|
return tryActive, nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 06:15:16 +00:00
|
|
|
// Remove from opened cache (active blobovnicza should always be opened).
|
|
|
|
// Because `onEvict` callback is called in `Remove`, we need to update
|
|
|
|
// active map beforehand.
|
2022-12-26 10:53:36 +00:00
|
|
|
b.active[lvlPath] = active
|
2022-08-30 06:15:16 +00:00
|
|
|
|
2022-12-26 10:53:36 +00:00
|
|
|
activePath := filepath.Join(lvlPath, u64ToHexString(active.ind))
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2022-08-30 06:15:16 +00:00
|
|
|
b.opened.Remove(activePath)
|
|
|
|
if ok {
|
2022-12-26 10:53:36 +00:00
|
|
|
b.opened.Add(filepath.Join(lvlPath, u64ToHexString(tryActive.ind)), tryActive.blz)
|
2022-08-30 06:15:16 +00:00
|
|
|
}
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeBlobovniczaSuccessfullyActivated,
|
2022-08-30 06:15:16 +00:00
|
|
|
zap.String("path", activePath))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
return active, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// converts uint64 to hex string.
|
|
|
|
func u64ToHexString(ind uint64) string {
|
|
|
|
return strconv.FormatUint(ind, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts uint64 hex string to uint64.
|
|
|
|
func u64FromHexString(str string) uint64 {
|
|
|
|
v, err := strconv.ParseUint(str, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("blobovnicza name is not an index %s", str))
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2022-07-08 11:33:49 +00:00
|
|
|
|
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)
|
|
|
|
}
|