[#1809] node: Do not boot up if metabase is outdated

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-09-21 23:57:46 +03:00 committed by fyrchik
parent 8b3b16fe62
commit 4eb0ed11f8
4 changed files with 14 additions and 1 deletions

View file

@ -21,6 +21,7 @@ Changelog for NeoFS Node
### Fixed ### Fixed
- Description of command `netmap nodeinfo` (#1821) - Description of command `netmap nodeinfo` (#1821)
- Proper status for object.Delete if session token is missing (#1697) - Proper status for object.Delete if session token is missing (#1697)
- Fail startup if metabase has an old version (#1809)
### Removed ### Removed
- Remove WIF and NEP2 support in `neofs-cli`'s --wallet flag (#1128) - Remove WIF and NEP2 support in `neofs-cli`'s --wallet flag (#1128)

View file

@ -58,6 +58,8 @@ func (db *DB) Open(readOnly bool) error {
// Init initializes metabase. It creates static (CID-independent) buckets in underlying BoltDB instance. // Init initializes metabase. It creates static (CID-independent) buckets in underlying BoltDB instance.
// //
// Returns ErrOutdatedVersion if a database at the provided path is outdated.
//
// Does nothing if metabase has already been initialized and filled. To roll back the database to its initial state, // Does nothing if metabase has already been initialized and filled. To roll back the database to its initial state,
// use Reset. // use Reset.
func (db *DB) Init() error { func (db *DB) Init() error {

View file

@ -2,6 +2,7 @@ package meta
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
@ -12,6 +13,11 @@ const version = 2
var versionKey = []byte("version") var versionKey = []byte("version")
// ErrOutdatedVersion is returned on initializing
// an existing metabase that is not compatible with
// the current code version.
var ErrOutdatedVersion = errors.New("invalid version")
func checkVersion(tx *bbolt.Tx, initialized bool) error { func checkVersion(tx *bbolt.Tx, initialized bool) error {
b := tx.Bucket(shardInfoBucket) b := tx.Bucket(shardInfoBucket)
if b != nil { if b != nil {
@ -19,7 +25,7 @@ func checkVersion(tx *bbolt.Tx, initialized bool) error {
if len(data) == 8 { if len(data) == 8 {
stored := binary.LittleEndian.Uint64(data) stored := binary.LittleEndian.Uint64(data)
if stored != version { if stored != version {
return fmt.Errorf("invalid version: expected=%d, stored=%d", version, stored) return fmt.Errorf("%w: expected=%d, stored=%d", ErrOutdatedVersion, version, stored)
} }
} }
} }

View file

@ -116,6 +116,10 @@ func (s *Shard) Init() error {
for _, component := range components { for _, component := range components {
if err := component.Init(); err != nil { if err := component.Init(); err != nil {
if component == s.metaBase { if component == s.metaBase {
if errors.Is(err, meta.ErrOutdatedVersion) {
return err
}
err = s.handleMetabaseFailure("init", err) err = s.handleMetabaseFailure("init", err)
if err != nil { if err != nil {
return err return err