Skip to content

Commit

Permalink
Reintroduce the cache of instances known not to be owned by any ASG
Browse files Browse the repository at this point in the history
This now needs to be invalidated whenever we register a new ASG, as that
ASG may include instances that we previously cached as unowned.
  • Loading branch information
Nic Cope committed Nov 18, 2017
1 parent 0cdf2fc commit 45317a6
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const scaleToZeroSupported = false
type autoScalingGroups struct {
registeredAsgs []*asgInformation
instanceToAsg map[AwsRef]*Asg
notInRegisteredAsg map[AwsRef]bool
registeredAsgsMutex sync.RWMutex
instanceToAsgMutex sync.Mutex
service autoScalingWrapper
Expand All @@ -41,10 +42,11 @@ type autoScalingGroups struct {

func newAutoScalingGroups(service autoScalingWrapper) (*autoScalingGroups, error) {
registry := &autoScalingGroups{
registeredAsgs: make([]*asgInformation, 0),
service: service,
instanceToAsg: make(map[AwsRef]*Asg),
interrupt: make(chan struct{}),
registeredAsgs: make([]*asgInformation, 0),
service: service,
instanceToAsg: make(map[AwsRef]*Asg),
notInRegisteredAsg: make(map[AwsRef]bool),
interrupt: make(chan struct{}),
}
go wait.Until(func() {
if err := registry.regenerateCache(); err != nil {
Expand All @@ -67,6 +69,7 @@ func (m *autoScalingGroups) Register(asg *Asg) bool {
}
m.registeredAsgs[i].config = asg
glog.V(4).Infof("Updated ASG %s", asg.AwsRef.Name)
m.invalidateUnownedInstanceCache()
return true
}
}
Expand All @@ -75,6 +78,7 @@ func (m *autoScalingGroups) Register(asg *Asg) bool {
m.registeredAsgs = append(m.registeredAsgs, &asgInformation{
config: asg,
})
m.invalidateUnownedInstanceCache()
return true
}

Expand Down Expand Up @@ -110,6 +114,12 @@ func (m *autoScalingGroups) FindForInstance(instance *AwsRef) (*Asg, error) {
m.instanceToAsgMutex.Lock()
defer m.instanceToAsgMutex.Unlock()

if m.notInRegisteredAsg[*instance] {
// We already know we don't own this instance. Return early and avoid
// additional calls to describe ASGs.
return nil, nil
}

if config, found := m.instanceToAsg[*instance]; found {
return config, nil
}
Expand All @@ -119,9 +129,18 @@ func (m *autoScalingGroups) FindForInstance(instance *AwsRef) (*Asg, error) {
if config, found := m.instanceToAsg[*instance]; found {
return config, nil
}

m.notInRegisteredAsg[*instance] = true
return nil, nil
}

func (m *autoScalingGroups) invalidateUnownedInstanceCache() {
glog.V(4).Info("Invalidating unowned instance cache")
m.instanceToAsgMutex.Lock()
m.notInRegisteredAsg = make(map[AwsRef]bool)
m.instanceToAsgMutex.Unlock()
}

func (m *autoScalingGroups) regenerateCache() error {
m.instanceToAsgMutex.Lock()
defer m.instanceToAsgMutex.Unlock()
Expand Down

0 comments on commit 45317a6

Please sign in to comment.