frostfs-node/scripts/populate-metabase/util/populate.go
Aleksey Savchuk 919b9e3370
[#1223] scripts: Add script to populate metabase
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-08-22 14:46:54 +03:00

199 lines
3.7 KiB
Go

package util
import (
"context"
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"golang.org/x/sync/errgroup"
)
type EpochState struct{}
func (s EpochState) CurrentEpoch() uint64 {
return 0
}
func PopulateWithObjects(db *meta.DB, count uint, factory func() *objectSDK.Object) error {
g := &errgroup.Group{}
var i uint
for i = 0; i < count; i++ {
obj := factory()
id := []byte("path to blobovnicza")
prm := meta.PutPrm{}
prm.SetObject(obj)
prm.SetStorageID(id)
g.Go(func() error {
_, err := db.Put(context.Background(), prm)
return err
})
}
return g.Wait()
}
func PopulateWithBigObjects(db *meta.DB, count uint, factory func() *objectSDK.Object) error {
g := &errgroup.Group{}
var i uint
for ; i < count; i++ {
g.Go(func() error {
return populateWithBigObject(db, factory)
})
}
return g.Wait()
}
func populateWithBigObject(db *meta.DB, factory func() *objectSDK.Object) error {
t := &target{db: db}
pk, _ := keys.NewPrivateKey()
p := transformer.NewPayloadSizeLimiter(transformer.Params{
Key: &pk.PrivateKey,
NextTargetInit: func() transformer.ObjectWriter { return t },
NetworkState: EpochState{},
MaxSize: 10,
})
obj := factory()
payload := make([]byte, 30)
err := p.WriteHeader(context.Background(), obj)
if err != nil {
return err
}
_, err = p.Write(context.Background(), payload)
if err != nil {
return err
}
_, err = p.Close(context.Background())
if err != nil {
return err
}
return nil
}
type target struct {
db *meta.DB
}
func (t *target) WriteObject(ctx context.Context, obj *objectSDK.Object) error {
prm := meta.PutPrm{}
prm.SetObject(obj)
_, err := t.db.Put(ctx, prm)
return err
}
func PopulateGraveyard(db *meta.DB, count uint, factory func() *objectSDK.Object) error {
g := &errgroup.Group{}
var addrs []oid.Address
var i uint
for ; i < count; i++ {
var addr oid.Address
obj := factory()
g.Go(func() error {
prm := meta.PutPrm{}
prm.SetObject(obj)
_, err := db.Put(context.Background(), prm)
return err
})
cnrID, _ := obj.ContainerID()
addr.SetContainer(cnrID)
objID, _ := obj.ID()
addr.SetObject(objID)
addrs = append(addrs, addr)
}
if err := g.Wait(); err != nil {
return err
}
tomb := factory()
tomb.SetType(objectSDK.TypeTombstone)
g.Go(func() error {
prm := meta.PutPrm{}
prm.SetObject(tomb)
_, err := db.Put(context.Background(), prm)
return err
})
var addr oid.Address
cnrID, _ := tomb.ContainerID()
addr.SetContainer(cnrID)
objID, _ := tomb.ID()
addr.SetObject(objID)
prm := meta.InhumePrm{}
prm.SetAddresses(addrs...)
prm.SetTombstoneAddress(addr)
g.Go(func() error {
_, err := db.Inhume(context.Background(), prm)
return err
})
return g.Wait()
}
func PopulateLocked(db *meta.DB, count uint, factory func() *objectSDK.Object) error {
g := &errgroup.Group{}
var locked []oid.ID
var i uint
for ; i < count; i++ {
obj := factory()
g.Go(func() error {
prm := meta.PutPrm{}
prm.SetObject(obj)
_, err := db.Put(context.Background(), prm)
return err
})
id, _ := obj.ID()
locked = append(locked, id)
}
if err := g.Wait(); err != nil {
return err
}
locker := factory()
locker.SetType(objectSDK.TypeLock)
g.Go(func() error {
prm := meta.PutPrm{}
prm.SetObject(locker)
_, err := db.Put(context.Background(), prm)
return err
})
cnr, _ := locker.ContainerID()
id, _ := locker.ID()
g.Go(func() error {
err := db.Lock(context.Background(), cnr, id, locked)
return err
})
return g.Wait()
}