Merge pull request #130925 from serathius/watchcache-snapshotter-interface

Create Snapshotter interface to fake the implementation
This commit is contained in:
Kubernetes Prow Robot 2025-03-19 10:19:38 -07:00 committed by GitHub
commit f9b27edf39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 3 deletions

View File

@ -435,6 +435,16 @@ func newStoreSnapshotter() *storeSnapshotter {
return s return s
} }
var _ Snapshotter = (*storeSnapshotter)(nil)
type Snapshotter interface {
Reset()
GetLessOrEqual(rv uint64) (orderedLister, bool)
Add(rv uint64, indexer orderedLister)
RemoveLess(rv uint64)
Len() int
}
type storeSnapshotter struct { type storeSnapshotter struct {
mux sync.RWMutex mux sync.RWMutex
snapshots *btree.BTreeG[rvSnapshot] snapshots *btree.BTreeG[rvSnapshot]
@ -486,3 +496,10 @@ func (s *storeSnapshotter) RemoveLess(rv uint64) {
s.snapshots.DeleteMin() s.snapshots.DeleteMin()
} }
} }
func (s *storeSnapshotter) Len() int {
s.mux.RLock()
defer s.mux.RUnlock()
return s.snapshots.Len()
}

View File

@ -137,3 +137,22 @@ func (f fakeOrderedLister) ListPrefix(prefixKey, continueKey string) []interface
return nil return nil
} }
func (f fakeOrderedLister) Count(prefixKey, continueKey string) int { return 0 } func (f fakeOrderedLister) Count(prefixKey, continueKey string) int { return 0 }
type fakeSnapshotter struct {
getLessOrEqual func(rv uint64) (orderedLister, bool)
}
var _ Snapshotter = (*fakeSnapshotter)(nil)
func (f *fakeSnapshotter) Reset() {}
func (f *fakeSnapshotter) GetLessOrEqual(rv uint64) (orderedLister, bool) {
if f.getLessOrEqual == nil {
return nil, false
}
return f.getLessOrEqual(rv)
}
func (f *fakeSnapshotter) Add(rv uint64, indexer orderedLister) {}
func (f *fakeSnapshotter) RemoveLess(rv uint64) {}
func (f *fakeSnapshotter) Len() int {
return 0
}

View File

@ -154,7 +154,7 @@ type watchCache struct {
waitingUntilFresh *progress.ConditionalProgressRequester waitingUntilFresh *progress.ConditionalProgressRequester
// Stores previous snapshots of orderedLister to allow serving requests from previous revisions. // Stores previous snapshots of orderedLister to allow serving requests from previous revisions.
snapshots *storeSnapshotter snapshots Snapshotter
} }
func newWatchCache( func newWatchCache(

View File

@ -1363,7 +1363,7 @@ func TestCacheSnapshots(t *testing.T) {
clock.Step(DefaultEventFreshDuration + 1) clock.Step(DefaultEventFreshDuration + 1)
require.NoError(t, store.Update(makeTestPod("foo", 500))) require.NoError(t, store.Update(makeTestPod("foo", 500)))
assert.Equal(t, 1, store.capacity) assert.Equal(t, 1, store.capacity)
assert.Equal(t, 1, store.snapshots.snapshots.Len()) assert.Equal(t, 1, store.snapshots.Len())
_, found = store.snapshots.GetLessOrEqual(499) _, found = store.snapshots.GetLessOrEqual(499)
assert.False(t, found, "Expected overfilled cache to delete events below 500") assert.False(t, found, "Expected overfilled cache to delete events below 500")
@ -1377,7 +1377,7 @@ func TestCacheSnapshots(t *testing.T) {
t.Log("Add event to force capacity upsize") t.Log("Add event to force capacity upsize")
require.NoError(t, store.Update(makeTestPod("foo", 600))) require.NoError(t, store.Update(makeTestPod("foo", 600)))
assert.Equal(t, 2, store.capacity) assert.Equal(t, 2, store.capacity)
assert.Equal(t, 2, store.snapshots.snapshots.Len()) assert.Equal(t, 2, store.snapshots.Len())
t.Log("Test cache on rev 600") t.Log("Test cache on rev 600")
lister, found = store.snapshots.GetLessOrEqual(600) lister, found = store.snapshots.GetLessOrEqual(600)