fix: resolve small issues in db transactions (#5522)
* fix: use the db transaction reader and writer * fix: internal/server: use ctx when scanning rows * fix(host): pass db tx reader/writer into helper funcs * fix: more precise nil check against db.Reader & db.Writer --------- Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
1 parent
2d802fe3f5
commit
acc40e0d08
20 files changed
+139
-27
No files matched your search
@@ -4,6 +4,7 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/pagination"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
@@ -38,6 +39,8 @@ type options struct {
|
||||
withSecretsHmac []byte
|
||||
withStartPageAfterItem pagination.Item
|
||||
withWorkerFilter string
|
||||
WithReader db.Reader
|
||||
withWriter db.Writer
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
@@ -162,3 +165,12 @@ func WithWorkerFilter(wf string) Option {
|
||||
o.withWorkerFilter = wf
|
||||
}
|
||||
}
|
||||
|
||||
// WithReaderWriter is used to share the same database reader
|
||||
// and writer when executing sql within a transaction.
|
||||
func WithReaderWriter(r db.Reader, w db.Writer) Option {
|
||||
return func(o *options) {
|
||||
o.WithReader = r
|
||||
o.withWriter = w
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/pagination"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -113,4 +114,11 @@ func Test_GetOpts(t *testing.T) {
|
||||
testOpts.withWorkerFilter = `"test" in "/tags/type"`
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithReaderWriter", func(t *testing.T) {
|
||||
reader := &db.Db{}
|
||||
writer := &db.Db{}
|
||||
opts := getOpts(WithReaderWriter(reader, writer))
|
||||
assert.Equal(t, reader, opts.WithReader)
|
||||
assert.Equal(t, writer, opts.withWriter)
|
||||
})
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/host"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/libs/patchstruct"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -404,7 +405,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
func(read db.Reader, w db.Writer) error {
|
||||
msgs := make([]*oplog.Message, 0, 3)
|
||||
ticket, err := w.GetTicket(ctx, newCatalog)
|
||||
if err != nil {
|
||||
@@ -528,7 +529,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
if needSetSync {
|
||||
// We also need to mark all host sets in this catalog to be
|
||||
// synced as well.
|
||||
setsForCatalog, _, err := r.getSets(ctx, "", returnedCatalog.PublicId)
|
||||
setsForCatalog, _, err := r.getSets(ctx, "", returnedCatalog.PublicId, host.WithReaderWriter(read, w))
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get sets for host catalog"))
|
||||
}
|
||||
@@ -713,14 +714,19 @@ func (r *Repository) getCatalog(ctx context.Context, id string) (*HostCatalog, *
|
||||
return c, p, nil
|
||||
}
|
||||
|
||||
func (r *Repository) getPlugin(ctx context.Context, plgId string) (*plg.Plugin, error) {
|
||||
func (r *Repository) getPlugin(ctx context.Context, plgId string, opts ...Option) (*plg.Plugin, error) {
|
||||
const op = "plugin.(Repository).getPlugin"
|
||||
if plgId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no plugin id")
|
||||
}
|
||||
opt := getOpts(opts...)
|
||||
reader := r.reader
|
||||
if !util.IsNil(opt.WithReader) {
|
||||
reader = opt.WithReader
|
||||
}
|
||||
plg := plg.NewPlugin()
|
||||
plg.PublicId = plgId
|
||||
if err := r.reader.LookupByPublicId(ctx, plg); err != nil {
|
||||
if err := reader.LookupByPublicId(ctx, plg); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("unable to get host plugin with id %q", plgId)))
|
||||
}
|
||||
return plg, nil
|
||||
|
||||
@@ -804,6 +804,15 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
|
||||
limit = opts.WithLimit
|
||||
}
|
||||
|
||||
reader := r.reader
|
||||
writer := r.writer
|
||||
if !util.IsNil(opts.WithReader) {
|
||||
reader = opts.WithReader
|
||||
}
|
||||
if !util.IsNil(opts.WithWriter) {
|
||||
writer = opts.WithWriter
|
||||
}
|
||||
|
||||
args := make([]any, 0, 1)
|
||||
var where string
|
||||
|
||||
@@ -825,7 +834,7 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
|
||||
}
|
||||
|
||||
var aggHostSets []*hostSetAgg
|
||||
if err := r.reader.SearchWhere(ctx, &aggHostSets, where, args, dbArgs...); err != nil {
|
||||
if err := reader.SearchWhere(ctx, &aggHostSets, where, args, dbArgs...); err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", publicId)))
|
||||
}
|
||||
|
||||
@@ -844,7 +853,7 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
|
||||
}
|
||||
var plg *plugin.Plugin
|
||||
if plgId != "" {
|
||||
plg, err = r.getPlugin(ctx, plgId)
|
||||
plg, err = r.getPlugin(ctx, plgId, WithReaderWriter(reader, writer))
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user