Skip to content

Commit

Permalink
Merge pull request kubernetes#120324 from Miciah/automated-cherry-pic…
Browse files Browse the repository at this point in the history
…k-of-#118189-origin-release-1.26

Automated cherry pick of kubernetes#118189: TopologyAwareHints: Take lock in HasPopulatedHints
  • Loading branch information
k8s-ci-robot committed Sep 7, 2023
2 parents a1ae926 + 53994ce commit ce63847
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/controller/endpointslice/topologycache/topologycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ func (t *TopologyCache) AddHints(si *SliceInfo) ([]*discovery.EndpointSlice, []*
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 @@ -175,6 +177,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 @@ -263,6 +269,13 @@ func (t *TopologyCache) SetNodes(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
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ func TestTopologyCacheRace(t *testing.T) {
go func() {
cache.AddHints(sliceInfo)
}()
go func() {
cache.HasPopulatedHints(sliceInfo.ServiceKey)
}()
}

// Test Helpers
Expand Down

0 comments on commit ce63847

Please sign in to comment.