Skip to content

Commit 5587d2d

Browse files
committed
fix ratio calculation
1 parent 7c6b725 commit 5587d2d

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

internal/knowledge/extractor/plugins/compute/flavor_groups.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ func (e *FlavorGroupExtractor) Extract() ([]plugins.Feature, error) {
141141
smallest := flavors[len(flavors)-1]
142142

143143
// Compute RAM/core ratio (MiB per vCPU)
144+
// Use cross-multiplication to detect if ratios truly differ, avoiding integer truncation.
145+
// Two ratios mem1/vcpus1 == mem2/vcpus2 iff mem1*vcpus2 == mem2*vcpus1
146+
var refMemory, refVCPUs uint64
144147
var minRatio, maxRatio uint64 = ^uint64(0), 0
148+
allSameRatio := true
145149
for _, f := range flavors {
146150
if f.VCPUs == 0 {
147151
continue // Skip flavors with 0 vCPUs to avoid division by zero
@@ -153,12 +157,19 @@ func (e *FlavorGroupExtractor) Extract() ([]plugins.Feature, error) {
153157
if ratio > maxRatio {
154158
maxRatio = ratio
155159
}
160+
// Check if ratio matches reference using cross-multiplication
161+
if refVCPUs == 0 {
162+
refMemory, refVCPUs = f.MemoryMB, f.VCPUs
163+
} else if refMemory*f.VCPUs != f.MemoryMB*refVCPUs {
164+
allSameRatio = false
165+
}
156166
}
157167

158168
var ramCoreRatio, ramCoreRatioMin, ramCoreRatioMax *uint64
159-
if minRatio == maxRatio && maxRatio != 0 {
169+
if allSameRatio && refVCPUs != 0 {
160170
// All flavors have the same ratio
161-
ramCoreRatio = &minRatio
171+
computedRatio := refMemory / refVCPUs
172+
ramCoreRatio = &computedRatio
162173
} else if maxRatio != 0 {
163174
// Flavors have different ratios
164175
ramCoreRatioMin = &minRatio

internal/scheduling/reservations/commitments/api_info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestHandleInfo_HasCapacityEqualsHandlesCommitments(t *testing.T) {
102102
"ramCoreRatio": 4096, // Fixed: 4096 MiB per vCPU for all flavors
103103
},
104104
{
105-
// Group with variable ratio - should NOT accept commitments (HasCapacity=true, HandlesCommitments=false)
105+
// Group with variable ratio - should NOT accept commitments (HasCapacity=false, HandlesCommitments=false)
106106
"name": "v2_variable",
107107
"flavors": []map[string]interface{}{
108108
{"name": "v2_c4_m8", "vcpus": 4, "memoryMB": 8192, "diskGB": 50}, // 2048 MiB/vCPU

internal/scheduling/reservations/commitments/flavor_group_eligibility.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ func FlavorGroupCommitmentRejectionReason(fg *compute.FlavorGroupFeature) string
2222
if FlavorGroupAcceptsCommitments(fg) {
2323
return ""
2424
}
25-
var minRatio, maxRatio uint64
26-
if fg.RamCoreRatioMin != nil {
27-
minRatio = *fg.RamCoreRatioMin
28-
}
29-
if fg.RamCoreRatioMax != nil {
30-
maxRatio = *fg.RamCoreRatioMax
25+
// Differentiate between missing ratio metadata and variable ratio
26+
if fg.RamCoreRatioMin == nil && fg.RamCoreRatioMax == nil {
27+
return fmt.Sprintf("flavor group %q has no computable RAM/core ratio metadata and does not accept commitments", fg.Name)
3128
}
3229
return fmt.Sprintf("flavor group %q has variable RAM/core ratio (min=%d, max=%d) and does not accept commitments",
33-
fg.Name, minRatio, maxRatio)
30+
fg.Name, *fg.RamCoreRatioMin, *fg.RamCoreRatioMax)
3431
}

0 commit comments

Comments
 (0)