-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f803a6b
Add optimistic concurrency header `If-Match` to GetObject, GetRelatio…
BogdanIrimie fd94370
Update request with metadata instead of reading object or relation fr…
BogdanIrimie cb06ba2
Improve variable naming.
BogdanIrimie da573da
Add If-Match header check to DeleteManifest.
BogdanIrimie c59afbd
Add IfNotMatch header to GetObject and GetRelation
BogdanIrimie daf0ef2
Add code comments for optimistic concurrency check.
BogdanIrimie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 { | ||
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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do a DB call only if |
||
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 | ||
} | ||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.