This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
external_auction_distributor.go
165 lines (148 loc) · 4.54 KB
/
external_auction_distributor.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
package auctiondistributor
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/cheggaaa/pb"
"github.com/cloudfoundry-incubator/auction/auctiontypes"
"github.com/cloudfoundry-incubator/runtime-schema/models"
"github.com/cloudfoundry/gunk/workpool"
)
type externalAuctionDistributor struct {
hosts []string
auctionCommunicationMode string
maxConcurrent int
}
func NewExternalAuctionDistributor(hosts []string, maxConcurrent int, auctionCommunicationMode string) AuctionDistributor {
return &externalAuctionDistributor{
auctionCommunicationMode: auctionCommunicationMode,
maxConcurrent: maxConcurrent,
hosts: hosts,
}
}
func (d *externalAuctionDistributor) HoldStartAuctions(numAuctioneers int, startAuctions []models.LRPStartAuction, repAddresses []auctiontypes.RepAddress, rules auctiontypes.StartAuctionRules) []auctiontypes.StartAuctionResult {
startAuctionRequests := buildStartAuctionRequests(startAuctions, repAddresses, rules)
groupedRequests := map[int][]auctiontypes.StartAuctionRequest{}
i := 0
for _, request := range startAuctionRequests {
index := i % numAuctioneers
groupedRequests[index] = append(groupedRequests[index], request)
i++
}
bar := pb.StartNew(len(startAuctions))
workPool := workpool.NewWorkPool(50)
wg := &sync.WaitGroup{}
wg.Add(len(groupedRequests))
for i := range groupedRequests {
i := i
workPool.Submit(func() {
defer wg.Done()
payload, _ := json.Marshal(groupedRequests[i])
url := fmt.Sprintf("http://%s/start-auctions?mode=%s&maxConcurrent=%d", d.hosts[i], d.auctionCommunicationMode, d.maxConcurrent)
_, err := http.Post(url, "application/json", bytes.NewReader(payload))
if err != nil {
fmt.Println("Failed to run auctions on index", i, err.Error())
return
}
})
}
wg.Wait()
workPool.Stop()
results := []auctiontypes.StartAuctionResult{}
finishedAuctioneers := map[int]bool{}
start := time.Now()
for {
client := &http.Client{
Timeout: 3 * time.Second,
}
for i := range groupedRequests {
if finishedAuctioneers[i] {
continue
}
res, err := client.Get("http://" + d.hosts[i] + "/start-auctions-results")
if err != nil {
fmt.Println("Failed to get auctions on index", i, err.Error())
continue
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Failed to read body on index", i, err.Error())
res.Body.Close()
continue
}
if res.StatusCode >= 300 {
fmt.Println("Got unexpected status code on index", i, res.StatusCode, string(data))
res.Body.Close()
continue
}
result := []auctiontypes.StartAuctionResult{}
err = json.Unmarshal(data, &result)
if err != nil {
fmt.Println("Failed to decode results on index", i, err.Error(), string(data))
res.Body.Close()
continue
}
if res.StatusCode == http.StatusCreated {
finishedAuctioneers[i] = true
}
results = append(results, result...)
bar.Set(len(results))
res.Body.Close()
}
if len(finishedAuctioneers) == len(groupedRequests) {
break
}
if time.Since(start) > 5*time.Minute {
break
}
time.Sleep(100 * time.Millisecond)
}
bar.Finish()
return results
}
func (d *externalAuctionDistributor) HoldStopAuctions(numAuctioneers int, stopAuctions []models.LRPStopAuction, repAddresses []auctiontypes.RepAddress) []auctiontypes.StopAuctionResult {
stopAuctionRequests := buildStopAuctionRequests(stopAuctions, repAddresses)
groupedRequests := map[int][]auctiontypes.StopAuctionRequest{}
i := 0
for _, request := range stopAuctionRequests {
index := i % numAuctioneers
groupedRequests[index] = append(groupedRequests[index], request)
i++
}
results := []auctiontypes.StopAuctionResult{}
lock := &sync.Mutex{}
wg := &sync.WaitGroup{}
for i := 0; i < numAuctioneers; i++ {
if len(groupedRequests[i]) == 0 {
continue
}
go func(i int) {
defer wg.Done()
payload, _ := json.Marshal(groupedRequests[i])
url := fmt.Sprintf("http://%s/stop-auctions?mode=%s&maxConcurrent=%d", d.hosts[i], d.auctionCommunicationMode, d.maxConcurrent)
res, err := http.Post(url, "application/json", bytes.NewReader(payload))
if err != nil {
fmt.Println("Failed to run auctions on index", i, err.Error())
return
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
for {
result := auctiontypes.StopAuctionResult{}
err = decoder.Decode(&result)
if err != nil {
break
}
lock.Lock()
results = append(results, result)
lock.Unlock()
}
}(i)
}
wg.Wait()
return results
}