Skip to content

Commit 09e8598

Browse files
committed
apiserver/storage/cacher: cache supports pagination
1 parent 29defc1 commit 09e8598

File tree

2 files changed

+92
-7
lines changed

2 files changed

+92
-7
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,26 @@ func shouldDelegateList(opts storage.ListOptions) bool {
768768
consistentReadFromStorage := resourceVersion == "" && !(consistentListFromCacheEnabled && requestWatchProgressSupported)
769769
// Watch cache doesn't support continuations, so serve them from etcd.
770770
hasContinuation := len(pred.Continue) > 0
771-
// Serve paginated requests about revision "0" from watch cache to avoid overwhelming etcd.
772-
hasLimit := pred.Limit > 0 && resourceVersion != "0"
773771
// Watch cache only supports ResourceVersionMatchNotOlderThan (default).
774-
unsupportedMatch := match != "" && match != metav1.ResourceVersionMatchNotOlderThan
772+
// see https://kubernetes.io/docs/reference/using-api/api-concepts/#semantics-for-get-and-list
773+
isLegacyExactMatch := opts.Predicate.Limit > 0 && match == "" && len(resourceVersion) > 0 && resourceVersion != "0"
774+
unsupportedMatch := match != "" && match != metav1.ResourceVersionMatchNotOlderThan || isLegacyExactMatch
775775

776-
return consistentReadFromStorage || hasContinuation || hasLimit || unsupportedMatch
776+
return consistentReadFromStorage || hasContinuation || unsupportedMatch
777+
}
778+
779+
// computeListLimit determines whether the cacher should
780+
// apply a limit to an incoming LIST request and returns its value.
781+
//
782+
// note that this function doesn't check RVM nor the Continuation token.
783+
// these parameters are validated by the shouldDelegateList function.
784+
//
785+
// as of today, the limit is ignored for requests that set RV == 0
786+
func computeListLimit(opts storage.ListOptions) int64 {
787+
if opts.Predicate.Limit <= 0 || opts.ResourceVersion == "0" {
788+
return 0
789+
}
790+
return opts.Predicate.Limit
777791
}
778792

779793
func shouldDelegateListOnNotReadyCache(opts storage.ListOptions) bool {
@@ -883,13 +897,21 @@ func (c *Cacher) GetList(ctx context.Context, key string, opts storage.ListOptio
883897
// the elements in ListObject are Struct type, making slice will bring excessive memory consumption.
884898
// so we try to delay this action as much as possible
885899
var selectedObjects []runtime.Object
886-
for _, obj := range objs {
900+
var lastSelectedObjectKey string
901+
var hasMoreListItems bool
902+
limit := computeListLimit(opts)
903+
for i, obj := range objs {
887904
elem, ok := obj.(*storeElement)
888905
if !ok {
889906
return fmt.Errorf("non *storeElement returned from storage: %v", obj)
890907
}
891908
if filter(elem.Key, elem.Labels, elem.Fields) {
892909
selectedObjects = append(selectedObjects, elem.Object)
910+
lastSelectedObjectKey = elem.Key
911+
}
912+
if limit > 0 && int64(len(selectedObjects)) >= limit {
913+
hasMoreListItems = i < len(objs)-1
914+
break
893915
}
894916
}
895917
if len(selectedObjects) == 0 {
@@ -905,7 +927,12 @@ func (c *Cacher) GetList(ctx context.Context, key string, opts storage.ListOptio
905927
}
906928
span.AddEvent("Filtered items", attribute.Int("count", listVal.Len()))
907929
if c.versioner != nil {
908-
if err := c.versioner.UpdateList(listObj, readResourceVersion, "", nil); err != nil {
930+
continueValue, remainingItemCount, err := storage.PrepareContinueToken(lastSelectedObjectKey, key, int64(readResourceVersion), int64(len(objs)), hasMoreListItems, opts)
931+
if err != nil {
932+
return err
933+
}
934+
935+
if err = c.versioner.UpdateList(listObj, readResourceVersion, continueValue, remainingItemCount); err != nil {
909936
return err
910937
}
911938
}

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ func TestGetListCacheBypass(t *testing.T) {
201201
{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
202202
{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
203203

204-
{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
205204
{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false},
206205
{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
207206

@@ -214,6 +213,7 @@ func TestGetListCacheBypass(t *testing.T) {
214213
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false)
215214
testCases := append(commonTestCases,
216215
testCase{opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: true},
216+
testCase{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
217217
)
218218
for _, tc := range testCases {
219219
testGetListCacheBypass(t, tc.opts, tc.expectBypass)
@@ -233,6 +233,7 @@ func TestGetListCacheBypass(t *testing.T) {
233233

234234
testCases := append(commonTestCases,
235235
testCase{opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: false},
236+
testCase{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false},
236237
)
237238
for _, tc := range testCases {
238239
testGetListCacheBypass(t, tc.opts, tc.expectBypass)
@@ -2591,6 +2592,63 @@ func TestWatchStreamSeparation(t *testing.T) {
25912592
}
25922593
}
25932594

2595+
func TestComputeListLimit(t *testing.T) {
2596+
scenarios := []struct {
2597+
name string
2598+
opts storage.ListOptions
2599+
expectedLimit int64
2600+
}{
2601+
{
2602+
name: "limit is zero",
2603+
opts: storage.ListOptions{
2604+
Predicate: storage.SelectionPredicate{
2605+
Limit: 0,
2606+
},
2607+
},
2608+
expectedLimit: 0,
2609+
},
2610+
{
2611+
name: "limit is positive, RV is unset",
2612+
opts: storage.ListOptions{
2613+
Predicate: storage.SelectionPredicate{
2614+
Limit: 1,
2615+
},
2616+
ResourceVersion: "",
2617+
},
2618+
expectedLimit: 1,
2619+
},
2620+
{
2621+
name: "limit is positive, RV = 100",
2622+
opts: storage.ListOptions{
2623+
Predicate: storage.SelectionPredicate{
2624+
Limit: 1,
2625+
},
2626+
ResourceVersion: "100",
2627+
},
2628+
expectedLimit: 1,
2629+
},
2630+
{
2631+
name: "legacy case: limit is positive, RV = 0",
2632+
opts: storage.ListOptions{
2633+
Predicate: storage.SelectionPredicate{
2634+
Limit: 1,
2635+
},
2636+
ResourceVersion: "0",
2637+
},
2638+
expectedLimit: 0,
2639+
},
2640+
}
2641+
2642+
for _, scenario := range scenarios {
2643+
t.Run(scenario.name, func(t *testing.T) {
2644+
actualLimit := computeListLimit(scenario.opts)
2645+
if actualLimit != scenario.expectedLimit {
2646+
t.Errorf("computeListLimit returned = %v, expected %v", actualLimit, scenario.expectedLimit)
2647+
}
2648+
})
2649+
}
2650+
}
2651+
25942652
func watchAndWaitForBookmark(t *testing.T, ctx context.Context, etcdStorage storage.Interface) func() (resourceVersion uint64) {
25952653
opts := storage.ListOptions{ResourceVersion: "", Predicate: storage.Everything, Recursive: true}
25962654
opts.Predicate.AllowWatchBookmarks = true

0 commit comments

Comments
 (0)