-
Notifications
You must be signed in to change notification settings - Fork 74
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
fix: group.Set() not updating hot cache for all nodes #69
Open
xWiiLLz
wants to merge
4
commits into
mailgun:master
Choose a base branch
from
clinia:fix/hot-cache-set
base: master
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.
+750
−192
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -272,21 +272,78 @@ func (g *Group) Set(ctx context.Context, key string, value []byte, expire time.T | |
} | ||
|
||
_, err := g.setGroup.Do(key, func() (interface{}, error) { | ||
wg := sync.WaitGroup{} | ||
errs := make(chan error) | ||
|
||
// If remote peer owns this key | ||
owner, ok := g.peers.PickPeer(key) | ||
if ok { | ||
if err := g.setFromPeer(ctx, owner, key, value, expire); err != nil { | ||
if err := g.setFromPeer(ctx, owner, key, value, expire, false); err != nil { | ||
return nil, err | ||
} | ||
// TODO(thrawn01): Not sure if this is useful outside of tests... | ||
// maybe we should ALWAYS update the local cache? | ||
if hotCache { | ||
g.localSet(key, value, expire, &g.hotCache) | ||
if !hotCache { | ||
return nil, nil | ||
} | ||
return nil, nil | ||
|
||
g.localSet(key, value, expire, &g.hotCache) | ||
|
||
for _, peer := range g.peers.GetAll() { | ||
if peer == owner { | ||
// Avoid setting to owner a second time | ||
continue | ||
} | ||
wg.Add(1) | ||
go func(peer ProtoGetter) { | ||
errs <- g.setFromPeer(ctx, peer, key, value, expire, true) | ||
wg.Done() | ||
}(peer) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errs) | ||
}() | ||
|
||
var err error | ||
for e := range errs { | ||
if e != nil { | ||
err = errors.Join(err, e) | ||
} | ||
} | ||
|
||
return nil, err | ||
} | ||
// We own this key | ||
g.localSet(key, value, expire, &g.mainCache) | ||
|
||
if hotCache { | ||
// Also set to the hot cache of all peers | ||
|
||
for _, peer := range g.peers.GetAll() { | ||
wg.Add(1) | ||
go func(peer ProtoGetter) { | ||
errs <- g.setFromPeer(ctx, peer, key, value, expire, true) | ||
wg.Done() | ||
}(peer) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errs) | ||
}() | ||
|
||
var err error | ||
for e := range errs { | ||
if e != nil { | ||
err = errors.Join(err, e) | ||
} | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
}) | ||
return err | ||
|
@@ -329,11 +386,11 @@ func (g *Group) Remove(ctx context.Context, key string) error { | |
close(errs) | ||
}() | ||
|
||
// TODO(thrawn01): Should we report all errors? Reporting context | ||
// cancelled error for each peer doesn't make much sense. | ||
var err error | ||
for e := range errs { | ||
err = e | ||
if e != nil { | ||
err = errors.Join(err, e) | ||
} | ||
Comment on lines
+391
to
+393
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. If there were some errors but the last one yielded nil, the previous code would not return any error |
||
} | ||
|
||
return nil, err | ||
|
@@ -449,8 +506,8 @@ func (g *Group) getLocally(ctx context.Context, key string, dest Sink) (ByteView | |
|
||
func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (ByteView, error) { | ||
req := &pb.GetRequest{ | ||
Group: &g.name, | ||
Key: &key, | ||
Group: g.name, | ||
Key: key, | ||
} | ||
res := &pb.GetResponse{} | ||
err := peer.Get(ctx, req, res) | ||
|
@@ -459,8 +516,8 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) ( | |
} | ||
|
||
var expire time.Time | ||
if res.Expire != nil && *res.Expire != 0 { | ||
expire = time.Unix(*res.Expire/int64(time.Second), *res.Expire%int64(time.Second)) | ||
if res.Expire != 0 { | ||
expire = time.Unix(res.Expire/int64(time.Second), res.Expire%int64(time.Second)) | ||
if time.Now().After(expire) { | ||
return ByteView{}, errors.New("peer returned expired value") | ||
} | ||
|
@@ -473,24 +530,26 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) ( | |
return value, nil | ||
} | ||
|
||
func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, k string, v []byte, e time.Time) error { | ||
func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, k string, v []byte, e time.Time, hotCache bool) error { | ||
var expire int64 | ||
if !e.IsZero() { | ||
expire = e.UnixNano() | ||
} | ||
req := &pb.SetRequest{ | ||
Expire: &expire, | ||
Group: &g.name, | ||
Key: &k, | ||
Value: v, | ||
Expire: expire, | ||
Group: g.name, | ||
Key: k, | ||
Value: v, | ||
HotCache: hotCache, | ||
} | ||
|
||
return peer.Set(ctx, req) | ||
} | ||
|
||
func (g *Group) removeFromPeer(ctx context.Context, peer ProtoGetter, key string) error { | ||
req := &pb.GetRequest{ | ||
Group: &g.name, | ||
Key: &key, | ||
Group: g.name, | ||
Key: key, | ||
} | ||
return peer.Remove(ctx, req) | ||
} | ||
|
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
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.
I'd like to see this code deduplicated with its counterpart in the owner code path (line 324). Move to function?