2022-06-15 09:50:59 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2024-08-28 15:32:30 +00:00
|
|
|
"errors"
|
2022-06-15 09:50:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2022-06-15 09:50:59 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// version contains current metabase version.
|
2024-08-20 12:39:44 +00:00
|
|
|
const version = 3
|
2022-06-15 09:50:59 +00:00
|
|
|
|
|
|
|
var versionKey = []byte("version")
|
|
|
|
|
2022-09-21 20:57:46 +00:00
|
|
|
// ErrOutdatedVersion is returned on initializing
|
|
|
|
// an existing metabase that is not compatible with
|
|
|
|
// the current code version.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrOutdatedVersion = logicerr.New("invalid version, resynchronization is required")
|
2022-09-21 20:57:46 +00:00
|
|
|
|
2024-08-28 15:32:30 +00:00
|
|
|
var errVersionUndefinedNoInfoBucket = errors.New("version undefined: no info bucket")
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
func checkVersion(tx *bbolt.Tx, initialized bool) error {
|
2022-10-12 19:45:05 +00:00
|
|
|
var knownVersion bool
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b != nil {
|
|
|
|
data := b.Get(versionKey)
|
|
|
|
if len(data) == 8 {
|
2022-10-12 19:45:05 +00:00
|
|
|
knownVersion = true
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
stored := binary.LittleEndian.Uint64(data)
|
|
|
|
if stored != version {
|
2022-09-21 20:57:46 +00:00
|
|
|
return fmt.Errorf("%w: expected=%d, stored=%d", ErrOutdatedVersion, version, stored)
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 19:45:05 +00:00
|
|
|
|
|
|
|
if !initialized {
|
|
|
|
// new database, write version
|
2022-06-15 09:50:59 +00:00
|
|
|
return updateVersion(tx, version)
|
2022-10-12 19:45:05 +00:00
|
|
|
} 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
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
2022-10-12 19:45:05 +00:00
|
|
|
|
|
|
|
return nil
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-10-17 12:33:41 +00:00
|
|
|
return fmt.Errorf("can't create auxiliary bucket: %w", err)
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
|
|
|
return b.Put(versionKey, data)
|
|
|
|
}
|
2024-08-28 15:32:30 +00:00
|
|
|
|
|
|
|
func currentVersion(tx *bbolt.Tx) (uint64, error) {
|
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b == nil {
|
|
|
|
return 0, errVersionUndefinedNoInfoBucket
|
|
|
|
}
|
|
|
|
data := b.Get(versionKey)
|
|
|
|
if len(data) != 8 {
|
|
|
|
return 0, fmt.Errorf("version undefined: invalid version data length %d", len(data))
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint64(data), nil
|
|
|
|
}
|