2022-07-05 13:47:39 +00:00
|
|
|
package blobovniczatree
|
|
|
|
|
|
|
|
import (
|
2023-05-28 19:37:37 +00:00
|
|
|
"context"
|
2022-07-05 13:47:39 +00:00
|
|
|
"fmt"
|
2022-07-18 06:16:36 +00:00
|
|
|
"path/filepath"
|
2022-07-05 13:47:39 +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/local_object_storage/blobovnicza"
|
2022-07-05 13:47:39 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open opens blobovnicza tree.
|
|
|
|
func (b *Blobovniczas) Open(readOnly bool) error {
|
|
|
|
b.readOnly = readOnly
|
2023-06-05 07:25:25 +00:00
|
|
|
b.metrics.SetMode(readOnly)
|
2022-07-05 13:47:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes blobovnicza tree.
|
|
|
|
//
|
|
|
|
// Should be called exactly once.
|
|
|
|
func (b *Blobovniczas) Init() error {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeInitializingBlobovniczas)
|
2022-07-05 13:47:39 +00:00
|
|
|
|
|
|
|
if b.readOnly {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeReadonlyModeSkipBlobovniczasInitialization)
|
2022-07-05 13:47:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:37:37 +00:00
|
|
|
return b.iterateLeaves(context.TODO(), func(p string) (bool, error) {
|
2022-08-30 06:15:16 +00:00
|
|
|
blz, err := b.openBlobovniczaNoCache(p)
|
2022-08-29 07:49:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
defer blz.Close()
|
|
|
|
|
2022-07-05 13:47:39 +00:00
|
|
|
if err := blz.Init(); err != nil {
|
2022-08-29 07:49:14 +00:00
|
|
|
return true, fmt.Errorf("could not initialize blobovnicza structure %s: %w", p, err)
|
2022-07-05 13:47:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeBlobovniczaSuccessfullyInitializedClosing, zap.String("id", p))
|
2022-08-29 07:49:14 +00:00
|
|
|
return false, nil
|
2022-07-05 13:47:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
// Close implements common.Storage.
|
2022-07-05 13:47:39 +00:00
|
|
|
func (b *Blobovniczas) Close() error {
|
|
|
|
b.activeMtx.Lock()
|
|
|
|
|
|
|
|
b.lruMtx.Lock()
|
|
|
|
|
|
|
|
for p, v := range b.active {
|
|
|
|
if err := v.blz.Close(); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotCloseActiveBlobovnicza,
|
2022-07-05 13:47:39 +00:00
|
|
|
zap.String("path", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
b.opened.Remove(p)
|
|
|
|
}
|
|
|
|
for _, k := range b.opened.Keys() {
|
2022-12-30 10:01:01 +00:00
|
|
|
blz, _ := b.opened.Get(k)
|
2022-07-05 13:47:39 +00:00
|
|
|
if err := blz.Close(); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotCloseActiveBlobovnicza,
|
2022-12-30 10:01:01 +00:00
|
|
|
zap.String("path", k),
|
2022-07-05 13:47:39 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
b.opened.Remove(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.active = make(map[string]blobovniczaWithIndex)
|
2023-06-05 07:25:25 +00:00
|
|
|
b.metrics.Close()
|
2022-07-05 13:47:39 +00:00
|
|
|
|
|
|
|
b.lruMtx.Unlock()
|
|
|
|
|
|
|
|
b.activeMtx.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-18 06:16:36 +00:00
|
|
|
|
|
|
|
// opens and returns blobovnicza with path p.
|
|
|
|
//
|
|
|
|
// If blobovnicza is already opened and cached, instance from cache is returned w/o changes.
|
|
|
|
func (b *Blobovniczas) openBlobovnicza(p string) (*blobovnicza.Blobovnicza, error) {
|
|
|
|
b.lruMtx.Lock()
|
|
|
|
v, ok := b.opened.Get(p)
|
|
|
|
b.lruMtx.Unlock()
|
|
|
|
if ok {
|
|
|
|
// blobovnicza should be opened in cache
|
2022-12-30 10:01:01 +00:00
|
|
|
return v, nil
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 06:15:16 +00:00
|
|
|
lvlPath := filepath.Dir(p)
|
|
|
|
curIndex := u64FromHexString(filepath.Base(p))
|
|
|
|
|
|
|
|
b.activeMtx.RLock()
|
|
|
|
defer b.activeMtx.RUnlock()
|
|
|
|
|
|
|
|
active, ok := b.active[lvlPath]
|
|
|
|
if ok && active.ind == curIndex {
|
|
|
|
return active.blz, nil
|
2022-08-29 07:49:14 +00:00
|
|
|
}
|
2022-07-18 06:16:36 +00:00
|
|
|
|
|
|
|
b.lruMtx.Lock()
|
2022-08-30 06:15:16 +00:00
|
|
|
defer b.lruMtx.Unlock()
|
2022-08-29 07:49:14 +00:00
|
|
|
|
2022-08-30 06:15:16 +00:00
|
|
|
v, ok = b.opened.Get(p)
|
|
|
|
if ok {
|
2022-12-30 10:01:01 +00:00
|
|
|
return v, nil
|
2022-08-30 06:15:16 +00:00
|
|
|
}
|
2022-08-29 07:49:14 +00:00
|
|
|
|
2022-08-30 06:15:16 +00:00
|
|
|
blz, err := b.openBlobovniczaNoCache(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.opened.Add(p, blz)
|
2022-08-29 07:49:14 +00:00
|
|
|
|
|
|
|
return blz, nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 06:15:16 +00:00
|
|
|
func (b *Blobovniczas) openBlobovniczaNoCache(p string) (*blobovnicza.Blobovnicza, error) {
|
2022-08-29 07:49:14 +00:00
|
|
|
b.openMtx.Lock()
|
|
|
|
defer b.openMtx.Unlock()
|
|
|
|
|
2023-06-05 07:25:25 +00:00
|
|
|
path := filepath.Join(b.rootPath, p)
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
blz := blobovnicza.New(append(b.blzOpts,
|
|
|
|
blobovnicza.WithReadOnly(b.readOnly),
|
2023-06-05 07:25:25 +00:00
|
|
|
blobovnicza.WithPath(path),
|
|
|
|
blobovnicza.WithMetrics(b.metrics.BlobovnicaMetrics(path)),
|
2022-07-18 06:16:36 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
if err := blz.Open(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not open blobovnicza %s: %w", p, err)
|
|
|
|
}
|
|
|
|
return blz, nil
|
|
|
|
}
|