-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Ensure that initial events are sorted for WatchList #120897
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package cacher | |
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"sync" | ||
|
||
"k8s.io/apimachinery/pkg/fields" | ||
|
@@ -114,9 +115,24 @@ func newCacheInterval(startIndex, endIndex int, indexer indexerFunc, indexValida | |
} | ||
} | ||
|
||
type sortableWatchCacheEvents []*watchCacheEvent | ||
|
||
func (s sortableWatchCacheEvents) Len() int { | ||
return len(s) | ||
} | ||
|
||
func (s sortableWatchCacheEvents) Less(i, j int) bool { | ||
return s[i].Key < s[j].Key | ||
} | ||
|
||
func (s sortableWatchCacheEvents) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
// newCacheIntervalFromStore is meant to handle the case of rv=0, such that the events | ||
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. nit: update the doc saying the output will be sorted. 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. done |
||
// returned by Next() need to be events from a List() done on the underlying store of | ||
// the watch cache. | ||
// The items returned in the interval will be sorted by Key. | ||
func newCacheIntervalFromStore(resourceVersion uint64, store cache.Indexer, getAttrsFunc attrFunc) (*watchCacheInterval, error) { | ||
buffer := &watchCacheIntervalBuffer{} | ||
allItems := store.List() | ||
|
@@ -140,6 +156,7 @@ func newCacheIntervalFromStore(resourceVersion uint64, store cache.Indexer, getA | |
} | ||
buffer.endIndex++ | ||
} | ||
sort.Sort(sortableWatchCacheEvents(buffer.buffer)) | ||
wojtek-t marked this conversation as resolved.
Show resolved
Hide resolved
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. nit: consider using the newer slices.Sort function, which runs faster. 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. @wojtek-t mind checking if 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 see that it's not used anywhere in the codebase yet - we should do better analysis and migrate more if it appears better. 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. ok, thanks. |
||
ci := &watchCacheInterval{ | ||
startIndex: 0, | ||
// Simulate that we already have all the events we're looking for. | ||
|
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.
is key =
namespace/name
?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's the etcd key [that etcd is using to sort, as well as we're using elsewhere:
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apiserver/pkg/storage/cacher/watch_cache.go#L479-L491
]
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.
OK - I think I found it -
kubernetes/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go
Line 1556 in a05db0d