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.
|
|
|
|
// After the call, all the next ones will fail until the ResumeExecution call.
|
|
|
|
//
|
|
|
|
// Еhe 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.
|
|
|
|
func (e *StorageEngine) close() error {
|
2020-11-30 14:59:00 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
2021-10-19 13:10:55 +00:00
|
|
|
for _, p := range e.shardPools {
|
|
|
|
p.Release()
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (e *StorageEngine) exec(op func() error) error {
|
|
|
|
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:
|
|
|
|
// * err != nil, then blocks the execution. If exec wasn't blocked, calls close method.
|
|
|
|
// * 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
|
|
|
|
|
|
|
|
e.blockExec.err = err
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if prevErr != nil { // block -> ok
|
|
|
|
return e.open()
|
|
|
|
}
|
|
|
|
} else if prevErr == nil { // ok -> block
|
|
|
|
return e.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise do nothing
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockExecution block blocks the execution of any data-related operation. All blocked ops will return err.
|
|
|
|
// 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
|
|
|
|
// to complete).
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// to complete).
|
|
|
|
//
|
|
|
|
// Must not be called concurrently with either Open or Init.
|
|
|
|
func (e *StorageEngine) ResumeExecution() error {
|
|
|
|
return e.setBlockExecErr(nil)
|
|
|
|
}
|