Skip to content
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

Evict recentlyAccepted blocks based on wall-clock time #3460

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions utils/window/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type window[T any] struct {
minSize int

// mutex for synchronization
lock sync.RWMutex
lock sync.Mutex
// elements in the window
elements buffer.Deque[node[T]]
}
Expand Down Expand Up @@ -77,8 +77,9 @@ func (w *window[T]) Add(value T) {

// Oldest returns the oldest element in the window.
func (w *window[T]) Oldest() (T, bool) {
w.lock.RLock()
defer w.lock.RUnlock()
w.lock.Lock()
defer w.lock.Unlock()
w.removeStaleNodes()

oldest, ok := w.elements.PeekLeft()
if !ok {
Expand All @@ -89,8 +90,9 @@ func (w *window[T]) Oldest() (T, bool) {

// Length returns the number of elements in the window.
func (w *window[T]) Length() int {
w.lock.RLock()
defer w.lock.RUnlock()
w.lock.Lock()
defer w.lock.Unlock()
w.removeStaleNodes()

return w.elements.Len()
}
Expand All @@ -100,13 +102,9 @@ func (w *window[T]) removeStaleNodes() {
// If we're beyond the expiry threshold, removeStaleNodes this node from our
// window. Nodes are guaranteed to be strictly increasing in entry time,
// so we can break this loop once we find the first non-stale one.
newest, ok := w.elements.PeekRight()
if !ok {
return
}
for w.elements.Len() > w.minSize {
oldest, ok := w.elements.PeekLeft()
if !ok || newest.entryTime.Sub(oldest.entryTime) <= w.ttl {
if !ok || w.clock.Time().Sub(oldest.entryTime) <= w.ttl {
return
}
_, _ = w.elements.PopLeft()
Expand Down
24 changes: 17 additions & 7 deletions utils/window/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestTTLAdd(t *testing.T) {
require.Equal(4, oldest)
}

// TestTTLLength tests that elements are not evicted on Length
// TestTTLLength tests that elements are evicted on Length
func TestTTLLength(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -156,10 +156,10 @@ func TestTTLLength(t *testing.T) {
clock.Set(start.Add(testTTL + time.Second))

// No more elements should be present in the window.
require.Equal(3, window.Length())
require.Equal(0, window.Length())
}

// TestTTLOldest tests that stale elements are not evicted on calling Oldest
// TestTTLOldest tests that stale elements are evicted on calling Oldest
func TestTTLOldest(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -188,15 +188,25 @@ func TestTTLOldest(t *testing.T) {
require.Equal(1, oldest)
require.Equal(3, window.elements.Len())

// Now we're one second past the ttl of 10 seconds as defined in testTTL,
// Now we're one second before the ttl of 10 seconds as defined in testTTL,
// so all existing elements shoud still exist.
clock.Set(start.Add(testTTL + time.Second))
// Add 4 to the window to make it:
// [1, 2, 3, 4]
clock.Set(start.Add(testTTL - time.Second))
window.Add(4)

// Now there should be three elements in the window
oldest, ok = window.Oldest()
require.True(ok)
require.Equal(1, oldest)
require.Equal(3, window.elements.Len())
require.Equal(4, window.elements.Len())

// Now we're one second past the ttl of the initial 3 elements
// call to oldest should now evict 1,2,3 and return 4.
clock.Set(start.Add(testTTL + time.Second))
oldest, ok = window.Oldest()
require.True(ok)
require.Equal(4, oldest)
require.Equal(1, window.elements.Len())
}

// Tests that we bound the amount of elements in the window
Expand Down
Loading