Skip to content

DNM: test to get a better sense of call frequency #1009

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

Open
wants to merge 4 commits into
base: release-4.16
Choose a base branch
from
Open
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
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 Down Expand Up @@ -209,19 +210,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 +311,8 @@ type snapshotHeader struct {
pop context.CancelFunc
err error
priority int

requestSentinel time.Time
}

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

func (hdr *snapshotHeader) RequestSentinelActive() bool {
hdr.m.RLock()
defer hdr.m.RUnlock()

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

type sortableSnapshots struct {
snapshots []*snapshotHeader
preferredNamespace string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ type RegistrySourceProvider struct {
invalidator *sourceInvalidator
}

const defaultCacheLifetime time.Duration = 30 * time.Minute

func SourceProviderFromRegistryClientProvider(rcp RegistryClientProvider, catsrcLister v1alpha1listers.CatalogSourceLister, logger logrus.StdLogger) *RegistrySourceProvider {
return &RegistrySourceProvider{
rcp: rcp,
logger: logger,
catsrcLister: catsrcLister,
invalidator: &sourceInvalidator{
validChans: make(map[cache.SourceKey]chan struct{}),
ttl: 5 * time.Minute,
ttl: defaultCacheLifetime,
},
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.