-
Notifications
You must be signed in to change notification settings - Fork 515
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
[sotw][linear] Fix missing watch cleanup in linear cache for sotw watches subscribing to multiple resources #854
Closed
Closed
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
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 |
---|---|---|
|
@@ -27,7 +27,7 @@ import ( | |
"github.com/envoyproxy/go-control-plane/pkg/server/stream/v3" | ||
) | ||
|
||
type watches = map[chan Response]struct{} | ||
type watches = map[ResponseWatch]struct{} | ||
|
||
// LinearCache supports collections of opaque resources. This cache has a | ||
// single collection indexed by resource names and manages resource versions | ||
|
@@ -113,7 +113,7 @@ func NewLinearCache(typeURL string, opts ...LinearCacheOption) *LinearCache { | |
return out | ||
} | ||
|
||
func (cache *LinearCache) respond(value chan Response, staleResources []string) { | ||
func (cache *LinearCache) respond(watch ResponseWatch, staleResources []string) { | ||
var resources []types.ResourceWithTTL | ||
// TODO: optimize the resources slice creations across different clients | ||
if len(staleResources) == 0 { | ||
|
@@ -130,8 +130,8 @@ func (cache *LinearCache) respond(value chan Response, staleResources []string) | |
} | ||
} | ||
} | ||
value <- &RawResponse{ | ||
Request: &Request{TypeUrl: cache.typeURL}, | ||
watch.Response <- &RawResponse{ | ||
Request: watch.Request, | ||
Resources: resources, | ||
Version: cache.getVersion(), | ||
Ctx: context.Background(), | ||
|
@@ -140,18 +140,18 @@ func (cache *LinearCache) respond(value chan Response, staleResources []string) | |
|
||
func (cache *LinearCache) notifyAll(modified map[string]struct{}) { | ||
// de-duplicate watches that need to be responded | ||
notifyList := make(map[chan Response][]string) | ||
notifyList := make(map[ResponseWatch][]string) | ||
for name := range modified { | ||
for watch := range cache.watches[name] { | ||
notifyList[watch] = append(notifyList[watch], name) | ||
} | ||
delete(cache.watches, name) | ||
} | ||
for value, stale := range notifyList { | ||
cache.respond(value, stale) | ||
for watch, stale := range notifyList { | ||
cache.removeWatch(watch) | ||
cache.respond(watch, stale) | ||
} | ||
for value := range cache.watchAll { | ||
cache.respond(value, nil) | ||
for watch := range cache.watchAll { | ||
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. So 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. I can update that in a separate PR |
||
cache.respond(watch, nil) | ||
} | ||
cache.watchAll = make(watches) | ||
|
||
|
@@ -318,6 +318,8 @@ func (cache *LinearCache) CreateWatch(request *Request, _ stream.StreamState, va | |
err = errors.New("mis-matched version prefix") | ||
} | ||
|
||
watch := ResponseWatch{Request: request, Response: value} | ||
|
||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
|
||
|
@@ -337,16 +339,16 @@ func (cache *LinearCache) CreateWatch(request *Request, _ stream.StreamState, va | |
} | ||
} | ||
if stale { | ||
cache.respond(value, staleResources) | ||
cache.respond(watch, staleResources) | ||
return nil | ||
} | ||
// Create open watches since versions are up to date. | ||
if len(request.GetResourceNames()) == 0 { | ||
cache.watchAll[value] = struct{}{} | ||
cache.watchAll[watch] = struct{}{} | ||
return func() { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
delete(cache.watchAll, value) | ||
delete(cache.watchAll, watch) | ||
} | ||
} | ||
for _, name := range request.GetResourceNames() { | ||
|
@@ -355,19 +357,24 @@ func (cache *LinearCache) CreateWatch(request *Request, _ stream.StreamState, va | |
set = make(watches) | ||
cache.watches[name] = set | ||
} | ||
set[value] = struct{}{} | ||
set[watch] = struct{}{} | ||
} | ||
return func() { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
for _, name := range request.GetResourceNames() { | ||
set, exists := cache.watches[name] | ||
if exists { | ||
delete(set, value) | ||
} | ||
if len(set) == 0 { | ||
delete(cache.watches, name) | ||
} | ||
cache.removeWatch(watch) | ||
} | ||
} | ||
|
||
// Must be called under lock | ||
func (cache *LinearCache) removeWatch(watch ResponseWatch) { | ||
// Make sure we clean the watch for ALL resources it might be associated with, | ||
// as the channel will no longer be listened to | ||
for _, resource := range watch.Request.ResourceNames { | ||
resourceWatches := cache.watches[resource] | ||
delete(resourceWatches, watch) | ||
if len(resourceWatches) == 0 { | ||
delete(cache.watches, resource) | ||
} | ||
} | ||
} | ||
|
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
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.
This comment threw me off a bit when reading the code. It's not de-duplicating iiuc (there are no duplicates), it is constructing a mapping of watches -> list of resources names, no?
For linear cache/SOTW, here the list of strings in the mapping should simply be
request.ResourceNames
, no? Maybe it'd be clearer to just use this, since you have the request at heand in the ResponseWatch?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.
It is deduplicating through the map to send a simple reply with n resources rather than n replies with one
On whether to use request.ResourceNames, this would yield a different behavior as:
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 don't think it makes a difference in theory, the request contains all the resources you may need to respond to, so
modified
and what is inrequest.ResourceNames