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"
|
|
|
|
|
2020-11-30 14:59:00 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
for id, sh := range e.shards {
|
|
|
|
if err := sh.Open(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not open shard %s: %w", id, err)
|
2020-11-30 14:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes all StorageEngine's components.
|
|
|
|
func (e *StorageEngine) Init() error {
|
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
for id, sh := range e.shards {
|
|
|
|
if err := sh.Init(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not initialize shard %s: %w", id, err)
|
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:
|
2021-11-11 13:58:07 +00:00
|
|
|
// * err != nil, then blocks the execution. If exec wasn't blocked, calls close method
|
|
|
|
// (if err == errClosed => additionally releases pools and does not allow to resume executions).
|
2021-11-09 15:46:12 +00:00
|
|
|
// * otherwise, resumes execution. If exec was blocked, calls open method.
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
}
|