Skip to content

Commit 4363f9d

Browse files
authored
Merge pull request #178 from github/meiji163/no-hosts-no-err
Return 200 if no hosts found
2 parents b3fbe7e + a30dad6 commit 4363f9d

30 files changed

+6121
-8
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ require (
1717
vitess.io/vitess v2.1.1+incompatible
1818
)
1919

20+
require github.com/stretchr/testify v1.3.0
21+
2022
require (
23+
github.com/davecgh/go-spew v1.1.0 // indirect
2124
github.com/golang/protobuf v1.2.0 // indirect
2225
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
2326
github.com/hashicorp/golang-lru v0.5.0 // indirect
27+
github.com/pmezard/go-difflib v1.0.0 // indirect
2428
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pO
1010
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
1111
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
1212
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
13+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1314
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1415
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
1516
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
@@ -35,6 +36,7 @@ github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144T
3536
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
3637
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
3738
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
39+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3840
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3941
github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
4042
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
@@ -43,6 +45,7 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R
4345
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563 h1:dY6ETXrvDG7Sa4vE8ZQG4yqWg6UnOcbqTAahkV813vQ=
4446
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
4547
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
4649
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4750
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
4851
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

pkg/base/throttle_metric.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type MetricResult interface {
1717
type MetricResultFunc func() (metricResult MetricResult, threshold float64)
1818

1919
var ThresholdExceededError = errors.New("Threshold exceeded")
20-
var noHostsError = errors.New("No hosts found")
20+
var NoHostsError = errors.New("No hosts found")
2121
var noResultYetError = errors.New("Metric not collected yet")
2222
var NoSuchMetricError = errors.New("No such metric")
2323

@@ -31,7 +31,7 @@ func IsDialTcpError(e error) bool {
3131
type noHostsMetricResult struct{}
3232

3333
func (metricResult *noHostsMetricResult) Get() (float64, error) {
34-
return 0, noHostsError
34+
return 0, NoHostsError
3535
}
3636

3737
var NoHostsMetricResult = &noHostsMetricResult{}

pkg/mysql/mysql_inventory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mysql
77

88
import (
99
"fmt"
10+
1011
"github.com/github/freno/pkg/base"
1112
)
1213

pkg/throttle/check.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"fmt"
99

10+
"errors"
11+
1012
"github.com/github/freno/pkg/base"
1113
metrics "github.com/rcrowley/go-metrics"
1214
)
@@ -65,6 +67,9 @@ func (check *ThrottlerCheck) checkAppMetricResult(appName string, storeType stri
6567
} else if err == base.NoSuchMetricError {
6668
// not collected yet, or metric does not exist
6769
statusCode = http.StatusNotFound // 404
70+
} else if errors.Is(err, base.NoHostsError) {
71+
// If no hosts we report 0 for the metric.
72+
statusCode = http.StatusOK // 200
6873
} else if err != nil {
6974
// any error
7075
statusCode = http.StatusInternalServerError // 500

pkg/throttle/check_result.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package throttle
22

33
import (
4-
"github.com/github/freno/pkg/base"
54
"net/http"
5+
6+
"github.com/github/freno/pkg/base"
67
)
78

89
// CheckResult is the result for an app inquiring on a metric. It also exports as JSON via the API

pkg/throttle/throttler_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package throttle
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/github/freno/pkg/base"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func Test_checkAppMetricResult(t *testing.T) {
12+
throttler := NewThrottler()
13+
14+
t.Run("threshold exceeded", func(t *testing.T) {
15+
check := NewThrottlerCheck(throttler)
16+
metricResultFunc := func() (metricResult base.MetricResult, threshold float64) {
17+
return base.NewSimpleMetricResult(10.0), 1.0
18+
}
19+
result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{})
20+
assert.Equal(t, 10.0, result.Value)
21+
assert.EqualError(t, result.Error, base.ThresholdExceededError.Error())
22+
assert.Equal(t, http.StatusTooManyRequests, result.StatusCode)
23+
})
24+
t.Run("not throttled", func(t *testing.T) {
25+
check := NewThrottlerCheck(throttler)
26+
metricResultFunc := func() (metricResult base.MetricResult, threshold float64) {
27+
return base.NewSimpleMetricResult(1.0), 10.0
28+
}
29+
result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{})
30+
assert.Equal(t, 1.0, result.Value)
31+
assert.Nil(t, result.Error)
32+
assert.Equal(t, http.StatusOK, result.StatusCode)
33+
})
34+
t.Run("no hosts", func(t *testing.T) {
35+
check := NewThrottlerCheck(throttler)
36+
metricResultFunc := func() (metricResult base.MetricResult, threshold float64) {
37+
return base.NoHostsMetricResult, 10.0
38+
}
39+
result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{})
40+
assert.Equal(t, 0.0, result.Value)
41+
assert.EqualError(t, result.Error, base.NoHostsError.Error())
42+
assert.Equal(t, http.StatusOK, result.StatusCode)
43+
})
44+
t.Run("low priority denied", func(t *testing.T) {
45+
check := NewThrottlerCheck(throttler)
46+
throttler.nonLowPriorityAppRequestsThrottled.SetDefault("mysql/test-cluster", true)
47+
metricResultFunc := func() (metricResult base.MetricResult, threshold float64) {
48+
return base.NewSimpleMetricResult(1.0), 10.0
49+
}
50+
result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{LowPriority: true})
51+
assert.EqualError(t, result.Error, base.AppDeniedError.Error())
52+
assert.Equal(t, http.StatusExpectationFailed, result.StatusCode)
53+
54+
})
55+
}

vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)