-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provider/Azure: Add VM cache. #2683
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,16 @@ import ( | |
schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo" | ||
) | ||
|
||
var ( | ||
vmInstancesRefreshPeriod = 5 * time.Minute | ||
) | ||
|
||
var virtualMachinesStatusCache struct { | ||
lastRefresh time.Time | ||
mutex sync.Mutex | ||
virtualMachines []compute.VirtualMachine | ||
} | ||
|
||
// AgentPool implements NodeGroup interface for agent pools deployed by aks-engine. | ||
type AgentPool struct { | ||
azureRef | ||
|
@@ -117,9 +127,32 @@ func (as *AgentPool) MaxSize() int { | |
return as.maxSize | ||
} | ||
|
||
func (as *AgentPool) getVirtualMachinesFromCache() ([]compute.VirtualMachine, error) { | ||
virtualMachinesStatusCache.mutex.Lock() | ||
defer virtualMachinesStatusCache.mutex.Unlock() | ||
|
||
if virtualMachinesStatusCache.lastRefresh.Add(vmInstancesRefreshPeriod).After(time.Now()) { | ||
return virtualMachinesStatusCache.virtualMachines, nil | ||
} | ||
|
||
vms, err := as.GetVirtualMachines() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should check err before the cache update. or else, the cache may be cleaned on errors |
||
if err != nil { | ||
if isAzureRequestsThrottled(err) { | ||
klog.Warningf("getAllVirtualMachines: throttling with message %v, would return the cached vms", err) | ||
return virtualMachinesStatusCache.virtualMachines, nil | ||
} | ||
|
||
return []compute.VirtualMachine{}, err | ||
} | ||
virtualMachinesStatusCache.virtualMachines = vms | ||
virtualMachinesStatusCache.lastRefresh = time.Now() | ||
|
||
return vms, err | ||
} | ||
|
||
// GetVMIndexes gets indexes of all virtual machines belonging to the agent pool. | ||
func (as *AgentPool) GetVMIndexes() ([]int, map[int]string, error) { | ||
instances, err := as.GetVirtualMachines() | ||
instances, err := as.getVirtualMachinesFromCache() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
@@ -266,7 +299,7 @@ func (as *AgentPool) DecreaseTargetSize(delta int) error { | |
as.mutex.Lock() | ||
defer as.mutex.Unlock() | ||
|
||
nodes, err := as.GetVirtualMachines() | ||
nodes, err := as.getVirtualMachinesFromCache() | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -391,7 +424,7 @@ func (as *AgentPool) TemplateNodeInfo() (*schedulernodeinfo.NodeInfo, error) { | |
|
||
// Nodes returns a list of all nodes that belong to this node group. | ||
func (as *AgentPool) Nodes() ([]cloudprovider.Instance, error) { | ||
instances, err := as.GetVirtualMachines() | ||
instances, err := as.getVirtualMachinesFromCache() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there're three places of such codes, let's extract a new func for getting VMs from cache and refresh if the cache is outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finished.