-
Notifications
You must be signed in to change notification settings - Fork 224
Make optimistic version locking optional #856
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
Draft
sgtsquiggs
wants to merge
2
commits into
redis:main
Choose a base branch
from
sgtsquiggs:verless-om
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+804
−45
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -67,11 +67,19 @@ func (r *HashRepository[T]) FetchCache(ctx context.Context, id string, ttl time. | |
| return v, err | ||
| } | ||
|
|
||
| func (r *HashRepository[T]) toExec(entity *T) (val reflect.Value, exec rueidis.LuaExec) { | ||
| val = reflect.ValueOf(entity).Elem() | ||
| func (r *HashRepository[T]) toExec(entity *T) (verf reflect.Value, exec rueidis.LuaExec) { | ||
| val := reflect.ValueOf(entity).Elem() | ||
| if !r.schema.verless { | ||
| verf = val.Field(r.schema.ver.idx) | ||
| } else { | ||
| verf = reflect.ValueOf(int64(0)) // verless, set verf to a dummy value | ||
| } | ||
| fields := r.factory.NewConverter(val).ToHash() | ||
| keyVal := fields[r.schema.key.name] | ||
| verVal := fields[r.schema.ver.name] | ||
| var verVal string | ||
| if !r.schema.verless { | ||
| verVal = fields[r.schema.ver.name] | ||
| } | ||
| extVal := int64(0) | ||
| if r.schema.ext != nil { | ||
| if ext, ok := val.Field(r.schema.ext.idx).Interface().(time.Time); ok && !ext.IsZero() { | ||
|
|
@@ -98,34 +106,42 @@ func (r *HashRepository[T]) toExec(entity *T) (val reflect.Value, exec rueidis.L | |
| // Save the entity under the redis key of `{prefix}:{id}`. | ||
| // It also uses the `redis:",ver"` field and lua script to perform optimistic locking and prevent lost update. | ||
| func (r *HashRepository[T]) Save(ctx context.Context, entity *T) (err error) { | ||
| val, exec := r.toExec(entity) | ||
| verf, exec := r.toExec(entity) | ||
| str, err := hashSaveScript.Exec(ctx, r.client, exec.Keys, exec.Args).ToString() | ||
| if rueidis.IsRedisNil(err) { | ||
| if r.schema.verless { | ||
| return nil | ||
| } | ||
| return ErrVersionMismatch | ||
| } | ||
| if err == nil { | ||
| } else if err == nil { | ||
| ver, _ := strconv.ParseInt(str, 10, 64) | ||
| val.Field(r.schema.ver.idx).SetInt(ver) | ||
| verf.SetInt(ver) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // SaveMulti batches multiple HashRepository.Save at once | ||
| func (r *HashRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error { | ||
| errs := make([]error, len(entities)) | ||
| vals := make([]reflect.Value, len(entities)) | ||
| verf := make([]reflect.Value, len(entities)) | ||
| exec := make([]rueidis.LuaExec, len(entities)) | ||
| for i, entity := range entities { | ||
| vals[i], exec[i] = r.toExec(entity) | ||
| verf[i], exec[i] = r.toExec(entity) | ||
| } | ||
| for i, resp := range hashSaveScript.ExecMulti(ctx, r.client, exec...) { | ||
| if str, err := resp.ToString(); err != nil { | ||
| if errs[i] = err; rueidis.IsRedisNil(err) { | ||
| errs[i] = ErrVersionMismatch | ||
| str, err := resp.ToString() | ||
| if rueidis.IsRedisNil(err) { | ||
| if r.schema.verless { | ||
| continue | ||
|
Comment on lines
+134
to
+135
Collaborator
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. Same here. Can we delete this? |
||
| } | ||
| } else { | ||
| errs[i] = ErrVersionMismatch | ||
| continue | ||
| } | ||
| if err == nil { | ||
| ver, _ := strconv.ParseInt(str, 10, 64) | ||
| vals[i].Field(r.schema.ver.idx).SetInt(ver) | ||
| verf[i].SetInt(ver) | ||
| } else { | ||
| errs[i] = err | ||
| } | ||
| } | ||
| return errs | ||
|
|
@@ -274,6 +290,15 @@ func (r *HashRepository[T]) fromFields(fields map[string]string) (*T, error) { | |
| } | ||
|
|
||
| var hashSaveScript = rueidis.NewLuaScript(` | ||
| if (ARGV[1] == '') | ||
| then | ||
| local e = (#ARGV % 2 == 1) and table.remove(ARGV) or nil | ||
| if redis.call('HSET',KEYS[1],unpack(ARGV)) | ||
| then | ||
| if e then redis.call('PEXPIREAT',KEYS[1],e) end | ||
| end | ||
| return nil | ||
| end | ||
| local v = redis.call('HGET',KEYS[1],ARGV[1]) | ||
| if (not v or v == ARGV[2]) | ||
| then | ||
|
|
||
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.
When will this happen? If it never happens, can we just delete it?
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.
On the other hand, if the case does happen, we should not return nil either.
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.
I modified the script (here) to return nil if the ver arg is not present. Alternatively I could have used another script that does not perform optimistic locking, and forked based on r.schema.verless.
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.
Oh, can we align with the original behavior to return ARGV[2] as well?
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.
Absolutely. Wrote this PR quickly so I could fork and use
om- looking at this code today I don't remember a lot of it and it looks a little dodgy to me. I'll try to reimplement a bit cleaner this week.