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"
|
2021-09-06 14:28:07 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
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:
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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
|
2022-03-25 07:59:32 +00:00
|
|
|
|
|
|
|
onClose []func()
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type blobovniczaWithIndex struct {
|
|
|
|
ind uint64
|
|
|
|
|
|
|
|
blz *blobovnicza.Blobovnicza
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// makes slice of uint64 values from 0 to number-1.
|
|
|
|
func indexSlice(number uint64) []uint64 {
|
|
|
|
s := make([]uint64, number)
|
|
|
|
|
|
|
|
for i := range s {
|
|
|
|
s[i] = uint64(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// save object in the maximum weight blobobnicza.
|
|
|
|
//
|
|
|
|
// returns error if could not save object in any blobovnicza.
|
2022-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) Put(addr oid.Address, data []byte) (*blobovnicza.ID, error) {
|
2022-05-23 14:21:14 +00:00
|
|
|
var prm blobovnicza.PutPrm
|
2020-11-30 08:11:37 +00:00
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetMarshaledObject(data)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fn func(string) (bool, error)
|
|
|
|
id *blobovnicza.ID
|
|
|
|
)
|
|
|
|
|
|
|
|
fn = func(p string) (bool, error) {
|
|
|
|
active, err := b.getActivated(p)
|
|
|
|
if err != nil {
|
|
|
|
b.log.Debug("could not get active blobovnicza",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := active.blz.Put(prm); err != nil {
|
|
|
|
// check if blobovnicza is full
|
|
|
|
if errors.Is(err, blobovnicza.ErrFull) {
|
|
|
|
b.log.Debug("blobovnicza overflowed",
|
2022-02-02 13:28:08 +00:00
|
|
|
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := b.updateActive(p, &active.ind); err != nil {
|
|
|
|
b.log.Debug("could not update active blobovnicza",
|
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.log.Debug("could not put object to active blobovnicza",
|
2022-02-02 13:28:08 +00:00
|
|
|
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
p = filepath.Join(p, u64ToHexString(active.ind))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
id = blobovnicza.NewIDFromBytes([]byte(p))
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
storagelog.Write(b.log, storagelog.AddressField(addr), storagelog.OpField("Blobovniczas PUT"))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.iterateDeepest(addr, fn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if id == nil {
|
|
|
|
return nil, errPutFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
// Get reads object from blobovnicza tree.
|
2020-11-30 08:11:37 +00:00
|
|
|
//
|
|
|
|
// If blobocvnicza ID is specified, only this blobovnicza is processed.
|
2022-07-05 13:47:39 +00:00
|
|
|
// Otherwise, all Blobovniczas are processed descending weight.
|
2022-07-05 14:07:40 +00:00
|
|
|
func (b *Blobovniczas) Get(prm common.GetPrm) (res common.GetRes, err error) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var bPrm blobovnicza.GetPrm
|
2022-07-05 14:07:40 +00:00
|
|
|
bPrm.SetAddress(prm.Address)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
if prm.BlobovniczaID != nil {
|
|
|
|
blz, err := b.openBlobovnicza(prm.BlobovniczaID.String())
|
2020-11-30 08:11:37 +00:00
|
|
|
if err != nil {
|
2022-05-23 14:15:16 +00:00
|
|
|
return res, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.getObject(blz, bPrm)
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCache := make(map[string]struct{})
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
err = b.iterateSortedLeaves(&prm.Address, func(p string) (bool, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
dirPath := filepath.Dir(p)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
_, ok := activeCache[dirPath]
|
|
|
|
|
|
|
|
res, err = b.getObjectFromLevel(bPrm, p, !ok)
|
|
|
|
if err != nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
if !blobovnicza.IsErrNotFound(err) {
|
2020-11-30 08:11:37 +00:00
|
|
|
b.log.Debug("could not get object from level",
|
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCache[dirPath] = struct{}{}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
2020-11-30 08:11:37 +00:00
|
|
|
return err == nil, nil
|
|
|
|
})
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
if err == nil && res.Object == nil {
|
2020-11-30 08:11:37 +00:00
|
|
|
// not found in any blobovnicza
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
return res, errNotFound
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// Delete deletes object from blobovnicza tree.
|
2020-11-30 08:11:37 +00:00
|
|
|
//
|
|
|
|
// If blobocvnicza ID is specified, only this blobovnicza is processed.
|
2022-07-05 13:47:39 +00:00
|
|
|
// Otherwise, all Blobovniczas are processed descending weight.
|
2022-07-06 11:59:35 +00:00
|
|
|
func (b *Blobovniczas) Delete(prm common.DeletePrm) (res common.DeleteRes, err error) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var bPrm blobovnicza.DeletePrm
|
2022-07-06 11:59:35 +00:00
|
|
|
bPrm.SetAddress(prm.Address)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
if prm.BlobovniczaID != nil {
|
|
|
|
blz, err := b.openBlobovnicza(prm.BlobovniczaID.String())
|
2020-11-30 08:11:37 +00:00
|
|
|
if err != nil {
|
2022-05-23 14:15:16 +00:00
|
|
|
return res, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:28:07 +00:00
|
|
|
return b.deleteObject(blz, bPrm, prm)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeCache := make(map[string]struct{})
|
2022-05-23 14:15:16 +00:00
|
|
|
objectFound := false
|
2020-11-30 08:11:37 +00:00
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
err = b.iterateSortedLeaves(&prm.Address, func(p string) (bool, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
dirPath := filepath.Dir(p)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
// don't process active blobovnicza of the level twice
|
|
|
|
_, ok := activeCache[dirPath]
|
|
|
|
|
2021-09-06 14:28:07 +00:00
|
|
|
res, err = b.deleteObjectFromLevel(bPrm, p, !ok, prm)
|
2020-11-30 08:11:37 +00:00
|
|
|
if err != nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
if !blobovnicza.IsErrNotFound(err) {
|
2020-11-30 08:11:37 +00:00
|
|
|
b.log.Debug("could not remove object from level",
|
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCache[dirPath] = struct{}{}
|
|
|
|
|
|
|
|
if err == nil {
|
2022-05-23 14:15:16 +00:00
|
|
|
objectFound = true
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
2020-11-30 08:11:37 +00:00
|
|
|
return err == nil, nil
|
|
|
|
})
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
if err == nil && !objectFound {
|
2020-11-30 08:11:37 +00:00
|
|
|
// not found in any blobovnicza
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
return common.DeleteRes{}, errNotFound
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// GetRange reads range of object payload data from blobovnicza tree.
|
2020-11-30 16:15:17 +00:00
|
|
|
//
|
|
|
|
// If blobocvnicza ID is specified, only this blobovnicza is processed.
|
2022-07-05 13:47:39 +00:00
|
|
|
// Otherwise, all Blobovniczas are processed descending weight.
|
2022-07-06 11:54:23 +00:00
|
|
|
func (b *Blobovniczas) GetRange(prm common.GetRangePrm) (res common.GetRangeRes, err error) {
|
|
|
|
if prm.BlobovniczaID != nil {
|
|
|
|
blz, err := b.openBlobovnicza(prm.BlobovniczaID.String())
|
2020-11-30 16:15:17 +00:00
|
|
|
if err != nil {
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, err
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
return b.getObjectRange(blz, prm)
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeCache := make(map[string]struct{})
|
2022-05-23 14:15:16 +00:00
|
|
|
objectFound := false
|
2020-11-30 16:15:17 +00:00
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
err = b.iterateSortedLeaves(&prm.Address, func(p string) (bool, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
dirPath := filepath.Dir(p)
|
2020-11-30 16:15:17 +00:00
|
|
|
|
|
|
|
_, ok := activeCache[dirPath]
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
res, err = b.getRangeFromLevel(prm, p, !ok)
|
2020-11-30 16:15:17 +00:00
|
|
|
if err != nil {
|
2022-06-29 17:33:48 +00:00
|
|
|
outOfBounds := isErrOutOfRange(err)
|
2022-03-17 08:03:58 +00:00
|
|
|
if !blobovnicza.IsErrNotFound(err) && !outOfBounds {
|
2020-11-30 16:15:17 +00:00
|
|
|
b.log.Debug("could not get object from level",
|
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2022-03-04 12:57:43 +00:00
|
|
|
if outOfBounds {
|
|
|
|
return true, err
|
|
|
|
}
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeCache[dirPath] = struct{}{}
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
objectFound = err == nil
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
2020-11-30 16:15:17 +00:00
|
|
|
return err == nil, nil
|
|
|
|
})
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
if err == nil && !objectFound {
|
2020-11-30 16:15:17 +00:00
|
|
|
// not found in any blobovnicza
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, errNotFound
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
// tries to delete object from particular blobovnicza.
|
|
|
|
//
|
|
|
|
// returns no error if object was removed from some blobovnicza of the same level.
|
2022-07-06 11:59:35 +00:00
|
|
|
func (b *Blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath string, tryActive bool, dp common.DeletePrm) (common.DeleteRes, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
lvlPath := filepath.Dir(blzPath)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
// try to remove from blobovnicza if it is opened
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2020-11-30 08:11:37 +00:00
|
|
|
v, ok := b.opened.Get(blzPath)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
if ok {
|
2021-09-06 14:28:07 +00:00
|
|
|
if res, err := b.deleteObject(v.(*blobovnicza.Blobovnicza), prm, dp); err == nil {
|
2020-11-30 08:11:37 +00:00
|
|
|
return res, err
|
2022-03-17 08:03:58 +00:00
|
|
|
} else if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not remove object from opened blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// therefore the object is possibly placed in a lighter blobovnicza
|
|
|
|
|
|
|
|
// next we check in the active level blobobnicza:
|
|
|
|
// * the active blobovnicza is always opened.
|
|
|
|
b.activeMtx.RLock()
|
|
|
|
active, ok := b.active[lvlPath]
|
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
|
|
|
if ok && tryActive {
|
2021-09-06 14:28:07 +00:00
|
|
|
if res, err := b.deleteObject(active.blz, prm, dp); err == nil {
|
2020-11-30 08:11:37 +00:00
|
|
|
return res, err
|
2022-03-17 08:03:58 +00:00
|
|
|
} else if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not remove object from active blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then object is possibly placed in closed blobovnicza
|
|
|
|
|
|
|
|
// check if it makes sense to try to open the blob
|
2022-07-05 13:47:39 +00:00
|
|
|
// (Blobovniczas "after" the active one are empty anyway,
|
2020-11-30 08:11:37 +00:00
|
|
|
// and it's pointless to open them).
|
2022-02-02 13:28:08 +00:00
|
|
|
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("index is too big", zap.String("path", blzPath))
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
return common.DeleteRes{}, errNotFound
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open blobovnicza (cached inside)
|
|
|
|
blz, err := b.openBlobovnicza(blzPath)
|
|
|
|
if err != nil {
|
2022-07-06 11:59:35 +00:00
|
|
|
return common.DeleteRes{}, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:28:07 +00:00
|
|
|
return b.deleteObject(blz, prm, dp)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tries to read object from particular blobovnicza.
|
|
|
|
//
|
|
|
|
// returns error if object could not be read from any blobovnicza of the same level.
|
2022-07-05 14:07:40 +00:00
|
|
|
func (b *Blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string, tryActive bool) (common.GetRes, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
lvlPath := filepath.Dir(blzPath)
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
// try to read from blobovnicza if it is opened
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2020-11-30 08:11:37 +00:00
|
|
|
v, ok := b.opened.Get(blzPath)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
if ok {
|
|
|
|
if res, err := b.getObject(v.(*blobovnicza.Blobovnicza), prm); err == nil {
|
|
|
|
return res, err
|
2022-03-17 08:03:58 +00:00
|
|
|
} else if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not read object from opened blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// therefore the object is possibly placed in a lighter blobovnicza
|
|
|
|
|
|
|
|
// next we check in the active level blobobnicza:
|
|
|
|
// * the freshest objects are probably the most demanded;
|
|
|
|
// * the active blobovnicza is always opened.
|
|
|
|
b.activeMtx.RLock()
|
|
|
|
active, ok := b.active[lvlPath]
|
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
|
|
|
if ok && tryActive {
|
|
|
|
if res, err := b.getObject(active.blz, prm); err == nil {
|
|
|
|
return res, err
|
2022-03-17 08:03:58 +00:00
|
|
|
} else if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not get object from active blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-11-30 08:11:37 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then object is possibly placed in closed blobovnicza
|
|
|
|
|
|
|
|
// check if it makes sense to try to open the blob
|
2022-07-05 13:47:39 +00:00
|
|
|
// (Blobovniczas "after" the active one are empty anyway,
|
2020-11-30 08:11:37 +00:00
|
|
|
// and it's pointless to open them).
|
2022-02-02 13:28:08 +00:00
|
|
|
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("index is too big", zap.String("path", blzPath))
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{}, errNotFound
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open blobovnicza (cached inside)
|
|
|
|
blz, err := b.openBlobovnicza(blzPath)
|
|
|
|
if err != nil {
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{}, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.getObject(blz, prm)
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:15:17 +00:00
|
|
|
// tries to read range of object payload data from particular blobovnicza.
|
|
|
|
//
|
|
|
|
// returns error if object could not be read from any blobovnicza of the same level.
|
2022-07-06 11:54:23 +00:00
|
|
|
func (b *Blobovniczas) getRangeFromLevel(prm common.GetRangePrm, blzPath string, tryActive bool) (common.GetRangeRes, error) {
|
2022-02-02 13:28:08 +00:00
|
|
|
lvlPath := filepath.Dir(blzPath)
|
2020-11-30 16:15:17 +00:00
|
|
|
|
|
|
|
// try to read from blobovnicza if it is opened
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2020-11-30 16:15:17 +00:00
|
|
|
v, ok := b.opened.Get(blzPath)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 16:15:17 +00:00
|
|
|
if ok {
|
2020-12-08 16:53:01 +00:00
|
|
|
res, err := b.getObjectRange(v.(*blobovnicza.Blobovnicza), prm)
|
|
|
|
switch {
|
|
|
|
case err == nil,
|
2022-06-29 17:33:48 +00:00
|
|
|
isErrOutOfRange(err):
|
2020-11-30 16:15:17 +00:00
|
|
|
return res, err
|
2020-12-08 16:53:01 +00:00
|
|
|
default:
|
2022-03-17 08:03:58 +00:00
|
|
|
if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not read payload range from opened blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-12-08 16:53:01 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// therefore the object is possibly placed in a lighter blobovnicza
|
|
|
|
|
|
|
|
// next we check in the active level blobobnicza:
|
|
|
|
// * the freshest objects are probably the most demanded;
|
|
|
|
// * the active blobovnicza is always opened.
|
|
|
|
b.activeMtx.RLock()
|
|
|
|
active, ok := b.active[lvlPath]
|
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
|
|
|
if ok && tryActive {
|
2020-12-08 16:53:01 +00:00
|
|
|
res, err := b.getObjectRange(active.blz, prm)
|
|
|
|
switch {
|
|
|
|
case err == nil,
|
2022-06-29 17:33:48 +00:00
|
|
|
isErrOutOfRange(err):
|
2020-11-30 16:15:17 +00:00
|
|
|
return res, err
|
2020-12-08 16:53:01 +00:00
|
|
|
default:
|
2022-03-17 08:03:58 +00:00
|
|
|
if !blobovnicza.IsErrNotFound(err) {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("could not read payload range from active blobovnicza",
|
|
|
|
zap.String("path", blzPath),
|
2020-12-08 16:53:01 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then object is possibly placed in closed blobovnicza
|
|
|
|
|
|
|
|
// check if it makes sense to try to open the blob
|
2022-07-05 13:47:39 +00:00
|
|
|
// (Blobovniczas "after" the active one are empty anyway,
|
2020-11-30 16:15:17 +00:00
|
|
|
// and it's pointless to open them).
|
2022-02-02 13:28:08 +00:00
|
|
|
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
|
2022-06-02 17:20:27 +00:00
|
|
|
b.log.Debug("index is too big", zap.String("path", blzPath))
|
2022-03-17 08:03:58 +00:00
|
|
|
|
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, errNotFound
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open blobovnicza (cached inside)
|
|
|
|
blz, err := b.openBlobovnicza(blzPath)
|
|
|
|
if err != nil {
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, err
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.getObjectRange(blz, prm)
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
// removes object from blobovnicza and returns common.DeleteRes.
|
|
|
|
func (b *Blobovniczas) deleteObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.DeletePrm, dp common.DeletePrm) (common.DeleteRes, error) {
|
2020-11-30 08:11:37 +00:00
|
|
|
_, err := blz.Delete(prm)
|
|
|
|
if err != nil {
|
2022-07-06 11:59:35 +00:00
|
|
|
return common.DeleteRes{}, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:28:07 +00:00
|
|
|
storagelog.Write(b.log,
|
2022-07-06 11:59:35 +00:00
|
|
|
storagelog.AddressField(dp.Address),
|
2022-07-05 13:47:39 +00:00
|
|
|
storagelog.OpField("Blobovniczas DELETE"),
|
2022-07-06 11:59:35 +00:00
|
|
|
zap.Stringer("blobovnicza ID", dp.BlobovniczaID),
|
2021-09-06 14:28:07 +00:00
|
|
|
)
|
|
|
|
|
2022-07-06 11:59:35 +00:00
|
|
|
return common.DeleteRes{}, nil
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads object from blobovnicza and returns GetSmallRes.
|
2022-07-05 14:07:40 +00:00
|
|
|
func (b *Blobovniczas) getObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.GetPrm) (common.GetRes, error) {
|
2020-11-30 08:11:37 +00:00
|
|
|
res, err := blz.Get(prm)
|
|
|
|
if err != nil {
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{}, err
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
// decompress the data
|
2022-07-05 13:47:39 +00:00
|
|
|
data, err := b.Decompress(res.Object())
|
2020-12-02 09:58:42 +00:00
|
|
|
if err != nil {
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{}, fmt.Errorf("could not decompress object data: %w", err)
|
2020-12-02 09:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the object
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objectSDK.New()
|
2020-12-02 09:58:42 +00:00
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
|
2020-12-02 09:58:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
return common.GetRes{Object: obj}, nil
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 16:15:17 +00:00
|
|
|
// reads range of object payload data from blobovnicza and returns GetRangeSmallRes.
|
2022-07-06 11:54:23 +00:00
|
|
|
func (b *Blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm common.GetRangePrm) (common.GetRangeRes, error) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var gPrm blobovnicza.GetPrm
|
2022-07-06 11:54:23 +00:00
|
|
|
gPrm.SetAddress(prm.Address)
|
2020-12-02 09:58:42 +00:00
|
|
|
|
|
|
|
// we don't use GetRange call for now since blobovnicza
|
|
|
|
// stores data that is compressed on BlobStor side.
|
|
|
|
// If blobovnicza learns to do the compression itself,
|
2022-03-04 12:56:46 +00:00
|
|
|
// we can start using GetRange.
|
2020-12-02 09:58:42 +00:00
|
|
|
res, err := blz.Get(gPrm)
|
2020-11-30 16:15:17 +00:00
|
|
|
if err != nil {
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, err
|
2020-11-30 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
// decompress the data
|
2022-07-05 13:47:39 +00:00
|
|
|
data, err := b.Decompress(res.Object())
|
2020-12-02 09:58:42 +00:00
|
|
|
if err != nil {
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, fmt.Errorf("could not decompress object data: %w", err)
|
2020-12-02 09:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the object
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objectSDK.New()
|
2020-12-02 09:58:42 +00:00
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
|
2020-12-02 09:58:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
from := prm.Range.GetOffset()
|
|
|
|
to := from + prm.Range.GetLength()
|
2020-12-02 09:58:42 +00:00
|
|
|
payload := obj.Payload()
|
|
|
|
|
2022-07-29 08:11:55 +00:00
|
|
|
if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
|
2022-06-29 17:33:48 +00:00
|
|
|
var errOutOfRange apistatus.ObjectOutOfRange
|
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{}, errOutOfRange
|
2020-12-02 09:58:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 11:54:23 +00:00
|
|
|
return common.GetRangeRes{
|
|
|
|
Data: payload[from:to],
|
2020-11-30 16:15:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// iterator over the paths of Blobovniczas in random order.
|
|
|
|
func (b *Blobovniczas) iterateLeaves(f func(string) (bool, error)) error {
|
2020-11-30 08:11:37 +00:00
|
|
|
return b.iterateSortedLeaves(nil, f)
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// iterator over all Blobovniczas in unsorted order. Break on f's error return.
|
|
|
|
func (b *Blobovniczas) Iterate(ignoreErrors bool, f func(string, *blobovnicza.Blobovnicza) error) error {
|
2021-09-13 11:51:17 +00:00
|
|
|
return b.iterateLeaves(func(p string) (bool, error) {
|
|
|
|
blz, err := b.openBlobovnicza(p)
|
|
|
|
if err != nil {
|
2022-01-20 16:32:49 +00:00
|
|
|
if ignoreErrors {
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-09-13 11:51:17 +00:00
|
|
|
return false, fmt.Errorf("could not open blobovnicza %s: %w", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f(p, blz)
|
|
|
|
|
|
|
|
return err != nil, err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// iterator over the paths of Blobovniczas sorted by weight.
|
|
|
|
func (b *Blobovniczas) iterateSortedLeaves(addr *oid.Address, f func(string) (bool, error)) error {
|
2020-11-30 08:11:37 +00:00
|
|
|
_, err := b.iterateSorted(
|
|
|
|
addr,
|
|
|
|
make([]string, 0, b.blzShallowDepth),
|
|
|
|
b.blzShallowDepth,
|
2022-02-02 13:28:08 +00:00
|
|
|
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
// iterator over directories with Blobovniczas sorted by weight.
|
|
|
|
func (b *Blobovniczas) iterateDeepest(addr oid.Address, f func(string) (bool, error)) error {
|
2020-11-30 13:49:50 +00:00
|
|
|
depth := b.blzShallowDepth
|
|
|
|
if depth > 0 {
|
|
|
|
depth--
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
_, err := b.iterateSorted(
|
2022-05-31 17:00:41 +00:00
|
|
|
&addr,
|
2020-11-30 13:49:50 +00:00
|
|
|
make([]string, 0, depth),
|
|
|
|
depth,
|
2022-02-02 13:28:08 +00:00
|
|
|
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterator over particular level of directories.
|
2022-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) iterateSorted(addr *oid.Address, curPath []string, execDepth uint64, f func([]string) (bool, error)) (bool, error) {
|
2020-11-30 08:11:37 +00:00
|
|
|
indices := indexSlice(b.blzShallowWidth)
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
hrw.SortSliceByValue(indices, addressHash(addr, filepath.Join(curPath...)))
|
2020-11-30 08:11:37 +00:00
|
|
|
|
|
|
|
exec := uint64(len(curPath)) == execDepth
|
|
|
|
|
|
|
|
for i := range indices {
|
|
|
|
if i == 0 {
|
|
|
|
curPath = append(curPath, u64ToHexString(indices[i]))
|
|
|
|
} else {
|
|
|
|
curPath[len(curPath)-1] = u64ToHexString(indices[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if exec {
|
|
|
|
if stop, err := f(curPath); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
} else if stop, err := b.iterateSorted(addr, curPath, execDepth, f); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if tryActive, ok := b.active[p]; ok && tryActive.blz == active.blz {
|
|
|
|
return tryActive, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
// remove from opened cache (active blobovnicza should always be opened)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2020-11-30 08:11:37 +00:00
|
|
|
b.opened.Remove(p)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
b.active[p] = active
|
|
|
|
|
2021-08-24 10:52:22 +00:00
|
|
|
b.log.Debug("blobovnicza successfully activated",
|
2022-02-02 13:28:08 +00:00
|
|
|
zap.String("path", filepath.Join(p, u64ToHexString(active.ind))),
|
2020-11-30 08:11:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return active, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// opens and returns blobovnicza with path p.
|
|
|
|
//
|
|
|
|
// If blobovnicza is already opened and cached, instance from cache is returned w/o changes.
|
2022-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) openBlobovnicza(p string) (*blobovnicza.Blobovnicza, error) {
|
2021-04-15 14:58:15 +00:00
|
|
|
b.lruMtx.Lock()
|
|
|
|
v, ok := b.opened.Get(p)
|
|
|
|
b.lruMtx.Unlock()
|
|
|
|
if ok {
|
|
|
|
// blobovnicza should be opened in cache
|
|
|
|
return v.(*blobovnicza.Blobovnicza), nil
|
|
|
|
}
|
|
|
|
|
2020-12-14 09:39:22 +00:00
|
|
|
b.openMtx.Lock()
|
|
|
|
defer b.openMtx.Unlock()
|
|
|
|
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Lock()
|
2021-04-15 14:58:15 +00:00
|
|
|
v, ok = b.opened.Get(p)
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
2020-11-30 08:11:37 +00:00
|
|
|
if ok {
|
|
|
|
// blobovnicza should be opened in cache
|
|
|
|
return v.(*blobovnicza.Blobovnicza), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blz := blobovnicza.New(append(b.blzOpts,
|
2022-06-28 13:42:50 +00:00
|
|
|
blobovnicza.WithReadOnly(b.readOnly),
|
2022-07-05 13:47:39 +00:00
|
|
|
blobovnicza.WithPath(filepath.Join(b.rootPath, p)),
|
2020-11-30 08:11:37 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
if err := blz.Open(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not open blobovnicza %s: %w", p, err)
|
2020-11-30 08:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 13:29:14 +00:00
|
|
|
b.activeMtx.Lock()
|
|
|
|
b.lruMtx.Lock()
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
b.opened.Add(p, blz)
|
|
|
|
|
2021-04-20 13:29:14 +00:00
|
|
|
b.lruMtx.Unlock()
|
|
|
|
b.activeMtx.Unlock()
|
|
|
|
|
2020-11-30 08:11:37 +00:00
|
|
|
return blz, 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
|
|
|
|
}
|