From 45317a6979761858e69d88d12715f66b0225ac22 Mon Sep 17 00:00:00 2001 From: Nic Cope Date: Fri, 17 Nov 2017 20:23:37 -0800 Subject: [PATCH] Reintroduce the cache of instances known not to be owned by any ASG This now needs to be invalidated whenever we register a new ASG, as that ASG may include instances that we previously cached as unowned. --- .../cloudprovider/aws/auto_scaling_groups.go | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go index fa19ab28104b..cfff13aaca03 100644 --- a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go +++ b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go @@ -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 @@ -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 { @@ -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 } } @@ -75,6 +78,7 @@ func (m *autoScalingGroups) Register(asg *Asg) bool { m.registeredAsgs = append(m.registeredAsgs, &asgInformation{ config: asg, }) + m.invalidateUnownedInstanceCache() return true } @@ -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 } @@ -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()