Skip to content

primitive stampede protection #3598

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
44 changes: 38 additions & 6 deletions pkg/controller/registry/resolver/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"slices"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -147,7 +148,7 @@ func (c *NamespacedOperatorCache) Error() error {

func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
const (
CachePopulateTimeout = time.Minute
cachePopulateTimeout = time.Minute
)

sources := c.sp.Sources(namespaces...)
Expand All @@ -169,7 +170,9 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
if snapshot.Valid() {
result.snapshots[key] = snapshot
} else {
misses = append(misses, key)
if !snapshot.RequestSentinelActive() {
misses = append(misses, key)
}
}
}()
}
Expand Down Expand Up @@ -209,19 +212,35 @@ func (c *Cache) Namespaced(namespaces ...string) MultiCatalogOperatorFinder {
}
misses = misses[found:]

// remove any with a "live" outstanding request
misses = slices.DeleteFunc(misses, func(key SourceKey) bool {
hdr := c.snapshots[key]

// if we already have a request timestamp, we have an outstanding request, so prevent stacking
// and just send new requests if the previous one has expired
if hdr != nil && hdr.RequestSentinelActive() {
c.logger.Printf("Skipping new request for %s, already in progress", key)
return true
}
return false
})

for _, miss := range misses {
ctx, cancel := context.WithTimeout(context.Background(), CachePopulateTimeout)
ctx, cancel := context.WithTimeout(context.Background(), cachePopulateTimeout)

hdr := snapshotHeader{
key: miss,
pop: cancel,
priority: c.sourcePriorityProvider.Priority(miss),
key: miss,
pop: cancel,
priority: c.sourcePriorityProvider.Priority(miss),
requestSentinel: time.Now().Add(cachePopulateTimeout), // set sentinel to prevent stacking requests
}

hdr.m.Lock()
c.snapshots[miss] = &hdr
result.snapshots[miss] = &hdr

// don't adjust the request sentinel in the goroutine for any outcome, so that we don't stampede sources
// instead, reevaluate the sentinel during the next snapshot
go func(ctx context.Context, hdr *snapshotHeader, source Source) {
defer hdr.m.Unlock()
c.sem <- struct{}{}
Expand Down Expand Up @@ -294,6 +313,8 @@ type snapshotHeader struct {
pop context.CancelFunc
err error
priority int

requestSentinel time.Time
}

func (hdr *snapshotHeader) Cancel() {
Expand All @@ -314,6 +335,17 @@ func (hdr *snapshotHeader) Valid() bool {
return true
}

func (hdr *snapshotHeader) RequestSentinelActive() bool {
// hdr.m.RLock()
// defer hdr.m.RUnlock()
if hdr == nil {
return false
}


return time.Now().Before(hdr.requestSentinel)
}

type sortableSnapshots struct {
snapshots []*snapshotHeader
preferredNamespace string
Expand Down
Loading