Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concurrency headers #79

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions pkg/directory/v3/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/aserto-dev/go-directory/pkg/derr"
"github.com/aserto-dev/go-edge-ds/pkg/bdb"
"github.com/aserto-dev/go-edge-ds/pkg/ds"
"github.com/go-http-utils/headers"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/bufbuild/protovalidate-go"
Expand Down Expand Up @@ -46,6 +48,12 @@ func (s *Writer) SetObject(ctx context.Context, req *dsw3.SetObjectRequest) (*ds
return err
}

ifMatchHeader := metautils.ExtractIncoming(ctx).Get(headers.IfMatch)
// if the updReq.Etag == "" this means the this is an insert
if ifMatchHeader != "" && updReq.Etag != "" && ifMatchHeader != updReq.Etag {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition detects inserts because otherwise those would always be rejected with a HashMissmatch error.

return derr.ErrHashMismatch.Msgf("for object with type [%s] and id [%s]", updReq.Type, updReq.Id)
}

if etag == updReq.Etag {
s.logger.Trace().Str("key", ds.Object(req.Object).Key()).Str("etag-equal", etag).Msg("set_object")
resp.Result = updReq
Expand Down Expand Up @@ -74,15 +82,29 @@ func (s *Writer) DeleteObject(ctx context.Context, req *dsw3.DeleteObjectRequest
}

err := s.store.DB().Update(func(tx *bolt.Tx) error {
objIdent := &dsc3.ObjectIdentifier{ObjectType: req.GetObjectType(), ObjectId: req.GetObjectId()}
if err := bdb.Delete(ctx, tx, bdb.ObjectsPath, ds.ObjectIdentifier(objIdent).Key()); err != nil {
objIdent := ds.ObjectIdentifier(&dsc3.ObjectIdentifier{ObjectType: req.ObjectType, ObjectId: req.ObjectId})

ifMatchHeader := metautils.ExtractIncoming(ctx).Get(headers.IfMatch)
if ifMatchHeader != "" {
obj := &dsc3.Object{Type: req.ObjectType, Id: req.ObjectId}
updReq, err := bdb.UpdateMetadata(ctx, tx, bdb.ObjectsPath, ds.Object(obj).Key(), obj)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a DB call only if If-Match header is specified.

if err != nil {
return err
}

if ifMatchHeader != updReq.Etag {
return derr.ErrHashMismatch.Msgf("for object with type [%s] and id [%s]", updReq.Type, updReq.Id)
}
}

if err := bdb.Delete(ctx, tx, bdb.ObjectsPath, objIdent.Key()); err != nil {
return err
}

if req.GetWithRelations() {
{
// incoming object relations of object instance (result.type == incoming.subject.type && result.key == incoming.subject.key)
iter, err := bdb.NewScanIterator[dsc3.Relation](ctx, tx, bdb.RelationsSubPath, bdb.WithKeyFilter(ds.ObjectIdentifier(objIdent).Key()+ds.InstanceSeparator))
iter, err := bdb.NewScanIterator[dsc3.Relation](ctx, tx, bdb.RelationsSubPath, bdb.WithKeyFilter(objIdent.Key()+ds.InstanceSeparator))
if err != nil {
return err
}
Expand All @@ -100,7 +122,7 @@ func (s *Writer) DeleteObject(ctx context.Context, req *dsw3.DeleteObjectRequest
}
{
// outgoing object relations of object instance (result.type == outgoing.object.type && result.key == outgoing.object.key)
iter, err := bdb.NewScanIterator[dsc3.Relation](ctx, tx, bdb.RelationsObjPath, bdb.WithKeyFilter(ds.ObjectIdentifier(objIdent).Key()+ds.InstanceSeparator))
iter, err := bdb.NewScanIterator[dsc3.Relation](ctx, tx, bdb.RelationsObjPath, bdb.WithKeyFilter(objIdent.Key()+ds.InstanceSeparator))
if err != nil {
return err
}
Expand Down Expand Up @@ -142,6 +164,12 @@ func (s *Writer) SetRelation(ctx context.Context, req *dsw3.SetRelationRequest)
return err
}

ifMatchHeader := metautils.ExtractIncoming(ctx).Get(headers.IfMatch)
// if the updReq.Etag == "" this means the this is an insert
if ifMatchHeader != "" && updReq.Etag != "" && ifMatchHeader != updReq.Etag {
return derr.ErrHashMismatch.Msgf("for relation with objectType [%s], objectId [%s], relation [%s], subjectType [%s], SubjectId [%s]", updReq.ObjectType, updReq.ObjectId, updReq.Relation, updReq.SubjectType, updReq.SubjectId)
}

if etag == updReq.Etag {
s.logger.Trace().Str("key", ds.Relation(req.Relation).ObjKey()).Str("etag-equal", etag).Msg("set_relation")
resp.Result = updReq
Expand Down Expand Up @@ -175,14 +203,28 @@ func (s *Writer) DeleteRelation(ctx context.Context, req *dsw3.DeleteRelationReq
}

err := s.store.DB().Update(func(tx *bolt.Tx) error {
rel := ds.Relation(&dsc3.Relation{
protoRel := &dsc3.Relation{
ObjectType: req.ObjectType,
ObjectId: req.ObjectId,
Relation: req.Relation,
SubjectType: req.SubjectType,
SubjectId: req.SubjectId,
SubjectRelation: req.SubjectRelation,
})
}

rel := ds.Relation(protoRel)

ifMatchHeader := metautils.ExtractIncoming(ctx).Get(headers.IfMatch)
if ifMatchHeader != "" {
updReq, err := bdb.UpdateMetadata(ctx, tx, bdb.RelationsObjPath, ds.Relation(protoRel).ObjKey(), protoRel)
if err != nil {
return err
}

if ifMatchHeader != updReq.Etag {
return derr.ErrHashMismatch.Msgf("for relation with objectType [%s], objectId [%s], relation [%s], subjectType [%s], SubjectId [%s]", protoRel.ObjectType, protoRel.ObjectId, protoRel.Relation, protoRel.SubjectType, protoRel.SubjectId)
}
}

if err := bdb.Delete(ctx, tx, bdb.RelationsObjPath, rel.ObjKey()); err != nil {
return err
Expand Down