Skip to content
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

Throttler code: some refactoring and cleanup #16368

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5fb9a6e
rename variable mysqlMetricThresholds to metricThresholds
shlomi-noach Jul 11, 2024
c6d7047
rename function mysqlMetricThresholdsSnapshot to metricThresholdsSnap…
shlomi-noach Jul 11, 2024
9eb59b3
rename *Interval variables away from 'mysql'
shlomi-noach Jul 11, 2024
9f2716a
rename *Interval variables away from 'mysql'
shlomi-noach Jul 11, 2024
685441e
rename aggregateMySQLProbes -> aggregateMetricResults. Cleanup tests
shlomi-noach Jul 11, 2024
dd11de1
rename MySQLThrottleMetrics -> ThrottleMetrics. Rename mysqlThrottleM…
shlomi-noach Jul 11, 2024
c9ac150
renamed files
shlomi-noach Jul 11, 2024
dd3868f
rename file
shlomi-noach Jul 11, 2024
2886bf3
eliminatye throttle/mysql package; move everything under throttle/base
shlomi-noach Jul 11, 2024
f6e8a55
Remove unused type
shlomi-noach Jul 11, 2024
1ea1b51
remove ctx argument
shlomi-noach Jul 11, 2024
1bddcc0
rename aggregateMetricResults -> aggregateTabletMetricResults
shlomi-noach Jul 11, 2024
ec62bbe
shuffle code around, moving aggregateTabletMetricResults into 'base' …
shlomi-noach Jul 11, 2024
c82ea9a
remove ctx where unused
shlomi-noach Jul 11, 2024
3423780
terminology: remove 'MySQL' where irrelevant. Add where relevant
shlomi-noach Jul 11, 2024
1f584a1
terminology: remove 'MySQL' where irrelevant
shlomi-noach Jul 11, 2024
85994ed
renamed getMySQLStoreMetric -> getScopedMetric
shlomi-noach Jul 11, 2024
2492d6d
comment wording
shlomi-noach Jul 11, 2024
8c20483
adding missing files
shlomi-noach Jul 11, 2024
d913132
Merge branch 'main' into throttler-cleanup-types-and-names
shlomi-noach Jul 11, 2024
a8eeea7
update copyright year
shlomi-noach Jul 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,7 @@ limitations under the License.
SOFTWARE.
*/

package mysql

import (
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/base"
)

// TabletResultMap maps a tablet to a result
type TabletResultMap map[string]base.MetricResultMap

func (m TabletResultMap) Split(alias string) (withAlias TabletResultMap, all TabletResultMap) {
withAlias = make(TabletResultMap)
if val, ok := m[alias]; ok {
withAlias[alias] = val
}
return withAlias, m
}
package base

// Inventory has the operational data about probes, their metrics, and relevant configuration
type Inventory struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package mysql
package base

import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"

"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/base"
)

func TestTabletResultMapSplit(t *testing.T) {
tabletResultMap := TabletResultMap{
"a": make(base.MetricResultMap),
"b": make(base.MetricResultMap),
"c": make(base.MetricResultMap),
"a": make(MetricResultMap),
"b": make(MetricResultMap),
"c": make(MetricResultMap),
}
{
withAlias, all := tabletResultMap.Split("b")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ limitations under the License.
SOFTWARE.
*/

package mysql
package base

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ limitations under the License.
SOFTWARE.
*/

package mysql
package base

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,42 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// This codebase originates from https://github.com/github/freno, See https://github.com/github/freno/blob/master/LICENSE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this attribution anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this specific file. The code in this file is not derived from freno, and the note was needlessly copy+pasted from another file.

/*
MIT License

Copyright (c) 2017 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
package base

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package throttle
import "sort"

import (
"context"
"sort"
// TabletResultMap maps a tablet to a result
type TabletResultMap map[string]MetricResultMap

"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/base"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/mysql"
)
func (m TabletResultMap) Split(alias string) (withAlias TabletResultMap, all TabletResultMap) {
withAlias = make(TabletResultMap)
if val, ok := m[alias]; ok {
withAlias[alias] = val
}
return withAlias, m
}

func aggregateMySQLProbes(
ctx context.Context,
metricName base.MetricName,
tabletResultsMap mysql.TabletResultMap,
func AggregateTabletMetricResults(
metricName MetricName,
tabletResultsMap TabletResultMap,
ignoreHostsCount int,
IgnoreDialTCPErrors bool,
ignoreHostsThreshold float64,
) (worstMetric base.MetricResult) {
) (worstMetric MetricResult) {
// probes is known not to change. It can be *replaced*, but not changed.
// so it's safe to iterate it
probeValues := []float64{}
for _, tabletMetricResults := range tabletResultsMap {
tabletMetricResult, ok := tabletMetricResults[metricName]
if !ok {
return base.NoSuchMetric
return NoSuchMetric
}
if tabletMetricResult == nil {
return base.NoMetricResultYet
return NoMetricResultYet
}
value, err := tabletMetricResult.Get()
if err != nil {
if IgnoreDialTCPErrors && base.IsDialTCPError(err) {
if IgnoreDialTCPErrors && IsDialTCPError(err) {
continue
}
if ignoreHostsCount > 0 {
Expand All @@ -85,7 +64,7 @@ func aggregateMySQLProbes(
probeValues = append(probeValues, value)
}
if len(probeValues) == 0 {
return base.NoHostsMetricResult
return NoHostsMetricResult
}

// If we got here, that means no errors (or good-to-skip errors)
Expand Down Expand Up @@ -115,6 +94,6 @@ func aggregateMySQLProbes(
ignoreHostsCount = ignoreHostsCount - 1
}
worstValue := probeValues[len(probeValues)-1]
worstMetric = base.NewSimpleMetricResult(worstValue)
worstMetric = NewSimpleMetricResult(worstValue)
return worstMetric
}
199 changes: 199 additions & 0 deletions go/vt/vttablet/tabletserver/throttle/base/tablet_results_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright 2023 The Vitess Authors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 2024?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package base

import (
"testing"

"github.com/stretchr/testify/assert"
)

var (
alias1 = "zone1-0001"
alias2 = "zone1-0002"
alias3 = "zone1-0003"
alias4 = "zone1-0004"
alias5 = "zone1-0005"
)

const (
nonexistentMetricName MetricName = "nonexistent"
)

func newMetricResultMap(val float64) MetricResultMap {
return MetricResultMap{
DefaultMetricName: NewSimpleMetricResult(val),
LagMetricName: NewSimpleMetricResult(val),
LoadAvgMetricName: NewSimpleMetricResult(3.14),
}
}
func noSuchMetricMap() MetricResultMap {
result := make(MetricResultMap)
for _, metricName := range KnownMetricNames {
result[metricName] = NoSuchMetric
}
return result
}

func TestAggregateTabletMetricResultsNoErrors(t *testing.T) {
tabletResultsMap := TabletResultMap{
alias1: newMetricResultMap(1.2),
alias2: newMetricResultMap(1.7),
alias3: newMetricResultMap(0.3),
alias4: newMetricResultMap(0.6),
alias5: newMetricResultMap(1.1),
}

{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 0, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.7)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 1, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.2)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 2, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.1)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 3, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.6)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 4, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.3)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 5, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.3)
}
}

func TestAggregateTabletMetricResultsNoErrorsIgnoreHostsThreshold(t *testing.T) {
tabletResultsMap := TabletResultMap{
alias1: newMetricResultMap(1.2),
alias2: newMetricResultMap(1.7),
alias3: newMetricResultMap(0.3),
alias4: newMetricResultMap(0.6),
alias5: newMetricResultMap(1.1),
}

{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 0, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.7)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 1, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.2)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 2, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.1)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 3, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.6)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 4, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.6)
}
{
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 5, false, 1.0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 0.6)
}
}

func TestAggregateTabletMetricResultsWithErrors(t *testing.T) {
tabletResultsMap := TabletResultMap{
alias1: newMetricResultMap(1.2),
alias2: newMetricResultMap(1.7),
alias3: newMetricResultMap(0.3),
alias4: noSuchMetricMap(),
alias5: newMetricResultMap(1.1),
}

t.Run("nonexistent", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(nonexistentMetricName, tabletResultsMap, 0, false, 0)
_, err := worstMetric.Get()
assert.Error(t, err)
assert.Equal(t, ErrNoSuchMetric, err)
})
t.Run("no ignore", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 0, false, 0)
_, err := worstMetric.Get()
assert.Error(t, err)
assert.Equal(t, ErrNoSuchMetric, err)
})
t.Run("ignore 1", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 1, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, 1.7, value)
})
t.Run("ignore 2", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 2, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, 1.2, value)
})

tabletResultsMap[alias1][DefaultMetricName] = NoSuchMetric
t.Run("no such metric", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 0, false, 0)
_, err := worstMetric.Get()
assert.Error(t, err)
assert.Equal(t, ErrNoSuchMetric, err)
})
t.Run("no such metric, ignore 1", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 1, false, 0)
_, err := worstMetric.Get()
assert.Error(t, err)
assert.Equal(t, ErrNoSuchMetric, err)
})
t.Run("metric found", func(t *testing.T) {
worstMetric := AggregateTabletMetricResults(DefaultMetricName, tabletResultsMap, 2, false, 0)
value, err := worstMetric.Get()
assert.NoError(t, err)
assert.Equal(t, value, 1.7)
})
}
Loading
Loading