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

Reduce contention in cacher by eliminating sync.Pool #73846

Merged
merged 1 commit into from
Feb 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 20 additions & 17 deletions staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ type Cacher struct {
stopped bool
stopCh chan struct{}
stopWg sync.WaitGroup

// Used to avoid unnecessary allocations in underlying watchers.
timer *time.Timer
}

// NewCacherFromConfig creates a new Cacher responsible for servicing WATCH and LIST requests from
Expand Down Expand Up @@ -227,6 +230,7 @@ func NewCacherFromConfig(config Config) *Cacher {
// and there are no guarantees on the order that they will stop.
// So we will be simply closing the channel, and synchronizing on the WaitGroup.
stopCh: stopCh,
timer: time.NewTimer(time.Duration(0)),
}
watchCache.SetOnEvent(cacher.processEvent)
go cacher.dispatchEvents()
Expand All @@ -242,6 +246,14 @@ func NewCacherFromConfig(config Config) *Cacher {
}, time.Second, stopCh,
)
}()

// Ensure that timer is stopped.
if !cacher.timer.Stop() {
// Consume triggered (but not yet received) timer event
// so that future reuse does not get a spurious timeout.
<-cacher.timer.C
}

return cacher
}

Expand Down Expand Up @@ -621,13 +633,13 @@ func (c *Cacher) dispatchEvent(event *watchCacheEvent) {
defer c.Unlock()
// Iterate over "allWatchers" no matter what the trigger function is.
for _, watcher := range c.watchers.allWatchers {
watcher.add(event, c.dispatchTimeoutBudget)
watcher.add(event, c.timer, c.dispatchTimeoutBudget)
}
if supported {
// Iterate over watchers interested in the given values of the trigger.
for _, triggerValue := range triggerValues {
for _, watcher := range c.watchers.valueWatchers[triggerValue] {
watcher.add(event, c.dispatchTimeoutBudget)
watcher.add(event, c.timer, c.dispatchTimeoutBudget)
}
}
} else {
Expand All @@ -640,7 +652,7 @@ func (c *Cacher) dispatchEvent(event *watchCacheEvent) {
// Iterate over watchers interested in exact values for all values.
for _, watchers := range c.watchers.valueWatchers {
for _, watcher := range watchers {
watcher.add(event, c.dispatchTimeoutBudget)
watcher.add(event, c.timer, c.dispatchTimeoutBudget)
}
}
}
Expand Down Expand Up @@ -826,9 +838,7 @@ func (c *cacheWatcher) stop() {
}
}

var timerPool sync.Pool

func (c *cacheWatcher) add(event *watchCacheEvent, budget *timeBudget) {
func (c *cacheWatcher) add(event *watchCacheEvent, timer *time.Timer, budget *timeBudget) {
// Try to send the event immediately, without blocking.
select {
case c.input <- event:
Expand All @@ -842,23 +852,16 @@ func (c *cacheWatcher) add(event *watchCacheEvent, budget *timeBudget) {
startTime := time.Now()
timeout := budget.takeAvailable()

t, ok := timerPool.Get().(*time.Timer)
if ok {
t.Reset(timeout)
} else {
t = time.NewTimer(timeout)
}
defer timerPool.Put(t)
timer.Reset(timeout)

select {
case c.input <- event:
stopped := t.Stop()
if !stopped {
if !timer.Stop() {
// Consume triggered (but not yet received) timer event
// so that future reuse does not get a spurious timeout.
<-t.C
<-timer.C
}
case <-t.C:
case <-timer.C:
// This means that we couldn't send event to that watcher.
// Since we don't want to block on it infinitely,
// we simply terminate it.
Expand Down