allocation.go 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. package kubecost
  2. import (
  3. "bytes"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/opencost/opencost/pkg/log"
  10. "github.com/opencost/opencost/pkg/util"
  11. "github.com/opencost/opencost/pkg/util/json"
  12. )
  13. // TODO Clean-up use of IsEmpty; nil checks should be separated for safety.
  14. // TODO Consider making Allocation an interface, which is fulfilled by structs
  15. // like KubernetesAllocation, IdleAllocation, and ExternalAllocation.
  16. // ExternalSuffix indicates an external allocation
  17. const ExternalSuffix = "__external__"
  18. // IdleSuffix indicates an idle allocation property
  19. const IdleSuffix = "__idle__"
  20. // SharedSuffix indicates an shared allocation property
  21. const SharedSuffix = "__shared__"
  22. // UnallocatedSuffix indicates an unallocated allocation property
  23. const UnallocatedSuffix = "__unallocated__"
  24. // UnmountedSuffix indicated allocation to an unmounted PV
  25. const UnmountedSuffix = "__unmounted__"
  26. // ShareWeighted indicates that a shared resource should be shared as a
  27. // proportion of the cost of the remaining allocations.
  28. const ShareWeighted = "__weighted__"
  29. // ShareEven indicates that a shared resource should be shared evenly across
  30. // all remaining allocations.
  31. const ShareEven = "__even__"
  32. // ShareNone indicates that a shareable resource should not be shared
  33. const ShareNone = "__none__"
  34. // Allocation is a unit of resource allocation and cost for a given window
  35. // of time and for a given kubernetes construct with its associated set of
  36. // properties.
  37. // TODO:CLEANUP consider dropping name in favor of just Allocation and an
  38. // Assets-style key() function for AllocationSet.
  39. type Allocation struct {
  40. Name string `json:"name"`
  41. Properties *AllocationProperties `json:"properties,omitempty"`
  42. Window Window `json:"window"`
  43. Start time.Time `json:"start"`
  44. End time.Time `json:"end"`
  45. CPUCoreHours float64 `json:"cpuCoreHours"`
  46. CPUCoreRequestAverage float64 `json:"cpuCoreRequestAverage"`
  47. CPUCoreUsageAverage float64 `json:"cpuCoreUsageAverage"`
  48. CPUCost float64 `json:"cpuCost"`
  49. CPUCostAdjustment float64 `json:"cpuCostAdjustment"`
  50. GPUHours float64 `json:"gpuHours"`
  51. GPUCost float64 `json:"gpuCost"`
  52. GPUCostAdjustment float64 `json:"gpuCostAdjustment"`
  53. NetworkTransferBytes float64 `json:"networkTransferBytes"`
  54. NetworkReceiveBytes float64 `json:"networkReceiveBytes"`
  55. NetworkCost float64 `json:"networkCost"`
  56. NetworkCostAdjustment float64 `json:"networkCostAdjustment"`
  57. LoadBalancerCost float64 `json:"loadBalancerCost"`
  58. LoadBalancerCostAdjustment float64 `json:"loadBalancerCostAdjustment"`
  59. PVs PVAllocations `json:"-"`
  60. PVCostAdjustment float64 `json:"pvCostAdjustment"`
  61. RAMByteHours float64 `json:"ramByteHours"`
  62. RAMBytesRequestAverage float64 `json:"ramByteRequestAverage"`
  63. RAMBytesUsageAverage float64 `json:"ramByteUsageAverage"`
  64. RAMCost float64 `json:"ramCost"`
  65. RAMCostAdjustment float64 `json:"ramCostAdjustment"`
  66. SharedCost float64 `json:"sharedCost"`
  67. ExternalCost float64 `json:"externalCost"`
  68. // RawAllocationOnly is a pointer so if it is not present it will be
  69. // marshalled as null rather than as an object with Go default values.
  70. RawAllocationOnly *RawAllocationOnlyData `json:"rawAllocationOnly"`
  71. }
  72. // RawAllocationOnlyData is information that only belong in "raw" Allocations,
  73. // those which have not undergone aggregation, accumulation, or any other form
  74. // of combination to produce a new Allocation from other Allocations.
  75. //
  76. // Max usage data belongs here because computing the overall maximum from two
  77. // or more Allocations is a non-trivial operation that cannot be defined without
  78. // maintaining a large amount of state. Consider the following example:
  79. // _______________________________________________
  80. //
  81. // A1 Using 3 CPU ---- ----- ------
  82. // A2 Using 2 CPU ---- ----- ----
  83. // A3 Using 1 CPU --- --
  84. // _______________________________________________
  85. // Time ---->
  86. //
  87. // The logical maximum CPU usage is 5, but this cannot be calculated iteratively,
  88. // which is how we calculate aggregations and accumulations of Allocations currently.
  89. // This becomes a problem I could call "maximum sum of overlapping intervals" and is
  90. // essentially a variant of an interval scheduling algorithm.
  91. //
  92. // If we had types to differentiate between regular Allocations and AggregatedAllocations
  93. // then this type would be unnecessary and its fields would go into the regular Allocation
  94. // and not in the AggregatedAllocation.
  95. type RawAllocationOnlyData struct {
  96. CPUCoreUsageMax float64 `json:"cpuCoreUsageMax"`
  97. RAMBytesUsageMax float64 `json:"ramByteUsageMax"`
  98. }
  99. // PVAllocations is a map of Disk Asset Identifiers to the
  100. // usage of them by an Allocation as recorded in a PVAllocation
  101. type PVAllocations map[PVKey]*PVAllocation
  102. // Clone creates a deep copy of a PVAllocations
  103. func (pv *PVAllocations) Clone() PVAllocations {
  104. if pv == nil || *pv == nil {
  105. return nil
  106. }
  107. apv := *pv
  108. clonePV := make(map[PVKey]*PVAllocation, len(apv))
  109. for k, v := range apv {
  110. clonePV[k] = &PVAllocation{
  111. ByteHours: v.ByteHours,
  112. Cost: v.Cost,
  113. }
  114. }
  115. return clonePV
  116. }
  117. // Add adds contents of that to the calling PVAllocations
  118. func (pv *PVAllocations) Add(that PVAllocations) PVAllocations {
  119. apv := pv.Clone()
  120. if that != nil {
  121. if apv == nil {
  122. apv = PVAllocations{}
  123. }
  124. for pvKey, thatPVAlloc := range that {
  125. apvAlloc, ok := apv[pvKey]
  126. if !ok {
  127. apvAlloc = &PVAllocation{}
  128. }
  129. apvAlloc.Cost += thatPVAlloc.Cost
  130. apvAlloc.ByteHours += thatPVAlloc.ByteHours
  131. apv[pvKey] = apvAlloc
  132. }
  133. }
  134. return apv
  135. }
  136. // PVKey for identifying Disk type assets
  137. type PVKey struct {
  138. Cluster string `json:"cluster"`
  139. Name string `json:"name"`
  140. }
  141. // PVAllocation contains the byte hour usage
  142. // and cost of an Allocation for a single PV
  143. type PVAllocation struct {
  144. ByteHours float64 `json:"byteHours"`
  145. Cost float64 `json:"cost"`
  146. }
  147. // AllocationMatchFunc is a function that can be used to match Allocations by
  148. // returning true for any given Allocation if a condition is met.
  149. type AllocationMatchFunc func(*Allocation) bool
  150. // Add returns the result of summing the two given Allocations, which sums the
  151. // summary fields (e.g. costs, resources) and recomputes efficiency. Neither of
  152. // the two original Allocations are mutated in the process.
  153. func (a *Allocation) Add(that *Allocation) (*Allocation, error) {
  154. if a == nil {
  155. return that.Clone(), nil
  156. }
  157. if that == nil {
  158. return a.Clone(), nil
  159. }
  160. // Note: no need to clone "that", as add only mutates the receiver
  161. agg := a.Clone()
  162. agg.add(that)
  163. return agg, nil
  164. }
  165. // Clone returns a deep copy of the given Allocation
  166. func (a *Allocation) Clone() *Allocation {
  167. if a == nil {
  168. return nil
  169. }
  170. return &Allocation{
  171. Name: a.Name,
  172. Properties: a.Properties.Clone(),
  173. Window: a.Window.Clone(),
  174. Start: a.Start,
  175. End: a.End,
  176. CPUCoreHours: a.CPUCoreHours,
  177. CPUCoreRequestAverage: a.CPUCoreRequestAverage,
  178. CPUCoreUsageAverage: a.CPUCoreUsageAverage,
  179. CPUCost: a.CPUCost,
  180. CPUCostAdjustment: a.CPUCostAdjustment,
  181. GPUHours: a.GPUHours,
  182. GPUCost: a.GPUCost,
  183. GPUCostAdjustment: a.GPUCostAdjustment,
  184. NetworkTransferBytes: a.NetworkTransferBytes,
  185. NetworkReceiveBytes: a.NetworkReceiveBytes,
  186. NetworkCost: a.NetworkCost,
  187. NetworkCostAdjustment: a.NetworkCostAdjustment,
  188. LoadBalancerCost: a.LoadBalancerCost,
  189. LoadBalancerCostAdjustment: a.LoadBalancerCostAdjustment,
  190. PVs: a.PVs.Clone(),
  191. PVCostAdjustment: a.PVCostAdjustment,
  192. RAMByteHours: a.RAMByteHours,
  193. RAMBytesRequestAverage: a.RAMBytesRequestAverage,
  194. RAMBytesUsageAverage: a.RAMBytesUsageAverage,
  195. RAMCost: a.RAMCost,
  196. RAMCostAdjustment: a.RAMCostAdjustment,
  197. SharedCost: a.SharedCost,
  198. ExternalCost: a.ExternalCost,
  199. RawAllocationOnly: a.RawAllocationOnly.Clone(),
  200. }
  201. }
  202. // Clone returns a deep copy of the given RawAllocationOnlyData
  203. func (r *RawAllocationOnlyData) Clone() *RawAllocationOnlyData {
  204. if r == nil {
  205. return nil
  206. }
  207. return &RawAllocationOnlyData{
  208. CPUCoreUsageMax: r.CPUCoreUsageMax,
  209. RAMBytesUsageMax: r.RAMBytesUsageMax,
  210. }
  211. }
  212. // Equal returns true if the values held in the given Allocation precisely
  213. // match those of the receiving Allocation. nil does not match nil. Floating
  214. // point values need to match according to util.IsApproximately, which accounts
  215. // for small, reasonable floating point error margins.
  216. func (a *Allocation) Equal(that *Allocation) bool {
  217. if a == nil || that == nil {
  218. return false
  219. }
  220. if a.Name != that.Name {
  221. return false
  222. }
  223. if !a.Properties.Equal(that.Properties) {
  224. return false
  225. }
  226. if !a.Window.Equal(that.Window) {
  227. return false
  228. }
  229. if !a.Start.Equal(that.Start) {
  230. return false
  231. }
  232. if !a.End.Equal(that.End) {
  233. return false
  234. }
  235. if !util.IsApproximately(a.CPUCoreHours, that.CPUCoreHours) {
  236. return false
  237. }
  238. if !util.IsApproximately(a.CPUCost, that.CPUCost) {
  239. return false
  240. }
  241. if !util.IsApproximately(a.CPUCostAdjustment, that.CPUCostAdjustment) {
  242. return false
  243. }
  244. if !util.IsApproximately(a.GPUHours, that.GPUHours) {
  245. return false
  246. }
  247. if !util.IsApproximately(a.GPUCost, that.GPUCost) {
  248. return false
  249. }
  250. if !util.IsApproximately(a.GPUCostAdjustment, that.GPUCostAdjustment) {
  251. return false
  252. }
  253. if !util.IsApproximately(a.NetworkTransferBytes, that.NetworkTransferBytes) {
  254. return false
  255. }
  256. if !util.IsApproximately(a.NetworkReceiveBytes, that.NetworkReceiveBytes) {
  257. return false
  258. }
  259. if !util.IsApproximately(a.NetworkCost, that.NetworkCost) {
  260. return false
  261. }
  262. if !util.IsApproximately(a.NetworkCostAdjustment, that.NetworkCostAdjustment) {
  263. return false
  264. }
  265. if !util.IsApproximately(a.LoadBalancerCost, that.LoadBalancerCost) {
  266. return false
  267. }
  268. if !util.IsApproximately(a.LoadBalancerCostAdjustment, that.LoadBalancerCostAdjustment) {
  269. return false
  270. }
  271. if !util.IsApproximately(a.PVCostAdjustment, that.PVCostAdjustment) {
  272. return false
  273. }
  274. if !util.IsApproximately(a.RAMByteHours, that.RAMByteHours) {
  275. return false
  276. }
  277. if !util.IsApproximately(a.RAMCost, that.RAMCost) {
  278. return false
  279. }
  280. if !util.IsApproximately(a.RAMCostAdjustment, that.RAMCostAdjustment) {
  281. return false
  282. }
  283. if !util.IsApproximately(a.SharedCost, that.SharedCost) {
  284. return false
  285. }
  286. if !util.IsApproximately(a.ExternalCost, that.ExternalCost) {
  287. return false
  288. }
  289. if a.RawAllocationOnly == nil && that.RawAllocationOnly != nil {
  290. return false
  291. }
  292. if a.RawAllocationOnly != nil && that.RawAllocationOnly == nil {
  293. return false
  294. }
  295. if a.RawAllocationOnly != nil && that.RawAllocationOnly != nil {
  296. if !util.IsApproximately(a.RawAllocationOnly.CPUCoreUsageMax, that.RawAllocationOnly.CPUCoreUsageMax) {
  297. return false
  298. }
  299. if !util.IsApproximately(a.RawAllocationOnly.RAMBytesUsageMax, that.RawAllocationOnly.RAMBytesUsageMax) {
  300. return false
  301. }
  302. }
  303. aPVs := a.PVs
  304. thatPVs := that.PVs
  305. if len(aPVs) == len(thatPVs) {
  306. for k, pv := range aPVs {
  307. tv, ok := thatPVs[k]
  308. if !ok || *tv != *pv {
  309. return false
  310. }
  311. }
  312. } else {
  313. return false
  314. }
  315. return true
  316. }
  317. // TotalCost is the total cost of the Allocation including adjustments
  318. func (a *Allocation) TotalCost() float64 {
  319. if a == nil {
  320. return 0.0
  321. }
  322. return a.CPUTotalCost() + a.GPUTotalCost() + a.RAMTotalCost() + a.PVTotalCost() + a.NetworkTotalCost() + a.LBTotalCost() + a.SharedTotalCost() + a.ExternalCost
  323. }
  324. // CPUTotalCost calculates total CPU cost of Allocation including adjustment
  325. func (a *Allocation) CPUTotalCost() float64 {
  326. if a == nil {
  327. return 0.0
  328. }
  329. return a.CPUCost + a.CPUCostAdjustment
  330. }
  331. // GPUTotalCost calculates total GPU cost of Allocation including adjustment
  332. func (a *Allocation) GPUTotalCost() float64 {
  333. if a == nil {
  334. return 0.0
  335. }
  336. return a.GPUCost + a.GPUCostAdjustment
  337. }
  338. // RAMTotalCost calculates total RAM cost of Allocation including adjustment
  339. func (a *Allocation) RAMTotalCost() float64 {
  340. if a == nil {
  341. return 0.0
  342. }
  343. return a.RAMCost + a.RAMCostAdjustment
  344. }
  345. // PVTotalCost calculates total PV cost of Allocation including adjustment
  346. func (a *Allocation) PVTotalCost() float64 {
  347. if a == nil {
  348. return 0.0
  349. }
  350. return a.PVCost() + a.PVCostAdjustment
  351. }
  352. // NetworkTotalCost calculates total Network cost of Allocation including adjustment
  353. func (a *Allocation) NetworkTotalCost() float64 {
  354. if a == nil {
  355. return 0.0
  356. }
  357. return a.NetworkCost + a.NetworkCostAdjustment
  358. }
  359. // LBTotalCost calculates total LB cost of Allocation including adjustment
  360. // TODO deprecate
  361. func (a *Allocation) LBTotalCost() float64 {
  362. return a.LoadBalancerTotalCost()
  363. }
  364. // LoadBalancerTotalCost calculates total LB cost of Allocation including adjustment
  365. func (a *Allocation) LoadBalancerTotalCost() float64 {
  366. if a == nil {
  367. return 0.0
  368. }
  369. return a.LoadBalancerCost + a.LoadBalancerCostAdjustment
  370. }
  371. // SharedTotalCost calculates total shared cost of Allocation including adjustment
  372. func (a *Allocation) SharedTotalCost() float64 {
  373. if a == nil {
  374. return 0.0
  375. }
  376. return a.SharedCost
  377. }
  378. // PVCost calculate cumulative cost of all PVs that Allocation is attached to
  379. func (a *Allocation) PVCost() float64 {
  380. if a == nil {
  381. return 0.0
  382. }
  383. cost := 0.0
  384. for _, pv := range a.PVs {
  385. cost += pv.Cost
  386. }
  387. return cost
  388. }
  389. // PVByteHours calculate cumulative ByteHours of all PVs that Allocation is attached to
  390. func (a *Allocation) PVByteHours() float64 {
  391. if a == nil {
  392. return 0.0
  393. }
  394. byteHours := 0.0
  395. for _, pv := range a.PVs {
  396. byteHours += pv.ByteHours
  397. }
  398. return byteHours
  399. }
  400. // CPUEfficiency is the ratio of usage to request. If there is no request and
  401. // no usage or cost, then efficiency is zero. If there is no request, but there
  402. // is usage or cost, then efficiency is 100%.
  403. func (a *Allocation) CPUEfficiency() float64 {
  404. if a == nil {
  405. return 0.0
  406. }
  407. if a.CPUCoreRequestAverage > 0 {
  408. return a.CPUCoreUsageAverage / a.CPUCoreRequestAverage
  409. }
  410. if a.CPUCoreUsageAverage == 0.0 || a.CPUCost == 0.0 {
  411. return 0.0
  412. }
  413. return 1.0
  414. }
  415. // RAMEfficiency is the ratio of usage to request. If there is no request and
  416. // no usage or cost, then efficiency is zero. If there is no request, but there
  417. // is usage or cost, then efficiency is 100%.
  418. func (a *Allocation) RAMEfficiency() float64 {
  419. if a == nil {
  420. return 0.0
  421. }
  422. if a.RAMBytesRequestAverage > 0 {
  423. return a.RAMBytesUsageAverage / a.RAMBytesRequestAverage
  424. }
  425. if a.RAMBytesUsageAverage == 0.0 || a.RAMCost == 0.0 {
  426. return 0.0
  427. }
  428. return 1.0
  429. }
  430. // TotalEfficiency is the cost-weighted average of CPU and RAM efficiency. If
  431. // there is no cost at all, then efficiency is zero.
  432. func (a *Allocation) TotalEfficiency() float64 {
  433. if a == nil {
  434. return 0.0
  435. }
  436. if a.RAMTotalCost()+a.CPUTotalCost() > 0 {
  437. ramCostEff := a.RAMEfficiency() * a.RAMTotalCost()
  438. cpuCostEff := a.CPUEfficiency() * a.CPUTotalCost()
  439. return (ramCostEff + cpuCostEff) / (a.CPUTotalCost() + a.RAMTotalCost())
  440. }
  441. return 0.0
  442. }
  443. // CPUCores converts the Allocation's CPUCoreHours into average CPUCores
  444. func (a *Allocation) CPUCores() float64 {
  445. if a.Minutes() <= 0.0 {
  446. return 0.0
  447. }
  448. return a.CPUCoreHours / (a.Minutes() / 60.0)
  449. }
  450. // RAMBytes converts the Allocation's RAMByteHours into average RAMBytes
  451. func (a *Allocation) RAMBytes() float64 {
  452. if a.Minutes() <= 0.0 {
  453. return 0.0
  454. }
  455. return a.RAMByteHours / (a.Minutes() / 60.0)
  456. }
  457. // GPUs converts the Allocation's GPUHours into average GPUs
  458. func (a *Allocation) GPUs() float64 {
  459. if a.Minutes() <= 0.0 {
  460. return 0.0
  461. }
  462. return a.GPUHours / (a.Minutes() / 60.0)
  463. }
  464. // PVBytes converts the Allocation's PVByteHours into average PVBytes
  465. func (a *Allocation) PVBytes() float64 {
  466. if a.Minutes() <= 0.0 {
  467. return 0.0
  468. }
  469. return a.PVByteHours() / (a.Minutes() / 60.0)
  470. }
  471. // ResetAdjustments sets all cost adjustment fields to zero
  472. func (a *Allocation) ResetAdjustments() {
  473. if a == nil {
  474. return
  475. }
  476. a.CPUCostAdjustment = 0.0
  477. a.GPUCostAdjustment = 0.0
  478. a.RAMCostAdjustment = 0.0
  479. a.PVCostAdjustment = 0.0
  480. a.NetworkCostAdjustment = 0.0
  481. a.LoadBalancerCostAdjustment = 0.0
  482. }
  483. // MarshalJSON implements json.Marshaler interface
  484. func (a *Allocation) MarshalJSON() ([]byte, error) {
  485. buffer := bytes.NewBufferString("{")
  486. jsonEncodeString(buffer, "name", a.Name, ",")
  487. jsonEncode(buffer, "properties", a.Properties, ",")
  488. jsonEncode(buffer, "window", a.Window, ",")
  489. jsonEncodeString(buffer, "start", a.Start.Format(time.RFC3339), ",")
  490. jsonEncodeString(buffer, "end", a.End.Format(time.RFC3339), ",")
  491. jsonEncodeFloat64(buffer, "minutes", a.Minutes(), ",")
  492. jsonEncodeFloat64(buffer, "cpuCores", a.CPUCores(), ",")
  493. jsonEncodeFloat64(buffer, "cpuCoreRequestAverage", a.CPUCoreRequestAverage, ",")
  494. jsonEncodeFloat64(buffer, "cpuCoreUsageAverage", a.CPUCoreUsageAverage, ",")
  495. jsonEncodeFloat64(buffer, "cpuCoreHours", a.CPUCoreHours, ",")
  496. jsonEncodeFloat64(buffer, "cpuCost", a.CPUCost, ",")
  497. jsonEncodeFloat64(buffer, "cpuCostAdjustment", a.CPUCostAdjustment, ",")
  498. jsonEncodeFloat64(buffer, "cpuEfficiency", a.CPUEfficiency(), ",")
  499. jsonEncodeFloat64(buffer, "gpuCount", a.GPUs(), ",")
  500. jsonEncodeFloat64(buffer, "gpuHours", a.GPUHours, ",")
  501. jsonEncodeFloat64(buffer, "gpuCost", a.GPUCost, ",")
  502. jsonEncodeFloat64(buffer, "gpuCostAdjustment", a.GPUCostAdjustment, ",")
  503. jsonEncodeFloat64(buffer, "networkTransferBytes", a.NetworkTransferBytes, ",")
  504. jsonEncodeFloat64(buffer, "networkReceiveBytes", a.NetworkReceiveBytes, ",")
  505. jsonEncodeFloat64(buffer, "networkCost", a.NetworkCost, ",")
  506. jsonEncodeFloat64(buffer, "networkCostAdjustment", a.NetworkCostAdjustment, ",")
  507. jsonEncodeFloat64(buffer, "loadBalancerCost", a.LoadBalancerCost, ",")
  508. jsonEncodeFloat64(buffer, "loadBalancerCostAdjustment", a.LoadBalancerCostAdjustment, ",")
  509. jsonEncodeFloat64(buffer, "pvBytes", a.PVBytes(), ",")
  510. jsonEncodeFloat64(buffer, "pvByteHours", a.PVByteHours(), ",")
  511. jsonEncodeFloat64(buffer, "pvCost", a.PVCost(), ",")
  512. jsonEncode(buffer, "pvs", a.PVs, ",") // Todo Sean: this does not work properly
  513. jsonEncodeFloat64(buffer, "pvCostAdjustment", a.PVCostAdjustment, ",")
  514. jsonEncodeFloat64(buffer, "ramBytes", a.RAMBytes(), ",")
  515. jsonEncodeFloat64(buffer, "ramByteRequestAverage", a.RAMBytesRequestAverage, ",")
  516. jsonEncodeFloat64(buffer, "ramByteUsageAverage", a.RAMBytesUsageAverage, ",")
  517. jsonEncodeFloat64(buffer, "ramByteHours", a.RAMByteHours, ",")
  518. jsonEncodeFloat64(buffer, "ramCost", a.RAMCost, ",")
  519. jsonEncodeFloat64(buffer, "ramCostAdjustment", a.RAMCostAdjustment, ",")
  520. jsonEncodeFloat64(buffer, "ramEfficiency", a.RAMEfficiency(), ",")
  521. jsonEncodeFloat64(buffer, "sharedCost", a.SharedCost, ",")
  522. jsonEncodeFloat64(buffer, "externalCost", a.ExternalCost, ",")
  523. jsonEncodeFloat64(buffer, "totalCost", a.TotalCost(), ",")
  524. jsonEncodeFloat64(buffer, "totalEfficiency", a.TotalEfficiency(), ",")
  525. jsonEncode(buffer, "rawAllocationOnly", a.RawAllocationOnly, "")
  526. buffer.WriteString("}")
  527. return buffer.Bytes(), nil
  528. }
  529. // Resolution returns the duration of time covered by the Allocation
  530. func (a *Allocation) Resolution() time.Duration {
  531. return a.End.Sub(a.Start)
  532. }
  533. // IsAggregated is true if the given Allocation has been aggregated, which we
  534. // define by a lack of AllocationProperties.
  535. func (a *Allocation) IsAggregated() bool {
  536. return a == nil || a.Properties == nil
  537. }
  538. // IsExternal is true if the given Allocation represents external costs.
  539. func (a *Allocation) IsExternal() bool {
  540. if a == nil {
  541. return false
  542. }
  543. return strings.Contains(a.Name, ExternalSuffix)
  544. }
  545. // IsIdle is true if the given Allocation represents idle costs.
  546. func (a *Allocation) IsIdle() bool {
  547. if a == nil {
  548. return false
  549. }
  550. return strings.Contains(a.Name, IdleSuffix)
  551. }
  552. // IsUnallocated is true if the given Allocation represents unallocated costs.
  553. func (a *Allocation) IsUnallocated() bool {
  554. if a == nil {
  555. return false
  556. }
  557. return strings.Contains(a.Name, UnallocatedSuffix)
  558. }
  559. // IsUnmounted is true if the given Allocation represents unmounted volume costs.
  560. func (a *Allocation) IsUnmounted() bool {
  561. if a == nil {
  562. return false
  563. }
  564. return strings.Contains(a.Name, UnmountedSuffix)
  565. }
  566. // Minutes returns the number of minutes the Allocation represents, as defined
  567. // by the difference between the end and start times.
  568. func (a *Allocation) Minutes() float64 {
  569. if a == nil {
  570. return 0.0
  571. }
  572. return a.End.Sub(a.Start).Minutes()
  573. }
  574. // Share adds the TotalCost of the given Allocation to the SharedCost of the
  575. // receiving Allocation. No Start, End, Window, or AllocationProperties are considered.
  576. // Neither Allocation is mutated; a new Allocation is always returned.
  577. func (a *Allocation) Share(that *Allocation) (*Allocation, error) {
  578. if that == nil {
  579. return a.Clone(), nil
  580. }
  581. if a == nil {
  582. return nil, fmt.Errorf("cannot share with nil Allocation")
  583. }
  584. agg := a.Clone()
  585. agg.SharedCost += that.TotalCost()
  586. return agg, nil
  587. }
  588. // String represents the given Allocation as a string
  589. func (a *Allocation) String() string {
  590. if a == nil {
  591. return "<nil>"
  592. }
  593. return fmt.Sprintf("%s%s=%.2f", a.Name, NewWindow(&a.Start, &a.End), a.TotalCost())
  594. }
  595. func (a *Allocation) add(that *Allocation) {
  596. if a == nil {
  597. log.Warnf("Allocation.AggregateBy: trying to add a nil receiver")
  598. return
  599. }
  600. // Generate keys for each allocation to allow for special logic to set the controller
  601. // in the case of keys matching but controllers not matching.
  602. aggByForKey := []string{"cluster", "node", "namespace", "pod", "container"}
  603. leftKey := a.generateKey(aggByForKey, nil)
  604. rightKey := that.generateKey(aggByForKey, nil)
  605. leftProperties := a.Properties
  606. rightProperties := that.Properties
  607. // Preserve string properties that are matching between the two allocations
  608. a.Properties = a.Properties.Intersection(that.Properties)
  609. // Overwrite regular intersection logic for the controller name property in the
  610. // case that the Allocation keys are the same but the controllers are not.
  611. if leftKey == rightKey &&
  612. leftProperties != nil &&
  613. rightProperties != nil &&
  614. leftProperties.Controller != rightProperties.Controller {
  615. if leftProperties.Controller == "" {
  616. a.Properties.Controller = rightProperties.Controller
  617. } else if rightProperties.Controller == "" {
  618. a.Properties.Controller = leftProperties.Controller
  619. } else {
  620. controllers := []string{
  621. leftProperties.Controller,
  622. rightProperties.Controller,
  623. }
  624. sort.Strings(controllers)
  625. a.Properties.Controller = controllers[0]
  626. }
  627. }
  628. // Expand the window to encompass both Allocations
  629. a.Window = a.Window.Expand(that.Window)
  630. // Sum non-cumulative fields by turning them into cumulative, adding them,
  631. // and then converting them back into averages after minutes have been
  632. // combined (just below).
  633. cpuReqCoreMins := a.CPUCoreRequestAverage * a.Minutes()
  634. cpuReqCoreMins += that.CPUCoreRequestAverage * that.Minutes()
  635. cpuUseCoreMins := a.CPUCoreUsageAverage * a.Minutes()
  636. cpuUseCoreMins += that.CPUCoreUsageAverage * that.Minutes()
  637. ramReqByteMins := a.RAMBytesRequestAverage * a.Minutes()
  638. ramReqByteMins += that.RAMBytesRequestAverage * that.Minutes()
  639. ramUseByteMins := a.RAMBytesUsageAverage * a.Minutes()
  640. ramUseByteMins += that.RAMBytesUsageAverage * that.Minutes()
  641. // Expand Start and End to be the "max" of among the given Allocations
  642. if that.Start.Before(a.Start) {
  643. a.Start = that.Start
  644. }
  645. if that.End.After(a.End) {
  646. a.End = that.End
  647. }
  648. // Convert cumulative request and usage back into rates
  649. // TODO:TEST write a unit test that fails if this is done incorrectly
  650. if a.Minutes() > 0 {
  651. a.CPUCoreRequestAverage = cpuReqCoreMins / a.Minutes()
  652. a.CPUCoreUsageAverage = cpuUseCoreMins / a.Minutes()
  653. a.RAMBytesRequestAverage = ramReqByteMins / a.Minutes()
  654. a.RAMBytesUsageAverage = ramUseByteMins / a.Minutes()
  655. } else {
  656. a.CPUCoreRequestAverage = 0.0
  657. a.CPUCoreUsageAverage = 0.0
  658. a.RAMBytesRequestAverage = 0.0
  659. a.RAMBytesUsageAverage = 0.0
  660. }
  661. // Sum all cumulative resource fields
  662. a.CPUCoreHours += that.CPUCoreHours
  663. a.GPUHours += that.GPUHours
  664. a.RAMByteHours += that.RAMByteHours
  665. a.NetworkTransferBytes += that.NetworkTransferBytes
  666. a.NetworkReceiveBytes += that.NetworkReceiveBytes
  667. // Sum all cumulative cost fields
  668. a.CPUCost += that.CPUCost
  669. a.GPUCost += that.GPUCost
  670. a.RAMCost += that.RAMCost
  671. a.NetworkCost += that.NetworkCost
  672. a.LoadBalancerCost += that.LoadBalancerCost
  673. a.SharedCost += that.SharedCost
  674. a.ExternalCost += that.ExternalCost
  675. // Sum PVAllocations
  676. a.PVs = a.PVs.Add(that.PVs)
  677. // Sum all cumulative adjustment fields
  678. a.CPUCostAdjustment += that.CPUCostAdjustment
  679. a.RAMCostAdjustment += that.RAMCostAdjustment
  680. a.GPUCostAdjustment += that.GPUCostAdjustment
  681. a.PVCostAdjustment += that.PVCostAdjustment
  682. a.NetworkCostAdjustment += that.NetworkCostAdjustment
  683. a.LoadBalancerCostAdjustment += that.LoadBalancerCostAdjustment
  684. // Any data that is in a "raw allocation only" is not valid in any
  685. // sort of cumulative Allocation (like one that is added).
  686. a.RawAllocationOnly = nil
  687. }
  688. // AllocationSet stores a set of Allocations, each with a unique name, that share
  689. // a window. An AllocationSet is mutable, so treat it like a threadsafe map.
  690. type AllocationSet struct {
  691. sync.RWMutex
  692. allocations map[string]*Allocation
  693. externalKeys map[string]bool
  694. idleKeys map[string]bool
  695. FromSource string // stores the name of the source used to compute the data
  696. Window Window
  697. Warnings []string
  698. Errors []string
  699. }
  700. // NewAllocationSet instantiates a new AllocationSet and, optionally, inserts
  701. // the given list of Allocations
  702. func NewAllocationSet(start, end time.Time, allocs ...*Allocation) *AllocationSet {
  703. as := &AllocationSet{
  704. allocations: map[string]*Allocation{},
  705. externalKeys: map[string]bool{},
  706. idleKeys: map[string]bool{},
  707. Window: NewWindow(&start, &end),
  708. }
  709. for _, a := range allocs {
  710. as.Insert(a)
  711. }
  712. return as
  713. }
  714. // AllocationAggregationOptions provide advanced functionality to AggregateBy, including
  715. // filtering results and sharing allocations. FilterFuncs are a list of match
  716. // functions such that, if any function fails, the allocation is ignored.
  717. // ShareFuncs are a list of match functions such that, if any function
  718. // succeeds, the allocation is marked as a shared resource. ShareIdle is a
  719. // simple flag for sharing idle resources.
  720. type AllocationAggregationOptions struct {
  721. AllocationTotalsStore AllocationTotalsStore
  722. Filter AllocationFilter
  723. IdleByNode bool
  724. LabelConfig *LabelConfig
  725. MergeUnallocated bool
  726. Reconcile bool
  727. ReconcileNetwork bool
  728. ShareFuncs []AllocationMatchFunc
  729. ShareIdle string
  730. ShareSplit string
  731. SharedHourlyCosts map[string]float64
  732. SplitIdle bool
  733. }
  734. // AggregateBy aggregates the Allocations in the given AllocationSet by the given
  735. // AllocationProperty. This will only be legal if the AllocationSet is divisible by the
  736. // given AllocationProperty; e.g. Containers can be divided by Namespace, but not vice-a-versa.
  737. func (as *AllocationSet) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  738. // The order of operations for aggregating allocations is as follows:
  739. //
  740. // 1. Partition external, idle, and shared allocations into separate sets.
  741. // Also, create the aggSet into which the results will be aggregated.
  742. //
  743. // 2. Compute sharing coefficients for idle and shared resources
  744. // a) if idle allocation is to be shared, compute idle coefficients
  745. // b) if idle allocation is NOT shared, but filters are present, compute
  746. // idle filtration coefficients for the purpose of only returning the
  747. // portion of idle allocation that would have been shared with the
  748. // unfiltered results. (See unit tests 5.a,b,c)
  749. // c) generate shared allocation for them given shared overhead, which
  750. // must happen after (2a) and (2b)
  751. // d) if there are shared resources, compute share coefficients
  752. //
  753. // 3. Drop any allocation that fails any of the filters
  754. //
  755. // 4. Distribute idle allocations according to the idle coefficients
  756. //
  757. // 5. Generate aggregation key and insert allocation into the output set
  758. //
  759. // 6. If idle is shared and resources are shared, some idle might be shared
  760. // with a shared resource. Distribute that to the shared resources
  761. // prior to sharing them with the aggregated results.
  762. //
  763. // 7. Apply idle filtration coefficients from step (2b)
  764. //
  765. // 8. Distribute shared allocations according to the share coefficients.
  766. //
  767. // 9. If there are external allocations that can be aggregated into
  768. // the output (i.e. they can be used to generate a valid key for
  769. // the given properties) then aggregate; otherwise... ignore them?
  770. //
  771. // 10. Distribute any undistributed idle, in the case that idle
  772. // coefficients end up being zero and some idle is not shared.
  773. //
  774. // 11. If the merge idle option is enabled, merge any remaining idle
  775. // allocations into a single idle allocation. If there was any idle
  776. // whose costs were not distributed because there was no usage of a
  777. // specific resource type, re-add the idle to the aggregation with
  778. // only that type.
  779. if as.IsEmpty() {
  780. return nil
  781. }
  782. if options == nil {
  783. options = &AllocationAggregationOptions{}
  784. }
  785. if options.LabelConfig == nil {
  786. options.LabelConfig = NewLabelConfig()
  787. }
  788. // idleFiltrationCoefficients relies on this being explicitly set
  789. if options.ShareIdle != ShareWeighted {
  790. options.ShareIdle = ShareNone
  791. }
  792. // Pre-flatten the filter so we can just check == nil to see if there are
  793. // filters.
  794. if options.Filter != nil {
  795. options.Filter = options.Filter.Flattened()
  796. }
  797. var allocatedTotalsMap map[string]map[string]float64
  798. // If aggregateBy is nil, we don't aggregate anything. On the other hand,
  799. // an empty slice implies that we should aggregate everything. See
  800. // generateKey for why that makes sense.
  801. shouldAggregate := aggregateBy != nil
  802. shouldFilter := options.Filter != nil
  803. shouldShare := len(options.SharedHourlyCosts) > 0 || len(options.ShareFuncs) > 0
  804. if !shouldAggregate && !shouldFilter && !shouldShare && options.ShareIdle == ShareNone {
  805. // There is nothing for AggregateBy to do, so simply return nil
  806. return nil
  807. }
  808. // aggSet will collect the aggregated allocations
  809. aggSet := &AllocationSet{
  810. Window: as.Window.Clone(),
  811. }
  812. // externalSet will collect external allocations
  813. externalSet := &AllocationSet{
  814. Window: as.Window.Clone(),
  815. }
  816. // idleSet will be shared among aggSet after initial aggregation
  817. // is complete
  818. idleSet := &AllocationSet{
  819. Window: as.Window.Clone(),
  820. }
  821. // shareSet will be shared among aggSet after initial aggregation
  822. // is complete
  823. shareSet := &AllocationSet{
  824. Window: as.Window.Clone(),
  825. }
  826. as.Lock()
  827. defer as.Unlock()
  828. // (1) Loop and find all of the external, idle, and shared allocations. Add
  829. // them to their respective sets, removing them from the set of allocations
  830. // to aggregate.
  831. for _, alloc := range as.allocations {
  832. // External allocations get aggregated post-hoc (see step 6) and do
  833. // not necessarily contain complete sets of properties, so they are
  834. // moved to a separate AllocationSet.
  835. if alloc.IsExternal() {
  836. delete(as.externalKeys, alloc.Name)
  837. delete(as.allocations, alloc.Name)
  838. externalSet.Insert(alloc)
  839. continue
  840. }
  841. // Idle allocations should be separated into idleSet if they are to be
  842. // shared later on. If they are not to be shared, then add them to the
  843. // aggSet like any other allocation.
  844. if alloc.IsIdle() {
  845. delete(as.idleKeys, alloc.Name)
  846. delete(as.allocations, alloc.Name)
  847. if options.ShareIdle == ShareEven || options.ShareIdle == ShareWeighted {
  848. idleSet.Insert(alloc)
  849. } else {
  850. aggSet.Insert(alloc)
  851. }
  852. continue
  853. }
  854. // Shared allocations must be identified and separated prior to
  855. // aggregation and filtering. That is, if any of the ShareFuncs return
  856. // true for the allocation, then move it to shareSet.
  857. for _, sf := range options.ShareFuncs {
  858. if sf(alloc) {
  859. delete(as.idleKeys, alloc.Name)
  860. delete(as.allocations, alloc.Name)
  861. shareSet.Insert(alloc)
  862. break
  863. }
  864. }
  865. }
  866. // It's possible that no more un-shared, non-idle, non-external allocations
  867. // remain at this point. This always results in an emptySet, so return early.
  868. if len(as.allocations) == 0 {
  869. emptySet := &AllocationSet{
  870. Window: as.Window.Clone(),
  871. }
  872. as.allocations = emptySet.allocations
  873. return nil
  874. }
  875. // (2) In order to correctly share idle and shared costs, we first compute
  876. // sharing coefficients, which represent the proportion of each cost to
  877. // share with each allocation. Idle allocations are shared per-cluster or per-node,
  878. // per-allocation, and per-resource, while shared resources are shared per-
  879. // allocation only.
  880. //
  881. // For an idleCoefficient example, the entries:
  882. // [cluster1][cluster1/namespace1/pod1/container1][cpu] = 0.166667
  883. // [cluster1][cluster1/namespace1/pod1/container1][gpu] = 0.166667
  884. // [cluster1][cluster1/namespace1/pod1/container1][ram] = 0.687500
  885. // mean that the allocation "cluster1/namespace1/pod1/container1" will
  886. // receive 16.67% of cluster1's idle CPU and GPU costs and 68.75% of its
  887. // RAM costs.
  888. //
  889. // For a shareCoefficient example, the entries:
  890. // [namespace2] = 0.666667
  891. // [__filtered__] = 0.333333
  892. // mean that the post-aggregation allocation "namespace2" will receive
  893. // 66.67% of the shared resource costs, while the remaining 33.33% will
  894. // be filtered out, as they were shared with allocations that did not pass
  895. // one of the given filters.
  896. //
  897. // In order to maintain stable results when multiple operations are being
  898. // carried out (e.g. sharing idle, sharing resources, and filtering) these
  899. // coefficients are computed for the full set of allocations prior to
  900. // adding shared overhead and prior to applying filters.
  901. var err error
  902. // (2a) If there are idle costs to be shared, compute the coefficients for
  903. // sharing them among the non-idle, non-aggregated allocations (including
  904. // the shared allocations).
  905. var idleCoefficients map[string]map[string]map[string]float64
  906. if idleSet.Length() > 0 && options.ShareIdle != ShareNone {
  907. idleCoefficients, allocatedTotalsMap, err = computeIdleCoeffs(options, as, shareSet)
  908. if err != nil {
  909. log.Warnf("AllocationSet.AggregateBy: compute idle coeff: %s", err)
  910. return fmt.Errorf("error computing idle coefficients: %s", err)
  911. }
  912. }
  913. // (2b) If idle costs are not to be shared, but there are filters, then we
  914. // need to track the amount of each idle allocation to "filter" in order to
  915. // maintain parity with the results when idle is shared. That is, we want
  916. // to return only the idle costs that would have been shared with the given
  917. // results, even if the filter had not been applied.
  918. //
  919. // For example, consider these results from aggregating by namespace with
  920. // two clusters:
  921. //
  922. // namespace1: 25.00
  923. // namespace2: 30.00
  924. // namespace3: 15.00
  925. // idle: 30.00
  926. //
  927. // When we then filter by cluster==cluster1, namespaces 2 and 3 are
  928. // reduced by the amount that existed on cluster2. Then, idle must also be
  929. // reduced by the relevant amount:
  930. //
  931. // namespace1: 25.00
  932. // namespace2: 15.00
  933. // idle: 20.00
  934. //
  935. // Note that this can happen for any field, not just cluster, so we again
  936. // need to track this on a per-cluster or per-node, per-allocation, per-resource basis.
  937. var idleFiltrationCoefficients map[string]map[string]map[string]float64
  938. if shouldFilter && options.ShareIdle == ShareNone {
  939. idleFiltrationCoefficients, _, err = computeIdleCoeffs(options, as, shareSet)
  940. if err != nil {
  941. return fmt.Errorf("error computing idle filtration coefficients: %s", err)
  942. }
  943. }
  944. // (2c) Convert SharedHourlyCosts to Allocations in the shareSet. This must
  945. // come after idle coefficients are computed so that allocations generated
  946. // by shared overhead do not skew the idle coefficient computation.
  947. for name, cost := range options.SharedHourlyCosts {
  948. if cost > 0.0 {
  949. hours := as.Resolution().Hours()
  950. // If set ends in the future, adjust hours accordingly
  951. diff := time.Since(as.End())
  952. if diff < 0.0 {
  953. hours += diff.Hours()
  954. }
  955. totalSharedCost := cost * hours
  956. shareSet.Insert(&Allocation{
  957. Name: fmt.Sprintf("%s/%s", name, SharedSuffix),
  958. Start: as.Start(),
  959. End: as.End(),
  960. SharedCost: totalSharedCost,
  961. Properties: &AllocationProperties{Cluster: SharedSuffix}, // The allocation needs to belong to a cluster,but it really doesn't matter which one, so just make it clear.
  962. })
  963. }
  964. }
  965. // (2d) Compute share coefficients for shared resources. These are computed
  966. // after idle coefficients, and are computed for the aggregated allocations
  967. // of the main allocation set. See above for details and an example.
  968. var shareCoefficients map[string]float64
  969. if shareSet.Length() > 0 {
  970. shareCoefficients, err = computeShareCoeffs(aggregateBy, options, as)
  971. if err != nil {
  972. return fmt.Errorf("error computing share coefficients: %s", err)
  973. }
  974. }
  975. // (3-5) Filter, distribute idle cost, and aggregate (in that order)
  976. for _, alloc := range as.allocations {
  977. idleId, err := alloc.getIdleId(options)
  978. if err != nil {
  979. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  980. }
  981. skip := false
  982. // (3) If the allocation does not match the filter, immediately skip the
  983. // allocation.
  984. if options.Filter != nil {
  985. skip = !options.Filter.Matches(alloc)
  986. }
  987. if skip {
  988. // If we are tracking idle filtration coefficients, delete the
  989. // entry corresponding to the filtered allocation. (Deleting the
  990. // entry will result in that proportional amount being removed
  991. // from the idle allocation at the end of the process.)
  992. if idleFiltrationCoefficients != nil {
  993. if ifcc, ok := idleFiltrationCoefficients[idleId]; ok {
  994. delete(ifcc, alloc.Name)
  995. }
  996. }
  997. continue
  998. }
  999. // (4) Distribute idle allocations according to the idle coefficients
  1000. // NOTE: if idle allocation is off (i.e. ShareIdle == ShareNone) then
  1001. // all idle allocations will be in the aggSet at this point, so idleSet
  1002. // will be empty and we won't enter this block.
  1003. if idleSet.Length() > 0 {
  1004. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1005. for _, idleAlloc := range idleSet.allocations {
  1006. // Only share idle if the idleId matches; i.e. the allocation
  1007. // is from the same idleId as the idle costs
  1008. iaidleId, err := idleAlloc.getIdleId(options)
  1009. if err != nil {
  1010. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1011. return err
  1012. }
  1013. if iaidleId != idleId {
  1014. continue
  1015. }
  1016. // Make sure idle coefficients exist
  1017. if _, ok := idleCoefficients[idleId]; !ok {
  1018. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1019. continue
  1020. }
  1021. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1022. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1023. continue
  1024. }
  1025. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1026. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1027. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1028. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1029. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1030. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1031. alloc.CPUCost += idleCPUCost
  1032. alloc.GPUCost += idleGPUCost
  1033. alloc.RAMCost += idleRAMCost
  1034. }
  1035. }
  1036. // (5) generate key to use for aggregation-by-key and allocation name
  1037. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1038. alloc.Name = key
  1039. if options.MergeUnallocated && alloc.IsUnallocated() {
  1040. alloc.Name = UnallocatedSuffix
  1041. }
  1042. // Inserting the allocation with the generated key for a name will
  1043. // perform the actual basic aggregation step.
  1044. aggSet.Insert(alloc)
  1045. }
  1046. // (6) If idle is shared and resources are shared, it's possible that some
  1047. // amount of idle cost will be shared with a shared resource. Distribute
  1048. // that idle allocation, if it exists, to the respective shared allocations
  1049. // before sharing with the aggregated allocations.
  1050. if idleSet.Length() > 0 && shareSet.Length() > 0 {
  1051. for _, alloc := range shareSet.allocations {
  1052. idleId, err := alloc.getIdleId(options)
  1053. if err != nil {
  1054. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  1055. }
  1056. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1057. for _, idleAlloc := range idleSet.allocations {
  1058. // Only share idle if the idleId matches; i.e. the allocation
  1059. // is from the same idleId as the idle costs
  1060. iaidleId, _ := idleAlloc.getIdleId(options)
  1061. if iaidleId != idleId {
  1062. continue
  1063. }
  1064. // Make sure idle coefficients exist
  1065. if _, ok := idleCoefficients[idleId]; !ok {
  1066. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1067. continue
  1068. }
  1069. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1070. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1071. continue
  1072. }
  1073. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1074. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1075. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1076. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1077. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1078. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1079. alloc.CPUCost += idleCPUCost
  1080. alloc.GPUCost += idleGPUCost
  1081. alloc.RAMCost += idleRAMCost
  1082. }
  1083. }
  1084. }
  1085. // groupingIdleFiltrationCoeffs is used to track per-resource idle
  1086. // coefficients on a cluster-by-cluster or node-by-node basis depending
  1087. // on the IdleByNode option. It is, essentailly, an aggregation of
  1088. // idleFiltrationCoefficients after they have been
  1089. // filtered above (in step 3)
  1090. var groupingIdleFiltrationCoeffs map[string]map[string]float64
  1091. if idleFiltrationCoefficients != nil {
  1092. groupingIdleFiltrationCoeffs = map[string]map[string]float64{}
  1093. for idleId, m := range idleFiltrationCoefficients {
  1094. if _, ok := groupingIdleFiltrationCoeffs[idleId]; !ok {
  1095. groupingIdleFiltrationCoeffs[idleId] = map[string]float64{
  1096. "cpu": 0.0,
  1097. "gpu": 0.0,
  1098. "ram": 0.0,
  1099. }
  1100. }
  1101. for _, n := range m {
  1102. for resource, val := range n {
  1103. groupingIdleFiltrationCoeffs[idleId][resource] += val
  1104. }
  1105. }
  1106. }
  1107. }
  1108. // (7) If we have both un-shared idle allocations and idle filtration
  1109. // coefficients then apply those. See step (2b) for an example.
  1110. if len(aggSet.idleKeys) > 0 && groupingIdleFiltrationCoeffs != nil {
  1111. for idleKey := range aggSet.idleKeys {
  1112. idleAlloc := aggSet.Get(idleKey)
  1113. iaidleId, err := idleAlloc.getIdleId(options)
  1114. if err != nil {
  1115. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1116. return err
  1117. }
  1118. if resourceCoeffs, ok := groupingIdleFiltrationCoeffs[iaidleId]; ok {
  1119. idleAlloc.CPUCost *= resourceCoeffs["cpu"]
  1120. idleAlloc.CPUCoreHours *= resourceCoeffs["cpu"]
  1121. idleAlloc.RAMCost *= resourceCoeffs["ram"]
  1122. idleAlloc.RAMByteHours *= resourceCoeffs["ram"]
  1123. idleAlloc.GPUCost *= resourceCoeffs["gpu"]
  1124. idleAlloc.GPUHours *= resourceCoeffs["gpu"]
  1125. }
  1126. }
  1127. }
  1128. // (8) Distribute shared allocations according to the share coefficients.
  1129. if shareSet.Length() > 0 {
  1130. for _, alloc := range aggSet.allocations {
  1131. for _, sharedAlloc := range shareSet.allocations {
  1132. if _, ok := shareCoefficients[alloc.Name]; !ok {
  1133. if !alloc.IsIdle() && !alloc.IsUnmounted() {
  1134. log.Warnf("AllocationSet.AggregateBy: error getting share coefficienct for '%s'", alloc.Name)
  1135. }
  1136. continue
  1137. }
  1138. alloc.SharedCost += sharedAlloc.TotalCost() * shareCoefficients[alloc.Name]
  1139. }
  1140. }
  1141. }
  1142. // (9) Aggregate external allocations into aggregated allocations. This may
  1143. // not be possible for every external allocation, but attempt to find an
  1144. // exact key match, given each external allocation's proerties, and
  1145. // aggregate if an exact match is found.
  1146. for _, alloc := range externalSet.allocations {
  1147. skip := false
  1148. if options.Filter != nil {
  1149. skip = !options.Filter.Matches(alloc)
  1150. }
  1151. if !skip {
  1152. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1153. alloc.Name = key
  1154. aggSet.Insert(alloc)
  1155. }
  1156. }
  1157. // (10) In the edge case that some idle has not been distributed because
  1158. // there is no usage of that resource type, add idle back to
  1159. // aggregations with only that cost applied.
  1160. // E.g. in the case where we have a result that looks like this on the
  1161. // frontend:
  1162. // Name CPU GPU RAM
  1163. // __idle__ $10 $12 $6
  1164. // kubecost $2 $0 $1
  1165. // Sharing idle weighted would result in no idle GPU cost being
  1166. // distributed, because the coefficient for the kubecost GPU cost would
  1167. // be zero. Thus, instead we re-add idle to the aggSet with distributed
  1168. // costs zeroed out but the undistributed costs left in.
  1169. // Name CPU GPU RAM
  1170. // __idle__ $0 $12 $0
  1171. // kubecost $12 $0 $7
  1172. if idleSet.Length() > 0 {
  1173. for _, idleAlloc := range idleSet.allocations {
  1174. // if the idle does not apply to the non-filtered values, skip it
  1175. skip := false
  1176. if options.Filter != nil {
  1177. skip = !options.Filter.Matches(idleAlloc)
  1178. }
  1179. if skip {
  1180. continue
  1181. }
  1182. idleId, err := idleAlloc.getIdleId(options)
  1183. if err != nil {
  1184. log.Errorf("AllocationSet.AggregateBy: idle allocation is missing idleId %s", idleAlloc.Name)
  1185. continue
  1186. }
  1187. hasUndistributableCost := false
  1188. if idleAlloc.CPUCost > 0 && allocatedTotalsMap[idleId]["cpu"] == 0 {
  1189. hasUndistributableCost = true
  1190. } else {
  1191. idleAlloc.CPUCost = 0
  1192. }
  1193. if idleAlloc.GPUCost > 0 && allocatedTotalsMap[idleId]["gpu"] == 0 {
  1194. hasUndistributableCost = true
  1195. } else {
  1196. idleAlloc.GPUCost = 0
  1197. }
  1198. if idleAlloc.RAMCost > 0 && allocatedTotalsMap[idleId]["ram"] == 0 {
  1199. hasUndistributableCost = true
  1200. } else {
  1201. idleAlloc.RAMCost = 0
  1202. }
  1203. if hasUndistributableCost {
  1204. idleAlloc.Name = fmt.Sprintf("%s/%s", idleId, IdleSuffix)
  1205. aggSet.Insert(idleAlloc)
  1206. }
  1207. }
  1208. }
  1209. // (11) Combine all idle allocations into a single "__idle__" allocation
  1210. if !options.SplitIdle {
  1211. for _, idleAlloc := range aggSet.IdleAllocations() {
  1212. aggSet.Delete(idleAlloc.Name)
  1213. idleAlloc.Name = IdleSuffix
  1214. aggSet.Insert(idleAlloc)
  1215. }
  1216. }
  1217. // TODO revisit this (ideally we just remove sharing from this function!)
  1218. // If filters and shared resources and shared idle are all enabled then
  1219. // we will over-count idle by exactly the portion that gets shared with the
  1220. // filtered allocations -- and idle filtration will miss this because it
  1221. // only filters the non-idle filtered costs.
  1222. //
  1223. // Consider the following example, from unit tests:
  1224. // - namespace1 28.000
  1225. // - namespace2 36.000
  1226. // - namespace3 18.000
  1227. // - cluster1/idle 20.000
  1228. // - cluster2/idle 10.000
  1229. //
  1230. // Now, we want to share namespace1, filter namespace2, and share idle:
  1231. //
  1232. // 1. Distribute idle
  1233. // ns1 ns2 ns3
  1234. // non-idle 28.000 36.000 18.000
  1235. // idle 14.688 10.312 5.000
  1236. //
  1237. // 2. Share namespace1
  1238. //
  1239. // ns2 ns3
  1240. // non-idle 36.000 18.000
  1241. // idle 10.312 5.000
  1242. // shared non-idle 18.667 9.333
  1243. // shared idle 9.792 4.896 (***)
  1244. //
  1245. // 3. Filter out all but namespace2
  1246. //
  1247. // ns2 = 36.000 + 10.312 + 18.667 + 9.792 = 74.771
  1248. //
  1249. // So, if we had NOT shared idle, we would expect something like this:
  1250. //
  1251. // ns2 = 36.000 + 18.667 = 54.667
  1252. // idle = 10.312 + 9.792 = 20.104
  1253. //
  1254. // But we will instead get this:
  1255. //
  1256. // ns2 = 36.000 + 18.667 = 54.667
  1257. // idle = 10.312 + 14.688 = 25.000
  1258. //
  1259. // Which over-shoots idle by 4.896 (***), i.e. precisely the amount of idle
  1260. // cost corresponding to namespace1 AND shared with namespace3. Phew.
  1261. //
  1262. // I originally wanted to fix this, but after 2 days, I'm punting with the
  1263. // recommendation that we rewrite this function soon. Too difficult.
  1264. // - Niko
  1265. as.allocations = aggSet.allocations
  1266. return nil
  1267. }
  1268. func computeShareCoeffs(aggregateBy []string, options *AllocationAggregationOptions, as *AllocationSet) (map[string]float64, error) {
  1269. // Compute coeffs by totalling per-allocation, then dividing by the total.
  1270. coeffs := map[string]float64{}
  1271. // Compute totals for all allocations
  1272. total := 0.0
  1273. // ShareEven counts each aggregation with even weight, whereas ShareWeighted
  1274. // counts each aggregation proportionally to its respective costs
  1275. shareType := options.ShareSplit
  1276. // Record allocation values first, then normalize by totals to get percentages
  1277. for _, alloc := range as.allocations {
  1278. if alloc.IsIdle() {
  1279. // Skip idle allocations in coefficient calculation
  1280. continue
  1281. }
  1282. if alloc.IsUnmounted() {
  1283. // Skip unmounted allocations in coefficient calculation
  1284. continue
  1285. }
  1286. // Determine the post-aggregation key under which the allocation will
  1287. // be shared.
  1288. name := alloc.generateKey(aggregateBy, options.LabelConfig)
  1289. // If the current allocation will be filtered out in step 3, contribute
  1290. // its share of the shared coefficient to a "__filtered__" bin, which
  1291. // will ultimately be dropped. This step ensures that the shared cost
  1292. // of a non-filtered allocation will be conserved even when the filter
  1293. // is removed. (Otherwise, all the shared cost will get redistributed
  1294. // over the unfiltered results, inflating their shared costs.)
  1295. filtered := false
  1296. if options.Filter != nil {
  1297. filtered = !options.Filter.Matches(alloc)
  1298. }
  1299. if filtered {
  1300. name = "__filtered__"
  1301. }
  1302. if shareType == ShareEven {
  1303. // Even distribution is not additive - set to 1.0 for everything
  1304. coeffs[name] = 1.0
  1305. // Total for even distribution is always the number of coefficients
  1306. total = float64(len(coeffs))
  1307. } else {
  1308. // Both are additive for weighted distribution, where each
  1309. // cumulative coefficient will be divided by the total.
  1310. coeffs[name] += alloc.TotalCost() - alloc.SharedCost
  1311. total += alloc.TotalCost() - alloc.SharedCost
  1312. }
  1313. }
  1314. // Normalize coefficients by totals
  1315. for a := range coeffs {
  1316. if coeffs[a] > 0 && total > 0 {
  1317. coeffs[a] /= total
  1318. } else {
  1319. log.Warnf("ETL: invalid values for shared coefficients: %v, %v", coeffs[a], total)
  1320. coeffs[a] = 0.0
  1321. }
  1322. }
  1323. return coeffs, nil
  1324. }
  1325. func computeIdleCoeffs(options *AllocationAggregationOptions, as *AllocationSet, shareSet *AllocationSet) (map[string]map[string]map[string]float64, map[string]map[string]float64, error) {
  1326. types := []string{"cpu", "gpu", "ram"}
  1327. // Compute idle coefficients, then save them in AllocationAggregationOptions
  1328. // [idle_id][allocation name][resource] = [coeff]
  1329. coeffs := map[string]map[string]map[string]float64{}
  1330. // Compute totals per resource for CPU, GPU, RAM, and PV
  1331. // [idle_id][resource] = [total]
  1332. totals := map[string]map[string]float64{}
  1333. // Record allocation values first, then normalize by totals to get percentages
  1334. for _, alloc := range as.allocations {
  1335. if alloc.IsIdle() {
  1336. // Skip idle allocations in coefficient calculation
  1337. continue
  1338. }
  1339. idleId, err := alloc.getIdleId(options)
  1340. if err != nil {
  1341. log.DedupedWarningf(3, "Missing Idle Key for %s", alloc.Name)
  1342. }
  1343. // get the name key for the allocation
  1344. name := alloc.Name
  1345. // Create key based tables if they don't exist
  1346. if _, ok := coeffs[idleId]; !ok {
  1347. coeffs[idleId] = map[string]map[string]float64{}
  1348. }
  1349. if _, ok := totals[idleId]; !ok {
  1350. totals[idleId] = map[string]float64{}
  1351. }
  1352. if _, ok := coeffs[idleId][name]; !ok {
  1353. coeffs[idleId][name] = map[string]float64{}
  1354. }
  1355. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1356. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1357. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1358. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1359. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1360. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1361. }
  1362. // Do the same for shared allocations
  1363. for _, alloc := range shareSet.allocations {
  1364. if alloc.IsIdle() {
  1365. // Skip idle allocations in coefficient calculation
  1366. continue
  1367. }
  1368. // idleId will be providerId or cluster
  1369. idleId, err := alloc.getIdleId(options)
  1370. if err != nil {
  1371. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1372. }
  1373. // get the name key for the allocation
  1374. name := alloc.Name
  1375. // Create idleId based tables if they don't exist
  1376. if _, ok := coeffs[idleId]; !ok {
  1377. coeffs[idleId] = map[string]map[string]float64{}
  1378. }
  1379. if _, ok := totals[idleId]; !ok {
  1380. totals[idleId] = map[string]float64{}
  1381. }
  1382. if _, ok := coeffs[idleId][name]; !ok {
  1383. coeffs[idleId][name] = map[string]float64{}
  1384. }
  1385. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1386. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1387. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1388. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1389. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1390. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1391. }
  1392. // Normalize coefficients by totals
  1393. for id := range coeffs {
  1394. for a := range coeffs[id] {
  1395. for _, r := range types {
  1396. if coeffs[id][a][r] > 0 && totals[id][r] > 0 {
  1397. coeffs[id][a][r] /= totals[id][r]
  1398. }
  1399. }
  1400. }
  1401. }
  1402. return coeffs, totals, nil
  1403. }
  1404. // getIdleId returns the providerId or cluster of an Allocation depending on the IdleByNode
  1405. // option in the AllocationAggregationOptions and an error if the respective field is missing
  1406. func (a *Allocation) getIdleId(options *AllocationAggregationOptions) (string, error) {
  1407. var idleId string
  1408. if options.IdleByNode {
  1409. // Key allocations to ProviderId to match against node
  1410. idleId = fmt.Sprintf("%s/%s", a.Properties.Cluster, a.Properties.Node)
  1411. if idleId == "" {
  1412. return idleId, fmt.Errorf("ProviderId is not set")
  1413. }
  1414. } else {
  1415. // key the allocations by cluster id
  1416. idleId = a.Properties.Cluster
  1417. if idleId == "" {
  1418. return idleId, fmt.Errorf("ClusterProp is not set")
  1419. }
  1420. }
  1421. return idleId, nil
  1422. }
  1423. func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig) string {
  1424. if a == nil {
  1425. return ""
  1426. }
  1427. return a.Properties.GenerateKey(aggregateBy, labelConfig)
  1428. }
  1429. // Clone returns a new AllocationSet with a deep copy of the given
  1430. // AllocationSet's allocations.
  1431. func (as *AllocationSet) Clone() *AllocationSet {
  1432. if as == nil {
  1433. return nil
  1434. }
  1435. as.RLock()
  1436. defer as.RUnlock()
  1437. allocs := make(map[string]*Allocation, len(as.allocations))
  1438. for k, v := range as.allocations {
  1439. allocs[k] = v.Clone()
  1440. }
  1441. externalKeys := make(map[string]bool, len(as.externalKeys))
  1442. for k, v := range as.externalKeys {
  1443. externalKeys[k] = v
  1444. }
  1445. idleKeys := make(map[string]bool, len(as.idleKeys))
  1446. for k, v := range as.idleKeys {
  1447. idleKeys[k] = v
  1448. }
  1449. var errors []string
  1450. var warnings []string
  1451. if as.Errors != nil {
  1452. errors = make([]string, len(as.Errors))
  1453. copy(errors, as.Errors)
  1454. } else {
  1455. errors = nil
  1456. }
  1457. if as.Warnings != nil {
  1458. warnings := make([]string, len(as.Warnings))
  1459. copy(warnings, as.Warnings)
  1460. } else {
  1461. warnings = nil
  1462. }
  1463. return &AllocationSet{
  1464. allocations: allocs,
  1465. externalKeys: externalKeys,
  1466. idleKeys: idleKeys,
  1467. Window: as.Window.Clone(),
  1468. Errors: errors,
  1469. Warnings: warnings,
  1470. }
  1471. }
  1472. // Delete removes the allocation with the given name from the set
  1473. func (as *AllocationSet) Delete(name string) {
  1474. if as == nil {
  1475. return
  1476. }
  1477. as.Lock()
  1478. defer as.Unlock()
  1479. delete(as.externalKeys, name)
  1480. delete(as.idleKeys, name)
  1481. delete(as.allocations, name)
  1482. }
  1483. // Each invokes the given function for each Allocation in the set
  1484. func (as *AllocationSet) Each(f func(string, *Allocation)) {
  1485. if as == nil {
  1486. return
  1487. }
  1488. for k, a := range as.allocations {
  1489. f(k, a)
  1490. }
  1491. }
  1492. // End returns the End time of the AllocationSet window
  1493. func (as *AllocationSet) End() time.Time {
  1494. if as == nil {
  1495. log.Warnf("AllocationSet: calling End on nil AllocationSet")
  1496. return time.Unix(0, 0)
  1497. }
  1498. if as.Window.End() == nil {
  1499. log.Warnf("AllocationSet: AllocationSet with illegal window: End is nil; len(as.allocations)=%d", len(as.allocations))
  1500. return time.Unix(0, 0)
  1501. }
  1502. return *as.Window.End()
  1503. }
  1504. // Get returns the Allocation at the given key in the AllocationSet
  1505. func (as *AllocationSet) Get(key string) *Allocation {
  1506. as.RLock()
  1507. defer as.RUnlock()
  1508. if alloc, ok := as.allocations[key]; ok {
  1509. return alloc
  1510. }
  1511. return nil
  1512. }
  1513. // ExternalAllocations returns a map of the external allocations in the set.
  1514. // Returns clones of the actual Allocations, so mutability is not a problem.
  1515. func (as *AllocationSet) ExternalAllocations() map[string]*Allocation {
  1516. externals := map[string]*Allocation{}
  1517. if as.IsEmpty() {
  1518. return externals
  1519. }
  1520. as.RLock()
  1521. defer as.RUnlock()
  1522. for key := range as.externalKeys {
  1523. if alloc, ok := as.allocations[key]; ok {
  1524. externals[key] = alloc.Clone()
  1525. }
  1526. }
  1527. return externals
  1528. }
  1529. // ExternalCost returns the total aggregated external costs of the set
  1530. func (as *AllocationSet) ExternalCost() float64 {
  1531. if as.IsEmpty() {
  1532. return 0.0
  1533. }
  1534. as.RLock()
  1535. defer as.RUnlock()
  1536. externalCost := 0.0
  1537. for _, alloc := range as.allocations {
  1538. externalCost += alloc.ExternalCost
  1539. }
  1540. return externalCost
  1541. }
  1542. // IdleAllocations returns a map of the idle allocations in the AllocationSet.
  1543. // Returns clones of the actual Allocations, so mutability is not a problem.
  1544. func (as *AllocationSet) IdleAllocations() map[string]*Allocation {
  1545. idles := map[string]*Allocation{}
  1546. if as.IsEmpty() {
  1547. return idles
  1548. }
  1549. as.RLock()
  1550. defer as.RUnlock()
  1551. for key := range as.idleKeys {
  1552. if alloc, ok := as.allocations[key]; ok {
  1553. idles[key] = alloc.Clone()
  1554. }
  1555. }
  1556. return idles
  1557. }
  1558. // Insert aggregates the current entry in the AllocationSet by the given Allocation,
  1559. // but only if the Allocation is valid, i.e. matches the AllocationSet's window. If
  1560. // there is no existing entry, one is created. Nil error response indicates success.
  1561. func (as *AllocationSet) Insert(that *Allocation) error {
  1562. return as.insert(that)
  1563. }
  1564. func (as *AllocationSet) insert(that *Allocation) error {
  1565. if as == nil {
  1566. return fmt.Errorf("cannot insert into nil AllocationSet")
  1567. }
  1568. as.Lock()
  1569. defer as.Unlock()
  1570. if as.allocations == nil {
  1571. as.allocations = map[string]*Allocation{}
  1572. }
  1573. if as.externalKeys == nil {
  1574. as.externalKeys = map[string]bool{}
  1575. }
  1576. if as.idleKeys == nil {
  1577. as.idleKeys = map[string]bool{}
  1578. }
  1579. // Add the given Allocation to the existing entry, if there is one;
  1580. // otherwise just set directly into allocations
  1581. if _, ok := as.allocations[that.Name]; !ok {
  1582. as.allocations[that.Name] = that
  1583. } else {
  1584. as.allocations[that.Name].add(that)
  1585. }
  1586. // If the given Allocation is an external one, record that
  1587. if that.IsExternal() {
  1588. as.externalKeys[that.Name] = true
  1589. }
  1590. // If the given Allocation is an idle one, record that
  1591. if that.IsIdle() {
  1592. as.idleKeys[that.Name] = true
  1593. }
  1594. // Expand the window, just to be safe. It's possible that the Allocation will
  1595. // be set into the map without expanding it to the AllocationSet's window.
  1596. as.allocations[that.Name].Window = as.allocations[that.Name].Window.Expand(as.Window)
  1597. return nil
  1598. }
  1599. // IsEmpty returns true if the AllocationSet is nil, or if it contains
  1600. // zero allocations.
  1601. func (as *AllocationSet) IsEmpty() bool {
  1602. if as == nil || len(as.allocations) == 0 {
  1603. return true
  1604. }
  1605. as.RLock()
  1606. defer as.RUnlock()
  1607. return as.allocations == nil || len(as.allocations) == 0
  1608. }
  1609. // Length returns the number of Allocations in the set
  1610. func (as *AllocationSet) Length() int {
  1611. if as == nil {
  1612. return 0
  1613. }
  1614. as.RLock()
  1615. defer as.RUnlock()
  1616. return len(as.allocations)
  1617. }
  1618. // Map clones and returns a map of the AllocationSet's Allocations
  1619. func (as *AllocationSet) Map() map[string]*Allocation {
  1620. if as.IsEmpty() {
  1621. return map[string]*Allocation{}
  1622. }
  1623. return as.Clone().allocations
  1624. }
  1625. // MarshalJSON JSON-encodes the AllocationSet
  1626. func (as *AllocationSet) MarshalJSON() ([]byte, error) {
  1627. if as == nil {
  1628. return json.Marshal(map[string]*Allocation{})
  1629. }
  1630. as.RLock()
  1631. defer as.RUnlock()
  1632. return json.Marshal(as.allocations)
  1633. }
  1634. // ResetAdjustments sets all cost adjustment fields to zero
  1635. func (as *AllocationSet) ResetAdjustments() {
  1636. if as == nil {
  1637. return
  1638. }
  1639. as.Lock()
  1640. defer as.Unlock()
  1641. as.resetAdjustments()
  1642. }
  1643. func (as *AllocationSet) resetAdjustments() {
  1644. for _, a := range as.allocations {
  1645. a.ResetAdjustments()
  1646. }
  1647. }
  1648. // Resolution returns the AllocationSet's window duration
  1649. func (as *AllocationSet) Resolution() time.Duration {
  1650. return as.Window.Duration()
  1651. }
  1652. // Set uses the given Allocation to overwrite the existing entry in the
  1653. // AllocationSet under the Allocation's name.
  1654. func (as *AllocationSet) Set(alloc *Allocation) error {
  1655. if as.IsEmpty() {
  1656. as.Lock()
  1657. as.allocations = map[string]*Allocation{}
  1658. as.externalKeys = map[string]bool{}
  1659. as.idleKeys = map[string]bool{}
  1660. as.Unlock()
  1661. }
  1662. as.Lock()
  1663. defer as.Unlock()
  1664. as.allocations[alloc.Name] = alloc
  1665. // If the given Allocation is an external one, record that
  1666. if alloc.IsExternal() {
  1667. as.externalKeys[alloc.Name] = true
  1668. }
  1669. // If the given Allocation is an idle one, record that
  1670. if alloc.IsIdle() {
  1671. as.idleKeys[alloc.Name] = true
  1672. }
  1673. return nil
  1674. }
  1675. // Start returns the Start time of the AllocationSet window
  1676. func (as *AllocationSet) Start() time.Time {
  1677. if as == nil {
  1678. log.Warnf("AllocationSet: calling Start on nil AllocationSet")
  1679. return time.Unix(0, 0)
  1680. }
  1681. if as.Window.Start() == nil {
  1682. log.Warnf("AllocationSet: AllocationSet with illegal window: Start is nil; len(as.allocations)=%d", len(as.allocations))
  1683. return time.Unix(0, 0)
  1684. }
  1685. return *as.Window.Start()
  1686. }
  1687. // String represents the given Allocation as a string
  1688. func (as *AllocationSet) String() string {
  1689. if as == nil {
  1690. return "<nil>"
  1691. }
  1692. return fmt.Sprintf("AllocationSet{length: %d; window: %s; totalCost: %.2f}",
  1693. as.Length(), as.Window, as.TotalCost())
  1694. }
  1695. // TotalCost returns the sum of all TotalCosts of the allocations contained
  1696. func (as *AllocationSet) TotalCost() float64 {
  1697. if as.IsEmpty() {
  1698. return 0.0
  1699. }
  1700. as.RLock()
  1701. defer as.RUnlock()
  1702. tc := 0.0
  1703. for _, a := range as.allocations {
  1704. tc += a.TotalCost()
  1705. }
  1706. return tc
  1707. }
  1708. // UTCOffset returns the AllocationSet's configured UTCOffset.
  1709. func (as *AllocationSet) UTCOffset() time.Duration {
  1710. _, zone := as.Start().Zone()
  1711. return time.Duration(zone) * time.Second
  1712. }
  1713. func (as *AllocationSet) accumulate(that *AllocationSet) (*AllocationSet, error) {
  1714. if as.IsEmpty() {
  1715. return that.Clone(), nil
  1716. }
  1717. if that.IsEmpty() {
  1718. return as.Clone(), nil
  1719. }
  1720. // Set start, end to min(start), max(end)
  1721. start := as.Start()
  1722. end := as.End()
  1723. if that.Start().Before(start) {
  1724. start = that.Start()
  1725. }
  1726. if that.End().After(end) {
  1727. end = that.End()
  1728. }
  1729. acc := NewAllocationSet(start, end)
  1730. as.RLock()
  1731. defer as.RUnlock()
  1732. that.RLock()
  1733. defer that.RUnlock()
  1734. for _, alloc := range as.allocations {
  1735. err := acc.insert(alloc)
  1736. if err != nil {
  1737. return nil, err
  1738. }
  1739. }
  1740. for _, alloc := range that.allocations {
  1741. err := acc.insert(alloc)
  1742. if err != nil {
  1743. return nil, err
  1744. }
  1745. }
  1746. return acc, nil
  1747. }
  1748. // AllocationSetRange is a thread-safe slice of AllocationSets. It is meant to
  1749. // be used such that the AllocationSets held are consecutive and coherent with
  1750. // respect to using the same aggregation properties, UTC offset, and
  1751. // resolution. However these rules are not necessarily enforced, so use wisely.
  1752. type AllocationSetRange struct {
  1753. sync.RWMutex
  1754. allocations []*AllocationSet
  1755. FromStore string // stores the name of the store used to retrieve the data
  1756. }
  1757. // NewAllocationSetRange instantiates a new range composed of the given
  1758. // AllocationSets in the order provided.
  1759. func NewAllocationSetRange(allocs ...*AllocationSet) *AllocationSetRange {
  1760. return &AllocationSetRange{
  1761. allocations: allocs,
  1762. }
  1763. }
  1764. // Accumulate sums each AllocationSet in the given range, returning a single cumulative
  1765. // AllocationSet for the entire range.
  1766. func (asr *AllocationSetRange) Accumulate() (*AllocationSet, error) {
  1767. var allocSet *AllocationSet
  1768. var err error
  1769. asr.RLock()
  1770. defer asr.RUnlock()
  1771. for _, as := range asr.allocations {
  1772. allocSet, err = allocSet.accumulate(as)
  1773. if err != nil {
  1774. return nil, err
  1775. }
  1776. }
  1777. return allocSet, nil
  1778. }
  1779. // AccumulateBy sums AllocationSets based on the resolution given. The resolution given is subject to the scale used for the AllocationSets.
  1780. // Resolutions not evenly divisible by the AllocationSetRange window durations accumulate sets until a sum greater than or equal to the resolution is met,
  1781. // at which point AccumulateBy will start summing from 0 until the requested resolution is met again.
  1782. // If the requested resolution is smaller than the window of an AllocationSet then the resolution will default to the duration of a set.
  1783. // Resolutions larger than the duration of the entire AllocationSetRange will default to the duration of the range.
  1784. func (asr *AllocationSetRange) AccumulateBy(resolution time.Duration) (*AllocationSetRange, error) {
  1785. allocSetRange := NewAllocationSetRange()
  1786. var allocSet *AllocationSet
  1787. var err error
  1788. asr.Lock()
  1789. defer asr.Unlock()
  1790. for i, as := range asr.allocations {
  1791. allocSet, err = allocSet.accumulate(as)
  1792. if err != nil {
  1793. return nil, err
  1794. }
  1795. if allocSet != nil {
  1796. // check if end of asr to sum the final set
  1797. // If total asr accumulated sum <= resolution return 1 accumulated set
  1798. if allocSet.Window.Duration() >= resolution || i == len(asr.allocations)-1 {
  1799. allocSetRange.allocations = append(allocSetRange.allocations, allocSet)
  1800. allocSet = NewAllocationSet(time.Time{}, time.Time{})
  1801. }
  1802. }
  1803. }
  1804. return allocSetRange, nil
  1805. }
  1806. // AggregateBy aggregates each AllocationSet in the range by the given
  1807. // properties and options.
  1808. func (asr *AllocationSetRange) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  1809. aggRange := &AllocationSetRange{allocations: []*AllocationSet{}}
  1810. asr.Lock()
  1811. defer asr.Unlock()
  1812. for _, as := range asr.allocations {
  1813. err := as.AggregateBy(aggregateBy, options)
  1814. if err != nil {
  1815. return err
  1816. }
  1817. aggRange.allocations = append(aggRange.allocations, as)
  1818. }
  1819. asr.allocations = aggRange.allocations
  1820. return nil
  1821. }
  1822. // Append appends the given AllocationSet to the end of the range. It does not
  1823. // validate whether or not that violates window continuity.
  1824. func (asr *AllocationSetRange) Append(that *AllocationSet) {
  1825. asr.Lock()
  1826. defer asr.Unlock()
  1827. asr.allocations = append(asr.allocations, that)
  1828. }
  1829. // Each invokes the given function for each AllocationSet in the range
  1830. func (asr *AllocationSetRange) Each(f func(int, *AllocationSet)) {
  1831. if asr == nil {
  1832. return
  1833. }
  1834. for i, as := range asr.allocations {
  1835. f(i, as)
  1836. }
  1837. }
  1838. // Get retrieves the AllocationSet at the given index of the range.
  1839. func (asr *AllocationSetRange) Get(i int) (*AllocationSet, error) {
  1840. if i < 0 || i >= len(asr.allocations) {
  1841. return nil, fmt.Errorf("AllocationSetRange: index out of range: %d", i)
  1842. }
  1843. asr.RLock()
  1844. defer asr.RUnlock()
  1845. return asr.allocations[i], nil
  1846. }
  1847. // InsertRange merges the given AllocationSetRange into the receiving one by
  1848. // lining up sets with matching windows, then inserting each allocation from
  1849. // the given ASR into the respective set in the receiving ASR. If the given
  1850. // ASR contains an AllocationSet from a window that does not exist in the
  1851. // receiving ASR, then an error is returned. However, the given ASR does not
  1852. // need to cover the full range of the receiver.
  1853. func (asr *AllocationSetRange) InsertRange(that *AllocationSetRange) error {
  1854. if asr == nil {
  1855. return fmt.Errorf("cannot insert range into nil AllocationSetRange")
  1856. }
  1857. // keys maps window to index in asr
  1858. keys := map[string]int{}
  1859. asr.Each(func(i int, as *AllocationSet) {
  1860. if as == nil {
  1861. return
  1862. }
  1863. keys[as.Window.String()] = i
  1864. })
  1865. // Nothing to merge, so simply return
  1866. if len(keys) == 0 {
  1867. return nil
  1868. }
  1869. var err error
  1870. that.Each(func(j int, thatAS *AllocationSet) {
  1871. if thatAS == nil || err != nil {
  1872. return
  1873. }
  1874. // Find matching AllocationSet in asr
  1875. i, ok := keys[thatAS.Window.String()]
  1876. if !ok {
  1877. err = fmt.Errorf("cannot merge AllocationSet into window that does not exist: %s", thatAS.Window.String())
  1878. return
  1879. }
  1880. as, err := asr.Get(i)
  1881. if err != nil {
  1882. err = fmt.Errorf("AllocationSetRange index does not exist: %d", i)
  1883. return
  1884. }
  1885. // Insert each Allocation from the given set
  1886. thatAS.Each(func(k string, alloc *Allocation) {
  1887. err = as.Insert(alloc)
  1888. if err != nil {
  1889. err = fmt.Errorf("error inserting allocation: %s", err)
  1890. return
  1891. }
  1892. })
  1893. })
  1894. // err might be nil
  1895. return err
  1896. }
  1897. // Length returns the length of the range, which is zero if nil
  1898. func (asr *AllocationSetRange) Length() int {
  1899. if asr == nil || asr.allocations == nil {
  1900. return 0
  1901. }
  1902. asr.RLock()
  1903. defer asr.RUnlock()
  1904. return len(asr.allocations)
  1905. }
  1906. // MarshalJSON JSON-encodes the range
  1907. func (asr *AllocationSetRange) MarshalJSON() ([]byte, error) {
  1908. if asr == nil {
  1909. return json.Marshal([]*AllocationSet{})
  1910. }
  1911. asr.RLock()
  1912. defer asr.RUnlock()
  1913. return json.Marshal(asr.allocations)
  1914. }
  1915. // Slice copies the underlying slice of AllocationSets, maintaining order,
  1916. // and returns the copied slice.
  1917. func (asr *AllocationSetRange) Slice() []*AllocationSet {
  1918. if asr == nil || asr.allocations == nil {
  1919. return nil
  1920. }
  1921. asr.RLock()
  1922. defer asr.RUnlock()
  1923. copy := []*AllocationSet{}
  1924. for _, as := range asr.allocations {
  1925. copy = append(copy, as.Clone())
  1926. }
  1927. return copy
  1928. }
  1929. // String represents the given AllocationSetRange as a string
  1930. func (asr *AllocationSetRange) String() string {
  1931. if asr == nil {
  1932. return "<nil>"
  1933. }
  1934. return fmt.Sprintf("AllocationSetRange{length: %d}", asr.Length())
  1935. }
  1936. // UTCOffset returns the detected UTCOffset of the AllocationSets within the
  1937. // range. Defaults to 0 if the range is nil or empty. Does not warn if there
  1938. // are sets with conflicting UTCOffsets (just returns the first).
  1939. func (asr *AllocationSetRange) UTCOffset() time.Duration {
  1940. if asr.Length() == 0 {
  1941. return 0
  1942. }
  1943. as, err := asr.Get(0)
  1944. if err != nil {
  1945. return 0
  1946. }
  1947. return as.UTCOffset()
  1948. }
  1949. // Window returns the full window that the AllocationSetRange spans, from the
  1950. // start of the first AllocationSet to the end of the last one.
  1951. func (asr *AllocationSetRange) Window() Window {
  1952. if asr == nil || asr.Length() == 0 {
  1953. return NewWindow(nil, nil)
  1954. }
  1955. start := asr.allocations[0].Start()
  1956. end := asr.allocations[asr.Length()-1].End()
  1957. return NewWindow(&start, &end)
  1958. }
  1959. // Start returns the earliest start of all Allocations in the AllocationSetRange.
  1960. // It returns an error if there are no allocations.
  1961. func (asr *AllocationSetRange) Start() (time.Time, error) {
  1962. start := time.Time{}
  1963. firstStartNotSet := true
  1964. asr.Each(func(i int, as *AllocationSet) {
  1965. as.Each(func(s string, a *Allocation) {
  1966. if firstStartNotSet {
  1967. start = a.Start
  1968. firstStartNotSet = false
  1969. }
  1970. if a.Start.Before(start) {
  1971. start = a.Start
  1972. }
  1973. })
  1974. })
  1975. if firstStartNotSet {
  1976. return start, fmt.Errorf("had no data to compute a start from")
  1977. }
  1978. return start, nil
  1979. }
  1980. // End returns the latest end of all Allocations in the AllocationSetRange.
  1981. // It returns an error if there are no allocations.
  1982. func (asr *AllocationSetRange) End() (time.Time, error) {
  1983. end := time.Time{}
  1984. firstEndNotSet := true
  1985. asr.Each(func(i int, as *AllocationSet) {
  1986. as.Each(func(s string, a *Allocation) {
  1987. if firstEndNotSet {
  1988. end = a.End
  1989. firstEndNotSet = false
  1990. }
  1991. if a.End.After(end) {
  1992. end = a.End
  1993. }
  1994. })
  1995. })
  1996. if firstEndNotSet {
  1997. return end, fmt.Errorf("had no data to compute an end from")
  1998. }
  1999. return end, nil
  2000. }
  2001. // Minutes returns the duration, in minutes, between the earliest start
  2002. // and the latest end of all allocations in the AllocationSetRange.
  2003. func (asr *AllocationSetRange) Minutes() float64 {
  2004. start, err := asr.Start()
  2005. if err != nil {
  2006. return 0
  2007. }
  2008. end, err := asr.End()
  2009. if err != nil {
  2010. return 0
  2011. }
  2012. duration := end.Sub(start)
  2013. return duration.Minutes()
  2014. }
  2015. // TotalCost returns the sum of all TotalCosts of the allocations contained
  2016. func (asr *AllocationSetRange) TotalCost() float64 {
  2017. if asr == nil || len(asr.allocations) == 0 {
  2018. return 0.0
  2019. }
  2020. asr.RLock()
  2021. defer asr.RUnlock()
  2022. tc := 0.0
  2023. for _, as := range asr.allocations {
  2024. tc += as.TotalCost()
  2025. }
  2026. return tc
  2027. }