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
Memdb datastore MVCC improvements #319
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f13ae90
extract out change tracking across revisions from postgres
jakedt 3e33ff9
test that namespaces are removed from list when deleted
jakedt a1cef57
fix v1 watch test to not delete non-existent relationship
jakedt d6dadff
change memdb datastore to use a single transaction log for namespaces…
jakedt 9673916
use canonical terms for memdb datastore
jakedt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0" | ||
"github.com/rs/zerolog/log" | ||
"github.com/shopspring/decimal" | ||
|
||
"github.com/authzed/spicedb/internal/datastore" | ||
"github.com/authzed/spicedb/pkg/tuple" | ||
) | ||
|
||
// Changes represents a set of tuple mutations that are kept self-consistent | ||
// across one or more transaction revisions. | ||
type Changes map[uint64]*changeRecord | ||
|
||
type changeRecord struct { | ||
tupleTouches map[string]*v0.RelationTuple | ||
tupleDeletes map[string]*v0.RelationTuple | ||
} | ||
|
||
// NewChanges creates a new Changes object for change tracking and de-duplication. | ||
func NewChanges() Changes { | ||
return make(Changes) | ||
} | ||
|
||
// AddChange adds a specific change to the complete list of tracked changes | ||
func (ch Changes) AddChange( | ||
ctx context.Context, | ||
revTxID uint64, | ||
tpl *v0.RelationTuple, | ||
op v0.RelationTupleUpdate_Operation, | ||
) { | ||
revisionChanges, ok := ch[revTxID] | ||
if !ok { | ||
revisionChanges = &changeRecord{ | ||
tupleTouches: make(map[string]*v0.RelationTuple), | ||
tupleDeletes: make(map[string]*v0.RelationTuple), | ||
} | ||
ch[revTxID] = revisionChanges | ||
} | ||
|
||
tplKey := tuple.String(tpl) | ||
|
||
switch op { | ||
case v0.RelationTupleUpdate_TOUCH: | ||
// If there was a delete for the same tuple at the same revision, drop it | ||
delete(revisionChanges.tupleDeletes, tplKey) | ||
|
||
revisionChanges.tupleTouches[tplKey] = tpl | ||
|
||
case v0.RelationTupleUpdate_DELETE: | ||
_, alreadyTouched := revisionChanges.tupleTouches[tplKey] | ||
if !alreadyTouched { | ||
revisionChanges.tupleDeletes[tplKey] = tpl | ||
} | ||
default: | ||
log.Ctx(ctx).Fatal().Stringer("operation", op).Msg("unknown change operation") | ||
} | ||
} | ||
|
||
// AsRevisionChanges returns the list of changes processed so far as a datastore watch | ||
// compatible, ordered, changelist. | ||
func (ch Changes) AsRevisionChanges() (changes []*datastore.RevisionChanges) { | ||
revisionsWithChanges := make([]uint64, 0, len(ch)) | ||
for revTxID := range ch { | ||
revisionsWithChanges = append(revisionsWithChanges, revTxID) | ||
} | ||
sort.Slice(revisionsWithChanges, func(i int, j int) bool { | ||
return revisionsWithChanges[i] < revisionsWithChanges[j] | ||
}) | ||
|
||
for _, revTxID := range revisionsWithChanges { | ||
revisionChange := &datastore.RevisionChanges{ | ||
Revision: revisionFromTransactionID(revTxID), | ||
} | ||
|
||
revisionChangeRecord := ch[revTxID] | ||
for _, tpl := range revisionChangeRecord.tupleTouches { | ||
revisionChange.Changes = append(revisionChange.Changes, &v0.RelationTupleUpdate{ | ||
Operation: v0.RelationTupleUpdate_TOUCH, | ||
Tuple: tpl, | ||
}) | ||
} | ||
for _, tpl := range revisionChangeRecord.tupleDeletes { | ||
revisionChange.Changes = append(revisionChange.Changes, &v0.RelationTupleUpdate{ | ||
Operation: v0.RelationTupleUpdate_DELETE, | ||
Tuple: tpl, | ||
}) | ||
} | ||
changes = append(changes, revisionChange) | ||
} | ||
|
||
return | ||
} | ||
|
||
func revisionFromTransactionID(txID uint64) datastore.Revision { | ||
return decimal.NewFromInt(int64(txID)) | ||
} |
Oops, something went wrong.
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.
Why the difference between if I have a delete and then touch, we're left with just the touch, but if I have touch and then delete, both will be emitted?
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 won't, the test cases confirm this.