forked from cloudfoundry/auction
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auction_rep.go
294 lines (244 loc) · 8.29 KB
/
auction_rep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package auctionrep
import (
"errors"
"math"
"sync"
"github.com/cloudfoundry-incubator/auction/auctiontypes"
"github.com/cloudfoundry-incubator/runtime-schema/models"
)
type AuctionRep struct {
repGuid string
delegate auctiontypes.AuctionRepDelegate
lock *sync.Mutex
}
type InstanceScoreInfo struct {
RemainingResources auctiontypes.Resources
TotalResources auctiontypes.Resources
NumInstancesOnRepForProcessGuid int
NumInstancesDesiredForProcessGuid int
Index int
ProcessGuid string
RepAZNumber int
TotalNumAZs int
}
func New(repGuid string, delegate auctiontypes.AuctionRepDelegate) *AuctionRep {
return &AuctionRep{
repGuid: repGuid,
delegate: delegate,
lock: &sync.Mutex{},
}
}
func (rep *AuctionRep) Guid() string {
return rep.repGuid
}
func (rep *AuctionRep) AZNumber() int {
return rep.delegate.AZNumber()
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) BidForStartAuction(startAuctionInfo auctiontypes.StartAuctionInfo) (float64, error) {
rep.lock.Lock()
defer rep.lock.Unlock()
instanceScoreInfo, err := rep.instanceScoreInfo(
startAuctionInfo.ProcessGuid,
startAuctionInfo.NumInstances,
startAuctionInfo.Index,
startAuctionInfo.NumAZs,
)
if err != nil {
return 0, err
}
err = rep.satisfiesConstraints(startAuctionInfo, instanceScoreInfo)
if err != nil {
return 0, err
}
return rep.startAuctionBid(instanceScoreInfo), nil
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) RebidThenTentativelyReserve(startAuctionInfo auctiontypes.StartAuctionInfo) (float64, error) {
rep.lock.Lock()
defer rep.lock.Unlock()
instanceScoreInfo, err := rep.instanceScoreInfo(
startAuctionInfo.ProcessGuid,
startAuctionInfo.NumInstances,
startAuctionInfo.Index,
startAuctionInfo.NumAZs,
)
if err != nil {
return 0, err
}
err = rep.satisfiesConstraints(startAuctionInfo, instanceScoreInfo)
if err != nil {
return 0, err
}
bid := rep.startAuctionBid(instanceScoreInfo)
//then reserve
err = rep.delegate.Reserve(startAuctionInfo)
if err != nil {
return 0, err
}
return bid, nil
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) ReleaseReservation(startAuctionInfo auctiontypes.StartAuctionInfo) error {
rep.lock.Lock()
defer rep.lock.Unlock()
return rep.delegate.ReleaseReservation(startAuctionInfo)
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) Run(startAuction models.LRPStartAuction) error {
rep.lock.Lock()
defer rep.lock.Unlock()
return rep.delegate.Run(startAuction)
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) BidForStopAuction(stopAuctionInfo auctiontypes.StopAuctionInfo) (float64, []string, error) {
rep.lock.Lock()
defer rep.lock.Unlock()
instanceScoreInfo, err := rep.instanceScoreInfo(
stopAuctionInfo.ProcessGuid,
stopAuctionInfo.NumInstances,
stopAuctionInfo.Index,
stopAuctionInfo.NumAZs,
)
if err != nil {
return 0, nil, err
}
instanceGuids, err := rep.delegate.InstanceGuidsForProcessGuidAndIndex(stopAuctionInfo.ProcessGuid, stopAuctionInfo.Index)
if err != nil {
return 0, nil, err
}
err = rep.isRunningProcessIndex(instanceGuids)
if err != nil {
return 0, nil, err
}
return rep.stopAuctionBid(instanceScoreInfo), instanceGuids, nil
}
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) Stop(stopInstance models.StopLRPInstance) error {
rep.lock.Lock()
defer rep.lock.Unlock()
return rep.delegate.Stop(stopInstance)
}
// simulation-only
func (rep *AuctionRep) TotalResources() auctiontypes.Resources {
totalResources, _ := rep.delegate.TotalResources()
return totalResources
}
// simulation-only
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) Reset() {
rep.lock.Lock()
defer rep.lock.Unlock()
simDelegate, ok := rep.delegate.(auctiontypes.SimulationAuctionRepDelegate)
if !ok {
println("not reseting")
return
}
simDelegate.SetSimulatedInstances([]auctiontypes.SimulatedInstance{})
}
// simulation-only
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) SetSimulatedInstances(instances []auctiontypes.SimulatedInstance) {
rep.lock.Lock()
defer rep.lock.Unlock()
simDelegate, ok := rep.delegate.(auctiontypes.SimulationAuctionRepDelegate)
if !ok {
println("not setting instances")
return
}
simDelegate.SetSimulatedInstances(instances)
}
// simulation-only
// must lock here; the publicly visible operations should be atomic
func (rep *AuctionRep) SimulatedInstances() []auctiontypes.SimulatedInstance {
rep.lock.Lock()
defer rep.lock.Unlock()
simDelegate, ok := rep.delegate.(auctiontypes.SimulationAuctionRepDelegate)
if !ok {
println("not fetching instances")
return []auctiontypes.SimulatedInstance{}
}
return simDelegate.SimulatedInstances()
}
// private internals -- no locks here
func (rep *AuctionRep) instanceScoreInfo(processGuid string, numInstances int, index int, totalNumAZs int) (InstanceScoreInfo, error) {
remaining, err := rep.delegate.RemainingResources()
if err != nil {
return InstanceScoreInfo{}, err
}
total, err := rep.delegate.TotalResources()
if err != nil {
return InstanceScoreInfo{}, err
}
nInstancesOnRep, err := rep.delegate.NumInstancesForProcessGuid(processGuid)
if err != nil {
return InstanceScoreInfo{}, err
}
repAZNumber := rep.delegate.AZNumber()
return InstanceScoreInfo{
RemainingResources: remaining,
TotalResources: total,
NumInstancesOnRepForProcessGuid: nInstancesOnRep,
NumInstancesDesiredForProcessGuid: numInstances,
Index: index,
ProcessGuid: processGuid,
RepAZNumber: repAZNumber,
TotalNumAZs: totalNumAZs,
}, nil
}
// private internals -- no locks here
func (rep *AuctionRep) satisfiesConstraints(startAuctionInfo auctiontypes.StartAuctionInfo, instanceScoreInfo InstanceScoreInfo) error {
remaining := instanceScoreInfo.RemainingResources
hasEnoughMemory := remaining.MemoryMB >= startAuctionInfo.MemoryMB
hasEnoughDisk := remaining.DiskMB >= startAuctionInfo.DiskMB
hasEnoughContainers := remaining.Containers > 0
if hasEnoughMemory && hasEnoughDisk && hasEnoughContainers {
return nil
} else {
return auctiontypes.InsufficientResources
}
}
// private internals -- no locks here
func (rep *AuctionRep) isRunningProcessIndex(instanceGuids []string) error {
if len(instanceGuids) == 0 {
return errors.New("not-running-instance")
}
return nil
}
// private internals -- no locks here
func (rep *AuctionRep) startAuctionBid(instanceScoreInfo InstanceScoreInfo) float64 {
remaining := instanceScoreInfo.RemainingResources
total := instanceScoreInfo.TotalResources
fractionUsedContainers := 1.0 - float64(remaining.Containers)/float64(total.Containers)
fractionUsedDisk := 1.0 - float64(remaining.DiskMB)/float64(total.DiskMB)
fractionUsedMemory := 1.0 - float64(remaining.MemoryMB)/float64(total.MemoryMB)
fractionInstancesForProcessGuid := float64(instanceScoreInfo.NumInstancesOnRepForProcessGuid) / float64(instanceScoreInfo.NumInstancesDesiredForProcessGuid)
azBalancingTerm := -math.Mod(
float64(instanceScoreInfo.Index+hash(instanceScoreInfo.ProcessGuid)+instanceScoreInfo.RepAZNumber),
float64(instanceScoreInfo.TotalNumAZs),
)
return (1.0/3.0)*fractionUsedContainers +
(1.0/3.0)*fractionUsedDisk +
(1.0/3.0)*fractionUsedMemory +
(1.0/1.0)*fractionInstancesForProcessGuid +
(1.0/1.0)*azBalancingTerm
}
func hash(s string) int {
h := 0
for _, b := range []byte(s) {
h += int(b)
}
return h
}
// private internals -- no locks here
func (rep *AuctionRep) stopAuctionBid(instanceScoreInfo InstanceScoreInfo) float64 {
remaining := instanceScoreInfo.RemainingResources
total := instanceScoreInfo.TotalResources
fractionUsedContainers := 1.0 - float64(remaining.Containers)/float64(total.Containers)
fractionUsedDisk := 1.0 - float64(remaining.DiskMB)/float64(total.DiskMB)
fractionUsedMemory := 1.0 - float64(remaining.MemoryMB)/float64(total.MemoryMB)
return (1.0/3.0)*fractionUsedContainers +
(1.0/3.0)*fractionUsedDisk +
(1.0/3.0)*fractionUsedMemory +
(1.0/1.0)*float64(instanceScoreInfo.NumInstancesOnRepForProcessGuid)
}