frostfs-node/pkg/local_object_storage/blobstor/control.go
Dmitrii Stepanov 98ca6a7e53
All checks were successful
DCO action / DCO (pull_request) Successful in 2m27s
Vulncheck / Vulncheck (pull_request) Successful in 2m7s
Build / Build Components (1.20) (pull_request) Successful in 2m44s
Tests and linters / Staticcheck (pull_request) Successful in 3m18s
Tests and linters / Tests (1.21) (pull_request) Successful in 5m0s
Tests and linters / Tests with -race (pull_request) Successful in 5m40s
Build / Build Components (1.21) (pull_request) Successful in 12m49s
Tests and linters / Lint (pull_request) Successful in 12m57s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m8s
[#661] blobstor: Refactor storage selection
Use special flag to select storages by storage ID.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-09-18 15:46:38 +03:00

102 lines
2.2 KiB
Go

package blobstor
import (
"context"
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
"go.uber.org/zap"
)
// Open opens BlobStor.
func (b *BlobStor) Open(ctx context.Context, readOnly bool) error {
b.log.Debug(logs.BlobstorOpening)
for i := range b.storage {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := b.storage[i].Storage.Open(readOnly)
if err != nil {
return err
}
}
b.metrics.SetMode(readOnly)
return nil
}
// ErrInitBlobovniczas is returned when blobovnicza initialization fails.
var ErrInitBlobovniczas = errors.New("failure on blobovnicza initialization stage")
// Init initializes internal data structures and system resources.
//
// If BlobStor is already initialized, no action is taken.
//
// Returns wrapped ErrInitBlobovniczas on blobovnicza tree's initializaiton failure.
func (b *BlobStor) Init() error {
b.log.Debug(logs.BlobstorInitializing)
if err := b.compression.Init(); err != nil {
return err
}
for i := range b.storage {
err := b.storage[i].Storage.Init()
if err != nil {
return fmt.Errorf("%w: %v", ErrInitBlobovniczas, err)
}
}
return nil
}
// Close releases all internal resources of BlobStor.
func (b *BlobStor) Close() error {
b.log.Debug(logs.BlobstorClosing)
var firstErr error
for i := range b.storage {
err := b.storage[i].Storage.Close()
if err != nil {
b.log.Info(logs.BlobstorCouldntCloseStorage, zap.String("error", err.Error()))
if firstErr == nil {
firstErr = err
}
continue
}
}
err := b.compression.Close()
if firstErr == nil {
firstErr = err
}
if firstErr == nil {
b.metrics.Close()
}
return firstErr
}
func (b *BlobStor) selectStorages(storageID []byte) []common.Storage {
var res []common.Storage
if storageID == nil {
for idx := range b.storage {
res = append(res, b.storage[idx].Storage)
}
} else if len(storageID) > 0 {
for idx := range b.storage {
if b.storage[idx].SupportsStorageID {
res = append(res, b.storage[idx].Storage)
}
}
} else {
for idx := range b.storage {
if !b.storage[idx].SupportsStorageID {
res = append(res, b.storage[idx].Storage)
}
}
}
return res
}