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:
160 files changed
+4369
-3164
No files matched your search
+49
-25
@@ -2,6 +2,7 @@ package oplog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/go-dbw"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@@ -24,29 +26,30 @@ type Queue struct {
|
||||
mx sync.Mutex
|
||||
}
|
||||
|
||||
// Add message to queue. typeName defines the type of message added to the
|
||||
// add message to queue. typeName defines the type of message added to the
|
||||
// queue and allows the msg to be removed using a TypeCatalog with a
|
||||
// coresponding typeName entry. OpType defines the msg's operation (create, add,
|
||||
// update, etc). If OpType == OpType_OP_TYPE_UPDATE, the WithFieldMaskPaths()
|
||||
// and SetToNullPaths() options are supported.
|
||||
func (q *Queue) Add(m proto.Message, typeName string, t OpType, opt ...Option) error {
|
||||
func (q *Queue) add(ctx context.Context, m proto.Message, typeName string, t OpType, opt ...Option) error {
|
||||
const op = "oplog.(Queue).Add"
|
||||
// we're not checking the Catalog for nil, since it's not used
|
||||
// when Adding messages to the queue
|
||||
opts := GetOpts(opt...)
|
||||
withFieldMasks := opts[optionWithFieldMaskPaths].([]string)
|
||||
withNullPaths := opts[optionWithSetToNullPaths].([]string)
|
||||
withOperationOptions := opts[optionWithOperationOptions].([]dbw.Option)
|
||||
|
||||
if _, ok := m.(ReplayableMessage); !ok {
|
||||
return errors.NewDeprecated(errors.InvalidParameter, op, fmt.Sprintf("%T is not a replayable message", m))
|
||||
return errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("%T is not a replayable message", m))
|
||||
}
|
||||
value, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
return errors.NewDeprecated(errors.Encode, op, "error marshaling add parameter", errors.WithWrap(err))
|
||||
return errors.New(ctx, errors.Encode, op, "error marshaling add parameter", errors.WithWrap(err))
|
||||
}
|
||||
if t == OpType_OP_TYPE_UPDATE {
|
||||
if len(withFieldMasks) == 0 && len(withNullPaths) == 0 {
|
||||
return errors.NewDeprecated(errors.InvalidParameter, op, "missing field masks or null paths for update")
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "missing field masks or null paths for update")
|
||||
}
|
||||
fMasks := withFieldMasks
|
||||
if fMasks == nil {
|
||||
@@ -58,12 +61,19 @@ func (q *Queue) Add(m proto.Message, typeName string, t OpType, opt ...Option) e
|
||||
}
|
||||
i, _, _, err := common.Intersection(fMasks, nullPaths)
|
||||
if err != nil {
|
||||
return errors.WrapDeprecated(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if len(i) != 0 {
|
||||
return errors.NewDeprecated(errors.InvalidParameter, op, fmt.Sprintf("field masks and null paths intersect with: %s", i))
|
||||
return errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("field masks and null paths intersect with: %s", i))
|
||||
}
|
||||
}
|
||||
var operationOpts *OperationOptions
|
||||
if len(withOperationOptions) > 0 {
|
||||
dbwOptions := dbw.GetOpts(withOperationOptions...)
|
||||
operationOpts, err = convertFromDbwOpts(ctx, dbwOptions)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
}
|
||||
msg := &AnyOperation{
|
||||
TypeName: typeName,
|
||||
@@ -71,61 +81,70 @@ func (q *Queue) Add(m proto.Message, typeName string, t OpType, opt ...Option) e
|
||||
OperationType: t,
|
||||
FieldMask: &field_mask.FieldMask{Paths: withFieldMasks},
|
||||
NullMask: &field_mask.FieldMask{Paths: withNullPaths},
|
||||
Options: operationOpts,
|
||||
}
|
||||
data, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return errors.NewDeprecated(errors.Encode, op, "error marshaling message", errors.WithWrap(err))
|
||||
return errors.New(ctx, errors.Encode, op, "error marshaling message", errors.WithWrap(err))
|
||||
}
|
||||
q.mx.Lock()
|
||||
defer q.mx.Unlock()
|
||||
err = binary.Write(q, binary.LittleEndian, uint32(len(data)))
|
||||
if err != nil {
|
||||
return errors.NewDeprecated(errors.Io, op, "binary write error", errors.WithWrap(err))
|
||||
return errors.New(ctx, errors.Io, op, "binary write error", errors.WithWrap(err))
|
||||
}
|
||||
n, err := q.Write(data)
|
||||
if err != nil {
|
||||
return errors.NewDeprecated(errors.Io, op, "error writing to queue buffer", errors.WithWrap(err))
|
||||
return errors.New(ctx, errors.Io, op, "error writing to queue buffer", errors.WithWrap(err))
|
||||
}
|
||||
if n != len(data) {
|
||||
return errors.NewDeprecated(errors.Io, op, fmt.Sprintf("error writing to queue buffer (incorrect number of bytes %d of %d)", n, len(data)))
|
||||
return errors.New(ctx, errors.Io, op, fmt.Sprintf("error writing to queue buffer (incorrect number of bytes %d of %d)", n, len(data)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove pb message from the queue and EOF if empty. It also returns the OpType
|
||||
type queueEntry struct {
|
||||
msg proto.Message
|
||||
opType OpType
|
||||
fieldMask []string
|
||||
setToNullPaths []string
|
||||
operationOptions *OperationOptions
|
||||
}
|
||||
|
||||
// remove pb message from the queue and EOF if empty. It also returns the OpType
|
||||
// for the msg and if it's OpType_OP_TYPE_UPDATE, the it will also return the
|
||||
// fieldMask and setToNullPaths for the update operation.
|
||||
func (q *Queue) Remove() (proto.Message, OpType, []string, []string, error) {
|
||||
func (q *Queue) remove(ctx context.Context) (*queueEntry, error) {
|
||||
const op = "oplog.(Queue).Remove"
|
||||
if q.Catalog == nil {
|
||||
return nil, OpType_OP_TYPE_UNSPECIFIED, nil, nil, errors.NewDeprecated(errors.InvalidParameter, op, "nil catalog")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil catalog")
|
||||
}
|
||||
q.mx.Lock()
|
||||
defer q.mx.Unlock()
|
||||
var n uint32
|
||||
err := binary.Read(q, binary.LittleEndian, &n)
|
||||
if err == io.EOF {
|
||||
return nil, 0, nil, nil, err // intentionally not wrapping error, return io.EOF so client can handle it correctly
|
||||
return nil, err // intentionally not wrapping error, return io.EOF so client can handle it correctly
|
||||
}
|
||||
if err != nil {
|
||||
return nil, 0, nil, nil, errors.NewDeprecated(errors.Io, op, "binary read error", errors.WithWrap(err))
|
||||
return nil, errors.New(ctx, errors.Io, op, "binary read error", errors.WithWrap(err))
|
||||
}
|
||||
data := q.Next(int(n))
|
||||
msg := new(AnyOperation)
|
||||
err = proto.Unmarshal(data, msg)
|
||||
if err != nil {
|
||||
return nil, 0, nil, nil, errors.NewDeprecated(errors.Decode, op, "error unmarshaling message", errors.WithWrap(err))
|
||||
return nil, errors.New(ctx, errors.Decode, op, "error unmarshaling message", errors.WithWrap(err))
|
||||
}
|
||||
if msg.Value == nil {
|
||||
return nil, 0, nil, nil, nil
|
||||
return nil, nil
|
||||
}
|
||||
any, err := q.Catalog.Get(msg.TypeName)
|
||||
any, err := q.Catalog.Get(ctx, msg.TypeName)
|
||||
if err != nil {
|
||||
return nil, 0, nil, nil, errors.WrapDeprecated(err, op, errors.WithMsg(fmt.Sprintf("error getting the TypeName: %s", msg.TypeName)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("error getting the TypeName: %s", msg.TypeName)))
|
||||
}
|
||||
pm := any.(proto.Message)
|
||||
if err = proto.Unmarshal(msg.Value, pm); err != nil {
|
||||
return nil, 0, nil, nil, errors.NewDeprecated(errors.Decode, op, "error unmarshaling value", errors.WithWrap(err))
|
||||
return nil, errors.New(ctx, errors.Decode, op, "error unmarshaling value", errors.WithWrap(err))
|
||||
}
|
||||
var masks, nullPaths []string
|
||||
if msg.OperationType == OpType_OP_TYPE_UPDATE {
|
||||
@@ -136,9 +155,14 @@ func (q *Queue) Remove() (proto.Message, OpType, []string, []string, error) {
|
||||
nullPaths = msg.NullMask.GetPaths()
|
||||
}
|
||||
if len(masks) == 0 && len(nullPaths) == 0 {
|
||||
return nil, 0, nil, nil, errors.NewDeprecated(errors.InvalidParameter, op, "field mask or null paths is required")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "field mask or null paths is required")
|
||||
}
|
||||
|
||||
}
|
||||
return pm, msg.OperationType, masks, nullPaths, nil
|
||||
return &queueEntry{
|
||||
msg: pm,
|
||||
opType: msg.OperationType,
|
||||
fieldMask: masks,
|
||||
setToNullPaths: nullPaths,
|
||||
operationOptions: msg.GetOptions(),
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user