-
Notifications
You must be signed in to change notification settings - Fork 515
/
allocation.go
3501 lines (3034 loc) · 111 KB
/
allocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package opencost
import (
"fmt"
"math"
"sort"
"strings"
"time"
"github.com/opencost/opencost/core/pkg/filter"
"github.com/opencost/opencost/core/pkg/filter/ast"
"github.com/opencost/opencost/core/pkg/filter/matcher"
"github.com/opencost/opencost/core/pkg/log"
"github.com/opencost/opencost/core/pkg/util"
"github.com/opencost/opencost/core/pkg/util/timeutil"
"golang.org/x/exp/slices"
)
// TODO Clean-up use of IsEmpty; nil checks should be separated for safety.
// TODO Consider making Allocation an interface, which is fulfilled by structs
// like KubernetesAllocation, IdleAllocation, and ExternalAllocation.
// ExternalSuffix indicates an external allocation
const ExternalSuffix = "__external__"
// IdleSuffix indicates an idle allocation property
const IdleSuffix = "__idle__"
// SharedSuffix indicates an shared allocation property
const SharedSuffix = "__shared__"
// UnallocatedSuffix indicates an unallocated allocation property
const UnallocatedSuffix = "__unallocated__"
// UnmountedSuffix indicated allocation to an unmounted resource (PV or LB)
const UnmountedSuffix = "__unmounted__"
// ShareWeighted indicates that a shared resource should be shared as a
// proportion of the cost of the remaining allocations.
const ShareWeighted = "__weighted__"
// ShareEven indicates that a shared resource should be shared evenly across
// all remaining allocations.
const ShareEven = "__even__"
// ShareNone indicates that a shareable resource should not be shared
const ShareNone = "__none__"
// Allocation is a unit of resource allocation and cost for a given window
// of time and for a given kubernetes construct with its associated set of
// properties.
// TODO:CLEANUP consider dropping name in favor of just Allocation and an
// Assets-style key() function for AllocationSet.
type Allocation struct {
Name string `json:"name"`
Properties *AllocationProperties `json:"properties,omitempty"`
Window Window `json:"window"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
CPUCoreHours float64 `json:"cpuCoreHours"`
CPUCoreRequestAverage float64 `json:"cpuCoreRequestAverage"`
CPUCoreUsageAverage float64 `json:"cpuCoreUsageAverage"`
CPUCost float64 `json:"cpuCost"`
CPUCostAdjustment float64 `json:"cpuCostAdjustment"`
GPUHours float64 `json:"gpuHours"`
GPUCost float64 `json:"gpuCost"`
GPUCostAdjustment float64 `json:"gpuCostAdjustment"`
NetworkTransferBytes float64 `json:"networkTransferBytes"`
NetworkReceiveBytes float64 `json:"networkReceiveBytes"`
NetworkCost float64 `json:"networkCost"`
NetworkCrossZoneCost float64 `json:"networkCrossZoneCost"` // @bingen:field[version=16]
NetworkCrossRegionCost float64 `json:"networkCrossRegionCost"` // @bingen:field[version=16]
NetworkInternetCost float64 `json:"networkInternetCost"` // @bingen:field[version=16]
NetworkCostAdjustment float64 `json:"networkCostAdjustment"`
LoadBalancerCost float64 `json:"loadBalancerCost"`
LoadBalancerCostAdjustment float64 `json:"loadBalancerCostAdjustment"`
PVs PVAllocations `json:"pvs"`
PVCostAdjustment float64 `json:"pvCostAdjustment"`
RAMByteHours float64 `json:"ramByteHours"`
RAMBytesRequestAverage float64 `json:"ramByteRequestAverage"`
RAMBytesUsageAverage float64 `json:"ramByteUsageAverage"`
RAMCost float64 `json:"ramCost"`
RAMCostAdjustment float64 `json:"ramCostAdjustment"`
SharedCost float64 `json:"sharedCost"`
ExternalCost float64 `json:"externalCost"`
// RawAllocationOnly is a pointer so if it is not present it will be
// marshalled as null rather than as an object with Go default values.
RawAllocationOnly *RawAllocationOnlyData `json:"rawAllocationOnly"`
// ProportionalAssetResourceCost represents the per-resource costs of the
// allocation as a percentage of the per-resource total cost of the
// asset on which the allocation was run. It is optionally computed
// and appended to an Allocation, and so by default is is nil.
ProportionalAssetResourceCosts ProportionalAssetResourceCosts `json:"proportionalAssetResourceCosts"` //@bingen:field[ignore]
SharedCostBreakdown SharedCostBreakdowns `json:"sharedCostBreakdown"` //@bingen:field[ignore]
LoadBalancers LbAllocations `json:"LoadBalancers"` //@bingen:field[version=18]
// UnmountedPVCost is used to track how much of the cost in PVs is for an
// unmounted PV. It is not additive of PVCost() and need not be sent in API
// responses.
UnmountedPVCost float64 `json:"-"` //@bingen:field[ignore]
}
type LbAllocations map[string]*LbAllocation
func (orig LbAllocations) Clone() LbAllocations {
if orig == nil {
return nil
}
newAllocs := LbAllocations{}
for key, lbAlloc := range orig {
newAllocs[key] = &LbAllocation{
Service: lbAlloc.Service,
Cost: lbAlloc.Cost,
Private: lbAlloc.Private,
Ip: lbAlloc.Ip,
Hours: lbAlloc.Hours,
}
}
return newAllocs
}
type LbAllocation struct {
Service string `json:"service"`
Cost float64 `json:"cost"`
Private bool `json:"private"`
Ip string `json:"ip"` //@bingen:field[version=19]
Hours float64 `json:"hours"` //@bingen:field[version=21]
Adjustment float64 `json:"adjustment"` //@bingen:field[ignore]
}
func (lba *LbAllocation) SanitizeNaN() {
if lba == nil {
return
}
if math.IsNaN(lba.Cost) {
log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Cost service:%s", lba.Service)
lba.Cost = 0
}
if math.IsNaN(lba.Hours) {
log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Hours service:%s", lba.Service)
lba.Hours = 0
}
}
// RawAllocationOnlyData is information that only belong in "raw" Allocations,
// those which have not undergone aggregation, accumulation, or any other form
// of combination to produce a new Allocation from other Allocations.
//
// Max usage data belongs here because computing the overall maximum from two
// or more Allocations is a non-trivial operation that cannot be defined without
// maintaining a large amount of state. Consider the following example:
// _______________________________________________
//
// A1 Using 3 CPU ---- ----- ------
// A2 Using 2 CPU ---- ----- ----
// A3 Using 1 CPU --- --
// _______________________________________________
//
// Time ---->
//
// The logical maximum CPU usage is 5, but this cannot be calculated iteratively,
// which is how we calculate aggregations and accumulations of Allocations currently.
// This becomes a problem I could call "maximum sum of overlapping intervals" and is
// essentially a variant of an interval scheduling algorithm.
//
// If we had types to differentiate between regular Allocations and AggregatedAllocations
// then this type would be unnecessary and its fields would go into the regular Allocation
// and not in the AggregatedAllocation.
type RawAllocationOnlyData struct {
CPUCoreUsageMax float64 `json:"cpuCoreUsageMax"`
RAMBytesUsageMax float64 `json:"ramByteUsageMax"`
}
// Clone returns a deep copy of the given RawAllocationOnlyData
func (r *RawAllocationOnlyData) Clone() *RawAllocationOnlyData {
if r == nil {
return nil
}
return &RawAllocationOnlyData{
CPUCoreUsageMax: r.CPUCoreUsageMax,
RAMBytesUsageMax: r.RAMBytesUsageMax,
}
}
// Equal returns true if the RawAllocationOnlyData is approximately equal
func (r *RawAllocationOnlyData) Equal(that *RawAllocationOnlyData) bool {
if r == nil && that == nil {
return true
}
if r == nil || that == nil {
return false
}
return util.IsApproximately(r.CPUCoreUsageMax, that.CPUCoreUsageMax) &&
util.IsApproximately(r.RAMBytesUsageMax, that.RAMBytesUsageMax)
}
func (r *RawAllocationOnlyData) SanitizeNaN() {
if r == nil {
return
}
if math.IsNaN(r.CPUCoreUsageMax) {
log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for CPUCoreUsageMax")
r.CPUCoreUsageMax = 0
}
if math.IsNaN(r.RAMBytesUsageMax) {
log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for RAMBytesUsageMax")
r.RAMBytesUsageMax = 0
}
}
// PVAllocations is a map of Disk Asset Identifiers to the
// usage of them by an Allocation as recorded in a PVAllocation
type PVAllocations map[PVKey]*PVAllocation
// Clone creates a deep copy of a PVAllocations
func (pv PVAllocations) Clone() PVAllocations {
if pv == nil {
return nil
}
clonePV := make(map[PVKey]*PVAllocation, len(pv))
for k, v := range pv {
clonePV[k] = &PVAllocation{
ByteHours: v.ByteHours,
Cost: v.Cost,
ProviderID: v.ProviderID,
}
}
return clonePV
}
// Add adds contents of that to the calling PVAllocations
func (pv PVAllocations) Add(that PVAllocations) PVAllocations {
apv := pv.Clone()
if that != nil {
if apv == nil {
apv = PVAllocations{}
}
for pvKey, thatPVAlloc := range that {
apvAlloc, ok := apv[pvKey]
if !ok {
apvAlloc = &PVAllocation{}
}
apvAlloc.Cost += thatPVAlloc.Cost
apvAlloc.ByteHours += thatPVAlloc.ByteHours
apv[pvKey] = apvAlloc
if apvAlloc.ProviderID == thatPVAlloc.ProviderID {
apv[pvKey].ProviderID = apvAlloc.ProviderID
} else {
apv[pvKey].ProviderID = ""
}
}
}
return apv
}
// Equal returns true if the two PVAllocations are equal in length and contain the same keys
// and values.
func (this PVAllocations) Equal(that PVAllocations) bool {
if this == nil && that == nil {
return true
}
if this == nil || that == nil {
return false
}
if len(this) != len(that) {
return false
}
for k, pv := range this {
tv, ok := that[k]
if !ok || !pv.Equal(tv) {
return false
}
}
return true
}
func (pvs PVAllocations) SanitizeNaN() {
for _, pv := range pvs {
pv.SanitizeNaN()
}
}
// PVKey for identifying Disk type assets
type PVKey struct {
Cluster string `json:"cluster"`
Name string `json:"name"`
}
func (pvk *PVKey) String() string {
return fmt.Sprintf("cluster=%s:name=%s", pvk.Cluster, pvk.Name)
}
// FromString populates PVKey fields from string
func (pvk *PVKey) FromString(key string) error {
splitKey := strings.Split(key, ":")
if len(splitKey) != 2 {
return fmt.Errorf("PVKey string '%s' has the incorrect format", key)
}
pvk.Cluster = strings.TrimPrefix(splitKey[0], "cluster=")
pvk.Name = strings.TrimPrefix(splitKey[1], "name=")
return nil
}
// PVAllocation contains the byte hour usage
// and cost of an Allocation for a single PV
type PVAllocation struct {
ByteHours float64 `json:"byteHours"`
Cost float64 `json:"cost"`
ProviderID string `json:"providerID"` // @bingen:field[version=20]
Adjustment float64 `json:"adjustment"` //@bingen:field[ignore]
}
// Equal returns true if the two PVAllocation instances contain approximately the same
// values.
func (pva *PVAllocation) Equal(that *PVAllocation) bool {
if pva == nil && that == nil {
return true
}
if pva == nil || that == nil {
return false
}
return util.IsApproximately(pva.ByteHours, that.ByteHours) &&
util.IsApproximately(pva.Cost, that.Cost)
}
func (pva *PVAllocation) SanitizeNaN() {
if pva == nil {
return
}
if math.IsNaN(pva.ByteHours) {
log.DedupedWarningf(5, "PVAllocation: Unexpected NaN found for ByteHours")
pva.ByteHours = 0
}
if math.IsNaN(pva.Cost) {
log.DedupedWarningf(5, "PVAllocation: Unexpected NaN found for Cost")
pva.Cost = 0
}
}
type ProportionalAssetResourceCost struct {
Cluster string `json:"cluster"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
ProviderID string `json:"providerID,omitempty"`
CPUPercentage float64 `json:"cpuPercentage"`
GPUPercentage float64 `json:"gpuPercentage"`
RAMPercentage float64 `json:"ramPercentage"`
LoadBalancerPercentage float64 `json:"loadBalancerPercentage"`
PVPercentage float64 `json:"pvPercentage"`
NodeResourceCostPercentage float64 `json:"nodeResourceCostPercentage"`
GPUTotalCost float64 `json:"-"`
GPUProportionalCost float64 `json:"-"`
CPUTotalCost float64 `json:"-"`
CPUProportionalCost float64 `json:"-"`
RAMTotalCost float64 `json:"-"`
RAMProportionalCost float64 `json:"-"`
LoadBalancerProportionalCost float64 `json:"-"`
LoadBalancerTotalCost float64 `json:"-"`
PVProportionalCost float64 `json:"-"`
PVTotalCost float64 `json:"-"`
}
func (parc ProportionalAssetResourceCost) Key(insertByName bool) string {
if insertByName {
return parc.Cluster + "," + parc.Name
} else {
return parc.Cluster
}
}
type ProportionalAssetResourceCosts map[string]ProportionalAssetResourceCost
func (parcs ProportionalAssetResourceCosts) Clone() ProportionalAssetResourceCosts {
cloned := ProportionalAssetResourceCosts{}
for key, parc := range parcs {
cloned[key] = parc
}
return cloned
}
func (parcs ProportionalAssetResourceCosts) Insert(parc ProportionalAssetResourceCost, insertByName bool) {
if !insertByName {
parc.Name = ""
parc.Type = ""
parc.ProviderID = ""
}
if curr, ok := parcs[parc.Key(insertByName)]; ok {
toInsert := ProportionalAssetResourceCost{
Name: curr.Name,
Type: curr.Type,
Cluster: curr.Cluster,
ProviderID: curr.ProviderID,
CPUProportionalCost: curr.CPUProportionalCost + parc.CPUProportionalCost,
RAMProportionalCost: curr.RAMProportionalCost + parc.RAMProportionalCost,
GPUProportionalCost: curr.GPUProportionalCost + parc.GPUProportionalCost,
PVProportionalCost: curr.PVProportionalCost + parc.PVProportionalCost,
LoadBalancerProportionalCost: curr.LoadBalancerProportionalCost + parc.LoadBalancerProportionalCost,
}
ComputePercentages(&toInsert)
parcs[parc.Key(insertByName)] = toInsert
} else {
ComputePercentages(&parc)
parcs[parc.Key(insertByName)] = parc
}
}
func ComputePercentages(toInsert *ProportionalAssetResourceCost) {
totalNodeCost := toInsert.RAMTotalCost + toInsert.CPUTotalCost + toInsert.GPUTotalCost
if toInsert.CPUTotalCost > 0 {
toInsert.CPUPercentage = toInsert.CPUProportionalCost / toInsert.CPUTotalCost
}
if toInsert.GPUTotalCost > 0 {
toInsert.GPUPercentage = toInsert.GPUProportionalCost / toInsert.GPUTotalCost
}
if toInsert.LoadBalancerTotalCost > 0 {
toInsert.LoadBalancerPercentage = toInsert.LoadBalancerProportionalCost / toInsert.LoadBalancerTotalCost
}
if toInsert.RAMTotalCost > 0 {
toInsert.RAMPercentage = toInsert.RAMProportionalCost / toInsert.RAMTotalCost
}
if toInsert.PVTotalCost > 0 {
toInsert.PVPercentage = toInsert.PVProportionalCost / toInsert.PVTotalCost
}
ramFraction := toInsert.RAMTotalCost / totalNodeCost
if ramFraction != ramFraction || ramFraction < 0 {
ramFraction = 0
}
cpuFraction := toInsert.CPUTotalCost / totalNodeCost
if cpuFraction != cpuFraction || cpuFraction < 0 {
cpuFraction = 0
}
gpuFraction := toInsert.GPUTotalCost / totalNodeCost
if gpuFraction != gpuFraction || gpuFraction < 0 {
gpuFraction = 0
}
toInsert.NodeResourceCostPercentage = (toInsert.RAMPercentage * ramFraction) +
(toInsert.CPUPercentage * cpuFraction) + (toInsert.GPUPercentage * gpuFraction)
}
func (parcs ProportionalAssetResourceCosts) Add(that ProportionalAssetResourceCosts) {
for _, parc := range that {
// if name field is empty, we know this is a cluster level PARC aggregation
insertByName := true
if parc.Name == "" {
insertByName = false
}
parcs.Insert(parc, insertByName)
}
}
func (parcs ProportionalAssetResourceCosts) SanitizeNaN() {
for key, parc := range parcs {
if math.IsNaN(parc.CPUPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUPercentage name:%s", parc.Name)
parc.CPUPercentage = 0
}
if math.IsNaN(parc.GPUPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUPercentage name:%s", parc.Name)
parc.GPUPercentage = 0
}
if math.IsNaN(parc.RAMPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMPercentage name:%s", parc.Name)
parc.RAMPercentage = 0
}
if math.IsNaN(parc.LoadBalancerPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerPercentage name:%s", parc.Name)
parc.LoadBalancerPercentage = 0
}
if math.IsNaN(parc.PVPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVPercentage name:%s", parc.Name)
parc.PVPercentage = 0
}
if math.IsNaN(parc.NodeResourceCostPercentage) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for NodeResourceCostPercentage name:%s", parc.Name)
parc.NodeResourceCostPercentage = 0
}
if math.IsNaN(parc.GPUTotalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUTotalCost name:%s", parc.Name)
parc.GPUTotalCost = 0
}
if math.IsNaN(parc.GPUProportionalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUProportionalCost name:%s", parc.Name)
parc.GPUProportionalCost = 0
}
if math.IsNaN(parc.CPUTotalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUTotalCost name:%s", parc.Name)
parc.CPUTotalCost = 0
}
if math.IsNaN(parc.CPUProportionalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUProportionalCost name:%s", parc.Name)
parc.CPUProportionalCost = 0
}
if math.IsNaN(parc.RAMTotalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMTotalCost name:%s", parc.Name)
parc.RAMTotalCost = 0
}
if math.IsNaN(parc.RAMProportionalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMProportionalCost name:%s", parc.Name)
parc.RAMProportionalCost = 0
}
if math.IsNaN(parc.LoadBalancerProportionalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerProportionalCost name:%s", parc.Name)
parc.LoadBalancerProportionalCost = 0
}
if math.IsNaN(parc.LoadBalancerTotalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerTotalCost name:%s", parc.Name)
parc.LoadBalancerTotalCost = 0
}
if math.IsNaN(parc.PVProportionalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVProportionalCost name:%s", parc.Name)
parc.PVProportionalCost = 0
}
if math.IsNaN(parc.PVTotalCost) {
log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVTotalCost name:%s", parc.Name)
parc.PVTotalCost = 0
}
parcs[key] = parc
}
}
type SharedCostBreakdown struct {
Name string `json:"name"`
TotalCost float64 `json:"totalCost"`
CPUCost float64 `json:"cpuCost,omitempty"`
GPUCost float64 `json:"gpuCost,omitempty"`
RAMCost float64 `json:"ramCost,omitempty"`
PVCost float64 `json:"pvCost,omitempty"`
NetworkCost float64 `json:"networkCost,omitempty"`
LBCost float64 `json:"loadBalancerCost,omitempty"`
ExternalCost float64 `json:"externalCost,omitempty"`
}
type SharedCostBreakdowns map[string]SharedCostBreakdown
func (scbs SharedCostBreakdowns) Clone() SharedCostBreakdowns {
cloned := SharedCostBreakdowns{}
for key, scb := range scbs {
cloned[key] = scb
}
return cloned
}
func (scbs SharedCostBreakdowns) Insert(scb SharedCostBreakdown) {
if curr, ok := scbs[scb.Name]; ok {
scbs[scb.Name] = SharedCostBreakdown{
Name: curr.Name,
TotalCost: curr.TotalCost + scb.TotalCost,
CPUCost: curr.CPUCost + scb.CPUCost,
GPUCost: curr.GPUCost + scb.GPUCost,
RAMCost: curr.RAMCost + scb.RAMCost,
PVCost: curr.PVCost + scb.PVCost,
NetworkCost: curr.NetworkCost + scb.NetworkCost,
LBCost: curr.LBCost + scb.LBCost,
ExternalCost: curr.ExternalCost + scb.ExternalCost,
}
} else {
scbs[scb.Name] = scb
}
}
func (scbs SharedCostBreakdowns) Add(that SharedCostBreakdowns) {
for _, scb := range that {
scbs.Insert(scb)
}
}
func (scbs SharedCostBreakdowns) SanitizeNaN() {
for key, scb := range scbs {
if math.IsNaN(scb.CPUCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for CPUCost name:%s", scb.Name)
scb.CPUCost = 0
}
if math.IsNaN(scb.GPUCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for GPUCost name:%s", scb.Name)
scb.GPUCost = 0
}
if math.IsNaN(scb.RAMCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for RAMCost name:%s", scb.Name)
scb.RAMCost = 0
}
if math.IsNaN(scb.PVCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for PVCost name:%s", scb.Name)
scb.PVCost = 0
}
if math.IsNaN(scb.NetworkCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for NetworkCost name:%s", scb.Name)
scb.NetworkCost = 0
}
if math.IsNaN(scb.LBCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for LBCost name:%s", scb.Name)
scb.LBCost = 0
}
if math.IsNaN(scb.ExternalCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for ExternalCost name:%s", scb.Name)
scb.ExternalCost = 0
}
if math.IsNaN(scb.TotalCost) {
log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for TotalCost name:%s", scb.Name)
scb.TotalCost = 0
}
scbs[key] = scb
}
}
// GetWindow returns the window of the struct
func (a *Allocation) GetWindow() Window {
return a.Window
}
// AllocationMatchFunc is a function that can be used to match Allocations by
// returning true for any given Allocation if a condition is met.
type AllocationMatchFunc func(*Allocation) bool
// Add returns the result of summing the two given Allocations, which sums the
// summary fields (e.g. costs, resources) and recomputes efficiency. Neither of
// the two original Allocations are mutated in the process.
func (a *Allocation) Add(that *Allocation) (*Allocation, error) {
if a == nil {
return that.Clone(), nil
}
if that == nil {
return a.Clone(), nil
}
// Note: no need to clone "that", as add only mutates the receiver
agg := a.Clone()
agg.add(that)
return agg, nil
}
// Clone returns a deep copy of the given Allocation
func (a *Allocation) Clone() *Allocation {
if a == nil {
return nil
}
return &Allocation{
Name: a.Name,
Properties: a.Properties.Clone(),
Window: a.Window.Clone(),
Start: a.Start,
End: a.End,
CPUCoreHours: a.CPUCoreHours,
CPUCoreRequestAverage: a.CPUCoreRequestAverage,
CPUCoreUsageAverage: a.CPUCoreUsageAverage,
CPUCost: a.CPUCost,
CPUCostAdjustment: a.CPUCostAdjustment,
GPUHours: a.GPUHours,
GPUCost: a.GPUCost,
GPUCostAdjustment: a.GPUCostAdjustment,
NetworkTransferBytes: a.NetworkTransferBytes,
NetworkReceiveBytes: a.NetworkReceiveBytes,
NetworkCost: a.NetworkCost,
NetworkCrossZoneCost: a.NetworkCrossZoneCost,
NetworkCrossRegionCost: a.NetworkCrossRegionCost,
NetworkInternetCost: a.NetworkInternetCost,
NetworkCostAdjustment: a.NetworkCostAdjustment,
LoadBalancerCost: a.LoadBalancerCost,
LoadBalancerCostAdjustment: a.LoadBalancerCostAdjustment,
PVs: a.PVs.Clone(),
PVCostAdjustment: a.PVCostAdjustment,
RAMByteHours: a.RAMByteHours,
RAMBytesRequestAverage: a.RAMBytesRequestAverage,
RAMBytesUsageAverage: a.RAMBytesUsageAverage,
RAMCost: a.RAMCost,
RAMCostAdjustment: a.RAMCostAdjustment,
SharedCost: a.SharedCost,
ExternalCost: a.ExternalCost,
RawAllocationOnly: a.RawAllocationOnly.Clone(),
ProportionalAssetResourceCosts: a.ProportionalAssetResourceCosts.Clone(),
SharedCostBreakdown: a.SharedCostBreakdown.Clone(),
LoadBalancers: a.LoadBalancers.Clone(),
UnmountedPVCost: a.UnmountedPVCost,
}
}
// Equal returns true if the values held in the given Allocation precisely
// match those of the receiving Allocation. nil does not match nil. Floating
// point values need to match according to util.IsApproximately, which accounts
// for small, reasonable floating point error margins.
func (a *Allocation) Equal(that *Allocation) bool {
if a == nil || that == nil {
return false
}
if a.Name != that.Name {
return false
}
if !a.Properties.Equal(that.Properties) {
return false
}
if !a.Window.Equal(that.Window) {
return false
}
if !a.Start.Equal(that.Start) {
return false
}
if !a.End.Equal(that.End) {
return false
}
if !util.IsApproximately(a.CPUCoreHours, that.CPUCoreHours) {
return false
}
if !util.IsApproximately(a.CPUCost, that.CPUCost) {
return false
}
if !util.IsApproximately(a.CPUCostAdjustment, that.CPUCostAdjustment) {
return false
}
if !util.IsApproximately(a.GPUHours, that.GPUHours) {
return false
}
if !util.IsApproximately(a.GPUCost, that.GPUCost) {
return false
}
if !util.IsApproximately(a.GPUCostAdjustment, that.GPUCostAdjustment) {
return false
}
if !util.IsApproximately(a.NetworkTransferBytes, that.NetworkTransferBytes) {
return false
}
if !util.IsApproximately(a.NetworkReceiveBytes, that.NetworkReceiveBytes) {
return false
}
if !util.IsApproximately(a.NetworkCost, that.NetworkCost) {
return false
}
if !util.IsApproximately(a.NetworkCrossZoneCost, that.NetworkCrossZoneCost) {
return false
}
if !util.IsApproximately(a.NetworkCrossRegionCost, that.NetworkCrossRegionCost) {
return false
}
if !util.IsApproximately(a.NetworkInternetCost, that.NetworkInternetCost) {
return false
}
if !util.IsApproximately(a.NetworkCostAdjustment, that.NetworkCostAdjustment) {
return false
}
if !util.IsApproximately(a.LoadBalancerCost, that.LoadBalancerCost) {
return false
}
if !util.IsApproximately(a.LoadBalancerCostAdjustment, that.LoadBalancerCostAdjustment) {
return false
}
if !util.IsApproximately(a.PVCostAdjustment, that.PVCostAdjustment) {
return false
}
if !util.IsApproximately(a.RAMByteHours, that.RAMByteHours) {
return false
}
if !util.IsApproximately(a.RAMCost, that.RAMCost) {
return false
}
if !util.IsApproximately(a.RAMCostAdjustment, that.RAMCostAdjustment) {
return false
}
if !util.IsApproximately(a.SharedCost, that.SharedCost) {
return false
}
if !util.IsApproximately(a.ExternalCost, that.ExternalCost) {
return false
}
if !a.RawAllocationOnly.Equal(that.RawAllocationOnly) {
return false
}
if !a.PVs.Equal(that.PVs) {
return false
}
if !util.IsApproximately(a.UnmountedPVCost, that.UnmountedPVCost) {
return false
}
return true
}
// TotalCost is the total cost of the Allocation including adjustments
func (a *Allocation) TotalCost() float64 {
if a == nil {
return 0.0
}
return a.CPUTotalCost() + a.GPUTotalCost() + a.RAMTotalCost() + a.PVTotalCost() + a.NetworkTotalCost() + a.LBTotalCost() + a.SharedTotalCost() + a.ExternalCost
}
// CPUTotalCost calculates total CPU cost of Allocation including adjustment
func (a *Allocation) CPUTotalCost() float64 {
if a == nil {
return 0.0
}
return a.CPUCost + a.CPUCostAdjustment
}
// GPUTotalCost calculates total GPU cost of Allocation including adjustment
func (a *Allocation) GPUTotalCost() float64 {
if a == nil {
return 0.0
}
return a.GPUCost + a.GPUCostAdjustment
}
// RAMTotalCost calculates total RAM cost of Allocation including adjustment
func (a *Allocation) RAMTotalCost() float64 {
if a == nil {
return 0.0
}
return a.RAMCost + a.RAMCostAdjustment
}
// PVTotalCost calculates total PV cost of Allocation including adjustment
func (a *Allocation) PVTotalCost() float64 {
if a == nil {
return 0.0
}
return a.PVCost() + a.PVCostAdjustment
}
// NetworkTotalCost calculates total Network cost of Allocation including adjustment
func (a *Allocation) NetworkTotalCost() float64 {
if a == nil {
return 0.0
}
return a.NetworkCost + a.NetworkCostAdjustment
}
// LBTotalCost calculates total LB cost of Allocation including adjustment
// TODO deprecate
func (a *Allocation) LBTotalCost() float64 {
return a.LoadBalancerTotalCost()
}
// LoadBalancerTotalCost calculates total LB cost of Allocation including adjustment
func (a *Allocation) LoadBalancerTotalCost() float64 {
if a == nil {
return 0.0
}
return a.LoadBalancerCost + a.LoadBalancerCostAdjustment
}
// SharedTotalCost calculates total shared cost of Allocation including adjustment
func (a *Allocation) SharedTotalCost() float64 {
if a == nil {
return 0.0
}
return a.SharedCost
}
// PVCost calculate cumulative cost of all PVs that Allocation is attached to
func (a *Allocation) PVCost() float64 {
if a == nil {
return 0.0
}
cost := 0.0
for _, pv := range a.PVs {
cost += pv.Cost
}
return cost
}
// PVByteHours calculate cumulative ByteHours of all PVs that Allocation is attached to
func (a *Allocation) PVByteHours() float64 {
if a == nil {
return 0.0
}
byteHours := 0.0
for _, pv := range a.PVs {
byteHours += pv.ByteHours
}
return byteHours
}
// CPUEfficiency is the ratio of usage to request. If there is no request and
// no usage or cost, then efficiency is zero. If there is no request, but there
// is usage or cost, then efficiency is 100%.
func (a *Allocation) CPUEfficiency() float64 {
if a == nil {
return 0.0
}
if a.CPUCoreRequestAverage > 0 {
return a.CPUCoreUsageAverage / a.CPUCoreRequestAverage
}
if a.CPUCoreUsageAverage == 0.0 || a.CPUCost == 0.0 {
return 0.0
}
return 1.0
}
// RAMEfficiency is the ratio of usage to request. If there is no request and
// no usage or cost, then efficiency is zero. If there is no request, but there
// is usage or cost, then efficiency is 100%.
func (a *Allocation) RAMEfficiency() float64 {
if a == nil {
return 0.0
}
if a.RAMBytesRequestAverage > 0 {
return a.RAMBytesUsageAverage / a.RAMBytesRequestAverage
}
if a.RAMBytesUsageAverage == 0.0 || a.RAMCost == 0.0 {
return 0.0
}
return 1.0
}
// TotalEfficiency is the cost-weighted average of CPU and RAM efficiency. If
// there is no cost at all, then efficiency is zero.
func (a *Allocation) TotalEfficiency() float64 {
if a == nil {
return 0.0
}
if a.RAMTotalCost()+a.CPUTotalCost() > 0 {
ramCostEff := a.RAMEfficiency() * a.RAMTotalCost()
cpuCostEff := a.CPUEfficiency() * a.CPUTotalCost()
return (ramCostEff + cpuCostEff) / (a.CPUTotalCost() + a.RAMTotalCost())
}
return 0.0
}
// CPUCores converts the Allocation's CPUCoreHours into average CPUCores
func (a *Allocation) CPUCores() float64 {
if a.Minutes() <= 0.0 {
return 0.0
}
return a.CPUCoreHours / (a.Minutes() / 60.0)
}
// RAMBytes converts the Allocation's RAMByteHours into average RAMBytes
func (a *Allocation) RAMBytes() float64 {
if a.Minutes() <= 0.0 {
return 0.0
}
return a.RAMByteHours / (a.Minutes() / 60.0)
}
// GPUs converts the Allocation's GPUHours into average GPUs
func (a *Allocation) GPUs() float64 {
if a.Minutes() <= 0.0 {
return 0.0
}
return a.GPUHours / (a.Minutes() / 60.0)
}
// PVBytes converts the Allocation's PVByteHours into average PVBytes
func (a *Allocation) PVBytes() float64 {
if a.Minutes() <= 0.0 {
return 0.0
}
return a.PVByteHours() / (a.Minutes() / 60.0)
}
// ResetAdjustments sets all cost adjustment fields to zero
func (a *Allocation) ResetAdjustments() {
if a == nil {
return
}
a.CPUCostAdjustment = 0.0
a.GPUCostAdjustment = 0.0