mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #130925 from serathius/watchcache-snapshotter-interface
Create Snapshotter interface to fake the implementation
This commit is contained in:
commit
f9b27edf39
@ -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()
|
||||||
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
@ -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(
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user