pkg/revocation: pass ctx into opening the database

Opening a databases requires ctx, this is first step to passing ctx
to the appropriate level.

Change-Id: I12700f39a320206d8a2a4e054452319f8585b44b
This commit is contained in:
Egon Elbre 2020-10-28 16:01:41 +02:00
parent 9b2e00a38b
commit d0beaa4a87
15 changed files with 47 additions and 41 deletions

View File

@ -75,7 +75,7 @@ func cmdRun(cmd *cobra.Command, args []string) error {
return errs.New("error opening authorizations database: %+v", err)
}
revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("error creating revocation database: %+v", err)
}

View File

@ -193,7 +193,7 @@ func cmdRevokePeerCA(cmd *cobra.Command, args []string) (err error) {
return err
}
revDB, err := revocation.NewDB(revokePeerCACfg.RevocationDBURL)
revDB, err := revocation.OpenDB(ctx, revokePeerCACfg.RevocationDBURL)
if err != nil {
return err
}

View File

@ -189,7 +189,7 @@ func cmdAuthorize(cmd *cobra.Command, args []string) (err error) {
// Ensure we dont enforce a signed Peer Identity
config.Signer.TLS.UsePeerCAWhitelist = false
revocationDB, err := revocation.NewDBFromCfg(config.Signer.TLS)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Signer.TLS)
if err != nil {
return errs.New("error creating revocation database: %+v", err)
}

View File

@ -42,7 +42,7 @@ func cmdRevocations(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
revCfg.RevocationDBURL = "bolt://" + filepath.Join(configDir, args[0], "revocations.db")
}
revDB, err := revocation.NewDB(revCfg.RevocationDBURL)
revDB, err := revocation.OpenDB(ctx, revCfg.RevocationDBURL)
if err != nil {
return err
}

View File

@ -50,7 +50,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, pointerDB.Close())
}()
revocationDB, err := revocation.NewDBFromCfg(runCfg.Config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Config.Server.Config)
if err != nil {
return errs.New("Error creating revocation database on satellite api: %+v", err)
}

View File

@ -44,7 +44,7 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, pointerDB.Close())
}()
revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("Error creating revocation database GC: %+v", err)
}

View File

@ -343,7 +343,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, pointerDB.Close())
}()
revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("Error creating revocation database: %+v", err)
}

View File

@ -46,7 +46,7 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, pointerDB.Close())
}()
revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("Error creating revocation database: %+v", err)
}

View File

@ -158,7 +158,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, db.Close())
}()
revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config)
if err != nil {
return errs.New("Error creating revocation database: %+v", err)
}

View File

@ -4,6 +4,8 @@
package revocation
import (
"context"
"storj.io/common/peertls/extensions"
"storj.io/common/peertls/tlsopts"
"storj.io/storj/private/dbutil"
@ -11,18 +13,18 @@ import (
"storj.io/storj/storage/redis"
)
// NewDBFromCfg is a convenience method to create a revocation DB
// OpenDBFromCfg is a convenience method to create a revocation DB
// directly from a config. If the revocation extension option is not set, it
// returns a nil db with no error.
func NewDBFromCfg(cfg tlsopts.Config) (*DB, error) {
func OpenDBFromCfg(ctx context.Context, cfg tlsopts.Config) (*DB, error) {
if !cfg.Extensions.Revocation {
return &DB{}, nil
}
return NewDB(cfg.RevocationDBURL)
return OpenDB(ctx, cfg.RevocationDBURL)
}
// NewDB returns a new revocation database given the URL.
func NewDB(dbURL string) (*DB, error) {
// OpenDB returns a new revocation database given the URL.
func OpenDB(ctx context.Context, dbURL string) (*DB, error) {
driver, source, _, err := dbutil.SplitConnStr(dbURL)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
@ -30,12 +32,12 @@ func NewDB(dbURL string) (*DB, error) {
var db *DB
switch driver {
case "bolt":
db, err = newDBBolt(source)
db, err = openDBBolt(ctx, source)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
}
case "redis":
db, err = newDBRedis(dbURL)
db, err = openDBRedis(ctx, dbURL)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)
}
@ -45,8 +47,8 @@ func NewDB(dbURL string) (*DB, error) {
return db, nil
}
// newDBBolt creates a bolt-backed DB.
func newDBBolt(path string) (*DB, error) {
// openDBBolt creates a bolt-backed DB.
func openDBBolt(ctx context.Context, path string) (*DB, error) {
client, err := boltdb.New(path, extensions.RevocationBucket)
if err != nil {
return nil, err
@ -56,8 +58,8 @@ func newDBBolt(path string) (*DB, error) {
}, nil
}
// newDBRedis creates a redis-backed DB.
func newDBRedis(address string) (*DB, error) {
// openDBRedis creates a redis-backed DB.
func openDBRedis(ctx context.Context, address string) (*DB, error) {
client, err := redis.NewClientFrom(address)
if err != nil {
return nil, err

View File

@ -110,7 +110,7 @@ func TestNewOptions(t *testing.T) {
for _, c := range cases {
t.Log(c.testID)
revocationDB, err := revocation.NewDBFromCfg(c.config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, c.config)
require.NoError(t, err)
tlsOptions, err := tlsopts.NewOptions(fi, c.config, revocationDB)

View File

@ -323,6 +323,8 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes
}()
for i := 0; i < count; i++ {
ctx := context.TODO()
prefix := "satellite" + strconv.Itoa(i)
log := planet.log.Named(prefix)
@ -336,7 +338,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes
return nil, err
}
db, err := satellitedbtest.CreateMasterDB(context.TODO(), log.Named("db"), planet.config.Name, "S", i, satelliteDatabases.MasterDB)
db, err := satellitedbtest.CreateMasterDB(ctx, log.Named("db"), planet.config.Name, "S", i, satelliteDatabases.MasterDB)
if err != nil {
return nil, err
}
@ -351,7 +353,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes
}
planet.databases = append(planet.databases, db)
pointerDB, err := satellitedbtest.CreatePointerDB(context.TODO(), log.Named("pointerdb"), planet.config.Name, "P", i, satelliteDatabases.PointerDB)
pointerDB, err := satellitedbtest.CreatePointerDB(ctx, log.Named("pointerdb"), planet.config.Name, "P", i, satelliteDatabases.PointerDB)
if err != nil {
return nil, err
}
@ -620,7 +622,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes
versionInfo := planet.NewVersionInfo()
revocationDB, err := revocation.NewDBFromCfg(config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config)
if err != nil {
return xs, errs.Wrap(err)
}
@ -641,27 +643,27 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes
return xs, err
}
err = db.TestingMigrateToLatest(context.TODO())
err = db.TestingMigrateToLatest(ctx)
if err != nil {
return nil, err
}
api, err := planet.newAPI(i, identity, db, pointerDB, config, versionInfo)
api, err := planet.newAPI(ctx, i, identity, db, pointerDB, config, versionInfo)
if err != nil {
return xs, err
}
adminPeer, err := planet.newAdmin(i, identity, db, config, versionInfo)
adminPeer, err := planet.newAdmin(ctx, i, identity, db, config, versionInfo)
if err != nil {
return xs, err
}
repairerPeer, err := planet.newRepairer(i, identity, db, pointerDB, config, versionInfo)
repairerPeer, err := planet.newRepairer(ctx, i, identity, db, pointerDB, config, versionInfo)
if err != nil {
return xs, err
}
gcPeer, err := planet.newGarbageCollection(i, identity, db, pointerDB, config, versionInfo)
gcPeer, err := planet.newGarbageCollection(ctx, i, identity, db, pointerDB, config, versionInfo)
if err != nil {
return xs, err
}
@ -753,12 +755,12 @@ func createNewSystem(log *zap.Logger, config satellite.Config, peer *satellite.C
return system
}
func (planet *Planet) newAPI(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.API, error) {
func (planet *Planet) newAPI(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.API, error) {
prefix := "satellite-api" + strconv.Itoa(count)
log := planet.log.Named(prefix)
var err error
revocationDB, err := revocation.NewDBFromCfg(config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config)
if err != nil {
return nil, errs.Wrap(err)
}
@ -776,18 +778,18 @@ func (planet *Planet) newAPI(count int, identity *identity.FullIdentity, db sate
return satellite.NewAPI(log, identity, db, pointerDB, revocationDB, liveAccounting, rollupsWriteCache, &config, versionInfo, nil)
}
func (planet *Planet) newAdmin(count int, identity *identity.FullIdentity, db satellite.DB, config satellite.Config, versionInfo version.Info) (*satellite.Admin, error) {
func (planet *Planet) newAdmin(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, config satellite.Config, versionInfo version.Info) (*satellite.Admin, error) {
prefix := "satellite-admin" + strconv.Itoa(count)
log := planet.log.Named(prefix)
return satellite.NewAdmin(log, identity, db, versionInfo, &config, nil)
}
func (planet *Planet) newRepairer(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.Repairer, error) {
func (planet *Planet) newRepairer(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.Repairer, error) {
prefix := "satellite-repairer" + strconv.Itoa(count)
log := planet.log.Named(prefix)
revocationDB, err := revocation.NewDBFromCfg(config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config)
if err != nil {
return nil, errs.Wrap(err)
}
@ -807,11 +809,11 @@ func (cache rollupsWriteCacheCloser) Close() error {
return cache.RollupsWriteCache.CloseAndFlush(context.TODO())
}
func (planet *Planet) newGarbageCollection(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.GarbageCollection, error) {
func (planet *Planet) newGarbageCollection(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.GarbageCollection, error) {
prefix := "satellite-gc" + strconv.Itoa(count)
log := planet.log.Named(prefix)
revocationDB, err := revocation.NewDBFromCfg(config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config)
if err != nil {
return nil, errs.Wrap(err)
}

View File

@ -72,6 +72,8 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod
}
for i := 0; i < count; i++ {
ctx := context.TODO()
prefix := "storage" + strconv.Itoa(i)
log := planet.log.Named(prefix)
storageDir := filepath.Join(planet.directory, prefix)
@ -207,7 +209,7 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod
}
}
revocationDB, err := revocation.NewDBFromCfg(config.Server.Config)
revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config)
if err != nil {
return xs, errs.Wrap(err)
}
@ -221,7 +223,7 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod
// Mark the peer's PieceDeleter as in testing mode, so it is easy to wait on the deleter
peer.Storage2.PieceDeleter.SetupTest()
err = db.MigrateToLatest(context.TODO())
err = db.MigrateToLatest(ctx)
if err != nil {
return nil, err
}

View File

@ -209,7 +209,7 @@ func TestDownloadFromUnresponsiveNode(t *testing.T) {
},
}
revocationDB, err := revocation.NewDBFromCfg(tlscfg)
revocationDB, err := revocation.OpenDBFromCfg(ctx, tlscfg)
require.NoError(t, err)
tlsOptions, err := tlsopts.NewOptions(storageNode.Identity, tlscfg, revocationDB)

View File

@ -27,7 +27,7 @@ func RunDBs(t *testing.T, test func(*testing.T, extensions.RevocationDB, storage
// Test using redis-backed revocation DB
dbURL := "redis://" + redis.Addr() + "?db=0"
db, err := revocation.NewDB(dbURL)
db, err := revocation.OpenDB(ctx, dbURL)
require.NoError(t, err)
defer ctx.Check(db.Close)
@ -39,7 +39,7 @@ func RunDBs(t *testing.T, test func(*testing.T, extensions.RevocationDB, storage
defer ctx.Cleanup()
// Test using bolt-backed revocation DB
db, err := revocation.NewDB("bolt://" + ctx.File("revocations.db"))
db, err := revocation.OpenDB(ctx, "bolt://"+ctx.File("revocations.db"))
require.NoError(t, err)
defer ctx.Check(db.Close)