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:
Egon Elbre 2020-10-29 10:01:53 +02:00
parent 89ce1fe626
commit e3985799a1
11 changed files with 28 additions and 24 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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})