Skip to content

Add bypass #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/freno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/github/freno/pkg/http"
"github.com/github/freno/pkg/throttle"
"github.com/outbrain/golib/log"
"os"
"strconv"
)

// AppVersion has to be filled by ldflags:
Expand Down Expand Up @@ -120,6 +122,16 @@ func httpServe() error {
throttler := throttle.NewThrottler()
log.Infof("Starting consensus service")
log.Infof("- forced leadership: %+v", group.ForceLeadership)

bypassEnabled, err := strconv.ParseBool(os.Getenv("FRENO_BYPASS_ENABLED"))
if err != nil {
log.Infof("error parsing FRENO_BYPASS_ENABLED: %s", os.Getenv("FRENO_BYPASS_ENABLED"))
bypassEnabled = false
}
if bypassEnabled {
log.Info("- bypass on no hosts is enabled!")
}
throttler.BypassOnNoHostsFound = bypassEnabled
consensusServiceProvider, err := group.NewConsensusServiceProvider(throttler)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/throttle/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func aggregateMySQLProbes(
ignoreHostsCount int,
ignoreDialTcpErrors bool,
ignoreHostsThreshold float64,
bypassOnNohosts bool,
) (worstMetric base.MetricResult) {
// probes is known not to change. It can be *replaced*, but not changed.
// so it's safe to iterate it
Expand Down Expand Up @@ -46,6 +47,9 @@ func aggregateMySQLProbes(
probeValues = append(probeValues, value)
}
if len(probeValues) == 0 {
if bypassOnNohosts {
return base.NewSimpleMetricResult(0.0)
}
return base.NoHostsMetricResult
}

Expand Down
49 changes: 28 additions & 21 deletions pkg/throttle/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,48 @@ func TestAggregateMySQLProbesNoErrors(t *testing.T) {
probes[clusterKey.Key] = &mysql.Probe{Key: clusterKey.Key}
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.7)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.2)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.1)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 3, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 3, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.6)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 4, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 4, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.3)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 5, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 5, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.3)
}
{
var emptyProbes mysql.Probes = map[mysql.InstanceKey](*mysql.Probe){}
worstMetric := aggregateMySQLProbes(&emptyProbes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 5, false, 0, true)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.0)
}
}

func TestAggregateMySQLProbesNoErrorsIgnoreHostsThreshold(t *testing.T) {
Expand Down Expand Up @@ -117,37 +124,37 @@ func TestAggregateMySQLProbesNoErrorsIgnoreHostsThreshold(t *testing.T) {
probes[clusterKey.Key] = &mysql.Probe{Key: clusterKey.Key}
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.7)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.2)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.1)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 3, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 3, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.6)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 4, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 4, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.6)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 5, false, 1.0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 5, false, 1.0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 0.6)
Expand Down Expand Up @@ -180,39 +187,39 @@ func TestAggregateMySQLProbesWithErrors(t *testing.T) {
probes[clusterKey.Key] = &mysql.Probe{Key: clusterKey.Key}
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
_, err := worstMetric.Get()
test.S(t).ExpectNotNil(err)
test.S(t).ExpectEquals(err, base.NoSuchMetricError)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.7)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.2)
}

instanceResultsMap[key1cluster] = base.NoSuchMetric
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
_, err := worstMetric.Get()
test.S(t).ExpectNotNil(err)
test.S(t).ExpectEquals(err, base.NoSuchMetricError)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 1, false, 0, false)
_, err := worstMetric.Get()
test.S(t).ExpectNotNil(err)
test.S(t).ExpectEquals(err, base.NoSuchMetricError)
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 2, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.7)
Expand Down Expand Up @@ -245,13 +252,13 @@ func TestAggregateMySQLProbesWithHttpChecks(t *testing.T) {
probes[clusterKey.Key] = &mysql.Probe{Key: clusterKey.Key}
}
{
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
_, err := worstMetric.Get()
test.S(t).ExpectNil(err)
}
{
clusterInstanceHttpCheckResultMap[mysql.MySQLHttpCheckHashKey(clusterName, &key2)] = http.StatusNotFound
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
value, err := worstMetric.Get()
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(value, 1.2)
Expand All @@ -260,7 +267,7 @@ func TestAggregateMySQLProbesWithHttpChecks(t *testing.T) {
for hashKey := range clusterInstanceHttpCheckResultMap {
clusterInstanceHttpCheckResultMap[hashKey] = http.StatusNotFound
}
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0)
worstMetric := aggregateMySQLProbes(&probes, clusterName, instanceResultsMap, clusterInstanceHttpCheckResultMap, 0, false, 0, false)
_, err := worstMetric.Get()
test.S(t).ExpectNotNil(err)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type Throttler struct {

nonLowPriorityAppRequestsThrottled *cache.Cache
httpClient *http.Client

BypassOnNoHostsFound bool
}

func NewThrottler() *Throttler {
Expand Down Expand Up @@ -331,6 +333,10 @@ func (throttler *Throttler) refreshMySQLInventory() error {
}
}
if len(totalHosts) == 0 {
if throttler.BypassOnNoHostsFound {
log.Infof("No hosts for pool: %+v, but bypass is enabled", poolName)
return nil
}
return log.Errorf("Unable to get any HAproxy hosts for pool: %+v", poolName)
}

Expand Down Expand Up @@ -441,7 +447,7 @@ func (throttler *Throttler) aggregateMySQLMetrics() error {
metricName := fmt.Sprintf("mysql/%s", clusterName)
ignoreHostsCount := throttler.mysqlInventory.IgnoreHostsCount[clusterName]
ignoreHostsThreshold := throttler.mysqlInventory.IgnoreHostsThreshold[clusterName]
aggregatedMetric := aggregateMySQLProbes(probes, clusterName, throttler.mysqlInventory.InstanceKeyMetrics, throttler.mysqlInventory.ClusterInstanceHttpChecks, ignoreHostsCount, config.Settings().Stores.MySQL.IgnoreDialTcpErrors, ignoreHostsThreshold)
aggregatedMetric := aggregateMySQLProbes(probes, clusterName, throttler.mysqlInventory.InstanceKeyMetrics, throttler.mysqlInventory.ClusterInstanceHttpChecks, ignoreHostsCount, config.Settings().Stores.MySQL.IgnoreDialTcpErrors, ignoreHostsThreshold, throttler.BypassOnNoHostsFound)
go throttler.aggregatedMetrics.Set(metricName, aggregatedMetric, cache.DefaultExpiration)
if throttler.memcacheClient != nil {
go func() {
Expand Down
Loading