Skip to content

Commit

Permalink
Merge pull request #7028 from damikag/refactor-mig-fetch-2
Browse files Browse the repository at this point in the history
Rename migInstancesState to migInstancesStateCount in gce cache
  • Loading branch information
k8s-ci-robot committed Jul 11, 2024
2 parents ed061a4 + 275c183 commit a5d04db
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
22 changes: 11 additions & 11 deletions cluster-autoscaler/cloudprovider/gce/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type GceCache struct {
machinesCache map[MachineTypeKey]MachineType
migTargetSizeCache map[GceRef]int64
migBaseNameCache map[GceRef]string
migInstancesStateCache map[GceRef]map[cloudprovider.InstanceState]int64
migInstancesStateCountCache map[GceRef]map[cloudprovider.InstanceState]int64
listManagedInstancesResultsCache map[GceRef]string
instanceTemplateNameCache map[GceRef]InstanceTemplateName
instanceTemplatesCache map[GceRef]*gce.InstanceTemplate
Expand All @@ -92,7 +92,7 @@ func NewGceCache() *GceCache {
machinesCache: map[MachineTypeKey]MachineType{},
migTargetSizeCache: map[GceRef]int64{},
migBaseNameCache: map[GceRef]string{},
migInstancesStateCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
migInstancesStateCountCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
listManagedInstancesResultsCache: map[GceRef]string{},
instanceTemplateNameCache: map[GceRef]InstanceTemplateName{},
instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{},
Expand Down Expand Up @@ -549,24 +549,24 @@ func (gc *GceCache) InvalidateAllListManagedInstancesResults() {
gc.listManagedInstancesResultsCache = make(map[GceRef]string)
}

// GetMigInstancesState returns instancesState for the given mig from cache.
func (gc *GceCache) GetMigInstancesState(migRef GceRef) (instanceState map[cloudprovider.InstanceState]int64, found bool) {
// GetMigInstancesStateCount returns counts of instances in different states for the given mig from cache.
func (gc *GceCache) GetMigInstancesStateCount(migRef GceRef) (instanceState map[cloudprovider.InstanceState]int64, found bool) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()
instanceState, found = gc.migInstancesStateCache[migRef]
instanceState, found = gc.migInstancesStateCountCache[migRef]
return
}

// SetMigInstancesState sets the InstancesState for a given mig in cache.
func (gc *GceCache) SetMigInstancesState(migRef GceRef, instanceState map[cloudprovider.InstanceState]int64) {
// SetMigInstancesStateCount sets the counts of instances in different states for a given mig in cache.
func (gc *GceCache) SetMigInstancesStateCount(migRef GceRef, instanceState map[cloudprovider.InstanceState]int64) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()
gc.migInstancesStateCache[migRef] = instanceState
gc.migInstancesStateCountCache[migRef] = instanceState
}

// InvalidateMigInstancesState invalidates all migInstancesStateCache entries.
func (gc *GceCache) InvalidateMigInstancesState() {
// InvalidateMigInstancesStateCount invalidates all migInstancesStateCountCache entries.
func (gc *GceCache) InvalidateMigInstancesStateCount() {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()
gc.migInstancesStateCache = make(map[GceRef]map[cloudprovider.InstanceState]int64)
gc.migInstancesStateCountCache = make(map[GceRef]map[cloudprovider.InstanceState]int64)
}
2 changes: 1 addition & 1 deletion cluster-autoscaler/cloudprovider/gce/gce_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func newTestGceManager(t *testing.T, testServerURL string, regional bool) *gceMa
instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{},
kubeEnvCache: map[GceRef]KubeEnv{},
migBaseNameCache: map[GceRef]string{},
migInstancesStateCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
migInstancesStateCountCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
listManagedInstancesResultsCache: map[GceRef]string{},
}
migLister := NewMigLister(cache)
Expand Down
30 changes: 15 additions & 15 deletions cluster-autoscaler/cloudprovider/gce/mig_info_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *cachingMigInfoProvider) RegenerateMigInstancesCache() error {
}

func (c *cachingMigInfoProvider) bulkListMigInstances() error {
c.cache.InvalidateMigInstancesState()
c.cache.InvalidateMigInstancesStateCount()
err := c.fillMigInfoCache()
if err != nil {
return err
Expand Down Expand Up @@ -221,11 +221,11 @@ func groupInstancesToMigs(instances []GceInstance) map[GceRef][]GceInstance {

func (c *cachingMigInfoProvider) isMigInstancesConsistent(mig Mig, migToInstances map[GceRef][]GceInstance) bool {
migRef := mig.GceRef()
state, found := c.cache.GetMigInstancesState(migRef)
instancesStateCount, found := c.cache.GetMigInstancesStateCount(migRef)
if !found {
return false
}
instanceCount := state[cloudprovider.InstanceRunning] + state[cloudprovider.InstanceCreating] + state[cloudprovider.InstanceDeleting]
instanceCount := instancesStateCount[cloudprovider.InstanceRunning] + instancesStateCount[cloudprovider.InstanceCreating] + instancesStateCount[cloudprovider.InstanceDeleting]

instances, found := migToInstances[migRef]
if !found && instanceCount > 0 {
Expand All @@ -236,11 +236,11 @@ func (c *cachingMigInfoProvider) isMigInstancesConsistent(mig Mig, migToInstance

func (c *cachingMigInfoProvider) isMigCreatingOrDeletingInstances(mig Mig) bool {
migRef := mig.GceRef()
state, found := c.cache.GetMigInstancesState(migRef)
instancesStateCount, found := c.cache.GetMigInstancesStateCount(migRef)
if !found {
return false
}
return state[cloudprovider.InstanceCreating] > 0 || state[cloudprovider.InstanceDeleting] > 0
return instancesStateCount[cloudprovider.InstanceCreating] > 0 || instancesStateCount[cloudprovider.InstanceDeleting] > 0
}

// updateMigInstancesCache updates the mig instances for each mig
Expand Down Expand Up @@ -477,7 +477,7 @@ func (c *cachingMigInfoProvider) fillMigInfoCache() error {
c.cache.SetMigTargetSize(zoneMigRef, zoneMig.TargetSize)
c.cache.SetMigBasename(zoneMigRef, zoneMig.BaseInstanceName)
c.cache.SetListManagedInstancesResults(zoneMigRef, zoneMig.ListManagedInstancesResults)
c.cache.SetMigInstancesState(zoneMigRef, createInstancesState(zoneMig.TargetSize, zoneMig.CurrentActions))
c.cache.SetMigInstancesStateCount(zoneMigRef, createInstancesStateCount(zoneMig.TargetSize, zoneMig.CurrentActions))

templateUrl, err := url.Parse(zoneMig.InstanceTemplate)
if err == nil {
Expand Down Expand Up @@ -564,20 +564,20 @@ func (c *cachingMigInfoProvider) GetListManagedInstancesResults(migRef GceRef) (
return listManagedInstancesResults, nil
}

func createInstancesState(targetSize int64, actionsSummary *gce.InstanceGroupManagerActionsSummary) map[cloudprovider.InstanceState]int64 {
func createInstancesStateCount(targetSize int64, actionsSummary *gce.InstanceGroupManagerActionsSummary) map[cloudprovider.InstanceState]int64 {
if actionsSummary == nil {
return nil
}
state := map[cloudprovider.InstanceState]int64{
stateCount := map[cloudprovider.InstanceState]int64{
cloudprovider.InstanceCreating: 0,
cloudprovider.InstanceDeleting: 0,
cloudprovider.InstanceRunning: 0,
}
state[getInstanceState("ABANDONING")] += actionsSummary.Abandoning
state[getInstanceState("CREATING")] += actionsSummary.Creating
state[getInstanceState("CREATING_WITHOUT_RETRIES")] += actionsSummary.CreatingWithoutRetries
state[getInstanceState("DELETING")] += actionsSummary.Deleting
state[getInstanceState("RECREATING")] += actionsSummary.Recreating
state[cloudprovider.InstanceRunning] = targetSize - state[cloudprovider.InstanceCreating]
return state
stateCount[getInstanceState("ABANDONING")] += actionsSummary.Abandoning
stateCount[getInstanceState("CREATING")] += actionsSummary.Creating
stateCount[getInstanceState("CREATING_WITHOUT_RETRIES")] += actionsSummary.CreatingWithoutRetries
stateCount[getInstanceState("DELETING")] += actionsSummary.Deleting
stateCount[getInstanceState("RECREATING")] += actionsSummary.Recreating
stateCount[cloudprovider.InstanceRunning] = targetSize - stateCount[cloudprovider.InstanceCreating]
return stateCount
}
28 changes: 14 additions & 14 deletions cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
migBaseNameCache: map[GceRef]string{},
listManagedInstancesResultsCache: map[GceRef]string{},
instanceTemplateNameCache: map[GceRef]InstanceTemplateName{},
migInstancesStateCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
migInstancesStateCountCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
},
fetchMigInstances: fetchMigInstancesConst(mig1Instances),
fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{mig1Igm}),
Expand All @@ -709,7 +709,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
migBaseNameCache: map[GceRef]string{},
listManagedInstancesResultsCache: map[GceRef]string{},
instanceTemplateNameCache: map[GceRef]InstanceTemplateName{},
migInstancesStateCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
migInstancesStateCountCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
},
fetchMigInstances: fetchMigInstancesConst(mig2Instances),
fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{mig2Igm}),
Expand All @@ -735,7 +735,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
migBaseNameCache: map[GceRef]string{},
listManagedInstancesResultsCache: map[GceRef]string{},
instanceTemplateNameCache: map[GceRef]InstanceTemplateName{},
migInstancesStateCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
migInstancesStateCountCache: map[GceRef]map[cloudprovider.InstanceState]int64{},
},
fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{mig2Igm}),
fetchAllInstances: fetchAllInstancesInZone(map[string][]GceInstance{"myzone2": {instance3, instance6}}),
Expand Down Expand Up @@ -1273,8 +1273,8 @@ func TestCreateInstancesState(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
state := createInstancesState(tc.targetSize, tc.actionSummary)
assert.Equal(t, tc.want, state)
stateCount := createInstancesStateCount(tc.targetSize, tc.actionSummary)
assert.Equal(t, tc.want, stateCount)
})
}
}
Expand Down Expand Up @@ -1742,7 +1742,7 @@ func TestIsMigInstancesConsistent(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cache := GceCache{
migInstancesStateCache: tc.migInstancesStateCache,
migInstancesStateCountCache: tc.migInstancesStateCache,
}
provider := &cachingMigInfoProvider{
cache: &cache,
Expand Down Expand Up @@ -1805,7 +1805,7 @@ func TestIsMigInCreatingOrDeletingInstanceState(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cache := GceCache{
migInstancesStateCache: tc.migInstancesStateCache,
migInstancesStateCountCache: tc.migInstancesStateCache,
}
provider := &cachingMigInfoProvider{
cache: &cache,
Expand Down Expand Up @@ -1878,12 +1878,12 @@ func TestUpdateMigInstancesCache(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cache := GceCache{
migs: tc.migs,
instances: make(map[GceRef][]GceInstance),
instancesUpdateTime: make(map[GceRef]time.Time),
migBaseNameCache: make(map[GceRef]string),
migInstancesStateCache: tc.migInstancesStateCache,
instancesToMig: make(map[GceRef]GceRef),
migs: tc.migs,
instances: make(map[GceRef][]GceInstance),
instancesUpdateTime: make(map[GceRef]time.Time),
migBaseNameCache: make(map[GceRef]string),
migInstancesStateCountCache: tc.migInstancesStateCache,
instancesToMig: make(map[GceRef]GceRef),
}
migLister := NewMigLister(&cache)
client := &mockAutoscalingGceClient{
Expand Down Expand Up @@ -1921,7 +1921,7 @@ func emptyCache() *GceCache {
instancesUpdateTime: make(map[GceRef]time.Time),
migTargetSizeCache: make(map[GceRef]int64),
migBaseNameCache: make(map[GceRef]string),
migInstancesStateCache: make(map[GceRef]map[cloudprovider.InstanceState]int64),
migInstancesStateCountCache: make(map[GceRef]map[cloudprovider.InstanceState]int64),
listManagedInstancesResultsCache: make(map[GceRef]string),
instanceTemplateNameCache: make(map[GceRef]InstanceTemplateName),
instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate),
Expand Down

0 comments on commit a5d04db

Please sign in to comment.