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

TopologyAwareHints: Take lock in HasPopulatedHints #118189

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions staging/src/k8s.io/endpointslice/topologycache/topologycache.go
Expand Up @@ -153,8 +153,10 @@ func (t *TopologyCache) AddHints(logger klog.Logger, si *SliceInfo) ([]*discover
return slicesToCreate, slicesToUpdate, events
}

hintsEnabled := t.hintsPopulatedByService.Has(si.ServiceKey)
t.SetHints(si.ServiceKey, si.AddressType, allocatedHintsByZone)
t.lock.Lock()
defer t.lock.Unlock()
hintsEnabled := t.hasPopulatedHintsLocked(si.ServiceKey)
t.setHintsLocked(si.ServiceKey, si.AddressType, allocatedHintsByZone)

// if hints were not enabled before, we publish an event to indicate we enabled them.
if !hintsEnabled {
Expand All @@ -174,6 +176,10 @@ func (t *TopologyCache) SetHints(serviceKey string, addrType discovery.AddressTy
t.lock.Lock()
defer t.lock.Unlock()

t.setHintsLocked(serviceKey, addrType, allocatedHintsByZone)
}

func (t *TopologyCache) setHintsLocked(serviceKey string, addrType discovery.AddressType, allocatedHintsByZone EndpointZoneInfo) {
_, ok := t.endpointsByService[serviceKey]
if !ok {
t.endpointsByService[serviceKey] = map[discovery.AddressType]EndpointZoneInfo{}
Expand Down Expand Up @@ -262,6 +268,13 @@ func (t *TopologyCache) SetNodes(logger klog.Logger, nodes []*v1.Node) {

// HasPopulatedHints checks whether there are populated hints for a given service in the cache.
func (t *TopologyCache) HasPopulatedHints(serviceKey string) bool {
t.lock.Lock()
defer t.lock.Unlock()

return t.hasPopulatedHintsLocked(serviceKey)
}

func (t *TopologyCache) hasPopulatedHintsLocked(serviceKey string) bool {
return t.hintsPopulatedByService.Has(serviceKey)
}

Expand Down
Expand Up @@ -690,6 +690,9 @@ func TestTopologyCacheRace(t *testing.T) {
go func() {
cache.AddHints(logger, sliceInfo)
}()
go func() {
cache.HasPopulatedHints(sliceInfo.ServiceKey)
}()
}

// Test Helpers
Expand Down