storage/{cockroachkv,postgreskv}: add ctx to opening
Database opening usually dial and hence we should pass ctx to them. Change-Id: Iecf41241aaa94d54506cbc80b0e53449848d8819
This commit is contained in:
parent
89ce1fe626
commit
e3985799a1
@ -42,7 +42,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
|
||||
err = errs.Combine(err, db.Close())
|
||||
}()
|
||||
|
||||
pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), runCfg.Config.Metainfo.DatabaseURL)
|
||||
pointerDB, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), runCfg.Config.Metainfo.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("Error creating metainfodb connection on satellite api: %+v", err)
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) {
|
||||
err = errs.Combine(err, db.Close())
|
||||
}()
|
||||
|
||||
pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
pointerDB, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("Error creating pointerDB connection GC: %+v", err)
|
||||
}
|
||||
|
@ -335,7 +335,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
||||
err = errs.Combine(err, db.Close())
|
||||
}()
|
||||
|
||||
pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
pointerDB, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("Error creating metainfodb connection: %+v", err)
|
||||
}
|
||||
@ -412,7 +412,7 @@ func cmdMigrationRun(cmd *cobra.Command, args []string) (err error) {
|
||||
return errs.New("Error creating tables for master database on satellite: %+v", err)
|
||||
}
|
||||
|
||||
pdb, err := metainfo.NewStore(log.Named("migration"), runCfg.Metainfo.DatabaseURL)
|
||||
pdb, err := metainfo.OpenStore(ctx, log.Named("migration"), runCfg.Metainfo.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("Error creating pointer database connection on satellite: %+v", err)
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) {
|
||||
err = errs.Combine(err, db.Close())
|
||||
}()
|
||||
|
||||
pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
pointerDB, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), runCfg.Metainfo.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("Error creating metainfo database connection: %+v", err)
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func cmdDelete(cmd *cobra.Command, args []string) (err error) {
|
||||
ctx, _ := process.Ctx(cmd)
|
||||
|
||||
log := zap.L()
|
||||
db, err := metainfo.NewStore(log.Named("pointerdb"), deleteCfg.DatabaseURL)
|
||||
db, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), deleteCfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("error connecting database: %+v", err)
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func cmdDetect(cmd *cobra.Command, args []string) (err error) {
|
||||
log.Warn("Failed to initialize telemetry batcher on segment reaper", zap.Error(err))
|
||||
}
|
||||
|
||||
db, err := metainfo.NewStore(log.Named("pointerdb"), detectCfg.DatabaseURL)
|
||||
db, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), detectCfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return errs.New("error connecting database: %+v", err)
|
||||
}
|
||||
|
@ -81,8 +81,8 @@ type PointerDB interface {
|
||||
storage.KeyValueStore
|
||||
}
|
||||
|
||||
// NewStore returns database for storing pointer data.
|
||||
func NewStore(logger *zap.Logger, dbURLString string) (db PointerDB, err error) {
|
||||
// OpenStore returns database for storing pointer data.
|
||||
func OpenStore(ctx context.Context, logger *zap.Logger, dbURLString string) (db PointerDB, err error) {
|
||||
_, source, implementation, err := dbutil.SplitConnStr(dbURLString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -90,9 +90,9 @@ func NewStore(logger *zap.Logger, dbURLString string) (db PointerDB, err error)
|
||||
|
||||
switch implementation {
|
||||
case dbutil.Postgres:
|
||||
db, err = postgreskv.New(source)
|
||||
db, err = postgreskv.Open(ctx, source)
|
||||
case dbutil.Cockroach:
|
||||
db, err = cockroachkv.New(source)
|
||||
db, err = cockroachkv.Open(ctx, source)
|
||||
default:
|
||||
err = Error.New("unsupported db implementation: %s", dbURLString)
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ func CreatePointerDB(ctx context.Context, log *zap.Logger, name string, category
|
||||
// CreatePointerDBOnTopOf creates a new satellite database on top of an already existing
|
||||
// temporary database.
|
||||
func CreatePointerDBOnTopOf(ctx context.Context, log *zap.Logger, tempDB *dbutil.TempDatabase) (db metainfo.PointerDB, err error) {
|
||||
pointerDB, err := metainfo.NewStore(log.Named("pointerdb"), tempDB.ConnStr)
|
||||
pointerDB, err := metainfo.OpenStore(ctx, log.Named("pointerdb"), tempDB.ConnStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -33,8 +33,8 @@ type Client struct {
|
||||
lookupLimit int
|
||||
}
|
||||
|
||||
// New instantiates a new cockroachkv client given db URL.
|
||||
func New(dbURL string) (*Client, error) {
|
||||
// Open connects a new cockroachkv client given db URL.
|
||||
func Open(ctx context.Context, dbURL string) (*Client, error) {
|
||||
dbURL = pgutil.CheckApplicationName(dbURL)
|
||||
|
||||
db, err := tagsql.Open("cockroach", dbURL)
|
||||
|
@ -30,8 +30,8 @@ type Client struct {
|
||||
lookupLimit int
|
||||
}
|
||||
|
||||
// New instantiates a new postgreskv client given db URL.
|
||||
func New(dbURL string) (*Client, error) {
|
||||
// Open connects a new postgreskv client given db URL.
|
||||
func Open(ctx context.Context, dbURL string) (*Client, error) {
|
||||
dbURL = pgutil.CheckApplicationName(dbURL)
|
||||
|
||||
db, err := tagsql.Open("pgx", dbURL)
|
||||
|
@ -20,10 +20,10 @@ import (
|
||||
"storj.io/storj/storage/testsuite"
|
||||
)
|
||||
|
||||
func newTestPostgres(t testing.TB) (store *Client, cleanup func()) {
|
||||
func openTestPostgres(ctx context.Context, t testing.TB) (store *Client, cleanup func()) {
|
||||
connstr := pgtest.PickPostgres(t)
|
||||
|
||||
pgdb, err := New(connstr)
|
||||
pgdb, err := Open(ctx, connstr)
|
||||
if err != nil {
|
||||
t.Fatalf("init: %v", err)
|
||||
}
|
||||
@ -36,12 +36,12 @@ func newTestPostgres(t testing.TB) (store *Client, cleanup func()) {
|
||||
}
|
||||
|
||||
func TestSuite(t *testing.T) {
|
||||
store, cleanup := newTestPostgres(t)
|
||||
defer cleanup()
|
||||
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
store, cleanup := openTestPostgres(ctx, t)
|
||||
defer cleanup()
|
||||
|
||||
err := store.MigrateToLatest(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -56,7 +56,7 @@ func TestThatMigrationActuallyHappened(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
store, cleanup := newTestPostgres(t)
|
||||
store, cleanup := openTestPostgres(ctx, t)
|
||||
defer cleanup()
|
||||
|
||||
rows, err := store.db.Query(ctx, `
|
||||
@ -96,7 +96,9 @@ func TestThatMigrationActuallyHappened(t *testing.T) {
|
||||
func BenchmarkSuite(b *testing.B) {
|
||||
b.Skip("broken")
|
||||
|
||||
store, cleanup := newTestPostgres(b)
|
||||
ctx := context.Background()
|
||||
|
||||
store, cleanup := openTestPostgres(ctx, b)
|
||||
defer cleanup()
|
||||
|
||||
testsuite.RunBenchmarks(b, store)
|
||||
@ -157,7 +159,9 @@ func (store *pgLongBenchmarkStore) BulkDeleteAll(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func BenchmarkSuiteLong(b *testing.B) {
|
||||
store, cleanup := newTestPostgres(b)
|
||||
ctx := context.Background()
|
||||
|
||||
store, cleanup := openTestPostgres(ctx, b)
|
||||
defer cleanup()
|
||||
|
||||
testsuite.BenchmarkPathOperationsInLargeDb(b, &pgLongBenchmarkStore{store})
|
||||
|
Loading…
Reference in New Issue
Block a user