allocation.go 72 KB

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