/
haproxy.go
275 lines (235 loc) · 8.18 KB
/
haproxy.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
package bluegreen
import (
"encoding/csv"
"fmt"
"github.com/ContainX/depcon/marathon"
"github.com/ContainX/depcon/utils"
"io"
"math"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
const (
HAProxyStatsQP = "/haproxy?stats;csv"
HAProxyPidsQP = "/_haproxy_getpids"
BackendRE = `(?i)^(\d+)_(\d+)_(\d+)_(\d+)_(\d+)$`
)
// Simple HTTP Test to determine if the current LB is the correct URL. Better to test this before we modify Marathon with this
// existing deployment
func (c *BGClient) isProxyAlive() {
resp := c.http.HttpGet(c.opts.LoadBalancer+HAProxyStatsQP, nil)
if resp.Error != nil {
log.Fatal("HAProxy is not responding or is invalid or Stats service not enabled.\n\n", resp.Error.Error())
}
}
func (c *BGClient) checkIfTasksDrained(app, existingApp *marathon.Application, stepStartedAt time.Time) bool {
time.Sleep(c.opts.StepDelay)
existingApp = c.refreshAppOrPanic(existingApp.ID)
app = c.refreshAppOrPanic(app.ID)
targetInstances, _ := strconv.Atoi(app.Labels[DeployTargetInstances])
log.Info("Existing app running %d instance, new app running %d instances", existingApp.Instances, app.Instances)
hosts, err := proxiesFromURI(c.opts.LoadBalancer)
if err != nil {
log.Error("Error with HAProxy Stats URL: %s", err.Error())
}
var errCaught error = nil
var csvData string
for _, h := range hosts {
log.Debug("Querying HAProxy stats: %s", h+HAProxyStatsQP)
resp := c.http.HttpGet(h+HAProxyStatsQP, nil)
if resp.Error != nil {
errCaught = resp.Error
} else {
csvData = csvData + resp.Content
}
resp = c.http.HttpGet(h+HAProxyPidsQP, nil)
if resp.Error != nil {
errCaught = resp.Error
} else {
pids := strings.Split(resp.Content, " ")
log.Debug("Pids: %v, length: %d, time constraint: %v", pids, len(pids), (time.Now().Sub(stepStartedAt) < c.opts.StepDelay))
if len(pids) > 1 && time.Now().Sub(stepStartedAt) < c.opts.StepDelay {
log.Info("Waiting for %d, pids on %s", len(pids), h)
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
}
if errCaught != nil {
log.Warning("Caught error when retrieving HAProxy stats from %s: Error (%s)", h, errCaught.Error())
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
}
pinfo := parseProxyBackends(csvData, app)
if len(pinfo.backends)/pinfo.instanceCount != (app.Instances + existingApp.Instances) {
log.Debug("HAProxy hasn't updated: %d / %d != (%d + %d)", len(pinfo.backends), pinfo.instanceCount, app.Instances, existingApp.Instances)
// HAProxy hasn't updated yet, try again
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
backendsUp := backendsForStatus(pinfo, "UP")
if len(backendsUp)/pinfo.instanceCount < targetInstances {
log.Debug("Waiting until health state: %d / %d < %d", len(backendsUp), pinfo.instanceCount, targetInstances)
// Wait until we're in a health state
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
// Double check that current draining backends are finished serving requests
backendsDrained := backendsForStatus(pinfo, "MAINT")
if len(backendsDrained)/pinfo.instanceCount < 1 {
log.Debug("No backends have started draining yet: %d / %d < 1", len(backendsDrained), pinfo.instanceCount)
// No backends have started draining yet
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
for _, be := range backendsDrained {
// Verify that the backends have no sessions or pending connections.
// This is likely overkill, but we'll do it anyway to be safe.
if intOrZero(string(be[pinfo.hmap["qcur"]])) > 0 || intOrZero(string(be[pinfo.hmap["scur"]])) > 0 {
// Backends are not yet defined
return c.checkIfTasksDrained(app, existingApp, stepStartedAt)
}
}
// If we made it here, all the backends are drained and we can start removing tasks, with prejudice
hostPorts := hostPortsFromBackends(pinfo.hmap, backendsDrained, pinfo.instanceCount)
tasksToKill := findTasksToKill(existingApp.Tasks, hostPorts)
log.Info("There are %d drained backends, about to kill & scale for these tasks:\n%s", len(tasksToKill), strings.Join(tasksToKill, "\n"))
if app.Instances == targetInstances && len(tasksToKill) == existingApp.Instances {
log.Info("About to delete old app %s", existingApp.ID)
if _, err := c.marathon.DestroyApplication(existingApp.ID); err != nil {
return false
}
return true
}
// Scale new app up
instances := int(math.Floor(float64(app.Instances + (app.Instances+1)/2)))
if instances >= existingApp.Instances {
instances = targetInstances
}
log.Info("Scaling new app up to %d instances", instances)
if _, err := c.marathon.ScaleApplication(app.ID, instances); err != nil {
panic("Failed to scale application: " + err.Error())
}
//Scale old app down
log.Info("Scaling old app down to %d instances", len(tasksToKill))
if err := c.marathon.KillTasksAndScale(tasksToKill...); err != nil {
log.Error("Failure killing tasks: %v", tasksToKill)
}
return c.checkIfTasksDrained(app, existingApp, time.Now())
}
func findTasksToKill(tasks []*marathon.Task, hostPorts map[string][]int) []string {
tasksToKill := map[string]string{}
for _, task := range tasks {
if _, ok := hostPorts[task.Host]; ok {
for _, p := range hostPorts[task.Host] {
if utils.IntInSlice(p, task.Ports) {
tasksToKill[task.ID] = task.ID
}
}
}
}
return utils.MapStringKeysToSlice(tasksToKill)
}
func hostPortsFromBackends(hmap map[string]int, backends [][]string, instanceCount int) map[string][]int {
regex := regexp.MustCompile(BackendRE)
counts := map[string]int{}
hostPorts := map[string][]int{}
for _, be := range backends {
svname := string(be[hmap["svname"]])
if _, ok := counts[svname]; ok {
counts[svname] += 1
} else {
counts[svname] = 1
}
if counts[svname] == instanceCount {
if regex.MatchString(svname) {
m := regex.FindStringSubmatch(svname)
host := strings.Join(m[1:5], ".")
port := m[5]
if _, ok := hostPorts[host]; ok {
hostPorts[host] = append(hostPorts[host], intOrZero(port))
} else {
hostPorts[host] = []int{intOrZero(port)}
}
}
}
}
return hostPorts
}
func backendsForStatus(pinfo *proxyInfo, status string) [][]string {
var results [][]string
for _, b := range pinfo.backends {
if b[pinfo.hmap["status"]] == status {
results = append(results, b)
}
}
return results
}
func parseProxyBackends(data string, app *marathon.Application) *proxyInfo {
pi := &proxyInfo{
instanceCount: 0,
hmap: map[string]int{},
backends: make([][]string, 0),
}
var headers []string
r := csv.NewReader(strings.NewReader(data))
for {
row, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if []rune(row[0])[0] == '#' {
headers = row
pi.instanceCount += 1
continue
}
if row[0] == fmt.Sprintf("%s_%s", app.Labels[DeployGroup], app.Labels[DeployProxyPort]) && notBackFrontend(row[1]) {
pi.backends = append(pi.backends, row)
}
}
log.Info("Found %d backends across %d HAProxy instances", len(pi.backends), pi.instanceCount)
// Create header map of column to index
for i := 0; i < len(headers); i++ {
pi.hmap[headers[i]] = i
}
return pi
}
func notBackFrontend(value string) bool {
return value != "BACKEND" && value != "FRONTEND"
}
func (c *BGClient) refreshAppOrPanic(id string) *marathon.Application {
log.Debug("Enter: refreshAppOrPanic -> %s", id)
// Retry in case of minor network errors
for i := 0; i < 3; i++ {
if a, err := c.marathon.GetApplication(id); err != nil {
log.Error("Error refresh app info: %s, Will retry %d more times before giving up", err.Error(), 3-(i+1))
time.Sleep(time.Duration(3) * time.Second)
} else {
log.Debug("refreshAppOrPanic: returning %s", sprintApp(a))
return a
}
}
panic("Failure to refresh application " + id)
}
func proxiesFromURI(uri string) ([]string, error) {
url, err := url.Parse(uri)
if err != nil {
return nil, err
}
hp := strings.Split(url.Host, ":")
host := hp[0]
port := hp[1]
ips, err := net.LookupIP(host)
if err != nil {
log.Debug("Lookup IP failed for: %s, error: %s", host, err.Error())
return []string{url.String()}, nil
}
results := []string{}
for _, ip := range ips {
url.Host = ip.String() + ":" + port
results = append(results, url.String())
}
return results, nil
}