refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785)

* chore: Add new dbw dependency

* refactor: Convert to use dbw.DB

* refactor (db): Change GetTicket(...) signature

* feature (db tests): Added TestDeleteWhere(...)
Adding this allows us to eliminate a leak of the gorm library

* refactor: Add ctx to ScanRows(...) signature
This is required to refactor all the deprecated errors pkg calls.
This refactor required adding a context to quite a few call chains
and in particular the job scheduler bits

* feature (db/tests): Add support for test opt: WithTestLogLevel(...)
This test only option allows callers to specify a log level for the
underlying db package (where supported/applicable) when calling
TestSetup(...)

* refactor (db): Simplify Db type
Only store the underlying *DB and create the *RW on demand when
needed.

* refactor (oplog): Port to using dbw pkg for db operations

* refactor (db): Remove gormDB(...) func

* tests (db): Add unit tests for TestDeleteWhere(...)

* chore: make fmt changes to formatting
This commit is contained in:
Jim authored and GitHub committed 2022-01-05 14:38:38 -05:00
1 parent 002d004114
commit 591ec476e7
160 files changed
+4369 -3164

No files matched your search

@@ -86,7 +86,7 @@ func (r *OrphanedHostCleanupJob) Run(ctx context.Context) error {
}
// NextRunIn returns the default run frequency of the cleanup job.
func (r *OrphanedHostCleanupJob) NextRunIn() (time.Duration, error) {
func (r *OrphanedHostCleanupJob) NextRunIn(_ context.Context) (time.Duration, error) {
const op = "plugin.(OrphanedHostCleanupJob).NextRunIn"
return orphanedHostCleanupJobRunInterval, nil
}
+6 -6
View File
@@ -115,9 +115,9 @@ func (r *SetSyncJob) Run(ctx context.Context) error {
}
// NextRunIn queries the plugin host set db to determine when the next set should be synced.
func (r *SetSyncJob) NextRunIn() (time.Duration, error) {
func (r *SetSyncJob) NextRunIn(ctx context.Context) (time.Duration, error) {
const op = "plugin.(SetSyncJob).NextRunIn"
next, err := nextSync(r)
next, err := nextSync(ctx, r)
if err != nil {
return setSyncJobRunInterval, errors.WrapDeprecated(err, op)
}
@@ -134,7 +134,7 @@ func (r *SetSyncJob) Description() string {
return "Periodically syncs plugin based catalog hosts and host set memberships."
}
func nextSync(j scheduler.Job) (time.Duration, error) {
func nextSync(ctx context.Context, j scheduler.Job) (time.Duration, error) {
const op = "plugin.nextSync"
var query string
var r db.Reader
@@ -162,7 +162,7 @@ func nextSync(j scheduler.Job) (time.Duration, error) {
ResyncIn time.Duration
}
var n NextResync
err = r.ScanRows(rows, &n)
err = r.ScanRows(ctx, rows, &n)
if err != nil {
return 0, errors.WrapDeprecated(err, op)
}
@@ -409,7 +409,7 @@ func (r *SetSyncJob) upsertAndCleanHosts(
db.ExpBackoff{},
func(r db.Reader, w db.Writer) error {
msgs := make([]*oplog.Message, 0)
ticket, err := w.GetTicket(ret)
ticket, err := w.GetTicket(ctx, ret)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
@@ -524,7 +524,7 @@ func (r *SetSyncJob) upsertAndCleanHosts(
func(r db.Reader, w db.Writer) error {
msgs := make([]*oplog.Message, 0)
ticket, err := w.GetTicket(hs)
ticket, err := w.GetTicket(ctx, hs)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
+1 -1
View File
@@ -525,7 +525,7 @@ func TestSetSyncJob_NextRunIn(t *testing.T) {
_, err = rw.Update(ctx, hostSet, fieldMaskPaths, setToNullPaths)
require.NoError(err)
got, err := r.NextRunIn()
got, err := r.NextRunIn(context.Background())
require.NoError(err)
// Round to five seconds to account for lost time between updating set and determining next run
assert.Equal(tt.want.Round(5*time.Second), got.Round(5*time.Second))
@@ -106,7 +106,7 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
msgs := make([]*oplog.Message, 0, 3)
ticket, err := w.GetTicket(c)
ticket, err := w.GetTicket(ctx, c)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
@@ -341,7 +341,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
msgs := make([]*oplog.Message, 0, 3)
ticket, err := w.GetTicket(newCatalog)
ticket, err := w.GetTicket(ctx, newCatalog)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
@@ -302,7 +302,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
assert.NoError(db.TestVerifyOplog(t, rw, got.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
cSecret := allocHostCatalogSecret()
err = rw.LookupWhere(ctx, &cSecret, "catalog_id=?", got.GetPublicId())
err = rw.LookupWhere(ctx, &cSecret, "catalog_id=?", []interface{}{got.GetPublicId()})
if tt.wantSecret == nil {
assert.Nil(got.Secrets)
require.Error(t, err)
@@ -608,7 +608,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
require := require.New(t)
cSecret := allocHostCatalogSecret()
err := dbRW.LookupWhere(ctx, &cSecret, "catalog_id=?", gotCatalog.GetPublicId())
err := dbRW.LookupWhere(ctx, &cSecret, "catalog_id=?", []interface{}{gotCatalog.GetPublicId()})
require.NoError(err)
require.Empty(cSecret.Secret)
require.NotEmpty(cSecret.CtSecret)
@@ -629,7 +629,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
assert := assert.New(t)
cSecret := allocHostCatalogSecret()
err := dbRW.LookupWhere(ctx, &cSecret, "catalog_id=?", gotCatalog.GetPublicId())
err := dbRW.LookupWhere(ctx, &cSecret, "catalog_id=?", []interface{}{gotCatalog.GetPublicId()})
assert.Error(err)
assert.True(errors.IsNotFoundError(err))
}
@@ -1453,10 +1453,12 @@ func (j *testSyncJob) Status() scheduler.JobStatus {
}
}
func (j *testSyncJob) Run(_ context.Context) error { return nil }
func (j *testSyncJob) NextRunIn() (time.Duration, error) { return setSyncJobRunInterval, nil }
func (j *testSyncJob) Name() string { return setSyncJobName }
func (j *testSyncJob) Description() string { return setSyncJobName }
func (j *testSyncJob) Run(_ context.Context) error { return nil }
func (j *testSyncJob) NextRunIn(_ context.Context) (time.Duration, error) {
return setSyncJobRunInterval, nil
}
func (j *testSyncJob) Name() string { return setSyncJobName }
func (j *testSyncJob) Description() string { return setSyncJobName }
func TestRepository_UpdateCatalog_SyncSets(t *testing.T) {
ctx := context.Background()
+2 -2
View File
@@ -117,7 +117,7 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
msgs := make([]*oplog.Message, 0, len(preferredEndpoints)+2)
ticket, err := w.GetTicket(s)
ticket, err := w.GetTicket(ctx, s)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
@@ -365,7 +365,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
func(reader db.Reader, w db.Writer) error {
returnedSet = newSet.clone()
msgs := make([]*oplog.Message, 0, len(preferredEndpoints)+len(currentSet.PreferredEndpoints)+2)
ticket, err := w.GetTicket(s)
ticket, err := w.GetTicket(ctx, s)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
+4 -4
View File
@@ -44,8 +44,8 @@ type hostInfo struct {
func createNewHostMap(ctx context.Context,
catalog *HostCatalog,
phs []*plgpb.ListHostsResponseHost,
currentHostMap map[string]*Host) (map[string]*hostInfo, error) {
currentHostMap map[string]*Host) (map[string]*hostInfo, error,
) {
const op = "plugin.createNewHostMap"
newHostMap := make(map[string]*hostInfo, len(phs))
@@ -200,8 +200,8 @@ func getSetChanges(
currentHostMap map[string]*Host,
newHostMap map[string]*hostInfo) (
setMembershipsToAdd, setMembershipsToRemove map[string][]string,
allSetIds map[string]struct{}) {
allSetIds map[string]struct{},
) {
// First, find sets that hosts should be added to: hosts that are new or
// have new set IDs returned.
for newHostId, newHost := range newHostMap {