storj/storagenode/storagenodedb/storagenodedbtest/snapshot.go
Andrew Harding 4fdea51d5c storagenode/storagenodedb: faster test db init
Running all of the migrations necessary to initialize a storage node
database takes a significant amount of time during runs.

The package current supports initializing a database from manually coalesced
migration data (i.e. snapshot) which improves the situation somewhat.

This change takes things a bit further by changing the snapshot code to
instead hydrate the database directory from a pre-generated snapshot zip
file.

name                                old time/op  new time/op  delta
Run_StorageNodeCount_4/Postgres-16   2.50s ± 0%   0.16s ± 0%   ~     (p=1.000 n=1+1)

Change-Id: I213bbba5f9199497fbe8ce889b627e853f8b29a0
2022-12-01 20:45:36 +00:00

62 lines
1.3 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedbtest
import (
"archive/zip"
"bytes"
"context"
_ "embed"
"io"
"os"
"path/filepath"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/storagenode/storagenodedb"
)
//go:generate go run ./gensnapshot testdata/snapshot.zip
//go:embed testdata/snapshot.zip
var snapshotZip []byte
// OpenNew opens a new storage node database pre-populated with a newly
// initialized and migrated database snapshot.
func OpenNew(ctx context.Context, log *zap.Logger, config storagenodedb.Config) (*storagenodedb.DB, error) {
db, err := storagenodedb.OpenNew(ctx, log, config)
if err != nil {
return nil, err
}
if err := deploySnapshot(db.DBDirectory()); err != nil {
return nil, err
}
return db, nil
}
func deploySnapshot(storageDir string) error {
zipReader, err := zip.NewReader(bytes.NewReader(snapshotZip), int64(len(snapshotZip)))
if err != nil {
return errs.Wrap(err)
}
for _, f := range zipReader.File {
rc, err := f.Open()
if err != nil {
return errs.Wrap(err)
}
data, err := io.ReadAll(rc)
if err != nil {
return errs.Wrap(err)
}
if err := os.WriteFile(filepath.Join(storageDir, f.Name), data, 0644); err != nil {
return errs.Wrap(err)
}
if err := rc.Close(); err != nil {
return errs.Wrap(err)
}
}
return nil
}