-
Notifications
You must be signed in to change notification settings - Fork 2
/
bidder.go
285 lines (230 loc) · 5.99 KB
/
bidder.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
package main
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
"github.com/broadbent/airship/auctioneer"
"github.com/rs/xid"
)
var auctioneerRoot = "http://192.168.2.1:8080"
var userIDRoot = "service_"
var userID string
var phases = 3
// var bidIncrement = 10
// var startBid = 10
// var startPause = "10s"
var stagePause = "5s"
var phasePause = "7s"
var bidPause = "0.5s"
var minPause = 5
var maxPause = 15
var bidders = 1
var locations = map[string]int{
"datacenter": 22,
"residence": 6,
"exchange": 6,
}
func main() {
var userIDSuffix = flag.String("user", "a", "user ID suffice (a, b, c, etc.)")
flag.Parse()
userID = userIDRoot + *userIDSuffix
startBidder()
}
func startBidder() bool {
log.Printf("Bidder %v started.\n", userID)
randomiseLocationQuotas()
log.Printf("Quota is a follows: %v", locations)
auctions := fetchAuctions(true)
items := determineTargetItems(auctions, locations)
bids := generateBids(items)
artificialSleep("", true)
biddingStage(bids)
artificialSleep(stagePause, false)
provisioningStage()
return true
}
func randomiseLocationQuotas() {
for location, _ := range locations {
locations[location] = randomNumber(0, locations[location])
}
}
func artificialSleep(duration string, random bool) {
var sleep time.Duration
if random {
sleep = randomDuration(minPause, maxPause)
} else {
sleep, _ = time.ParseDuration(duration)
}
time.Sleep(sleep)
}
func randomDuration(min, max int) time.Duration {
duration := time.Duration(randomNumber(min, max)) * time.Second
return duration
}
func randomNumber(min, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min) + min
}
func biddingStage(bids map[string]*auctioneer.Bid) {
for i := 0; i < phases; i++ {
biddingPhase(bids, i)
}
}
func biddingPhase(bids map[string]*auctioneer.Bid, phaseNumber int) {
log.Printf("Starting bidding phase %v.\n", phaseNumber)
executeBids(bids)
printBids(bids)
incrementBids(bids)
printLeading()
log.Printf("Ending bidding phase %v.\n", phaseNumber)
artificialSleep(phasePause, false)
}
func printBids(bids map[string]*auctioneer.Bid) {
log.Println("Bid summary is as follows:")
for _, bid := range bids {
log.Printf("Bid for %v, valued at %v, was submitted.", bid.UserTag, bid.Valuation)
}
}
func printLeading() { //could also check against user_tag?
log.Println("Leading the following auctions:")
auctions := fetchAuctions(true)
for _, auction := range auctions {
for _, item := range auction.Items {
if item.Leading.UserID == userID {
log.Printf("Leading item %v.", item.ID)
}
}
}
}
func provisioningStage() {
log.Printf("Starting provisioning phase.\n")
auctions := fetchAuctions(false)
for {
if len(auctions) > 0 { //TODO: Only provisions the first auction
items := winningAuctions(&auctions)
provision(items)
log.Printf("Ending provisioning phase.\n")
return
}
time.Sleep(10 * time.Second)
auctions = fetchAuctions(false)
}
}
func provision(items []auctioneer.Item) {
for _, item := range items {
provisionItem(item)
}
}
func provisionItem(item auctioneer.Item) {
var provision auctioneer.Provision
provision.Nodes = []string{item.ParentNode.ID}
provision.ImageName = "lyndon160/" + userID
provision.Memory = item.Memory
provision.Hours = 1
provision.PortBindings = make(map[string]int)
provision.PortBindings["internal"] = 80
makePost(provision, "/provision")
}
func winningAuctions(auctions *[]auctioneer.Auction) []auctioneer.Item {
var items []auctioneer.Item
for _, auction := range *auctions { //TODO: check if auction is no longer live
for _, item := range auction.Items {
if item.Leading.UserID == userID {
items = append(items, item)
}
}
}
return items
}
func fetchAuctions(live bool) []auctioneer.Auction {
var auctions []auctioneer.Auction
var path string
if live {
path = auctioneerRoot + "/auction/live"
} else {
path = auctioneerRoot + "/auction/expired"
}
resp, err := http.Get(path) //should the end point not be '/provision_docker_containers'?
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
json.Unmarshal(body, &auctions)
return auctions
}
func determineTargetItems(auctions []auctioneer.Auction, locationCriterium map[string]int) []auctioneer.Item {
var items []auctioneer.Item
for _, auction := range auctions {
randomisedItems := randomiseTargetItems(auction.Items)
for _, item := range randomisedItems {
for location, quota := range locationCriterium {
if item.ParentNode.Location == location {
if quota > 0 {
items = append(items, item)
locationCriterium[location] = quota - 1
}
}
}
}
}
return items
}
func randomiseTargetItems(items []auctioneer.Item) []auctioneer.Item {
dest := make([]auctioneer.Item, len(items))
perm := rand.Perm(len(items))
for i, v := range perm {
dest[v] = items[i]
}
return dest
}
func generateBids(items []auctioneer.Item) map[string]*auctioneer.Bid {
bids := make(map[string]*auctioneer.Bid)
for _, item := range items {
var bid auctioneer.Bid
bid.UserTag = xid.New().String()
bid.AuctionID = item.ParentAuctionID
bid.ItemID = item.ID
bid.UserID = userID
bid.Valuation = randomNumber(10, 20)
bids[bid.UserTag] = &bid
}
return bids
}
func incrementBids(bids map[string]*auctioneer.Bid) {
for _, bid := range bids {
bid.Valuation = bid.Valuation + randomNumber(10, 20)
}
}
func executeBids(bids map[string]*auctioneer.Bid) {
for _, bid := range bids {
artificialSleep(bidPause, false)
executeBid(bid)
}
}
func executeBid(bid *auctioneer.Bid) {
log.Printf("Placing bid now: %v.\n", bid.UserTag)
path := "/auction/bid"
makePost(bid, path)
}
func makePost(obj interface{}, path string) {
post, err := json.Marshal(obj)
if err != nil {
panic(err)
}
reader := bytes.NewReader(post)
path = auctioneerRoot + path
resp, err := http.Post(path, "application/json; charset=UTF-8", reader)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}