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"
|
|
|
|
|
2021-04-20 13:29:14 +00:00
|
|
|
"github.com/hashicorp/golang-lru/simplelru"
|
2020-11-30 08:11:37 +00:00
|
|
|
"github.com/nspcc-dev/hrw"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
2022-07-05 14:07:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2022-07-11 12:34:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/compression"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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
|
2021-04-20 13:29:14 +00:00
|
|
|
opened *simplelru.LRU
|
|
|
|
// 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")
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// NewBlobovniczaTree returns new instance of blobovnizas tree.
|
|
|
|
func NewBlobovniczaTree(opts ...Option) (blz *Blobovniczas) {
|
|
|
|
blz = new(Blobovniczas)
|
|
|
|
initConfig(&blz.cfg)
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&blz.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := simplelru.NewLRU(blz.openedCacheSize, func(key interface{}, value interface{}) {
|
2022-02-02 13:28:08 +00:00
|
|
|
if _, ok := blz.active[filepath.Dir(key.(string))]; ok {
|
2020-11-30 08:11:37 +00:00
|
|
|
return
|
|
|
|
} else if err := value.(*blobovnicza.Blobovnicza).Close(); err != nil {
|
2022-07-05 13:47:39 +00:00
|
|
|
blz.log.Error("could not close Blobovnicza",
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("id", key.(string)),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
2022-07-05 13:47:39 +00:00
|
|
|
blz.log.Debug("blobovnicza successfully closed on evict",
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("id", key.(string)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
cp := uint64(1)
|
2022-07-05 13:47:39 +00:00
|
|
|
for i := uint64(0); i < blz.blzShallowDepth; i++ {
|
|
|
|
cp *= blz.blzShallowWidth
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
blz.opened = cache
|
|
|
|
blz.active = make(map[string]blobovniczaWithIndex, cp)
|
|
|
|
|
|
|
|
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-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) getActivated(p string) (blobovniczaWithIndex, error) {
|
2020-11-30 08:11:37 +00:00
|
|
|
return b.updateAndGet(p, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updates active blobovnicza of p-level (dir).
|
|
|
|
//
|
|
|
|
// if current active blobovnicza's index is not old, it remains unchanged.
|
2022-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) updateActive(p string, old *uint64) error {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("updating active blobovnicza...", zap.String("path", p))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
_, err := b.updateAndGet(p, old)
|
|
|
|
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("active blobovnicza successfully updated", zap.String("path", p))
|
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-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) updateAndGet(p string, old *uint64) (blobovniczaWithIndex, error) {
|
2020-12-14 09:39:22 +00:00
|
|
|
b.activeMtx.RLock()
|
2020-11-30 08:11:37 +00:00
|
|
|
active, ok := b.active[p]
|
2020-12-14 09:39:22 +00:00
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
if ok {
|
|
|
|
if old != nil {
|
|
|
|
if active.ind == b.blzShallowWidth-1 {
|
2022-07-05 13:47:39 +00:00
|
|
|
return active, errors.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-02-02 13:28:08 +00:00
|
|
|
if active.blz, err = b.openBlobovnicza(filepath.Join(p, 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-08-30 06:15:16 +00:00
|
|
|
tryActive, ok := b.active[p]
|
|
|
|
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.
|
|
|
|
b.active[p] = active
|
|
|
|
|
|
|
|
activePath := filepath.Join(p, 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 {
|
|
|
|
b.opened.Add(filepath.Join(p, u64ToHexString(tryActive.ind)), tryActive.blz)
|
|
|
|
}
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2021-08-24 10:52:22 +00:00
|
|
|
b.log.Debug("blobovnicza successfully activated",
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return hrw.Hash([]byte(a + path))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|