Skip to content
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

CA-384191: Fix missing values for min VM restrictions #3242

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions XenModel/XenAPI-Extensions/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ public int MinVCPUs()
/// <returns>The value if found. If not found, null is returned instead</returns>
private static T? GetRestrictionValueFromMatchingTemplate<T>(VM vm, string field, string attribute) where T : struct
{
if (!vm.is_a_template && !string.IsNullOrEmpty(vm.reference_label))
if(vm.is_a_template)
return GetRestrictionValue<T>(vm, field, attribute);

if (!string.IsNullOrEmpty(vm.reference_label))
{
var matchingTemplate = vm.Connection.Cache.VMs
.FirstOrDefault(v => v.is_a_template && v.reference_label == vm.reference_label);
Expand Down Expand Up @@ -288,6 +291,13 @@ public int MinVCPUs()
var xn = xd?.SelectSingleNode($@"restrictions/restriction[@field='{field}']");
var resultString = xn?.Attributes?[attribute]?.Value;
T? result = null;

// avoid the expensive operation of throwing an exception
if (string.IsNullOrEmpty(resultString))
{
return null;
}

try
{
var converter = TypeDescriptor.GetConverter(typeof(T));
Expand Down Expand Up @@ -320,6 +330,7 @@ public int MinVCPUs()
private static List<T> GetRestrictionValueAcrossTemplates<T>(IXenObject vm, string field, string attribute) where T : struct
{
return vm.Connection.Cache.VMs
.AsParallel()
.Where(v => v.is_a_template)
.Select(v => GetRestrictionValue<T>(v, field, attribute))
.Where(value => value != null)
Expand All @@ -343,7 +354,8 @@ private static T GetMaxRestrictionValue<T>(VM vm, string field, T defaultValue)
return (T) value;

var templateValues = GetRestrictionValueAcrossTemplates<T>(vm, field, "max");
return templateValues.Count == 0 ? defaultValue : templateValues.Max();
templateValues.Add(defaultValue);
return templateValues.Max();
}

/// <summary>
Expand All @@ -362,7 +374,8 @@ private static T GetMinRestrictionValue<T>(VM vm, string field, T defaultValue)
return (T)value;

var templateValues = GetRestrictionValueAcrossTemplates<T>(vm, field, "min");
return templateValues.Count == 0 ? defaultValue : templateValues.Min();
templateValues.Add(defaultValue);
return templateValues.Min();
}

/// <summary>
Expand Down
Loading