frostfs-node/pkg/local_object_storage/metabase/version.go
Pavel Karpy f76516a883 [#1860] meta: Fix 0,1 -> 2+ version migration
In the 2nd version, there was a database format change: buckets have changed
their keys, so it becomes impossible to check the version in the 1 -> 2+
migrations because of different buckets that store info about the version.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2022-10-17 13:42:05 +03:00

61 lines
1.4 KiB
Go

package meta
import (
"encoding/binary"
"errors"
"fmt"
"go.etcd.io/bbolt"
)
// version contains current metabase version.
const version = 2
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 {
var knownVersion bool
b := tx.Bucket(shardInfoBucket)
if b != nil {
data := b.Get(versionKey)
if len(data) == 8 {
knownVersion = true
stored := binary.LittleEndian.Uint64(data)
if stored != version {
return fmt.Errorf("%w: expected=%d, stored=%d", ErrOutdatedVersion, version, stored)
}
}
}
if !initialized {
// new database, write version
return updateVersion(tx, version)
} else if !knownVersion {
// db is initialized but no version
// has been found; that could happen
// if the db is corrupted or the version
// is <2 (is outdated and requires resync
// anyway)
return ErrOutdatedVersion
}
return nil
}
func updateVersion(tx *bbolt.Tx, version uint64) error {
data := make([]byte, 8)
binary.LittleEndian.PutUint64(data, version)
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
if err != nil {
return fmt.Errorf("can't create auxilliary bucket: %w", err)
}
return b.Put(versionKey, data)
}