2020-11-30 14:59:00 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-11-09 15:46:12 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-06-08 11:03:29 +00:00
|
|
|
"sync"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2022-06-27 08:36:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2022-06-08 11:03:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2020-11-30 14:59:00 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-06-27 08:36:32 +00:00
|
|
|
type shardInitError struct {
|
|
|
|
err error
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
// Open opens all StorageEngine's components.
|
|
|
|
func (e *StorageEngine) Open() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
return e.open()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) open() error {
|
2020-11-30 14:59:00 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
2022-06-08 11:03:29 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var errCh = make(chan error, len(e.shards))
|
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
for id, sh := range e.shards {
|
2022-06-08 11:03:29 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(id string, sh *shard.Shard) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := sh.Open(); err != nil {
|
|
|
|
errCh <- fmt.Errorf("could not open shard %s: %w", id, err)
|
|
|
|
}
|
|
|
|
}(id, sh.Shard)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(errCh)
|
|
|
|
|
|
|
|
for err := range errCh {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-30 14:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes all StorageEngine's components.
|
|
|
|
func (e *StorageEngine) Init() error {
|
2022-06-27 08:36:32 +00:00
|
|
|
e.mtx.Lock()
|
|
|
|
defer e.mtx.Unlock()
|
2020-11-30 14:59:00 +00:00
|
|
|
|
2022-06-08 11:03:29 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-06-27 08:36:32 +00:00
|
|
|
var errCh = make(chan shardInitError, len(e.shards))
|
2022-06-08 11:03:29 +00:00
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
for id, sh := range e.shards {
|
2022-06-08 11:03:29 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(id string, sh *shard.Shard) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := sh.Init(); err != nil {
|
2022-06-27 08:36:32 +00:00
|
|
|
errCh <- shardInitError{
|
|
|
|
err: err,
|
|
|
|
id: id,
|
|
|
|
}
|
2022-06-08 11:03:29 +00:00
|
|
|
}
|
|
|
|
}(id, sh.Shard)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(errCh)
|
|
|
|
|
2022-06-27 08:36:32 +00:00
|
|
|
for res := range errCh {
|
|
|
|
if res.err != nil {
|
|
|
|
if errors.Is(res.err, blobstor.ErrInitBlobovniczas) {
|
|
|
|
delete(e.shards, res.id)
|
|
|
|
|
|
|
|
e.log.Error("shard initialization failure, skipping",
|
|
|
|
zap.String("id", res.id),
|
|
|
|
zap.Error(res.err))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return fmt.Errorf("could not initialize shard %s: %w", res.id, res.err)
|
2020-11-30 14:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:36:32 +00:00
|
|
|
if len(e.shards) == 0 {
|
|
|
|
return errors.New("failed initialization on all shards")
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
var errClosed = errors.New("storage engine is closed")
|
|
|
|
|
|
|
|
// Close releases all StorageEngine's components. Waits for all data-related operations to complete.
|
2021-11-11 13:58:07 +00:00
|
|
|
// After the call, all the next ones will fail.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
2021-11-10 15:00:30 +00:00
|
|
|
// The method is supposed to be called when the application exits.
|
2020-11-30 14:59:00 +00:00
|
|
|
func (e *StorageEngine) Close() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
return e.setBlockExecErr(errClosed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// closes all shards. Never returns an error, shard errors are logged.
|
2021-11-11 13:58:07 +00:00
|
|
|
func (e *StorageEngine) close(releasePools bool) error {
|
2020-11-30 14:59:00 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
2021-11-11 13:58:07 +00:00
|
|
|
if releasePools {
|
|
|
|
for _, p := range e.shardPools {
|
|
|
|
p.Release()
|
|
|
|
}
|
2021-10-19 13:10:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
for id, sh := range e.shards {
|
|
|
|
if err := sh.Close(); err != nil {
|
|
|
|
e.log.Debug("could not close shard",
|
|
|
|
zap.String("id", id),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-09 15:46:12 +00:00
|
|
|
|
|
|
|
// executes op if execution is not blocked, otherwise returns blocking error.
|
|
|
|
//
|
|
|
|
// Can be called concurrently with setBlockExecErr.
|
2021-11-10 15:00:30 +00:00
|
|
|
func (e *StorageEngine) execIfNotBlocked(op func() error) error {
|
2021-11-09 15:46:12 +00:00
|
|
|
e.blockExec.mtx.RLock()
|
|
|
|
defer e.blockExec.mtx.RUnlock()
|
|
|
|
|
|
|
|
if e.blockExec.err != nil {
|
|
|
|
return e.blockExec.err
|
|
|
|
}
|
|
|
|
|
|
|
|
return op()
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets the flag of blocking execution of all data operations according to err:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - err != nil, then blocks the execution. If exec wasn't blocked, calls close method
|
2021-11-11 13:58:07 +00:00
|
|
|
// (if err == errClosed => additionally releases pools and does not allow to resume executions).
|
2022-08-15 16:20:20 +00:00
|
|
|
// - otherwise, resumes execution. If exec was blocked, calls open method.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Can be called concurrently with exec. In this case it waits for all executions to complete.
|
|
|
|
func (e *StorageEngine) setBlockExecErr(err error) error {
|
|
|
|
e.blockExec.mtx.Lock()
|
|
|
|
defer e.blockExec.mtx.Unlock()
|
|
|
|
|
|
|
|
prevErr := e.blockExec.err
|
|
|
|
|
2021-11-11 13:58:07 +00:00
|
|
|
wasClosed := errors.Is(prevErr, errClosed)
|
|
|
|
if wasClosed {
|
|
|
|
return errClosed
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
e.blockExec.err = err
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if prevErr != nil { // block -> ok
|
|
|
|
return e.open()
|
|
|
|
}
|
|
|
|
} else if prevErr == nil { // ok -> block
|
2021-11-11 13:58:07 +00:00
|
|
|
return e.close(errors.Is(err, errClosed))
|
2021-11-09 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise do nothing
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:00:30 +00:00
|
|
|
// BlockExecution blocks the execution of any data-related operation. All blocked ops will return err.
|
2021-11-09 15:46:12 +00:00
|
|
|
// To resume the execution, use ResumeExecution method.
|
|
|
|
//
|
|
|
|
// Сan be called regardless of the fact of the previous blocking. If execution wasn't blocked, releases all resources
|
|
|
|
// similar to Close. Can be called concurrently with Close and any data related method (waits for all executions
|
2021-11-11 13:58:07 +00:00
|
|
|
// to complete). Returns error if any Close has been called before.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Must not be called concurrently with either Open or Init.
|
|
|
|
//
|
|
|
|
// Note: technically passing nil error will resume the execution, otherwise, it is recommended to call ResumeExecution
|
|
|
|
// for this.
|
|
|
|
func (e *StorageEngine) BlockExecution(err error) error {
|
|
|
|
return e.setBlockExecErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResumeExecution resumes the execution of any data-related operation.
|
|
|
|
// To block the execution, use BlockExecution method.
|
|
|
|
//
|
|
|
|
// Сan be called regardless of the fact of the previous blocking. If execution was blocked, prepares all resources
|
|
|
|
// similar to Open. Can be called concurrently with Close and any data related method (waits for all executions
|
2021-11-11 13:58:07 +00:00
|
|
|
// to complete). Returns error if any Close has been called before.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Must not be called concurrently with either Open or Init.
|
|
|
|
func (e *StorageEngine) ResumeExecution() error {
|
|
|
|
return e.setBlockExecErr(nil)
|
|
|
|
}
|