Skip to content

Commit

Permalink
perf: Reduce Memory Usage from the InstanceType Provider (#5318)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Dec 15, 2023
1 parent fe1b3c6 commit d71bcd8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ const (

const (
// DefaultCleanupInterval triggers cache cleanup (lazy eviction) at this interval.
DefaultCleanupInterval = 10 * time.Minute
DefaultCleanupInterval = time.Minute
)
14 changes: 9 additions & 5 deletions pkg/providers/instancetype/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func (p *Provider) List(ctx context.Context, kc *corev1beta1.KubeletConfiguratio
// Compute fully initialized instance types hash key
subnetHash, _ := hashstructure.Hash(subnets, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
kcHash, _ := hashstructure.Hash(kc, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
key := fmt.Sprintf("%d-%d-%d-%s-%016x-%016x", p.instanceTypesSeqNum, p.instanceTypeOfferingsSeqNum, p.unavailableOfferings.SeqNum, nodeClass.UID, subnetHash, kcHash)

blockDeviceMappingsHash, _ := hashstructure.Hash(nodeClass.Spec.BlockDeviceMappings, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
key := fmt.Sprintf("%d-%d-%d-%016x-%016x-%016x-%s", p.instanceTypesSeqNum, p.instanceTypeOfferingsSeqNum, p.unavailableOfferings.SeqNum, subnetHash, kcHash, blockDeviceMappingsHash, aws.StringValue(nodeClass.Spec.AMIFamily))
if item, ok := p.cache.Get(key); ok {
return item.([]*cloudprovider.InstanceType), nil
}
Expand Down Expand Up @@ -221,10 +221,12 @@ func (p *Provider) getInstanceTypeOfferings(ctx context.Context) (map[string]set
}); err != nil {
return nil, fmt.Errorf("describing instance type zone offerings, %w", err)
}
if p.cm.HasChanged("instance-type-count", len(instanceTypeOfferings)) {
if p.cm.HasChanged("instance-type-offering", instanceTypeOfferings) {
// Only update instanceTypesSeqNun with the instance type offerings have been changed
// This is to not create new keys with duplicate instance type offerings option
atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1)
logging.FromContext(ctx).With("instance-type-count", len(instanceTypeOfferings)).Debugf("discovered offerings for instance types")
}
atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1)
p.cache.SetDefault(InstanceTypeOfferingsCacheKey, instanceTypeOfferings)
return instanceTypeOfferings, nil
}
Expand Down Expand Up @@ -261,10 +263,12 @@ func (p *Provider) GetInstanceTypes(ctx context.Context) ([]*ec2.InstanceTypeInf
return nil, fmt.Errorf("fetching instance types using ec2.DescribeInstanceTypes, %w", err)
}
if p.cm.HasChanged("instance-types", instanceTypes) {
// Only update instanceTypesSeqNun with the instance types have been changed
// This is to not create new keys with duplicate instance types option
atomic.AddUint64(&p.instanceTypesSeqNum, 1)
logging.FromContext(ctx).With(
"count", len(instanceTypes)).Debugf("discovered instance types")
}
atomic.AddUint64(&p.instanceTypesSeqNum, 1)
p.cache.SetDefault(InstanceTypesCacheKey, instanceTypes)
return instanceTypes, nil
}

0 comments on commit d71bcd8

Please sign in to comment.