Skip to content

Commit 7b93605

Browse files
author
Shlomi Noach
authored
Merge pull request #56 from github/cross-cluster-errors
looking into cross-cluster correlated errors
2 parents d42d296 + a18b5a0 commit 7b93605

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

go/haproxy/parser.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import (
55
"io/ioutil"
66
"net/http"
77
"strings"
8+
"time"
9+
10+
"github.com/patrickmn/go-cache"
811
)
912

13+
var csvCache = cache.New(time.Second, time.Second)
14+
1015
// parseHeader parses the HAPRoxy CSV header, which lists column names.
1116
// Returned is a header-to-index map
1217
func parseHeader(header string) (tokensMap map[string]int) {
@@ -31,23 +36,33 @@ func parseLines(csv string) []string {
3136
// Such list indicates the hosts which can be expected to be active, which is then the list freno will probe.
3237
func ParseHosts(csvLines []string, poolName string) (hosts []string, err error) {
3338
if len(csvLines) < 1 {
34-
return hosts, fmt.Errorf("No lines found haproxy CSV; expecting at least a header")
39+
return hosts, fmt.Errorf("Haproxy CSV parsing error: no lines found.")
40+
}
41+
if len(csvLines) == 1 {
42+
return hosts, fmt.Errorf("Haproxy CSV parsing error: only found a header.")
3543
}
3644
var tokensMap map[string]int
45+
poolFound := false
3746
for i, line := range csvLines {
3847
if i == 0 {
3948
tokensMap = parseHeader(csvLines[0])
4049
continue
4150
}
4251
tokens := strings.Split(line, ",")
4352
if tokens[tokensMap["pxname"]] == poolName {
53+
poolFound = true
4454
if host := tokens[tokensMap["svname"]]; host != "BACKEND" && host != "FRONTEND" {
45-
if status := tokens[tokensMap["status"]]; status == "UP" || status == "DOWN" {
55+
status := tokens[tokensMap["status"]]
56+
status = strings.Split(status, " ")[0]
57+
if status == "UP" || status == "DOWN" {
4658
hosts = append(hosts, host)
4759
}
4860
}
4961
}
5062
}
63+
if !poolFound {
64+
return hosts, fmt.Errorf("Haproxy CSV parsing error: did not find %+v pool", poolName)
65+
}
5166
return hosts, nil
5267
}
5368

@@ -61,6 +76,11 @@ func ParseCsvHosts(csv string, poolName string) (hosts []string, err error) {
6176
// Read will read HAProxy URI and return with the CSV text
6277
func Read(host string, port int) (csv string, err error) {
6378
haproxyUrl := fmt.Sprintf("http://%s:%d/;csv;norefresh", host, port)
79+
80+
if cachedCSV, found := csvCache.Get(haproxyUrl); found {
81+
return cachedCSV.(string), nil
82+
}
83+
6484
resp, err := http.Get(haproxyUrl)
6585
if resp != nil {
6686
defer resp.Body.Close()
@@ -72,5 +92,8 @@ func Read(host string, port int) (csv string, err error) {
7292
if err != nil {
7393
return "", err
7494
}
75-
return string(body), nil
95+
96+
csv = string(body)
97+
csvCache.Set(haproxyUrl, csv, cache.DefaultExpiration)
98+
return csv, nil
7699
}

go/mysql/mysql_throttle_metric.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ func ReadThrottleMetric(probe *Probe) (mySQLThrottleMetric *MySQLThrottleMetric)
3636
mySQLThrottleMetric.Key = probe.Key
3737

3838
defer func(metric *MySQLThrottleMetric, started time.Time) {
39-
metrics.GetOrRegisterTimer("probes.latency", nil).Update(time.Since(started))
40-
metrics.GetOrRegisterCounter("probes.total", nil).Inc(1)
41-
if metric.Err != nil {
42-
metrics.GetOrRegisterCounter("probes.error", nil).Inc(1)
43-
}
39+
go func() {
40+
metrics.GetOrRegisterTimer("probes.latency", nil).Update(time.Since(started))
41+
metrics.GetOrRegisterCounter("probes.total", nil).Inc(1)
42+
if metric.Err != nil {
43+
metrics.GetOrRegisterCounter("probes.error", nil).Inc(1)
44+
}
45+
}()
4446
}(mySQLThrottleMetric, started)
4547

4648
dbUri := probe.GetDBUri("information_schema")
47-
4849
db, fromCache, err := sqlutils.GetDB(dbUri)
4950

5051
if err != nil {

go/mysql/probe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Probes map[InstanceKey](*Probe)
2828
type ClusterProbes struct {
2929
ClusterName string
3030
IgnoreHostsCount int
31-
Probes *Probes
31+
InstanceProbes *Probes
3232
}
3333

3434
func NewProbes() *Probes {

go/throttle/throttler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,26 @@ func (throttler *Throttler) refreshMySQLInventory() error {
211211
clusterProbes := &mysql.ClusterProbes{
212212
ClusterName: clusterName,
213213
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
214-
Probes: mysql.NewProbes(),
214+
InstanceProbes: mysql.NewProbes(),
215215
}
216216
for _, host := range hosts {
217217
key := mysql.InstanceKey{Hostname: host, Port: clusterSettings.Port}
218-
addInstanceKey(&key, clusterSettings, clusterProbes.Probes)
218+
addInstanceKey(&key, clusterSettings, clusterProbes.InstanceProbes)
219219
}
220220
throttler.mysqlClusterProbesChan <- clusterProbes
221221
return nil
222222
}
223223
if !clusterSettings.StaticHostsSettings.IsEmpty() {
224224
clusterProbes := &mysql.ClusterProbes{
225-
ClusterName: clusterName,
226-
Probes: mysql.NewProbes(),
225+
ClusterName: clusterName,
226+
InstanceProbes: mysql.NewProbes(),
227227
}
228228
for _, host := range clusterSettings.StaticHostsSettings.Hosts {
229229
key, err := mysql.ParseInstanceKey(host, clusterSettings.Port)
230230
if err != nil {
231231
return log.Errore(err)
232232
}
233-
addInstanceKey(key, clusterSettings, clusterProbes.Probes)
233+
addInstanceKey(key, clusterSettings, clusterProbes.InstanceProbes)
234234
}
235235
throttler.mysqlClusterProbesChan <- clusterProbes
236236
return nil
@@ -244,7 +244,7 @@ func (throttler *Throttler) refreshMySQLInventory() error {
244244
// synchronous update of inventory
245245
func (throttler *Throttler) updateMySQLClusterProbes(clusterProbes *mysql.ClusterProbes) error {
246246
log.Debugf("onMySQLClusterProbes: %s", clusterProbes.ClusterName)
247-
throttler.mysqlInventory.ClustersProbes[clusterProbes.ClusterName] = clusterProbes.Probes
247+
throttler.mysqlInventory.ClustersProbes[clusterProbes.ClusterName] = clusterProbes.InstanceProbes
248248
throttler.mysqlInventory.IgnoreHostsCount[clusterProbes.ClusterName] = clusterProbes.IgnoreHostsCount
249249
return nil
250250
}

0 commit comments

Comments
 (0)