Browse Source

Migrate code to use CoefficientComponent instead of slices

Kaelan Patel 4 years ago
parent
commit
4f1fc283d5
3 changed files with 41 additions and 37 deletions
  1. 1 1
      pkg/costmodel/allocation.go
  2. 13 9
      pkg/costmodel/intervals.go
  3. 27 27
      pkg/costmodel/intervals_test.go

+ 1 - 1
pkg/costmodel/allocation.go

@@ -423,7 +423,7 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 	// Build out a PV price coefficient for each pod with a PVC. Each
 	// PVC-pod relation needs a coefficient which modifies the PV cost
 	// such that PV costs can be shared between all pods using that PVC.
-	sharedPVCCostCoefficientMap := make(map[pvcKey]map[podKey][][]float64)
+	sharedPVCCostCoefficientMap := make(map[pvcKey]map[podKey][]CoefficientComponent)
 	for pvcKey, podIntervalMap := range pvcPodIntervalMap {
 
 		// Get single-point intervals from alloc-PVC relation windows.

+ 13 - 9
pkg/costmodel/intervals.go

@@ -16,10 +16,11 @@ type IntervalPoint struct {
 	Key       podKey
 }
 
-// CostCoefficient is a representitive struct holding two fields which describe an interval:
+// CoefficientComponent is a representitive struct holding two fields which describe an interval
+// as part of a single number cost coefficient calculation:
 // 1. Proportion: The division of cost based on how many pods were running between those points
 // 2. Time: The ratio of the time between those points to the total time that pod was running
-type CostCoefficient struct {
+type CoefficientComponent struct {
 	Proportion float64
 	Time       float64
 }
@@ -71,9 +72,9 @@ func sortIntervalPoints(intervals []IntervalPoint) {
 // getPVCCostCoefficients gets a coefficient which represents the scale
 // factor that each PVC in a pvcIntervalMap and corresponding slice of
 // IntervalPoints intervals uses to calculate a cost for that PVC's PV.
-func getPVCCostCoefficients(intervals []IntervalPoint, pvcIntervalMap map[podKey]kubecost.Window) map[podKey][][]float64 {
+func getPVCCostCoefficients(intervals []IntervalPoint, pvcIntervalMap map[podKey]kubecost.Window) map[podKey][]CoefficientComponent {
 
-	pvcCostCoefficientMap := make(map[podKey][][]float64)
+	pvcCostCoefficientMap := make(map[podKey][]CoefficientComponent)
 
 	// pvcCostCoefficientMap is mutated in this function. The format is
 	// such that the individual coefficient components are preserved for
@@ -97,7 +98,10 @@ func getPVCCostCoefficients(intervals []IntervalPoint, pvcIntervalMap map[podKey
 			for key := range activeKeys {
 				pvcCostCoefficientMap[key] = append(
 					pvcCostCoefficientMap[key],
-					[]float64{1.0 / activePods, point.Time.Sub(prevPoint.Time).Minutes() / pvcIntervalMap[key].Duration().Minutes()},
+					CoefficientComponent{
+						Time:       point.Time.Sub(prevPoint.Time).Minutes() / pvcIntervalMap[key].Duration().Minutes(),
+						Proportion: 1.0 / activePods,
+					},
 				)
 			}
 		}
@@ -122,16 +126,16 @@ func getPVCCostCoefficients(intervals []IntervalPoint, pvcIntervalMap map[podKey
 // getCoefficient takes the components of a PVC-pod PV cost coefficient
 // determined by getPVCCostCoefficient and gets the resulting single
 // floating point coefficient.
-func getCoefficient(coefficientComponents [][]float64) float64 {
+func getCoefficient(coefficientComponents []CoefficientComponent) float64 {
 
 	coefficient := 0.0
 
 	for i := range coefficientComponents {
 
-		cost := coefficientComponents[i][0]
-		duration := coefficientComponents[i][1]
+		proportion := coefficientComponents[i].Proportion
+		time := coefficientComponents[i].Time
 
-		coefficient += cost * duration
+		coefficient += proportion * time
 
 	}
 

+ 27 - 27
pkg/costmodel/intervals_test.go

@@ -153,7 +153,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
 		name           string
 		pvcIntervalMap map[podKey]kubecost.Window
 		intervals      []IntervalPoint
-		expected       map[podKey][][]float64
+		expected       map[podKey][]CoefficientComponent
 	}{
 		{
 			name: "four pods w/ various overlaps",
@@ -197,30 +197,30 @@ func TestGetPVCCostCoefficients(t *testing.T) {
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod3"}),
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod1"}),
 			},
-			expected: map[podKey][][]float64{
+			expected: map[podKey][]CoefficientComponent{
 				podKey{
 					Pod: "Pod1",
-				}: [][]float64{
-					[]float64{0.5, 0.25},
-					[]float64{1, 0.25},
-					[]float64{0.5, 0.25},
-					[]float64{1.0 / 3.0, 0.25},
+				}: []CoefficientComponent{
+					CoefficientComponent{0.5, 0.25},
+					CoefficientComponent{1, 0.25},
+					CoefficientComponent{0.5, 0.25},
+					CoefficientComponent{1.0 / 3.0, 0.25},
 				},
 				podKey{
 					Pod: "Pod2",
-				}: [][]float64{
-					[]float64{0.5, 0.50},
-					[]float64{1.0 / 3.0, 0.50},
+				}: []CoefficientComponent{
+					CoefficientComponent{0.5, 0.50},
+					CoefficientComponent{1.0 / 3.0, 0.50},
 				},
 				podKey{
 					Pod: "Pod3",
-				}: [][]float64{
-					[]float64{1.0 / 3.0, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{1.0 / 3.0, 1.0},
 				},
 				podKey{
 					Pod: "Pod4",
-				}: [][]float64{
-					[]float64{0.5, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{0.5, 1.0},
 				},
 			},
 		},
@@ -248,16 +248,16 @@ func TestGetPVCCostCoefficients(t *testing.T) {
 				NewIntervalPoint(time.Date(2021, 2, 19, 8, 30, 0, 0, time.UTC), "end", podKey{Pod: "Pod1"}),
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod2"}),
 			},
-			expected: map[podKey][][]float64{
+			expected: map[podKey][]CoefficientComponent{
 				podKey{
 					Pod: "Pod1",
-				}: [][]float64{
-					[]float64{1.0, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{1.0, 1.0},
 				},
 				podKey{
 					Pod: "Pod2",
-				}: [][]float64{
-					[]float64{1.0, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{1.0, 1.0},
 				},
 			},
 		},
@@ -285,16 +285,16 @@ func TestGetPVCCostCoefficients(t *testing.T) {
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod1"}),
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod2"}),
 			},
-			expected: map[podKey][][]float64{
+			expected: map[podKey][]CoefficientComponent{
 				podKey{
 					Pod: "Pod1",
-				}: [][]float64{
-					[]float64{0.5, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{0.5, 1.0},
 				},
 				podKey{
 					Pod: "Pod2",
-				}: [][]float64{
-					[]float64{0.5, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{0.5, 1.0},
 				},
 			},
 		},
@@ -313,11 +313,11 @@ func TestGetPVCCostCoefficients(t *testing.T) {
 				NewIntervalPoint(time.Date(2021, 2, 19, 8, 0, 0, 0, time.UTC), "start", podKey{Pod: "Pod1"}),
 				NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", podKey{Pod: "Pod1"}),
 			},
-			expected: map[podKey][][]float64{
+			expected: map[podKey][]CoefficientComponent{
 				podKey{
 					Pod: "Pod1",
-				}: [][]float64{
-					[]float64{1.0, 1.0},
+				}: []CoefficientComponent{
+					CoefficientComponent{1.0, 1.0},
 				},
 			},
 		},