Skip to content

Commit

Permalink
mig_info_provider.go:fillMigInstances will now use locking when calli…
Browse files Browse the repository at this point in the history
…ng the gce api.

This is to avoid multiple gce calls for the same mig during scale down (which is done in parallel).
  • Loading branch information
jayantjain93 committed Feb 3, 2022
1 parent 0d52c16 commit a906da2
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions cluster-autoscaler/cloudprovider/gce/mig_info_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ type MigInfoProvider interface {
}

type cachingMigInfoProvider struct {
mutex sync.Mutex
migInfoMutex sync.Mutex
cache *GceCache
migLister MigLister
gceClient AutoscalingGceClient
projectId string
concurrentGceRefreshes int
migInstanceMutex sync.Mutex
}

// NewCachingMigInfoProvider creates an instance of caching MigInfoProvider
Expand Down Expand Up @@ -85,6 +86,9 @@ func (c *cachingMigInfoProvider) GetMigInstances(migRef GceRef) ([]cloudprovider

// GetMigForInstance returns MIG ref for a given instance
func (c *cachingMigInfoProvider) GetMigForInstance(instanceRef GceRef) (Mig, error) {
c.migInstanceMutex.Lock()
defer c.migInstanceMutex.Unlock()

mig, found, err := c.getCachedMigForInstance(instanceRef)
if found {
return mig, err
Expand Down Expand Up @@ -160,8 +164,8 @@ func (c *cachingMigInfoProvider) fillMigInstances(migRef GceRef) error {
}

func (c *cachingMigInfoProvider) GetMigTargetSize(migRef GceRef) (int64, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.migInfoMutex.Lock()
defer c.migInfoMutex.Unlock()

targetSize, found := c.cache.GetMigTargetSize(migRef)
if found {
Expand All @@ -185,8 +189,8 @@ func (c *cachingMigInfoProvider) GetMigTargetSize(migRef GceRef) (int64, error)
}

func (c *cachingMigInfoProvider) GetMigBasename(migRef GceRef) (string, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.migInfoMutex.Lock()
defer c.migInfoMutex.Unlock()

basename, found := c.cache.GetMigBasename(migRef)
if found {
Expand All @@ -210,8 +214,8 @@ func (c *cachingMigInfoProvider) GetMigBasename(migRef GceRef) (string, error) {
}

func (c *cachingMigInfoProvider) GetMigInstanceTemplateName(migRef GceRef) (string, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.migInfoMutex.Lock()
defer c.migInfoMutex.Unlock()

templateName, found := c.cache.GetMigInstanceTemplateName(migRef)
if found {
Expand Down Expand Up @@ -240,8 +244,8 @@ func (c *cachingMigInfoProvider) GetMigInstanceTemplate(migRef GceRef) (*gce.Ins
return nil, err
}

c.mutex.Lock()
defer c.mutex.Unlock()
c.migInfoMutex.Lock()
defer c.migInfoMutex.Unlock()

template, found := c.cache.GetMigInstanceTemplate(migRef)
if found && template.Name == templateName {
Expand All @@ -257,7 +261,7 @@ func (c *cachingMigInfoProvider) GetMigInstanceTemplate(migRef GceRef) (*gce.Ins
return template, nil
}

// filMigInfoCache needs to be called with mutex locked
// filMigInfoCache needs to be called with migInfoMutex locked
func (c *cachingMigInfoProvider) fillMigInfoCache() error {
var zones []string
for zone := range c.listAllZonesWithMigs() {
Expand Down

0 comments on commit a906da2

Please sign in to comment.